Skip to content

Commit fa9414a

Browse files
authored
Fix more parent races (#1031)
1 parent 3f44634 commit fa9414a

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

internal/ast/utilities.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,8 @@ func newParentInChildrenSetter() func(node *Node) bool {
885885
}
886886

887887
state.visit = func(node *Node) bool {
888-
if state.parent != nil {
888+
if state.parent != nil && node.Parent != state.parent {
889+
// Avoid data races on no-ops
889890
node.Parent = state.parent
890891
}
891892
saveParent := state.parent

internal/binder/binder.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,10 @@ func (b *Binder) bind(node *ast.Node) bool {
762762
func (b *Binder) setJSDocParents(node *ast.Node) {
763763
for _, jsdoc := range node.JSDoc(b.file) {
764764
setParent(jsdoc, node)
765-
ast.SetParentInChildren(jsdoc)
765+
if jsdoc.Kind != ast.KindJSDocImportTag {
766+
// JSDocImportTag children have parents set during parsing for module resolution purposes.
767+
ast.SetParentInChildren(jsdoc)
768+
}
766769
}
767770
}
768771

internal/parser/references.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func collectModuleReferences(file *ast.SourceFile, node *ast.Statement, inAmbien
3131
if moduleNameExpr != nil && ast.IsStringLiteral(moduleNameExpr) {
3232
moduleName := moduleNameExpr.AsStringLiteral().Text
3333
if moduleName != "" && (!inAmbientModule || !tspath.IsExternalModuleNameRelative(moduleName)) {
34-
ast.SetParentInChildren(node) // we need parent data on imports before the program is fully bound, so we ensure it's set here
34+
if node.Kind != ast.KindJSImportDeclaration {
35+
ast.SetParentInChildren(node) // we need parent data on imports before the program is fully bound, so we ensure it's set here
36+
}
3537
ast.SetImportsOfSourceFile(file, append(file.Imports(), moduleNameExpr))
3638
// !!! removed `&& p.currentNodeModulesDepth == 0`
3739
if file.UsesUriStyleNodeCoreModules != core.TSTrue && !file.IsDeclarationFile {

testdata/baselines/reference/submodule/conformance/importTag17.errors.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/a.js(1,29): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
1+
/a.js(5,15): error TS2749: 'Import' refers to a value, but is being used as a type here. Did you mean 'typeof Import'?
22
/a.js(12,15): error TS2749: 'Require' refers to a value, but is being used as a type here. Did you mean 'typeof Require'?
33

44

@@ -22,12 +22,12 @@
2222

2323
==== /a.js (2 errors) ====
2424
/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */
25-
~~~~~
26-
!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
2725
/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */
2826

2927
/**
3028
* @returns { Import }
29+
~~~~~~
30+
!!! error TS2749: 'Import' refers to a value, but is being used as a type here. Did you mean 'typeof Import'?
3131
*/
3232
export function f1() {
3333
return 1;

testdata/baselines/reference/submoduleAccepted/conformance/importTag17.errors.txt.diff

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
@@= skipped -0, +0 lines =@@
44
-/a.js(8,5): error TS2322: Type '1' is not assignable to type '"module"'.
55
-/a.js(15,5): error TS2322: Type '1' is not assignable to type '"script"'.
6-
+/a.js(1,29): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
6+
+/a.js(5,15): error TS2749: 'Import' refers to a value, but is being used as a type here. Did you mean 'typeof Import'?
77
+/a.js(12,15): error TS2749: 'Require' refers to a value, but is being used as a type here. Did you mean 'typeof Require'?
88

99

1010
==== /node_modules/@types/foo/package.json (0 errors) ====
11-
@@= skipped -21, +21 lines =@@
12-
13-
==== /a.js (2 errors) ====
14-
/** @import { Import } from 'foo' with { 'resolution-mode': 'import' } */
15-
+ ~~~~~
16-
+!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
17-
/** @import { Require } from 'foo' with { 'resolution-mode': 'require' } */
11+
@@= skipped -25, +25 lines =@@
1812

1913
/**
20-
@@= skipped -7, +9 lines =@@
14+
* @returns { Import }
15+
+ ~~~~~~
16+
+!!! error TS2749: 'Import' refers to a value, but is being used as a type here. Did you mean 'typeof Import'?
2117
*/
2218
export function f1() {
2319
return 1;

0 commit comments

Comments
 (0)