Skip to content

Simplify parser options passing, share ASTs even more #1158

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 28 commits into from
Jun 17, 2025

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented Jun 10, 2025

This PR adds ast.SourceFileParseOptions, a value type which is the input to file parsing. The parser now takes it instead of many parameters that have to be plumbed around, and then stores a copy on the SourceFile for later use in map keys and similar. This greatly simplifies GetSourceFile implementors.

Additionally, we're now able to produce better options (really, cache keys) for parsing. Declaration files now set no options at all, so can be fully shared between projects no matter what options are set. Regular source files have fewer options to be able to set the external module indicator (just two booleans). Further follow-up PRs will reduce this set even more.

The optimizations in this PR lead to the peak memory in testrunner to drop from 13GB to ~4GB.

Comment on lines +92 to 98
func (h *compilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile {
text, ok := h.FS().ReadFile(opts.FileName)
if !ok {
return nil
}
if tspath.FileExtensionIs(fileName, tspath.ExtensionJson) {
return parser.ParseJSONText(fileName, path, text)
}
return parser.ParseSourceFile(fileName, path, text, options, metadata, scanner.JSDocParsingModeParseForTypeErrors)
return parser.ParseSourceFile(opts, text, core.GetScriptKindFromFileName(opts.FileName))
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that the ScriptKind and the text are not parameters to GetSourceFile (not options). Implementations of GetSourceFile are in charge of fetching the file contents, and then they also override ScriptKind depending. Here, the core compiler only cares about the path to things, but in the LS the ScriptKind is determined by the language server (since the editor can say "actually, this is JS").

Comment on lines +193 to +195
if scriptKind == core.ScriptKindUnknown {
panic("ScriptKind must be specified when parsing source files.")
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Allowing the compiler to determine the ScriptKind itself is a problem because it means we save onto the SourceFiles the "wrong" value, which means that using it for keys in DocumentRegistry and so on fails later because those caches are unaware of what the parser used to do to it.

@jakebailey jakebailey marked this pull request as ready for review June 10, 2025 23:37
@jakebailey jakebailey requested a review from andrewbranch June 10, 2025 23:37
Comment on lines 8 to 11
type ExternalModuleIndicatorOptions struct {
jsx bool
force bool
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Shifting this code around now means that the entire cache key for the external module indicator is just these two booleans! So much more reuse.

func GetSourceFileAffectingCompilerOptions(fileName string, options *core.CompilerOptions) core.SourceFileAffectingCompilerOptions {
// Declaration files are not parsed/bound differently depending on compiler options.
if tspath.IsDeclarationFileName(fileName) {
return core.SourceFileAffectingCompilerOptions{}
Copy link
Member Author

Choose a reason for hiding this comment

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

It turns out that it's entirely safe to not use any options (source affecting, external module indicator) at all for declaration files, so we can now share these ASTs everywhere.

@jakebailey jakebailey changed the title Simplify parser options passing, eliminate ParseJSONText Simplify parser options passing, share ASTs even more Jun 17, 2025
@jakebailey jakebailey added this pull request to the merge queue Jun 17, 2025
Merged via the queue into main with commit 48fa6e4 Jun 17, 2025
22 checks passed
@jakebailey jakebailey deleted the jabaile/external-module-indicator-parse-options branch June 17, 2025 16:26
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.

2 participants