Skip to content

Commit dc1ffe9

Browse files
Switched over to entityNameToString
Signed-off-by: Titian Cernicova-Dragomir <[email protected]>
1 parent e1bb300 commit dc1ffe9

12 files changed

+548
-81
lines changed

src/compiler/debug.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
AssertionLevel,
55
BigIntLiteralType,
66
CheckMode,
7+
Comparer,
78
compareValues,
89
EmitFlags,
910
every,
@@ -386,18 +387,18 @@ export namespace Debug {
386387
* Formats an enum value as a string for debugging and debug assertions.
387388
*/
388389
export function formatEnum(value = 0, enumObject: any, isFlags?: boolean) {
389-
const members = getEnumMembers(enumObject);
390+
const members = getEnumMembers(enumObject, isFlags);
390391
if (value === 0) {
391392
return members.length > 0 && members[0][0] === 0 ? members[0][1] : "0";
392393
}
393394
if (isFlags) {
394395
const result: string[] = [];
395396
let remainingFlags = value;
396397
for (const [enumValue, enumName] of members) {
397-
if (enumValue > value) {
398+
if (enumValue > remainingFlags) {
398399
break;
399400
}
400-
if (enumValue !== 0 && enumValue & value) {
401+
if (enumValue !== 0 && enumValue & remainingFlags) {
401402
result.push(enumName);
402403
remainingFlags &= ~enumValue;
403404
}
@@ -418,7 +419,7 @@ export namespace Debug {
418419

419420
const enumMemberCache = new Map<any, SortedReadonlyArray<[number, string]>>();
420421

421-
function getEnumMembers(enumObject: any) {
422+
function getEnumMembers(enumObject: any, isFlags?: boolean) {
422423
// Assuming enum objects do not change at runtime, we can cache the enum members list
423424
// to reuse later. This saves us from reconstructing this each and every time we call
424425
// a formatting function (which can be expensive for large enums like SyntaxKind).
@@ -435,7 +436,10 @@ export namespace Debug {
435436
}
436437
}
437438

438-
const sorted = stableSort<[number, string]>(result, (x, y) => compareValues(x[0], y[0]));
439+
const comparer: Comparer<[number, string]> = isFlags ?
440+
(x, y) => compareValues(x[0] >>> 0, y[0] >>> 0) :
441+
(x, y) => compareValues(x[0], y[0]);
442+
const sorted = stableSort(result, comparer);
439443
enumMemberCache.set(enumObject, sorted);
440444
return sorted;
441445
}

src/compiler/transformers/declarations/emitResolver.ts

Lines changed: 46 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
__String,
3+
BigIntLiteral,
34
bindSourceFile,
45
CompilerOptions,
56
ComputedPropertyName,
@@ -14,6 +15,7 @@ import {
1415
determineIfDeclarationIsVisible,
1516
ElementAccessExpression,
1617
emptyArray,
18+
entityNameToString,
1719
EnumDeclaration,
1820
EnumMember,
1921
ExportSpecifier,
@@ -35,7 +37,6 @@ import {
3537
Identifier,
3638
InternalSymbolName,
3739
isAccessor,
38-
isBigIntLiteral,
3940
isBinaryExpression,
4041
isComputedPropertyName,
4142
isDeclarationReadonly,
@@ -54,39 +55,39 @@ import {
5455
isIdentifier,
5556
isInfinityOrNaNString,
5657
isModuleDeclaration,
57-
isNumericLiteral,
58-
isPrefixUnaryExpression,
5958
isPrimitiveLiteralValue,
6059
isPropertyAccessExpression,
6160
isPropertyName,
6261
isSetAccessor,
6362
isSetAccessorDeclaration,
6463
isStringLiteralLike,
65-
isTemplateExpression,
6664
isVarConst,
6765
isVariableDeclaration,
6866
LateBoundDeclaration,
69-
MemberName,
7067
ModifierFlags,
7168
Node,
7269
NodeFlags,
7370
nodeIsPresent,
7471
NoSubstitutionTemplateLiteral,
72+
NumericLiteral,
7573
objectAllocator,
7674
ParameterDeclaration,
7775
parsePseudoBigInt,
76+
PrefixUnaryExpression,
7877
PropertyAccessExpression,
7978
PropertyDeclaration,
8079
PropertyName,
8180
PropertySignature,
8281
skipParentheses,
8382
some,
8483
SourceFile,
84+
StringLiteralLike,
8585
Symbol,
8686
SymbolAccessibility,
8787
SymbolFlags,
8888
SymbolTable,
8989
SyntaxKind,
90+
TemplateExpression,
9091
VariableDeclaration,
9192
} from "../../_namespaces/ts";
9293

@@ -357,48 +358,44 @@ export function createEmitDeclarationResolver(file: SourceFile, options: Compile
357358
onNumericLiteral() {},
358359
});
359360
function clonePrimitiveLiteralValue(node: Expression): Expression {
360-
if (isNumericLiteral(node)) {
361-
return factory.createNumericLiteral(node.text);
362-
}
363-
if (isBigIntLiteral(node)) {
364-
return factory.createBigIntLiteral({ negative: false, base10Value: parsePseudoBigInt(node.text) });
365-
}
366-
if (isStringLiteralLike(node)) {
367-
return factory.createStringLiteral(node.text);
368-
}
369-
370-
if (node.kind === SyntaxKind.FalseKeyword) {
371-
return factory.createFalse();
372-
}
373-
374-
if (node.kind === SyntaxKind.TrueKeyword) {
375-
return factory.createTrue();
376-
}
377-
378-
if (isPrefixUnaryExpression(node)) {
379-
return factory.createPrefixUnaryExpression(
380-
node.operator,
381-
clonePrimitiveLiteralValue(node.operand),
382-
);
383-
}
384-
if (isTemplateExpression(node)) {
385-
const evaluatedValue = evaluate(node);
386-
if (evaluatedValue !== undefined) {
387-
return factory.createStringLiteral(evaluatedValue);
388-
}
389-
return factory.createTemplateExpression(
390-
factory.createTemplateHead(node.head.text, node.head.rawText, node.head.templateFlags),
391-
node.templateSpans.map(t =>
392-
factory.createTemplateSpan(
393-
clonePrimitiveLiteralValue(t.expression),
394-
t.literal.kind === SyntaxKind.TemplateMiddle ?
395-
factory.createTemplateMiddle(t.literal.text, t.literal.rawText, t.literal.templateFlags) :
396-
factory.createTemplateTail(t.literal.text, t.literal.rawText, t.literal.templateFlags),
397-
)
398-
),
399-
);
361+
switch(node.kind) {
362+
case SyntaxKind.NumericLiteral:
363+
return factory.createNumericLiteral((node as NumericLiteral).text);
364+
case SyntaxKind.BigIntLiteral:
365+
return factory.createBigIntLiteral({ negative: false, base10Value: parsePseudoBigInt((node as BigIntLiteral).text) });
366+
case SyntaxKind.StringLiteral:
367+
case SyntaxKind.NoSubstitutionTemplateLiteral:
368+
return factory.createStringLiteral((node as StringLiteralLike).text);
369+
case SyntaxKind.FalseKeyword:
370+
return factory.createFalse();
371+
case SyntaxKind.TrueKeyword:
372+
return factory.createTrue();
373+
case SyntaxKind.PrefixUnaryExpression:
374+
return factory.createPrefixUnaryExpression(
375+
(node as PrefixUnaryExpression).operator,
376+
clonePrimitiveLiteralValue((node as PrefixUnaryExpression).operand),
377+
);
378+
case SyntaxKind.TemplateExpression:
379+
const templateExpression = node as TemplateExpression
380+
const evaluatedValue = evaluate(templateExpression);
381+
if (evaluatedValue !== undefined) {
382+
return factory.createStringLiteral(evaluatedValue);
383+
}
384+
const templateHead = templateExpression.head
385+
return factory.createTemplateExpression(
386+
factory.createTemplateHead(templateHead.text, templateHead.rawText, templateHead.templateFlags),
387+
templateExpression.templateSpans.map(t =>
388+
factory.createTemplateSpan(
389+
clonePrimitiveLiteralValue(t.expression),
390+
t.literal.kind === SyntaxKind.TemplateMiddle ?
391+
factory.createTemplateMiddle(t.literal.text, t.literal.rawText, t.literal.templateFlags) :
392+
factory.createTemplateTail(t.literal.text, t.literal.rawText, t.literal.templateFlags),
393+
)
394+
),
395+
);
396+
default:
397+
Debug.assert(false, `Unable to clone unknown literal type. Kind: ${node.kind}`);
400398
}
401-
Debug.assert(false, `Unable to clone unknown literal type. Kind: ${node.kind}`);
402399
}
403400

404401
function isLiteralConstDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration): boolean {
@@ -627,37 +624,12 @@ function getSymbolName(name: ElementAccessExpression | PropertyName): __String |
627624
return getDynamicSymbolName(name);
628625
}
629626

630-
function getEntityNameComponent(identifier: MemberName): __String {
631-
const name = getPropertyNameForPropertyNameNode(identifier);
632-
if (name && name.indexOf(".")) {
633-
return name.replace(/\./g, "..") as __String;
634-
}
635-
return name;
636-
}
637-
638627
function getDynamicSymbolName(name: ElementAccessExpression | PropertyName) {
639-
let computedName = isComputedPropertyName(name) ? name.expression :
628+
const computedName = isComputedPropertyName(name) ? name.expression :
640629
isElementAccessExpression(name) ? name.argumentExpression :
641630
undefined;
642-
643-
if (computedName) {
644-
let fullId = "__!";
645-
// We only support dotted identifiers as property keys
646-
while (true) {
647-
if (isIdentifier(computedName)) {
648-
const componentName = getEntityNameComponent(computedName);
649-
fullId += componentName;
650-
return fullId as __String;
651-
}
652-
else if (isPropertyAccessExpression(computedName)) {
653-
const componentName = getEntityNameComponent(computedName.name);
654-
computedName = computedName.expression;
655-
fullId += componentName + ".";
656-
}
657-
else {
658-
return undefined;
659-
}
660-
}
631+
if (computedName && isEntityNameExpression(computedName)) {
632+
return ("__!" + entityNameToString(computedName)) as __String;
661633
}
662634
return undefined;
663635
}

tests/baselines/reference/isolated-declarations/auto-fixed/diff/isolatedDeclarationErrorsExpandoFunctions.d.ts.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// [[Reason: Function declarations are not fixed.]] ////
1+
// [[Reason: Expando function declarations are not fixed.]] ////
22

33
//// [tests/cases/compiler/isolatedDeclarationErrorsExpandoFunctions.ts] ////
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// [[Reason: Expando function declarations are not fixed.]] ////
2+
3+
//// [tests/cases/compiler/isolatedDeclarationLazySymbols.ts] ////
4+
5+
===================================================================
6+
--- TSC declarations
7+
+++ DTE declarations
8+
@@ -1,11 +1,8 @@
9+
10+
11+
//// [isolatedDeclarationLazySymbols.d.ts]
12+
export declare function foo(): void;
13+
-export declare namespace foo {
14+
- var b: string;
15+
-}
16+
declare const o: {
17+
readonly "prop.inner": "a";
18+
readonly prop: {
19+
readonly inner: "b";
20+
@@ -20,12 +17,13 @@
21+
export {};
22+
//# sourceMappingURL=isolatedDeclarationLazySymbols.d.ts.map
23+
/// [Errors] ////
24+
25+
+isolatedDeclarationLazySymbols.ts(13,1): error TS9023: Assigning properties to functions without declaring them is not supported with --isolatedDeclarations. Add an explicit declaration for the properties assigned to this function.
26+
isolatedDeclarationLazySymbols.ts(16,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
27+
28+
29+
-==== isolatedDeclarationLazySymbols.ts (1 errors) ====
30+
+==== isolatedDeclarationLazySymbols.ts (2 errors) ====
31+
export function foo(): void {
32+
33+
}
34+
35+
@@ -37,8 +35,10 @@
36+
} as const
37+
38+
foo[o["prop.inner"]] ="A";
39+
foo[o.prop.inner] = "B";
40+
+ ~~~~~~~~~~~~~~~~~
41+
+!!! error TS9023: Assigning properties to functions without declaring them is not supported with --isolatedDeclarations. Add an explicit declaration for the properties assigned to this function.
42+
43+
export class Foo {
44+
[o["prop.inner"]] ="A"
45+
~~~~~~~~~~~~~~~~~
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//// [tests/cases/compiler/isolatedDeclarationLazySymbols.ts] ////
2+
3+
//// [isolatedDeclarationLazySymbols.ts]
4+
export function foo(): void {
5+
6+
}
7+
8+
const o = {
9+
["prop.inner"]: "a",
10+
prop: {
11+
inner: "b",
12+
}
13+
} as const
14+
15+
foo[o["prop.inner"]] ="A";
16+
foo[o.prop.inner] = "B";
17+
18+
export class Foo {
19+
[o["prop.inner"]] ="A"
20+
[o.prop.inner] = "B"
21+
}
22+
23+
export let oo: {
24+
a: string;
25+
[o.prop.inner]: string;
26+
} = {
27+
[o['prop.inner']]:"A",
28+
[o.prop.inner]: "B",
29+
}
30+
31+
/// [Declarations] ////
32+
33+
34+
35+
//// [isolatedDeclarationLazySymbols.d.ts]
36+
export declare function foo(): void;
37+
declare const o: {
38+
readonly "prop.inner": "a";
39+
readonly prop: {
40+
readonly inner: "b";
41+
};
42+
};
43+
export declare class Foo {
44+
}
45+
export declare let oo: {
46+
a: string;
47+
[o.prop.inner]: string;
48+
};
49+
export {};
50+
//# sourceMappingURL=isolatedDeclarationLazySymbols.d.ts.map
51+
/// [Errors] ////
52+
53+
isolatedDeclarationLazySymbols.ts(13,1): error TS9023: Assigning properties to functions without declaring them is not supported with --isolatedDeclarations. Add an explicit declaration for the properties assigned to this function.
54+
isolatedDeclarationLazySymbols.ts(16,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
55+
56+
57+
==== isolatedDeclarationLazySymbols.ts (2 errors) ====
58+
export function foo(): void {
59+
60+
}
61+
62+
const o = {
63+
["prop.inner"]: "a",
64+
prop: {
65+
inner: "b",
66+
}
67+
} as const
68+
69+
foo[o["prop.inner"]] ="A";
70+
foo[o.prop.inner] = "B";
71+
~~~~~~~~~~~~~~~~~
72+
!!! error TS9023: Assigning properties to functions without declaring them is not supported with --isolatedDeclarations. Add an explicit declaration for the properties assigned to this function.
73+
74+
export class Foo {
75+
[o["prop.inner"]] ="A"
76+
~~~~~~~~~~~~~~~~~
77+
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
78+
[o.prop.inner] = "B"
79+
}
80+
81+
export let oo: {
82+
a: string;
83+
[o.prop.inner]: string;
84+
} = {
85+
[o['prop.inner']]:"A",
86+
[o.prop.inner]: "B",
87+
}

0 commit comments

Comments
 (0)