Resolve JavaScript/TypeScript import path to file Returns path relative to output_dir with file extension. Returns None for external packages.
(self, import_path: str)
| 534 | is_type_only=is_type) |
| 535 | |
| 536 | def _resolve_js_path(self, import_path: str) -> Optional[str]: |
| 537 | """Resolve JavaScript/TypeScript import path to file |
| 538 | |
| 539 | Returns path relative to output_dir with file extension. |
| 540 | Returns None for external packages. |
| 541 | """ |
| 542 | # Check if it's an external package (doesn't start with . or /) |
| 543 | is_external = not import_path.startswith( |
| 544 | '.') and not import_path.startswith('/') |
| 545 | |
| 546 | # External packages return None early |
| 547 | if is_external: |
| 548 | # Check if it might be a path alias |
| 549 | alias_resolved = self._resolve_alias_path(import_path) |
| 550 | if not alias_resolved: |
| 551 | # Not an alias, it's an external package |
| 552 | return None |
| 553 | resolved = alias_resolved |
| 554 | elif import_path.startswith('/'): |
| 555 | resolved = import_path.lstrip('/') |
| 556 | else: |
| 557 | # Handle relative paths - resolve relative to current_file's directory |
| 558 | resolved = os.path.join(self.current_dir, import_path) |
| 559 | resolved = os.path.normpath(resolved) |
| 560 | |
| 561 | # Helper function to convert to relative path from output_dir |
| 562 | def to_relative(path): |
| 563 | """Convert path to relative from output_dir. |
| 564 | |
| 565 | IMPORTANT: path must be absolute or will be treated as relative to cwd! |
| 566 | Always convert to absolute before calling relpath. |
| 567 | """ |
| 568 | # Convert both to absolute paths first |
| 569 | abs_output_dir = os.path.abspath(self.output_dir) |
| 570 | |
| 571 | # If path is already absolute, use it directly |
| 572 | if os.path.isabs(path): |
| 573 | abs_path = path |
| 574 | else: |
| 575 | # Path is relative - must be relative to output_dir |
| 576 | abs_path = os.path.abspath(os.path.join(self.output_dir, path)) |
| 577 | |
| 578 | # Now both are absolute, safe to use relpath |
| 579 | return os.path.relpath(abs_path, abs_output_dir) |
| 580 | |
| 581 | # Convert resolved path to absolute for existence checks |
| 582 | # resolved is relative to output_dir, so we need to join them |
| 583 | if os.path.isabs(resolved): |
| 584 | abs_resolved = resolved |
| 585 | elif os.path.isabs(self.output_dir): |
| 586 | abs_resolved = os.path.join(self.output_dir, resolved) |
| 587 | else: |
| 588 | # Both are relative, make absolute from current working directory |
| 589 | abs_resolved = os.path.abspath( |
| 590 | os.path.join(self.output_dir, resolved)) |
| 591 | |
| 592 | # Try as directory with index file first |
| 593 | if os.path.isdir(abs_resolved): |
no test coverage detected