Skip to content

More module specifier generation #1051

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

Merged
merged 5 commits into from
Jun 4, 2025

Conversation

andrewbranch
Copy link
Member

Follow-up to #851 and #791 eliminating more diffs

@Copilot Copilot AI review requested due to automatic review settings June 4, 2025 18:29
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors how module specifiers and output paths are generated by replacing the SourceFileMetaDataProvider dependency with a getEmitModuleFormatOfFile function and updating related transformer, compiler, and modulespecifiers APIs to use the revamped HasFileName interface and FileName field. Key changes include:

  • Replacing and updating transformer constructors (ESModuleTransformer and CommonJSModuleTransformer) to rely on the getEmitModuleFormatOfFile callback.
  • Updating module specifier generation to use the new FileName field in ModulePath and modifying several APIs throughout the compiler and printer packages.
  • Removing legacy interfaces and helper functions (e.g. SourceFileMetaDataProvider) while integrating new output path functionality.

Reviewed Changes

Copilot reviewed 308 out of 308 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/transformers/* Refactored transformer constructors and adjusted API parameters to use getEmitModuleFormatOfFile.
internal/modulespecifiers/* Updated ModulePath to use FileName instead of Path and revised related specifier logic.
internal/compiler/, internal/printer/, internal/ast/* Updated API signatures to work with the new HasFileName interface and outputpaths package.
internal/checker/checker.go Modified resolved module checks; potential nil-check removal may cause runtime issues.
internal/outputpaths/* Added and integrated new output paths functions to support the refactor.

Comment on lines 17 to +18
->Q : typeof import("sub")
+>Q : typeof import("./sub.js")
+>Q : typeof import("./sub")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is something that @weswigham mentioned, that type baselines in Strada did not use real module specifier generation, but they (intentionally?) do in Corsa. That significantly limits the number of diffs that will get completely deleted. IMO, it would be nice to align them and see a pure diff deletion, whether by changing Corsa or changing Strada now that we have a target tsgo-port branch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know where that might be? Happy to fix that and push it up.

Copy link
Member

@weswigham weswigham Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typeToTypeNode call in typeWriter.ts provides no symbol tracker, and thus no module resolution host. That's fine, but in such a case, the createBasicNodeBuilderModuleSpecifierResolutionHost call inside checker that makes a fallback only partially wraps the host program, and only when InternalNodeBuilderFlags.DoNotIncludeSymbolChain is passed (which, it is not, in this case). Make that fallback unconditional, and make the wrapper more complete, and you should match the new Corsa behavior.


func (tx *ImpliedModuleTransformer) getEmitModuleFormatOfFile(node *ast.SourceFile) core.ModuleKind {
// !!! host.getEmitModuleFormatOfFile?
return ast.GetEmitModuleFormatOfFileWorker(node, tx.compilerOptions, tx.sourceFileMetaDataProvider.GetSourceFileMetaData(node.Path()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and similar dupes) really bugged me when I was trying to do the module indicator 👍

for _, modulePath := range modulePaths {
targetPath := tspath.ToPath(modulePath.FileName, host.GetCurrentDirectory(), info.UseCaseSensitiveFileNames)
var existingImport *ast.StringLiteralLike
for _, importSpecifier := range importingSourceFile.Imports() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Strada, this code uses program file include reasons instead of file.imports, which I found very puzzling. I changed it in Strada and the only tests that fail were ones that use --outFile. (The existing imports it finds are synthesized ambient module imports inside ambient module declarations in the generated .d.ts bundle. Since those are attached to module declaration blocks instead of a source file, importingSourceFile.Imports() doesn’t find them. This code is supposed to be an optimization, so missing that edge case seems fine.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno. I'd have guessed something like "so it works for files which weren't assembled by the default program, since .imports is set by the program and not the parser", but in such a scenario, I kinda doubt the host would still provide file include reasons. I guess it could happen...?

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@andrewbranch andrewbranch added this pull request to the merge queue Jun 4, 2025
Merged via the queue into microsoft:main with commit af2ba73 Jun 4, 2025
23 checks passed
@andrewbranch andrewbranch deleted the more-module-diffs branch June 4, 2025 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants