Skip to content

Commit 06b2c40

Browse files
authored
Fix code that should have used IsRequireCall (#1195)
1 parent d2a87ed commit 06b2c40

File tree

99 files changed

+427
-915
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+427
-915
lines changed

internal/ast/utilities.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,7 +2705,7 @@ func ForEachDynamicImportOrRequireCall(
27052705
lastIndex, size := findImportOrRequire(file.Text(), 0)
27062706
for lastIndex >= 0 {
27072707
node := GetNodeAtPosition(file, lastIndex, isJavaScriptFile && includeTypeSpaceImports)
2708-
if isJavaScriptFile && IsRequireCall(node) {
2708+
if isJavaScriptFile && IsRequireCall(node, requireStringLiteralLikeArgument) {
27092709
if cb(node, node.Arguments()[0]) {
27102710
return true
27112711
}
@@ -2725,8 +2725,10 @@ func ForEachDynamicImportOrRequireCall(
27252725
return false
27262726
}
27272727

2728-
// IsVariableDeclarationInitializedToRequire should be used wherever parent pointers are set
2729-
func IsRequireCall(node *Node) bool {
2728+
// Returns true if the node is a CallExpression to the identifier 'require' with
2729+
// exactly one argument (of the form 'require("name")').
2730+
// This function does not test if the node is in a JavaScript file or not.
2731+
func IsRequireCall(node *Node, requireStringLiteralLikeArgument bool) bool {
27302732
if !IsCallExpression(node) {
27312733
return false
27322734
}
@@ -2737,7 +2739,7 @@ func IsRequireCall(node *Node) bool {
27372739
if len(call.Arguments.Nodes) != 1 {
27382740
return false
27392741
}
2740-
return IsStringLiteralLike(call.Arguments.Nodes[0])
2742+
return !requireStringLiteralLikeArgument || IsStringLiteralLike(call.Arguments.Nodes[0])
27412743
}
27422744

27432745
func IsUnterminatedLiteral(node *Node) bool {
@@ -2812,7 +2814,7 @@ func IsVariableDeclarationInitializedToRequire(node *Node) bool {
28122814
return node.Parent.Parent.ModifierFlags()&ModifierFlagsExport == 0 &&
28132815
node.AsVariableDeclaration().Initializer != nil &&
28142816
node.Type() == nil &&
2815-
IsRequireCall(node.AsVariableDeclaration().Initializer)
2817+
IsRequireCall(node.AsVariableDeclaration().Initializer, true /*requireStringLiteralLikeArgument*/)
28162818
}
28172819

28182820
func IsModuleExportsAccessExpression(node *Node) bool {

internal/binder/binder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ func (b *Binder) bindCallExpression(node *ast.Node) {
946946
if ast.IsInJSFile(node) &&
947947
b.file.ExternalModuleIndicator == nil &&
948948
b.file.CommonJSModuleIndicator == nil &&
949-
ast.IsVariableDeclarationInitializedToRequire(node.Parent) {
949+
ast.IsRequireCall(node, false /*requireStringLiteralLikeArgument*/) {
950950
b.file.CommonJSModuleIndicator = node
951951
b.bindSourceFileAsExternalModule()
952952
}

internal/binder/nameresolver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,10 @@ loop:
322322
}
323323
}
324324
if result == nil {
325-
if originalLocation != nil && originalLocation.Parent != nil && originalLocation.Parent.Parent != nil &&
326-
ast.IsVariableDeclarationInitializedToRequire(originalLocation.Parent.Parent) {
327-
return r.RequireSymbol
325+
if originalLocation != nil && ast.IsInJSFile(originalLocation) && originalLocation.Parent != nil {
326+
if ast.IsRequireCall(originalLocation.Parent, false /*requireStringLiteralLikeArgument*/) {
327+
return r.RequireSymbol
328+
}
328329
}
329330
}
330331
if nameNotFoundMessage != nil {

internal/checker/checker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6460,7 +6460,7 @@ func (c *Checker) checkAliasSymbol(node *ast.Node) {
64606460
}
64616461
}
64626462
}
6463-
if c.compilerOptions.VerbatimModuleSyntax.IsTrue() && !ast.IsImportEqualsDeclaration(node) && !ast.IsVariableDeclarationInitializedToRequire(node) && c.program.GetEmitModuleFormatOfFile(ast.GetSourceFileOfNode(node)) == core.ModuleKindCommonJS {
6463+
if c.compilerOptions.VerbatimModuleSyntax.IsTrue() && !ast.IsImportEqualsDeclaration(node) && !ast.IsInJSFile(node) && c.program.GetEmitModuleFormatOfFile(ast.GetSourceFileOfNode(node)) == core.ModuleKindCommonJS {
64646464
c.error(node, diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled)
64656465
} else if c.moduleKind == core.ModuleKindPreserve && !ast.IsImportEqualsDeclaration(node) && !ast.IsVariableDeclaration(node) && c.program.GetEmitModuleFormatOfFile(ast.GetSourceFileOfNode(node)) == core.ModuleKindCommonJS {
64666466
// In `--module preserve`, ESM input syntax emits ESM output syntax, but there will be times
@@ -6977,7 +6977,7 @@ func (c *Checker) getQuickTypeOfExpression(node *ast.Node) *Type {
69776977
return nil
69786978
// Optimize for the common case of a call to a function with a single non-generic call
69796979
// signature where we can just fetch the return type without checking the arguments.
6980-
case ast.IsCallExpression(expr) && expr.Expression().Kind != ast.KindSuperKeyword && !ast.IsVariableDeclarationInitializedToRequire(expr.Parent) && !c.isSymbolOrSymbolForCall(expr):
6980+
case ast.IsCallExpression(expr) && expr.Expression().Kind != ast.KindSuperKeyword && !ast.IsRequireCall(expr, true /*requireStringLiteralLikeArgument*/) && !c.isSymbolOrSymbolForCall(expr):
69816981
if isCallChain(expr) {
69826982
return c.getReturnTypeOfSingleNonGenericSignatureOfCallChain(expr)
69836983
}
@@ -14432,7 +14432,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1443214432
contextSpecifier = location.AsModuleDeclaration().Name()
1443314433
} else if ast.IsLiteralImportTypeNode(location) {
1443414434
contextSpecifier = location.AsImportTypeNode().Argument.AsLiteralTypeNode().Literal
14435-
} else if ast.IsVariableDeclarationInitializedToRequire(location) {
14435+
} else if ast.IsVariableDeclaration(location) && location.AsVariableDeclaration().Initializer != nil && ast.IsRequireCall(location.AsVariableDeclaration().Initializer, true /*requireStringLiteralLikeArgument*/) {
1443614436
contextSpecifier = location.AsVariableDeclaration().Initializer.AsCallExpression().Arguments.Nodes[0]
1443714437
} else {
1443814438
var ancestor *ast.Node
@@ -14894,7 +14894,7 @@ func (c *Checker) getTypeWithSyntheticDefaultImportType(t *Type, symbol *ast.Sym
1489414894
}
1489514895

1489614896
func (c *Checker) isCommonJSRequire(node *ast.Node) bool {
14897-
if !ast.IsVariableDeclarationInitializedToRequire(node.Parent) {
14897+
if !ast.IsRequireCall(node, true /*requireStringLiteralLikeArgument*/) {
1489814898
return false
1489914899
}
1490014900
if !ast.IsIdentifier(node.Expression()) {

internal/checker/nodebuilderimpl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ func tryGetModuleSpecifierFromDeclarationWorker(node *ast.Node) *ast.Node {
981981
switch node.Kind {
982982
case ast.KindVariableDeclaration, ast.KindBindingElement:
983983
requireCall := ast.FindAncestor(node.Initializer(), func(node *ast.Node) bool {
984-
return ast.IsRequireCall(node)
984+
return ast.IsRequireCall(node, true /*requireStringLiteralLikeArgument*/)
985985
})
986986
if requireCall == nil {
987987
return nil

internal/compiler/fileloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func importSyntaxAffectsModuleResolution(options *core.CompilerOptions) bool {
489489
}
490490

491491
func getEmitSyntaxForUsageLocationWorker(fileName string, meta *ast.SourceFileMetaData, usage *ast.Node, options *core.CompilerOptions) core.ResolutionMode {
492-
if ast.IsRequireCall(usage.Parent) || ast.IsExternalModuleReference(usage.Parent) && ast.IsImportEqualsDeclaration(usage.Parent.Parent) {
492+
if ast.IsRequireCall(usage.Parent, false /*requireStringLiteralLikeArgument*/) || ast.IsExternalModuleReference(usage.Parent) && ast.IsImportEqualsDeclaration(usage.Parent.Parent) {
493493
return core.ModuleKindCommonJS
494494
}
495495
fileEmitMode := ast.GetEmitModuleFormatOfFileWorker(fileName, options, meta)

internal/ls/utilities.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func tryGetImportFromModuleSpecifier(node *ast.StringLiteralLike) *ast.Node {
6767
case ast.KindExternalModuleReference:
6868
return node.Parent.Parent
6969
case ast.KindCallExpression:
70-
if ast.IsImportCall(node.Parent) || ast.IsRequireCall(node.Parent) {
70+
if ast.IsImportCall(node.Parent) || ast.IsRequireCall(node.Parent, false /*requireStringLiteralLikeArgument*/) {
7171
return node.Parent
7272
}
7373
return nil
@@ -88,7 +88,7 @@ func isModuleSpecifierLike(node *ast.Node) bool {
8888
return false
8989
}
9090

91-
if ast.IsVariableDeclarationInitializedToRequire(node.Parent) || ast.IsImportCall(node.Parent) {
91+
if ast.IsRequireCall(node.Parent, false /*requireStringLiteralLikeArgument*/) || ast.IsImportCall(node.Parent) {
9292
return node.Parent.AsCallExpression().Arguments.Nodes[0] == node
9393
}
9494

internal/transformers/commonjsmodule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ func (tx *CommonJSModuleTransformer) visitCallExpression(node *ast.CallExpressio
16451645
needsRewrite := false
16461646
if tx.compilerOptions.RewriteRelativeImportExtensions.IsTrue() {
16471647
if ast.IsImportCall(node.AsNode()) && len(node.Arguments.Nodes) > 0 ||
1648-
ast.IsInJSFile(node.AsNode()) && ast.IsRequireCall(node.AsNode()) {
1648+
ast.IsInJSFile(node.AsNode()) && ast.IsRequireCall(node.AsNode(), false /*requireStringLiteralLikeArgument*/) {
16491649
needsRewrite = true
16501650
}
16511651
}

internal/transformers/esmodule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func (tx *ESModuleTransformer) visitExportDeclaration(node *ast.ExportDeclaratio
244244
func (tx *ESModuleTransformer) visitCallExpression(node *ast.CallExpression) *ast.Node {
245245
if tx.compilerOptions.RewriteRelativeImportExtensions.IsTrue() {
246246
if ast.IsImportCall(node.AsNode()) && len(node.Arguments.Nodes) > 0 ||
247-
ast.IsInJSFile(node.AsNode()) && ast.IsRequireCall(node.AsNode()) {
247+
ast.IsInJSFile(node.AsNode()) && ast.IsRequireCall(node.AsNode(), false /*requireStringLiteralLikeArgument*/) {
248248
return tx.visitImportOrRequireCall(node)
249249
}
250250
}

testdata/baselines/reference/submodule/compiler/dynamicRequire.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function foo(name) {
77

88
var s = require("t/" + name)
99
>s : Symbol(s, Decl(a.js, 1, 7))
10+
>require : Symbol(require)
1011
>name : Symbol(name, Decl(a.js, 0, 13))
1112
}
1213

0 commit comments

Comments
 (0)