Skip to content

Fix typedef binding with CJS exports= #826

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 31 additions & 1 deletion internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type Binder struct {
flowNodePool core.Pool[ast.FlowNode]
flowListPool core.Pool[ast.FlowList]
singleDeclarationsPool core.Pool[*ast.Node]
delayedTypeAliases []*ast.Node
}

type ActiveLabel struct {
Expand Down Expand Up @@ -124,9 +125,32 @@ func bindSourceFile(file *ast.SourceFile, options *core.SourceFileAffectingCompi
b.bind(file.AsNode())
file.SymbolCount = b.symbolCount
file.ClassifiableNames = b.classifiableNames
b.delayedBindJSDocTypedefTag()
})
}

// top-level typedef binding is delayed because it changes based on whether `module.exports = x` is bound
func (b *Binder) delayedBindJSDocTypedefTag() {
if b.delayedTypeAliases == nil {
return
}
if b.file.Symbol != nil {
if exportEq := b.file.Symbol.Exports[ast.InternalSymbolNameExportEquals]; exportEq != nil && b.file.CommonJSModuleIndicator != nil {
for _, node := range b.delayedTypeAliases {
b.declareSymbol(ast.GetSymbolTable(&exportEq.Exports), exportEq /*parent*/, node, ast.SymbolFlagsTypeAlias, ast.SymbolFlagsTypeAliasExcludes)
Copy link
Member

@weswigham weswigham Apr 26, 2025

Choose a reason for hiding this comment

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

This works for one level of indirection to a local namespace-y thing, but what about an aliased namespacey thing? Eg

const mod = require("./mod.js")
/**
* @typedef {string} T
*/
module.exports = mod

or, abusing a ts construct to do it locally,

namespace ns {
  export namespace inner {}
}
import mod = ns.inner
/**
* @typedef {string} T
*/
module.exports = mod

mod's symbol will be an alias, and this will just patch the typedefs onto the alias symbol, and not the alias symbol's ultimate target, which, dollars to donuts, means it's going to effectively go missing without extra lookup logic in the checker, similar to what we used to have. 🥹

Copy link
Member Author

@sandersn sandersn Apr 28, 2025

Choose a reason for hiding this comment

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

Even simpler, this also reproduces the problem.

function f() { }
/** @typedef {string} T */
module.exports = f

b.declareSymbol(ast.GetLocals(b.file.AsNode()), b.file.Symbol, node, ast.SymbolFlagsTypeAlias, ast.SymbolFlagsTypeAliasExcludes)
}
return
}
}
// bind normally
b.container = b.file.AsNode()
b.blockScopeContainer = b.file.AsNode()
for _, node := range b.delayedTypeAliases {
b.bindBlockScopedDeclaration(node, ast.SymbolFlagsTypeAlias, ast.SymbolFlagsTypeAliasExcludes)
}
}

func (b *Binder) newSymbol(flags ast.SymbolFlags, name string) *ast.Symbol {
b.symbolCount++
result := b.symbolPool.New()
Expand Down Expand Up @@ -686,8 +710,14 @@ func (b *Binder) bind(node *ast.Node) bool {
b.bindBlockScopedDeclaration(node, ast.SymbolFlagsInterface, ast.SymbolFlagsInterfaceExcludes)
case ast.KindCallExpression:
b.bindCallExpression(node)
case ast.KindTypeAliasDeclaration, ast.KindJSTypeAliasDeclaration:
case ast.KindTypeAliasDeclaration:
b.bindBlockScopedDeclaration(node, ast.SymbolFlagsTypeAlias, ast.SymbolFlagsTypeAliasExcludes)
case ast.KindJSTypeAliasDeclaration:
if b.file.AsNode() == b.container {
b.delayedTypeAliases = append(b.delayedTypeAliases, node)
} else {
b.bindBlockScopedDeclaration(node, ast.SymbolFlagsTypeAlias, ast.SymbolFlagsTypeAliasExcludes)
}
case ast.KindEnumDeclaration:
b.bindEnumDeclaration(node)
case ast.KindModuleDeclaration:
Expand Down
1 change: 0 additions & 1 deletion internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) {
for _, tag := range j.AsJSDoc().Tags.Nodes {
switch tag.Kind {
case ast.KindJSDocTypedefTag:
// !!! Don't mark typedefs as exported if they are not in a module
typeExpression := tag.AsJSDocTypedefTag().TypeExpression
if typeExpression == nil {
break
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
input.js(5,48): error TS2304: Cannot find name 'P'.
input.js(48,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
input.js(52,17): error TS2322: Type '{ color: string; }' is not assignable to type '{ color: "blue" | "red"; }'.
Types of property 'color' are incompatible.
Type 'string' is not assignable to type '"blue" | "red"'.


==== input.js (3 errors) ====
==== input.js (2 errors) ====
/** @typedef {{ color: "red" | "blue" }} MyComponentProps */

/**
Expand Down Expand Up @@ -56,12 +55,8 @@ input.js(52,17): error TS2322: Type '{ color: string; }' is not assignable to ty
* @type {MyComponentProps}
*/
module.exports = {
~~~~~~~~~~~~~~~~~~
color: "red"
~~~~~~~~~~~~~~~~
}
~
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.

expectLiteral({ props: module.exports });
~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
index.js(9,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
index.js(9,34): error TS7006: Parameter 'options' implicitly has an 'any' type.


==== index.js (2 errors) ====
==== index.js (1 errors) ====
/**
* @typedef Options
* @property {string} opt
Expand All @@ -12,8 +11,6 @@ index.js(9,34): error TS7006: Parameter 'options' implicitly has an 'any' type.
* @param {Options} options
*/
module.exports = function loader(options) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.
~~~~~~~
!!! error TS7006: Parameter 'options' implicitly has an 'any' type.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
typescript-eslint.js(12,17): error TS7019: Rest parameter 'configs' implicitly has an 'any[]' type.
typescript-eslint.js(14,1): error TS2309: An export assignment cannot be used in a module with other exported elements.


==== eslint.config.js (0 errors) ====
Expand All @@ -26,7 +25,7 @@ typescript-eslint.js(14,1): error TS2309: An export assignment cannot be used in
},
};

==== typescript-eslint.js (2 errors) ====
==== typescript-eslint.js (1 errors) ====
/**
* @typedef {{ rules: Record<string, boolean> }} Plugin
*/
Expand All @@ -43,6 +42,4 @@ typescript-eslint.js(14,1): error TS2309: An export assignment cannot be used in
!!! error TS7019: Rest parameter 'configs' implicitly has an 'any[]' type.

module.exports = { config };
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.

Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import { writeFile, WriteFileOptions, WriteFileOptions as OtherName } from "fs";
/** @typedef {{ x: any }} JSDocType */

export { JSDocType };
>JSDocType : Symbol(JSDocType, Decl(index.js, 2, 4), Decl(index.js, 4, 8))
>JSDocType : Symbol(JSDocType, Decl(index.js, 4, 8), Decl(index.js, 2, 4))

export { JSDocType as ThisIsFine };
>JSDocType : Symbol(JSDocType, Decl(index.js, 2, 4), Decl(index.js, 4, 8))
>JSDocType : Symbol(JSDocType, Decl(index.js, 4, 8), Decl(index.js, 2, 4))
>ThisIsFine : Symbol(ThisIsFine, Decl(index.js, 5, 8))

export { WriteFileOptions };
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ context.js(4,14): error TS1340: Module './timer' does not refer to a type, but i
context.js(5,14): error TS1340: Module './hook' does not refer to a type, but is used as a type here. Did you mean 'typeof import('./hook')'?
context.js(6,31): error TS2694: Namespace 'Hook' has no exported member 'HookHandler'.
context.js(34,14): error TS2350: Only a void function can be called with the 'new' keyword.
context.js(48,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
hook.js(2,20): error TS1340: Module './context' does not refer to a type, but is used as a type here. Did you mean 'typeof import('./context')'?
hook.js(10,1): error TS2309: An export assignment cannot be used in a module with other exported elements.


==== timer.js (0 errors) ====
Expand All @@ -15,7 +13,7 @@ hook.js(10,1): error TS2309: An export assignment cannot be used in a module wit
this.timeout = timeout;
}
module.exports = Timer;
==== hook.js (2 errors) ====
==== hook.js (1 errors) ====
/**
* @typedef {(arg: import("./context")) => void} HookHandler
~~~~~~~~~~~~~~~~~~~
Expand All @@ -28,10 +26,8 @@ hook.js(10,1): error TS2309: An export assignment cannot be used in a module wit
this.handle = handle;
}
module.exports = Hook;
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.

==== context.js (5 errors) ====
==== context.js (4 errors) ====
/**
* Imports
*
Expand Down Expand Up @@ -88,6 +84,4 @@ hook.js(10,1): error TS2309: An export assignment cannot be used in a module wit
}
}
module.exports = Context;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ file.js(10,51): error TS2300: Duplicate identifier 'myTypes'.
file.js(13,13): error TS2300: Duplicate identifier 'myTypes'.
file.js(18,15): error TS2702: 'myTypes' only refers to a type, but is being used as a namespace here.
file.js(18,39): error TS2300: Duplicate identifier 'myTypes'.
file.js(20,9): error TS2300: Duplicate identifier 'myTypes'.
file2.js(6,11): error TS2315: Type 'Object' is not generic.
file2.js(12,23): error TS2702: 'myTypes' only refers to a type, but is being used as a namespace here.
file2.js(17,12): error TS2702: 'testFnTypes' only refers to a type, but is being used as a namespace here.


==== file.js (5 errors) ====
==== file.js (6 errors) ====
/**
* @namespace myTypes
* @global
Expand Down Expand Up @@ -39,6 +40,8 @@ file2.js(17,12): error TS2702: 'testFnTypes' only refers to a type, but is being
!!! error TS2300: Duplicate identifier 'myTypes'.

export {myTypes};
~~~~~~~
!!! error TS2300: Duplicate identifier 'myTypes'.
==== file2.js (3 errors) ====
import {myTypes} from './file.js';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const myTypes = {
/** @typedef {myTypes.typeB|Function} myTypes.typeC */

export {myTypes};
>myTypes : Symbol(myTypes, Decl(file.js, 9, 4), Decl(file.js, 19, 8))
>myTypes : Symbol(myTypes, Decl(file.js, 19, 8), Decl(file.js, 9, 4))

=== file2.js ===
import {myTypes} from './file.js';
Expand Down Expand Up @@ -65,5 +65,5 @@ function testFn(input) {

export {testFn, testFnTypes};
>testFn : Symbol(testFn, Decl(file2.js, 27, 8))
>testFnTypes : Symbol(testFnTypes, Decl(file2.js, 11, 4), Decl(file2.js, 27, 15))
>testFnTypes : Symbol(testFnTypes, Decl(file2.js, 27, 15), Decl(file2.js, 11, 4))

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

export {myTypes};
->myTypes : Symbol(myTypes, Decl(file.js, 19, 8), Decl(file.js, 9, 50), Decl(file.js, 12, 12), Decl(file.js, 17, 38))
+>myTypes : Symbol(myTypes, Decl(file.js, 9, 4), Decl(file.js, 19, 8))
+>myTypes : Symbol(myTypes, Decl(file.js, 19, 8), Decl(file.js, 9, 4))

=== file2.js ===
import {myTypes} from './file.js';
Expand All @@ -32,5 +32,5 @@
export {testFn, testFnTypes};
>testFn : Symbol(testFn, Decl(file2.js, 27, 8))
->testFnTypes : Symbol(testFnTypes, Decl(file2.js, 27, 15), Decl(file2.js, 11, 37))
+>testFnTypes : Symbol(testFnTypes, Decl(file2.js, 11, 4), Decl(file2.js, 27, 15))
+>testFnTypes : Symbol(testFnTypes, Decl(file2.js, 27, 15), Decl(file2.js, 11, 4))

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ file.js(10,51): error TS2300: Duplicate identifier 'myTypes'.
file.js(13,13): error TS2300: Duplicate identifier 'myTypes'.
file.js(18,15): error TS2702: 'myTypes' only refers to a type, but is being used as a namespace here.
file.js(18,39): error TS2300: Duplicate identifier 'myTypes'.
file.js(20,9): error TS2300: Duplicate identifier 'myTypes'.
file2.js(6,11): error TS2315: Type 'Object' is not generic.
file2.js(12,23): error TS2702: 'myTypes' only refers to a type, but is being used as a namespace here.
file2.js(17,12): error TS2702: 'testFnTypes' only refers to a type, but is being used as a namespace here.
file2.js(28,1): error TS2309: An export assignment cannot be used in a module with other exported elements.


==== file2.js (4 errors) ====
==== file2.js (3 errors) ====
const {myTypes} = require('./file.js');

/**
Expand Down Expand Up @@ -44,9 +44,7 @@ file2.js(28,1): error TS2309: An export assignment cannot be used in a module wi
}

module.exports = {testFn, testFnTypes};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2309: An export assignment cannot be used in a module with other exported elements.
==== file.js (5 errors) ====
==== file.js (6 errors) ====
/**
* @namespace myTypes
* @global
Expand Down Expand Up @@ -76,4 +74,6 @@ file2.js(28,1): error TS2309: An export assignment cannot be used in a module wi
~~~~~~~
!!! error TS2300: Duplicate identifier 'myTypes'.

exports.myTypes = myTypes;
exports.myTypes = myTypes;
~~~~~~~
!!! error TS2300: Duplicate identifier 'myTypes'.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const myTypes = {
/** @typedef {myTypes.typeB|Function} myTypes.typeC */

exports.myTypes = myTypes;
>exports.myTypes : Symbol(myTypes, Decl(file.js, 9, 4), Decl(file.js, 7, 2))
>exports.myTypes : Symbol(myTypes, Decl(file.js, 7, 2), Decl(file.js, 9, 4))
>exports : Symbol("file", Decl(file.js, 0, 0))
>myTypes : Symbol(myTypes, Decl(file.js, 9, 4), Decl(file.js, 7, 2))
>myTypes : Symbol(myTypes, Decl(file.js, 7, 2), Decl(file.js, 9, 4))
>myTypes : Symbol(myTypes, Decl(file.js, 5, 5), Decl(file.js, 9, 4), Decl(file.js, 12, 3), Decl(file.js, 17, 4))

Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
->exports : Symbol(myTypes, Decl(file.js, 7, 2))
->myTypes : Symbol(myTypes, Decl(file.js, 7, 2))
->myTypes : Symbol(myTypes, Decl(file.js, 5, 5), Decl(file.js, 9, 50), Decl(file.js, 12, 12), Decl(file.js, 17, 38))
+>exports.myTypes : Symbol(myTypes, Decl(file.js, 9, 4), Decl(file.js, 7, 2))
+>exports.myTypes : Symbol(myTypes, Decl(file.js, 7, 2), Decl(file.js, 9, 4))
+>exports : Symbol("file", Decl(file.js, 0, 0))
+>myTypes : Symbol(myTypes, Decl(file.js, 9, 4), Decl(file.js, 7, 2))
+>myTypes : Symbol(myTypes, Decl(file.js, 7, 2), Decl(file.js, 9, 4))
+>myTypes : Symbol(myTypes, Decl(file.js, 5, 5), Decl(file.js, 9, 4), Decl(file.js, 12, 3), Decl(file.js, 17, 4))

This file was deleted.

Loading