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 9 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 @@ -72,6 +72,7 @@ type Binder struct {
flowNodePool core.Pool[ast.FlowNode]
flowListPool core.Pool[ast.FlowList]
singleDeclarationsPool core.Pool[*ast.Node]
delayedTypeAliases []*ast.Node
}

func (b *Binder) options() core.SourceFileAffectingCompilerOptions {
Expand Down Expand Up @@ -125,9 +126,32 @@ func bindSourceFile(file *ast.SourceFile) {
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 @@ -687,8 +711,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
38 changes: 32 additions & 6 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13788,7 +13788,7 @@ func (c *Checker) getTargetOfImportEqualsDeclaration(node *ast.Node, dontResolve
moduleReference = ast.GetExternalModuleImportEqualsDeclarationExpression(node)
}
immediate := c.resolveExternalModuleName(node, moduleReference, false /*ignoreErrors*/)
resolved := c.resolveExternalModuleSymbol(immediate, false /*dontResolveAlias*/)
resolved := c.resolveExternalModuleSymbol(immediate, dontResolveAlias)
c.markSymbolOfAliasDeclarationIfTypeOnly(node, immediate, resolved, false /*overwriteEmpty*/, nil, "")
return resolved
}
Expand Down Expand Up @@ -15061,9 +15061,24 @@ func (c *Checker) resolveEntityName(name *ast.Node, meaning ast.SymbolFlags, ign
}

func (c *Checker) resolveQualifiedName(name *ast.Node, left *ast.Node, right *ast.Node, meaning ast.SymbolFlags, ignoreErrors bool, dontResolveAlias bool, location *ast.Node) *ast.Symbol {
namespace := c.resolveEntityName(left, ast.SymbolFlagsNamespace, ignoreErrors, false /*dontResolveAlias*/, location)
namespace := c.resolveEntityName(left, ast.SymbolFlagsNamespace, true /*ignoreErrors*/, false /*dontResolveAlias*/, location)
if namespace == nil || ast.NodeIsMissing(right) {
return nil
var immediate *ast.Symbol
alias := c.resolveEntityName(left, ast.SymbolFlagsAlias, true /*ignoreErrors*/, true /*dontResolveAlias*/, location)
if alias != nil {
immediate = c.getImmediateAliasedSymbol(alias)
if immediate != nil && !core.Some(immediate.Declarations, func(d *ast.Node) bool { return d.Kind == ast.KindJSExportAssignment }) {
immediate = nil
}
}
if immediate == nil {
if !ignoreErrors {
c.resolveEntityName(left, ast.SymbolFlagsNamespace, ignoreErrors, false /*dontResolveAlias*/, location)
}
return nil
} else {
namespace = immediate
}
}
if namespace == c.unknownSymbol {
return namespace
Expand Down Expand Up @@ -23529,9 +23544,20 @@ func (c *Checker) getTypeFromImportTypeNode(node *ast.Node) *Type {
}
next := core.OrElse(symbolFromModule, symbolFromVariable)
if next == nil {
c.error(current, diagnostics.Namespace_0_has_no_exported_member_1, c.getFullyQualifiedName(currentNamespace, nil), scanner.DeclarationNameToString(current))
links.resolvedType = c.errorType
return links.resolvedType
var symbolFromImmediateModule *ast.Symbol
if currentNamespace == moduleSymbol {
immediateModuleSymbol := c.resolveExternalModuleSymbol(innerModuleSymbol, true /*dontResolveAlias*/)
if immediateModuleSymbol != nil && core.Some(immediateModuleSymbol.Declarations, func(d *ast.Node) bool { return d.Kind == ast.KindJSExportAssignment }) {
symbolFromImmediateModule = c.getSymbol(c.getExportsOfSymbol(immediateModuleSymbol), current.Text(), meaning)
}
}
if symbolFromImmediateModule != nil {
next = symbolFromImmediateModule
} else {
c.error(current, diagnostics.Namespace_0_has_no_exported_member_1, c.getFullyQualifiedName(currentNamespace, nil), scanner.DeclarationNameToString(current))
links.resolvedType = c.errorType
return links.resolvedType
}
}
c.symbolNodeLinks.Get(current).resolvedSymbol = next
c.symbolNodeLinks.Get(current.Parent).resolvedSymbol = next
Expand Down
1 change: 0 additions & 1 deletion internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) {
func (p *Parser) reparseUnhosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node) {
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.

This file was deleted.

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,11 +1,10 @@
enumDef.js(16,18): error TS2339: Property 'Blah' does not exist on type '{ Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }'.
index.js(4,17): error TS2503: Cannot find namespace 'Host'.
index.js(8,21): error TS2304: Cannot find name 'Host'.
index.js(13,11): error TS2503: Cannot find namespace 'Host'.
index.js(18,11): error TS2503: Cannot find namespace 'Host'.


==== enumDef.js (1 errors) ====
==== enumDef.js (0 errors) ====
var Host = {};
Host.UserMetrics = {};
/** @enum {number} */
Expand All @@ -22,8 +21,6 @@ index.js(18,11): error TS2503: Cannot find namespace 'Host'.
* @typedef {string}
*/
Host.UserMetrics.Blah = {
~~~~
!!! error TS2339: Property 'Blah' does not exist on type '{ Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }'.
x: 12
}
==== index.js (4 errors) ====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ Host.UserMetrics.Action = {
* @typedef {string}
*/
Host.UserMetrics.Blah = {
>Host.UserMetrics.Blah : Symbol(Blah, Decl(enumDef.js, 8, 2))
>Host.UserMetrics : Symbol(UserMetrics, Decl(enumDef.js, 0, 14))
>Host : Symbol(Host, Decl(enumDef.js, 0, 3), Decl(enumDef.js, 10, 3))
>UserMetrics : Symbol(UserMetrics, Decl(enumDef.js, 0, 14))
>Blah : Symbol(Blah, Decl(enumDef.js, 8, 2))

x: 12
>x : Symbol(x, Decl(enumDef.js, 15, 25))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
->Host : Symbol(Host, Decl(enumDef.js, 0, 3), Decl(enumDef.js, 0, 14), Decl(enumDef.js, 1, 22), Decl(enumDef.js, 8, 2), Decl(enumDef.js, 10, 21))
->UserMetrics : Symbol(Host.UserMetrics, Decl(enumDef.js, 0, 14), Decl(enumDef.js, 3, 5), Decl(enumDef.js, 15, 5), Decl(enumDef.js, 10, 26))
->Blah : Symbol(Host.UserMetrics.Blah, Decl(enumDef.js, 8, 2), Decl(enumDef.js, 15, 17), Decl(enumDef.js, 13, 3))
+>Host.UserMetrics.Blah : Symbol(Blah, Decl(enumDef.js, 8, 2))
+>Host.UserMetrics : Symbol(UserMetrics, Decl(enumDef.js, 0, 14))
+>Host : Symbol(Host, Decl(enumDef.js, 0, 3), Decl(enumDef.js, 10, 3))
+>UserMetrics : Symbol(UserMetrics, Decl(enumDef.js, 0, 14))
+>Blah : Symbol(Blah, Decl(enumDef.js, 8, 2))

x: 12
>x : Symbol(x, Decl(enumDef.js, 15, 25))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

=== enumDef.js ===
var Host = {};
>Host : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }; }
>{} : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }; }
>Host : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }; }
>{} : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }; }

Host.UserMetrics = {};
>Host.UserMetrics = {} : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }
>Host.UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }
>Host : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }; }
>UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }
>{} : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }
>Host.UserMetrics = {} : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }
>Host.UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }
>Host : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }; }
>UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }
>{} : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }

/** @enum {number} */
Host.UserMetrics.Action = {
>Host.UserMetrics.Action = { WindowDocked: 1, WindowUndocked: 2, ScriptsBreakpointSet: 3, TimelineStarted: 4,} : { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }
>Host.UserMetrics.Action : { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }
>Host.UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }
>Host : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }; }
>UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }
>Host.UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }
>Host : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }; }
>UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }
>Action : { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }
>{ WindowDocked: 1, WindowUndocked: 2, ScriptsBreakpointSet: 3, TimelineStarted: 4,} : { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }

Expand Down Expand Up @@ -47,11 +47,11 @@ Host.UserMetrics.Action = {
*/
Host.UserMetrics.Blah = {
>Host.UserMetrics.Blah = { x: 12} : { x: number; }
>Host.UserMetrics.Blah : any
>Host.UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }
>Host : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }; }
>UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; }
>Blah : any
>Host.UserMetrics.Blah : { x: number; }
>Host.UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }
>Host : { UserMetrics: { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }; }
>UserMetrics : { Action: { WindowDocked: number; WindowUndocked: number; ScriptsBreakpointSet: number; TimelineStarted: number; }; Blah: { x: number; }; }
>Blah : { x: number; }
>{ x: 12} : { x: number; }

x: 12
Expand Down
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.

This file was deleted.

Loading