Skip to content

Commit f0da3d7

Browse files
author
Andy
authored
Fix declaration emit for typeof default export (#19471)
* Fix declaration emit for `typeof` default export * Add comment
1 parent 505ffab commit f0da3d7

7 files changed

+83
-10
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,7 +2496,7 @@ namespace ts {
24962496
if (type.flags & TypeFlags.EnumLiteral && !(type.flags & TypeFlags.Union)) {
24972497
const parentSymbol = getParentOfSymbol(type.symbol);
24982498
const parentName = symbolToName(parentSymbol, context, SymbolFlags.Type, /*expectsIdentifier*/ false);
2499-
const enumLiteralName = getDeclaredTypeOfSymbol(parentSymbol) === type ? parentName : createQualifiedName(parentName, getNameOfSymbol(type.symbol, context));
2499+
const enumLiteralName = getDeclaredTypeOfSymbol(parentSymbol) === type ? parentName : createQualifiedName(parentName, symbolName(type.symbol));
25002500
return createTypeReferenceNode(enumLiteralName, /*typeArguments*/ undefined);
25012501
}
25022502
if (type.flags & TypeFlags.EnumLike) {
@@ -3016,8 +3016,7 @@ namespace ts {
30163016
typeParameterNodes = mapToTypeNodes(typeParameters, context);
30173017
}
30183018

3019-
const symbolName = getNameOfSymbol(symbol, context);
3020-
const identifier = setEmitFlags(createIdentifier(symbolName, typeParameterNodes), EmitFlags.NoAsciiEscaping);
3019+
const identifier = setEmitFlags(createIdentifier(getNameOfSymbolAsWritten(symbol, context), typeParameterNodes), EmitFlags.NoAsciiEscaping);
30213020

30223021
return index > 0 ? createQualifiedName(createEntityNameFromSymbolChain(chain, index - 1), identifier) : identifier;
30233022
}
@@ -3129,7 +3128,14 @@ namespace ts {
31293128
symbolStack: Symbol[] | undefined;
31303129
}
31313130

3132-
function getNameOfSymbol(symbol: Symbol, context?: NodeBuilderContext): string {
3131+
/**
3132+
* Gets a human-readable name for a symbol.
3133+
* Should *not* be used for the right-hand side of a `.` -- use `symbolName(symbol)` for that instead.
3134+
*
3135+
* Unlike `symbolName(symbol)`, this will include quotes if the name is from a string literal.
3136+
* It will also use a representation of a number as written instead of a decimal form, e.g. `0o11` instead of `9`.
3137+
*/
3138+
function getNameOfSymbolAsWritten(symbol: Symbol, context?: NodeBuilderContext): string {
31333139
if (symbol.declarations && symbol.declarations.length) {
31343140
const declaration = symbol.declarations[0];
31353141
const name = getNameOfDeclaration(declaration);
@@ -3166,7 +3172,7 @@ namespace ts {
31663172
* for the name of the symbol if it is available to match how the user wrote the name.
31673173
*/
31683174
function appendSymbolNameOnly(symbol: Symbol, writer: SymbolWriter): void {
3169-
writer.writeSymbol(getNameOfSymbol(symbol), symbol);
3175+
writer.writeSymbol(getNameOfSymbolAsWritten(symbol), symbol);
31703176
}
31713177

31723178
/**
@@ -3175,7 +3181,7 @@ namespace ts {
31753181
* ensuring that any names written with literals use element accesses.
31763182
*/
31773183
function appendPropertyOrElementAccessForSymbol(symbol: Symbol, writer: SymbolWriter): void {
3178-
const symbolName = getNameOfSymbol(symbol);
3184+
const symbolName = symbol.escapedName === "default" ? "default" : getNameOfSymbolAsWritten(symbol);
31793185
const firstChar = symbolName.charCodeAt(0);
31803186
const needsElementAccess = !isIdentifierStart(firstChar, languageVersion);
31813187

@@ -18999,7 +19005,7 @@ namespace ts {
1899919005
case "arguments":
1900019006
case "prototype":
1900119007
const message = Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1;
19002-
const className = getNameOfSymbol(getSymbolOfNode(node));
19008+
const className = getNameOfSymbolAsWritten(getSymbolOfNode(node));
1900319009
error(memberNameNode, message, memberName, className);
1900419010
break;
1900519011
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [tests/cases/compiler/declarationEmitTypeofDefaultExport.ts] ////
2+
3+
//// [a.ts]
4+
export default class C {};
5+
6+
//// [b.ts]
7+
import * as a from "./a";
8+
export default a.default;
9+
10+
11+
//// [a.js]
12+
"use strict";
13+
exports.__esModule = true;
14+
var C = /** @class */ (function () {
15+
function C() {
16+
}
17+
return C;
18+
}());
19+
exports["default"] = C;
20+
;
21+
//// [b.js]
22+
"use strict";
23+
exports.__esModule = true;
24+
var a = require("./a");
25+
exports["default"] = a["default"];
26+
27+
28+
//// [a.d.ts]
29+
export default class C {
30+
}
31+
//// [b.d.ts]
32+
import * as a from "./a";
33+
declare const _default: typeof a.default;
34+
export default _default;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== /a.ts ===
2+
export default class C {};
3+
>C : Symbol(C, Decl(a.ts, 0, 0))
4+
5+
=== /b.ts ===
6+
import * as a from "./a";
7+
>a : Symbol(a, Decl(b.ts, 0, 6))
8+
9+
export default a.default;
10+
>a.default : Symbol(a.default, Decl(a.ts, 0, 0))
11+
>a : Symbol(a, Decl(b.ts, 0, 6))
12+
>default : Symbol(a.default, Decl(a.ts, 0, 0))
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== /a.ts ===
2+
export default class C {};
3+
>C : C
4+
5+
=== /b.ts ===
6+
import * as a from "./a";
7+
>a : typeof a
8+
9+
export default a.default;
10+
>a.default : typeof a.C
11+
>a : typeof a
12+
>default : typeof a.C
13+

tests/baselines/reference/literalsInComputedProperties1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ enum X {
172172

173173
let a = X["foo"];
174174
>a : X
175-
>X["foo"] : X."foo"
175+
>X["foo"] : X.foo
176176
>X : typeof X
177177
>"foo" : "foo"
178178

179179
let a0 = X["bar"];
180180
>a0 : X
181-
>X["bar"] : X.["bar"]
181+
>X["bar"] : X.bar
182182
>X : typeof X
183183
>"bar" : "bar"
184184

tests/baselines/reference/negateOperatorWithEnumType.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var ResultIsNumber3 = -(ENUM1.B + ENUM1[""]);
3131
>ENUM1.B : ENUM1.B
3232
>ENUM1 : typeof ENUM1
3333
>B : ENUM1.B
34-
>ENUM1[""] : ENUM1.""
34+
>ENUM1[""] : ENUM1.
3535
>ENUM1 : typeof ENUM1
3636
>"" : ""
3737

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @declaration: true
2+
// @filename: /a.ts
3+
export default class C {};
4+
5+
// @filename: /b.ts
6+
import * as a from "./a";
7+
export default a.default;

0 commit comments

Comments
 (0)