Skip to content

Commit 9b987eb

Browse files
authored
Dont bind a local symbol for a default export without a name (microsoft#23152)
1 parent 355125d commit 9b987eb

7 files changed

Lines changed: 86 additions & 5 deletions

src/compiler/binder.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,6 @@ namespace ts {
288288
}
289289
Debug.fail("Unknown binary declaration kind");
290290
break;
291-
292-
case SyntaxKind.FunctionDeclaration:
293-
case SyntaxKind.ClassDeclaration:
294-
return (hasModifier(node, ModifierFlags.Default) ? InternalSymbolName.Default : undefined);
295291
case SyntaxKind.JSDocFunctionType:
296292
return (isJSDocConstructSignature(node) ? InternalSymbolName.New : InternalSymbolName.Call);
297293
case SyntaxKind.Parameter:
@@ -459,6 +455,9 @@ namespace ts {
459455
if (node.kind === SyntaxKind.JSDocTypedefTag) Debug.assert(isInJavaScriptFile(node)); // We shouldn't add symbols for JSDoc nodes if not in a JS file.
460456
const isJSDocTypedefInJSDocNamespace = isJSDocTypedefTag(node) && node.name && node.name.kind === SyntaxKind.Identifier && node.name.isInJSDocNamespace;
461457
if ((!isAmbientModule(node) && (hasExportModifier || container.flags & NodeFlags.ExportContext)) || isJSDocTypedefInJSDocNamespace) {
458+
if (hasModifier(node, ModifierFlags.Default) && !getDeclarationName(node)) {
459+
return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); // No local symbol for an unnamed default!
460+
}
462461
const exportKind = symbolFlags & SymbolFlags.Value ? SymbolFlags.ExportValue : 0;
463462
const local = declareSymbol(container.locals, /*parent*/ undefined, node, exportKind, symbolExcludes);
464463
local.exportSymbol = declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes);

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ namespace ts {
12911291
}
12921292
}
12931293

1294-
if (result = lookup(moduleExports, name, meaning & SymbolFlags.ModuleMember)) {
1294+
if (name !== InternalSymbolName.Default && (result = lookup(moduleExports, name, meaning & SymbolFlags.ModuleMember))) {
12951295
break loop;
12961296
}
12971297
break;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/a.ts(4,24): error TS2304: Cannot find name 'default'.
2+
tests/cases/compiler/b.ts(2,24): error TS2304: Cannot find name 'default'.
3+
4+
5+
==== tests/cases/compiler/a.ts (1 errors) ====
6+
export default function () {
7+
return true;
8+
}
9+
export type X = typeof default; // expect error
10+
~~~~~~~
11+
!!! error TS2304: Cannot find name 'default'.
12+
13+
==== tests/cases/compiler/b.ts (1 errors) ====
14+
export default { a: true }
15+
export type X = typeof default; // expect error
16+
~~~~~~~
17+
!!! error TS2304: Cannot find name 'default'.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/defaultIsNotVisibleInLocalScope.ts] ////
2+
3+
//// [a.ts]
4+
export default function () {
5+
return true;
6+
}
7+
export type X = typeof default; // expect error
8+
9+
//// [b.ts]
10+
export default { a: true }
11+
export type X = typeof default; // expect error
12+
13+
//// [a.js]
14+
"use strict";
15+
exports.__esModule = true;
16+
function default_1() {
17+
return true;
18+
}
19+
exports["default"] = default_1;
20+
//// [b.js]
21+
"use strict";
22+
exports.__esModule = true;
23+
exports["default"] = { a: true };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default function () {
3+
return true;
4+
}
5+
export type X = typeof default; // expect error
6+
>X : Symbol(X, Decl(a.ts, 2, 1))
7+
8+
=== tests/cases/compiler/b.ts ===
9+
export default { a: true }
10+
>a : Symbol(a, Decl(b.ts, 0, 16))
11+
12+
export type X = typeof default; // expect error
13+
>X : Symbol(X, Decl(b.ts, 0, 26))
14+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default function () {
3+
return true;
4+
>true : true
5+
}
6+
export type X = typeof default; // expect error
7+
>X : any
8+
>default : any
9+
10+
=== tests/cases/compiler/b.ts ===
11+
export default { a: true }
12+
>{ a: true } : { a: boolean; }
13+
>a : boolean
14+
>true : true
15+
16+
export type X = typeof default; // expect error
17+
>X : any
18+
>default : any
19+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @filename: a.ts
2+
export default function () {
3+
return true;
4+
}
5+
export type X = typeof default; // expect error
6+
7+
// @filename: b.ts
8+
export default { a: true }
9+
export type X = typeof default; // expect error

0 commit comments

Comments
 (0)