Parse imports from code content (main entry point for backward compatibility) IMPORTANT: This function filters out external packages and only returns project files. External packages (like 'react', 'os', 'typing', 'java.util.List') are NOT included. Args: current_file: Pat
(current_file: str, code_content: str,
output_dir: str)
| 817 | |
| 818 | |
| 819 | def parse_imports(current_file: str, code_content: str, |
| 820 | output_dir: str) -> List[ImportInfo]: |
| 821 | """ |
| 822 | Parse imports from code content (main entry point for backward compatibility) |
| 823 | |
| 824 | IMPORTANT: This function filters out external packages and only returns project files. |
| 825 | External packages (like 'react', 'os', 'typing', 'java.util.List') are NOT included. |
| 826 | |
| 827 | Args: |
| 828 | current_file: Path to the file being parsed |
| 829 | code_content: Content of the file |
| 830 | output_dir: Root directory of the project |
| 831 | |
| 832 | Returns: |
| 833 | List of ImportInfo objects for project files only (external packages are excluded) |
| 834 | """ |
| 835 | # Detect file extension |
| 836 | file_ext = os.path.splitext(current_file)[1].lstrip( |
| 837 | '.').lower() if current_file else '' |
| 838 | current_dir = os.path.dirname(current_file) if current_file else '.' |
| 839 | |
| 840 | # Get appropriate parser |
| 841 | parser = ImportParserFactory.get_parser(file_ext, output_dir, current_file, |
| 842 | current_dir) |
| 843 | if not parser: |
| 844 | return [] |
| 845 | |
| 846 | # Parse all imports |
| 847 | all_imports = parser.parse(code_content) |
| 848 | |
| 849 | # Filter out external packages - only keep project files |
| 850 | project_imports = [] |
| 851 | for imp in all_imports: |
| 852 | source = imp.source_file |
| 853 | if not source: |
| 854 | continue |
| 855 | |
| 856 | # For Python files: |
| 857 | # - If source is resolved to a file path (contains / or \), it's a project file |
| 858 | # - If source is still a relative import notation (starts with .), check if it was resolved |
| 859 | # - Otherwise it's an external package |
| 860 | if file_ext in ('py', 'pyw'): |
| 861 | # If it contains path separators or file extension, it's been resolved to a file |
| 862 | if '/' in source or os.sep in source or source.endswith('.py'): |
| 863 | project_imports.append(imp) |
| 864 | # If it starts with . but wasn't resolved (no path sep), it's still relative notation |
| 865 | elif source.startswith('.'): |
| 866 | project_imports.append(imp) |
| 867 | # Otherwise it's an external package (os, sys, typing, numpy, etc.) |
| 868 | continue |
| 869 | |
| 870 | # For JavaScript/TypeScript/Java: filter external packages |
| 871 | # External packages: 'react', 'lodash', '@types/react', '@vue/cli', 'java.util.List' |
| 872 | # They don't start with '.', '/', or contain path separators (except scoped packages) |
| 873 | |
| 874 | # Check if it's a scoped package (starts with @ but file doesn't exist) |
| 875 | is_scoped_package = source.startswith('@') and not os.path.exists( |
| 876 | os.path.join(output_dir, source)) |