Skip to content
Closed
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: 32 additions & 0 deletions internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type Binder struct {
singleDeclarationsPool core.Pool[*ast.Node]
expandoAssignments []ExpandoAssignmentInfo
nestedCJSExports []*ast.Node
staleCJSIndicator bool
firstValidCJSExport *ast.Node
}

type ActiveLabel struct {
Expand Down Expand Up @@ -126,6 +128,7 @@ func bindSourceFile(file *ast.SourceFile) {
b.file = file
b.unreachableFlow = b.newFlowNode(ast.FlowFlagsUnreachable)
b.bind(file.AsNode())
b.fixStaleCJSIndicator()
b.bindDeferredExpandoAssignments()
file.SymbolCount = b.symbolCount
file.ClassifiableNames = b.classifiableNames
Expand Down Expand Up @@ -659,6 +662,15 @@ func (b *Binder) bind(node *ast.Node) bool {
node.AsBindingElement().FlowNode = b.currentFlow
b.bindVariableDeclarationOrBindingElement(node)
case ast.KindCommonJSExport:
if !ast.IsSourceFile(b.blockScopeContainer) && b.lookupName("exports", b.blockScopeContainer) != nil {
if b.file.CommonJSModuleIndicator == node {
b.staleCJSIndicator = true
}
break
}
if b.firstValidCJSExport == nil {
b.firstValidCJSExport = node
}
b.trackNestedCJSExport(node)
b.declareModuleMember(node, ast.SymbolFlagsFunctionScopedVariable, ast.SymbolFlagsFunctionScopedVariableExcludes)
case ast.KindPropertyDeclaration, ast.KindPropertySignature:
Expand Down Expand Up @@ -862,6 +874,15 @@ func (b *Binder) bindExportDeclaration(node *ast.Node) {
func (b *Binder) bindExportAssignment(node *ast.Node) {
container := b.container
if ast.IsJSExportAssignment(node) {
if !ast.IsSourceFile(b.blockScopeContainer) && b.lookupName("module", b.blockScopeContainer) != nil {
if b.file.CommonJSModuleIndicator == node {
b.staleCJSIndicator = true
}
return
}
if b.firstValidCJSExport == nil {
b.firstValidCJSExport = node
}
container = b.file.AsNode()
b.trackNestedCJSExport(node)
}
Expand Down Expand Up @@ -951,9 +972,20 @@ func (b *Binder) setCommonJSModuleIndicator(node *ast.Node) bool {
b.bindSourceFileAsExternalModule()
}
}
if b.firstValidCJSExport == nil {
b.firstValidCJSExport = node
}
return true
}

// fixStaleCJSIndicator corrects CommonJSModuleIndicator if it pointed to a skipped (locally-shadowed) CJS export.
func (b *Binder) fixStaleCJSIndicator() {
if !b.staleCJSIndicator {
return
}
b.file.CommonJSModuleIndicator = b.firstValidCJSExport
}

func (b *Binder) bindClassLikeDeclaration(node *ast.Node) {
name := node.Name()
switch node.Kind {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/dep.js(1,20): error TS7006: Parameter 'exports' implicitly has an 'any' type.


==== /dep.js (1 errors) ====
function something(exports) {
~~~~~~~
!!! error TS7006: Parameter 'exports' implicitly has an 'any' type.
exports.foo = 8;
}

export const foo = 7;

==== /main.ts (0 errors) ====
import { foo } from "./dep.js";

export const bar = foo + 1;

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/exportsInFunctionParam.ts] ////

=== /dep.js ===
function something(exports) {
>something : Symbol(something, Decl(dep.js, 0, 0))
>exports : Symbol(exports, Decl(dep.js, 0, 19))

exports.foo = 8;
>exports : Symbol(exports, Decl(dep.js, 0, 19))
}

export const foo = 7;
>foo : Symbol(foo, Decl(dep.js, 4, 12))

=== /main.ts ===
import { foo } from "./dep.js";
>foo : Symbol(foo, Decl(main.ts, 0, 8))

export const bar = foo + 1;
>bar : Symbol(bar, Decl(main.ts, 2, 12))
>foo : Symbol(foo, Decl(main.ts, 0, 8))

29 changes: 29 additions & 0 deletions testdata/baselines/reference/compiler/exportsInFunctionParam.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/exportsInFunctionParam.ts] ////

=== /dep.js ===
function something(exports) {
>something : (exports: any) => void
>exports : any

exports.foo = 8;
>exports.foo = 8 : 8
>exports.foo : any
>exports : any
>foo : any
>8 : 8
}

export const foo = 7;
>foo : 7
>7 : 7

=== /main.ts ===
import { foo } from "./dep.js";
>foo : 7

export const bar = foo + 1;
>bar : number
>foo + 1 : number
>foo : 7
>1 : 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/dep.js(1,20): error TS7006: Parameter 'module' implicitly has an 'any' type.


==== /dep.js (1 errors) ====
const something = (module) => {
~~~~~~
!!! error TS7006: Parameter 'module' implicitly has an 'any' type.
module.exports = 8;
};

export default 7;

==== /main.ts (0 errors) ====
import DefaultExport from "./dep.js";

export default 3 + DefaultExport;

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/moduleExportsInArrowFunctionParam.ts] ////

=== /dep.js ===
const something = (module) => {
>something : Symbol(something, Decl(dep.js, 0, 5))
>module : Symbol(module, Decl(dep.js, 0, 19))

module.exports = 8;
>module : Symbol(module, Decl(dep.js, 0, 19))

};

export default 7;

=== /main.ts ===
import DefaultExport from "./dep.js";
>DefaultExport : Symbol(DefaultExport, Decl(main.ts, 0, 6))

export default 3 + DefaultExport;
>DefaultExport : Symbol(DefaultExport, Decl(main.ts, 0, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/moduleExportsInArrowFunctionParam.ts] ////

=== /dep.js ===
const something = (module) => {
>something : (module: any) => void
>(module) => { module.exports = 8;} : (module: any) => void
>module : any

module.exports = 8;
>module.exports = 8 : 8
>module.exports : any
>module : any
>exports : any
>8 : 8

};

export default 7;

=== /main.ts ===
import DefaultExport from "./dep.js";
>DefaultExport : 7

export default 3 + DefaultExport;
>3 + DefaultExport : number
>3 : 3
>DefaultExport : 7

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/dep.js(1,20): error TS7006: Parameter 'module' implicitly has an 'any' type.


==== /dep.js (1 errors) ====
function something(module) {
~~~~~~
!!! error TS7006: Parameter 'module' implicitly has an 'any' type.
module.exports = 8;
}

export default 7;

==== /main.ts (0 errors) ====
import DefaultExport from "./dep.js";

export default 3 + DefaultExport;

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/moduleExportsInFunctionParam.ts] ////

=== /dep.js ===
function something(module) {
>something : Symbol(something, Decl(dep.js, 0, 0))
>module : Symbol(module, Decl(dep.js, 0, 19))

module.exports = 8;
>module : Symbol(module, Decl(dep.js, 0, 19))
}

export default 7;

=== /main.ts ===
import DefaultExport from "./dep.js";
>DefaultExport : Symbol(DefaultExport, Decl(main.ts, 0, 6))

export default 3 + DefaultExport;
>DefaultExport : Symbol(DefaultExport, Decl(main.ts, 0, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [tests/cases/compiler/moduleExportsInFunctionParam.ts] ////

=== /dep.js ===
function something(module) {
>something : (module: any) => void
>module : any

module.exports = 8;
>module.exports = 8 : 8
>module.exports : any
>module : any
>exports : any
>8 : 8
}

export default 7;

=== /main.ts ===
import DefaultExport from "./dep.js";
>DefaultExport : 7

export default 3 + DefaultExport;
>3 + DefaultExport : number
>3 : 3
>DefaultExport : 7

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/dep.js(1,20): error TS7006: Parameter 'module' implicitly has an 'any' type.


==== /dep.js (1 errors) ====
function something(module) {
~~~~~~
!!! error TS7006: Parameter 'module' implicitly has an 'any' type.
module.exports = 8;
}

export const foo = 7;

==== /main.ts (0 errors) ====
import { foo } from "./dep.js";

export const bar = foo + 1;

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/moduleExportsInFunctionParamNamed.ts] ////

=== /dep.js ===
function something(module) {
>something : Symbol(something, Decl(dep.js, 0, 0))
>module : Symbol(module, Decl(dep.js, 0, 19))

module.exports = 8;
>module : Symbol(module, Decl(dep.js, 0, 19))
}

export const foo = 7;
>foo : Symbol(foo, Decl(dep.js, 4, 12))

=== /main.ts ===
import { foo } from "./dep.js";
>foo : Symbol(foo, Decl(main.ts, 0, 8))

export const bar = foo + 1;
>bar : Symbol(bar, Decl(main.ts, 2, 12))
>foo : Symbol(foo, Decl(main.ts, 0, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/moduleExportsInFunctionParamNamed.ts] ////

=== /dep.js ===
function something(module) {
>something : (module: any) => void
>module : any

module.exports = 8;
>module.exports = 8 : 8
>module.exports : any
>module : any
>exports : any
>8 : 8
}

export const foo = 7;
>foo : 7
>7 : 7

=== /main.ts ===
import { foo } from "./dep.js";
>foo : 7

export const bar = foo + 1;
>bar : number
>foo + 1 : number
>foo : 7
>1 : 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/dep.js(1,16): error TS7006: Parameter 'module' implicitly has an 'any' type.


==== /dep.js (1 errors) ====
function setup(module) {
~~~~~~
!!! error TS7006: Parameter 'module' implicitly has an 'any' type.
module.exports = { configured: true };
}

export default 7;

==== /main.ts (0 errors) ====
import DefaultExport from "./dep.js";

export default 3 + DefaultExport;

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/moduleExportsInFunctionParamOnlyShadowed.ts] ////

=== /dep.js ===
function setup(module) {
>setup : Symbol(setup, Decl(dep.js, 0, 0))
>module : Symbol(module, Decl(dep.js, 0, 15))

module.exports = { configured: true };
>module : Symbol(module, Decl(dep.js, 0, 15))
>configured : Symbol(configured, Decl(dep.js, 1, 22))
}

export default 7;

=== /main.ts ===
import DefaultExport from "./dep.js";
>DefaultExport : Symbol(DefaultExport, Decl(main.ts, 0, 6))

export default 3 + DefaultExport;
>DefaultExport : Symbol(DefaultExport, Decl(main.ts, 0, 6))

Loading