Skip to content

[WIP] Port TypeScript PR #59767: Rewrite relative import extensions with flag #1114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We���ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 9, 2025

Port TypeScript PR #59767: Rewrite relative import extensions with flag

Analysis of what needs to be ported from the TypeScript PR to the Go implementation:

Current Status

  • Basic command line option already exists in internal/tsoptions/declscompiler.go
  • Emit helper already exists in internal/printer/helpers.go
  • Factory function already exists in internal/printer/factory.go
  • Basic utility functions already exist in internal/transformers/utilities.go
  • Some diagnostic messages already exist in internal/diagnostics/diagnostics_generated.go
  • Basic tests already exist in internal/transformers/commonjsmodule_test.go

Completed Implementation ✅

  1. Type checker validation logic in internal/checker/checker.go
    • Implemented error TS2876: When import path looks like filename but resolves elsewhere
    • Implemented error TS2877: When using TS extension that won't be rewritten (simplified without sourceFileMayBeEmitted check)
  2. Fixed shouldRewriteModuleSpecifier to match TypeScript implementation (removed extra TS extension check)
  3. Moved ShouldRewriteModuleSpecifier to tspath package to avoid import cycles
  4. Uncommented description field for the rewriteRelativeImportExtensions command line option
  5. All existing transformers tests pass
  6. New validation is working correctly - producing expected error diagnostics for test cases:
    • cjsErrors.ts - detecting unsafe relative imports
    • packageJsonImportsErrors.ts - package.json imports validation
    • nodeModulesTsFiles.ts - node_modules TypeScript files validation
    • nonTSExtensions.ts - non-TypeScript extension handling
  7. Baseline tests updated with new error diagnostics

Design Notes

  • Moved ShouldRewriteModuleSpecifier from transformers to tspath package to avoid import cycles
  • Simplified error TS2877 validation by removing sourceFileMayBeEmitted check (can be added later if needed)
  • Did not implement error TS2878 for project reference validation (complex cross-project validation, may defer)
  • Minor difference in relative path formatting (no ./ prefix) vs TypeScript implementation but functionally equivalent

Test Results

  • ✅ All existing tests pass
  • ✅ New error diagnostics generated correctly for rewriteRelativeImportExtensions test cases
  • ✅ Baseline tests updated to reflect new validation behavior

The implementation successfully ports the core functionality of TypeScript PR #59767, providing validation for the rewriteRelativeImportExtensions flag to catch unsafe import path rewrites.

Fixes #1113.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

… flag

Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants