Skip to content

Fix more parent races #1031

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 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/ast/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ func newParentInChildrenSetter() func(node *Node) bool {
}

state.visit = func(node *Node) bool {
if state.parent != nil {
if state.parent != nil && node.Parent != state.parent {
// Avoid data races on no-ops
node.Parent = state.parent
}
saveParent := state.parent
Expand Down
5 changes: 4 additions & 1 deletion internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,10 @@ func (b *Binder) bind(node *ast.Node) bool {
func (b *Binder) setJSDocParents(node *ast.Node) {
for _, jsdoc := range node.JSDoc(b.file) {
setParent(jsdoc, node)
ast.SetParentInChildren(jsdoc)
if jsdoc.Kind != ast.KindJSDocImportTag {
// JSDocImportTag children have parents set during parsing for module resolution purposes.
ast.SetParentInChildren(jsdoc)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion internal/parser/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func collectModuleReferences(file *ast.SourceFile, node *ast.Statement, inAmbien
if moduleNameExpr != nil && ast.IsStringLiteral(moduleNameExpr) {
moduleName := moduleNameExpr.AsStringLiteral().Text
if moduleName != "" && (!inAmbientModule || !tspath.IsExternalModuleNameRelative(moduleName)) {
ast.SetParentInChildren(node) // we need parent data on imports before the program is fully bound, so we ensure it's set here
if node.Kind != ast.KindJSImportDeclaration {
ast.SetParentInChildren(node) // we need parent data on imports before the program is fully bound, so we ensure it's set here
}
ast.SetImportsOfSourceFile(file, append(file.Imports(), moduleNameExpr))
// !!! removed `&& p.currentNodeModulesDepth == 0`
if file.UsesUriStyleNodeCoreModules != core.TSTrue && !file.IsDeclarationFile {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/a.js(1,29): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
/a.js(5,15): error TS2749: 'Import' refers to a value, but is being used as a type here. Did you mean 'typeof Import'?
/a.js(12,15): error TS2749: 'Require' refers to a value, but is being used as a type here. Did you mean 'typeof Require'?


Expand All @@ -22,12 +22,12 @@

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

/**
* @returns { Import }
~~~~~~
!!! error TS2749: 'Import' refers to a value, but is being used as a type here. Did you mean 'typeof Import'?
*/
export function f1() {
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
@@= skipped -0, +0 lines =@@
-/a.js(8,5): error TS2322: Type '1' is not assignable to type '"module"'.
-/a.js(15,5): error TS2322: Type '1' is not assignable to type '"script"'.
+/a.js(1,29): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
+/a.js(5,15): error TS2749: 'Import' refers to a value, but is being used as a type here. Did you mean 'typeof Import'?
+/a.js(12,15): error TS2749: 'Require' refers to a value, but is being used as a type here. Did you mean 'typeof Require'?


==== /node_modules/@types/foo/package.json (0 errors) ====
@@= skipped -21, +21 lines =@@

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

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