Fix declaration emit for typeof default export (#19471)

* Fix declaration emit for `typeof` default export

* Add comment
This commit is contained in:
Andy 2017-10-30 11:40:32 -07:00 committed by GitHub
parent 505ffab745
commit f0da3d7336
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 83 additions and 10 deletions

View file

@ -2496,7 +2496,7 @@ namespace ts {
if (type.flags & TypeFlags.EnumLiteral && !(type.flags & TypeFlags.Union)) {
const parentSymbol = getParentOfSymbol(type.symbol);
const parentName = symbolToName(parentSymbol, context, SymbolFlags.Type, /*expectsIdentifier*/ false);
const enumLiteralName = getDeclaredTypeOfSymbol(parentSymbol) === type ? parentName : createQualifiedName(parentName, getNameOfSymbol(type.symbol, context));
const enumLiteralName = getDeclaredTypeOfSymbol(parentSymbol) === type ? parentName : createQualifiedName(parentName, symbolName(type.symbol));
return createTypeReferenceNode(enumLiteralName, /*typeArguments*/ undefined);
}
if (type.flags & TypeFlags.EnumLike) {
@ -3016,8 +3016,7 @@ namespace ts {
typeParameterNodes = mapToTypeNodes(typeParameters, context);
}
const symbolName = getNameOfSymbol(symbol, context);
const identifier = setEmitFlags(createIdentifier(symbolName, typeParameterNodes), EmitFlags.NoAsciiEscaping);
const identifier = setEmitFlags(createIdentifier(getNameOfSymbolAsWritten(symbol, context), typeParameterNodes), EmitFlags.NoAsciiEscaping);
return index > 0 ? createQualifiedName(createEntityNameFromSymbolChain(chain, index - 1), identifier) : identifier;
}
@ -3129,7 +3128,14 @@ namespace ts {
symbolStack: Symbol[] | undefined;
}
function getNameOfSymbol(symbol: Symbol, context?: NodeBuilderContext): string {
/**
* Gets a human-readable name for a symbol.
* Should *not* be used for the right-hand side of a `.` -- use `symbolName(symbol)` for that instead.
*
* Unlike `symbolName(symbol)`, this will include quotes if the name is from a string literal.
* It will also use a representation of a number as written instead of a decimal form, e.g. `0o11` instead of `9`.
*/
function getNameOfSymbolAsWritten(symbol: Symbol, context?: NodeBuilderContext): string {
if (symbol.declarations && symbol.declarations.length) {
const declaration = symbol.declarations[0];
const name = getNameOfDeclaration(declaration);
@ -3166,7 +3172,7 @@ namespace ts {
* for the name of the symbol if it is available to match how the user wrote the name.
*/
function appendSymbolNameOnly(symbol: Symbol, writer: SymbolWriter): void {
writer.writeSymbol(getNameOfSymbol(symbol), symbol);
writer.writeSymbol(getNameOfSymbolAsWritten(symbol), symbol);
}
/**
@ -3175,7 +3181,7 @@ namespace ts {
* ensuring that any names written with literals use element accesses.
*/
function appendPropertyOrElementAccessForSymbol(symbol: Symbol, writer: SymbolWriter): void {
const symbolName = getNameOfSymbol(symbol);
const symbolName = symbol.escapedName === "default" ? "default" : getNameOfSymbolAsWritten(symbol);
const firstChar = symbolName.charCodeAt(0);
const needsElementAccess = !isIdentifierStart(firstChar, languageVersion);
@ -18999,7 +19005,7 @@ namespace ts {
case "arguments":
case "prototype":
const message = Diagnostics.Static_property_0_conflicts_with_built_in_property_Function_0_of_constructor_function_1;
const className = getNameOfSymbol(getSymbolOfNode(node));
const className = getNameOfSymbolAsWritten(getSymbolOfNode(node));
error(memberNameNode, message, memberName, className);
break;
}

View file

@ -0,0 +1,34 @@
//// [tests/cases/compiler/declarationEmitTypeofDefaultExport.ts] ////
//// [a.ts]
export default class C {};
//// [b.ts]
import * as a from "./a";
export default a.default;
//// [a.js]
"use strict";
exports.__esModule = true;
var C = /** @class */ (function () {
function C() {
}
return C;
}());
exports["default"] = C;
;
//// [b.js]
"use strict";
exports.__esModule = true;
var a = require("./a");
exports["default"] = a["default"];
//// [a.d.ts]
export default class C {
}
//// [b.d.ts]
import * as a from "./a";
declare const _default: typeof a.default;
export default _default;

View file

@ -0,0 +1,13 @@
=== /a.ts ===
export default class C {};
>C : Symbol(C, Decl(a.ts, 0, 0))
=== /b.ts ===
import * as a from "./a";
>a : Symbol(a, Decl(b.ts, 0, 6))
export default a.default;
>a.default : Symbol(a.default, Decl(a.ts, 0, 0))
>a : Symbol(a, Decl(b.ts, 0, 6))
>default : Symbol(a.default, Decl(a.ts, 0, 0))

View file

@ -0,0 +1,13 @@
=== /a.ts ===
export default class C {};
>C : C
=== /b.ts ===
import * as a from "./a";
>a : typeof a
export default a.default;
>a.default : typeof a.C
>a : typeof a
>default : typeof a.C

View file

@ -172,13 +172,13 @@ enum X {
let a = X["foo"];
>a : X
>X["foo"] : X."foo"
>X["foo"] : X.foo
>X : typeof X
>"foo" : "foo"
let a0 = X["bar"];
>a0 : X
>X["bar"] : X.["bar"]
>X["bar"] : X.bar
>X : typeof X
>"bar" : "bar"

View file

@ -31,7 +31,7 @@ var ResultIsNumber3 = -(ENUM1.B + ENUM1[""]);
>ENUM1.B : ENUM1.B
>ENUM1 : typeof ENUM1
>B : ENUM1.B
>ENUM1[""] : ENUM1.""
>ENUM1[""] : ENUM1.
>ENUM1 : typeof ENUM1
>"" : ""

View file

@ -0,0 +1,7 @@
// @declaration: true
// @filename: /a.ts
export default class C {};
// @filename: /b.ts
import * as a from "./a";
export default a.default;