-
Notifications
You must be signed in to change notification settings - Fork 647
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
jakebailey
merged 28 commits into
main
from
jabaile/external-module-indicator-parse-options
Jun 17, 2025
Merged
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
14e7b32
Create SourceFileParseOptions
jakebailey db149ad
Fix parsing mode
jakebailey d8c50f8
Script kind maybe
jakebailey b795913
partial
jakebailey 10aa6ee
Missed one
jakebailey 73ec3d9
Consolidate JSON parsing
jakebailey adfea3b
Tweak test
jakebailey c6b2b07
Binder doesn't need options anymore
jakebailey c3144c4
lint
jakebailey 12f1795
Don't set ExternalModuleIndicator on JSON files
jakebailey a127ce6
Refactor a bit
jakebailey 251e04a
Fix generator
jakebailey 4cb0223
Remove TODOs
jakebailey a9bf41c
Shift code around, remove dead
jakebailey 636eaf0
fmt
jakebailey b786e30
Merge branch 'main' into jabaile/external-module-indicator-parse-options
jakebailey f647de4
Merge branch 'main' into jabaile/external-module-indicator-parse-options
jakebailey 17da1a7
Merge branch 'main' into jabaile/external-module-indicator-parse-options
jakebailey 645fa76
value type
jakebailey 60f6c21
Shift everything around a bunch, new finer grained key
jakebailey 7783c4a
TODOs
jakebailey 64ec265
Merge branch 'main' into jabaile/external-module-indicator-parse-options
jakebailey bc84ad6
Condense key down even more, awesome
jakebailey 02183a2
No source file affecting for declaration files, amazing
jakebailey fa9628c
Move code around
jakebailey f7e3067
fmt
jakebailey 9f01650
move again
jakebailey 64249aa
Restore fileName for debugging
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package ast | ||
|
||
import ( | ||
"github.com/microsoft/typescript-go/internal/core" | ||
"github.com/microsoft/typescript-go/internal/tspath" | ||
) | ||
|
||
type ExternalModuleIndicatorOptions struct { | ||
jsx bool | ||
force bool | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 GetExternalModuleIndicatorOptions(fileName string, options *core.CompilerOptions, metadata SourceFileMetaData) ExternalModuleIndicatorOptions { | ||
if tspath.IsDeclarationFileName(fileName) { | ||
return ExternalModuleIndicatorOptions{} | ||
} | ||
|
||
switch options.GetEmitModuleDetectionKind() { | ||
case core.ModuleDetectionKindForce: | ||
// All non-declaration files are modules, declaration files still do the usual isFileProbablyExternalModule | ||
return ExternalModuleIndicatorOptions{force: true} | ||
case core.ModuleDetectionKindLegacy: | ||
// Files are modules if they have imports, exports, or import.meta | ||
return ExternalModuleIndicatorOptions{} | ||
case core.ModuleDetectionKindAuto: | ||
// If module is nodenext or node16, all esm format files are modules | ||
// If jsx is react-jsx or react-jsxdev then jsx tags force module-ness | ||
// otherwise, the presence of import or export statments (or import.meta) implies module-ness | ||
return ExternalModuleIndicatorOptions{ | ||
jsx: options.Jsx == core.JsxEmitReactJSX || options.Jsx == core.JsxEmitReactJSXDev, | ||
force: isFileForcedToBeModuleByFormat(fileName, options, metadata), | ||
} | ||
default: | ||
return ExternalModuleIndicatorOptions{} | ||
} | ||
} | ||
|
||
var isFileForcedToBeModuleByFormatExtensions = []string{tspath.ExtensionCjs, tspath.ExtensionCts, tspath.ExtensionMjs, tspath.ExtensionMts} | ||
|
||
func isFileForcedToBeModuleByFormat(fileName string, options *core.CompilerOptions, metadata SourceFileMetaData) bool { | ||
// Excludes declaration files - they still require an explicit `export {}` or the like | ||
// for back compat purposes. The only non-declaration files _not_ forced to be a module are `.js` files | ||
// that aren't esm-mode (meaning not in a `type: module` scope). | ||
if GetImpliedNodeFormatForEmitWorker(fileName, options.GetEmitModuleKind(), metadata) == core.ModuleKindESNext || tspath.FileExtensionIsOneOf(fileName, isFileForcedToBeModuleByFormatExtensions) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func SetExternalModuleIndicator(file *SourceFile, opts ExternalModuleIndicatorOptions) { | ||
file.ExternalModuleIndicator = getExternalModuleIndicator(file, opts) | ||
} | ||
|
||
func getExternalModuleIndicator(file *SourceFile, opts ExternalModuleIndicatorOptions) *Node { | ||
if file.ScriptKind == core.ScriptKindJSON { | ||
return nil | ||
} | ||
|
||
if node := isFileProbablyExternalModule(file); node != nil { | ||
return node | ||
} | ||
|
||
if file.IsDeclarationFile { | ||
return nil | ||
} | ||
|
||
if opts.jsx { | ||
if node := isFileModuleFromUsingJSXTag(file); node != nil { | ||
return node | ||
} | ||
} | ||
|
||
if opts.force { | ||
return file.AsNode() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func isFileProbablyExternalModule(sourceFile *SourceFile) *Node { | ||
for _, statement := range sourceFile.Statements.Nodes { | ||
if IsExternalModuleIndicator(statement) { | ||
return statement | ||
} | ||
} | ||
return getImportMetaIfNecessary(sourceFile) | ||
} | ||
|
||
func getImportMetaIfNecessary(sourceFile *SourceFile) *Node { | ||
if sourceFile.AsNode().Flags&NodeFlagsPossiblyContainsImportMeta != 0 { | ||
return findChildNode(sourceFile.AsNode(), IsImportMeta) | ||
} | ||
return nil | ||
} | ||
|
||
func findChildNode(root *Node, check func(*Node) bool) *Node { | ||
var result *Node | ||
var visit func(*Node) bool | ||
visit = func(node *Node) bool { | ||
if check(node) { | ||
result = node | ||
return true | ||
} | ||
return node.ForEachChild(visit) | ||
} | ||
visit(root) | ||
return result | ||
} | ||
|
||
func isFileModuleFromUsingJSXTag(file *SourceFile) *Node { | ||
return walkTreeForJSXTags(file.AsNode()) | ||
} | ||
|
||
// This is a somewhat unavoidable full tree walk to locate a JSX tag - `import.meta` requires the same, | ||
// but we avoid that walk (or parts of it) if at all possible using the `PossiblyContainsImportMeta` node flag. | ||
// Unfortunately, there's no `NodeFlag` space to do the same for JSX. | ||
func walkTreeForJSXTags(node *Node) *Node { | ||
var found *Node | ||
|
||
var visitor func(node *Node) bool | ||
visitor = func(node *Node) bool { | ||
if found != nil { | ||
return true | ||
} | ||
if node.SubtreeFacts()&SubtreeContainsJsx == 0 { | ||
return false | ||
} | ||
if IsJsxOpeningElement(node) || IsJsxFragment(node) { | ||
found = node | ||
return true | ||
} | ||
return node.ForEachChild(visitor) | ||
} | ||
visitor(node) | ||
|
||
return found | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.