Skip to content

Commit 3ee926e

Browse files
Preserve primitive literals as written in code.
1 parent a7a13de commit 3ee926e

File tree

93 files changed

+807
-770
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+807
-770
lines changed

src/compiler/transformers/declarations.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
canHaveModifiers,
1515
canProduceDiagnostics,
1616
ClassDeclaration,
17-
clonePrimitiveLiteralValue,
1817
compact,
1918
concatenate,
2019
ConditionalTypeNode,
@@ -99,7 +98,6 @@ import {
9998
isClassElement,
10099
isComputedPropertyName,
101100
isDeclaration,
102-
isDeclarationReadonly,
103101
isEntityName,
104102
isEntityNameExpression,
105103
isExpandoPropertyDeclaration,
@@ -147,7 +145,6 @@ import {
147145
isTypeParameterDeclaration,
148146
isTypeQueryNode,
149147
isVarAwaitUsing,
150-
isVarConstLike,
151148
isVariableDeclaration,
152149
isVarUsing,
153150
LateBoundDeclaration,
@@ -698,15 +695,12 @@ export function transformDeclarations(context: TransformationContext) {
698695
}
699696

700697
function shouldPrintWithInitializer(node: Node): node is CanHaveLiteralInitializer & { initializer: Expression } {
701-
if (canHaveLiteralInitializer(node) && node.initializer && !node.type && (isDeclarationReadonly(node) || isVariableDeclaration(node) && isVarConstLike(node))) {
702-
return isPrimitiveLiteralValue(node.initializer);
703-
}
704-
return false;
698+
return canHaveLiteralInitializer(node) && !node.type && resolver.isLiteralConstDeclaration(getParseTreeNode(node) as CanHaveLiteralInitializer); // TODO: Make safe
705699
}
706700

707701
function ensureNoInitializer(node: CanHaveLiteralInitializer) {
708702
if (shouldPrintWithInitializer(node)) {
709-
return isPrimitiveLiteralValue(node.initializer) ? clonePrimitiveLiteralValue(node.initializer) : resolver.createLiteralConstValue(getParseTreeNode(node, canHaveLiteralInitializer)!, symbolTracker);
703+
return isPrimitiveLiteralValue(node.initializer) ? node.initializer : resolver.createLiteralConstValue(getParseTreeNode(node, canHaveLiteralInitializer)!, symbolTracker);
710704
}
711705
return undefined;
712706
}

src/compiler/utilities.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10650,20 +10650,19 @@ export function getNameFromImportAttribute(node: ImportAttribute) {
1065010650
}
1065110651

1065210652
/** @internal */
10653-
export function isPrimitiveLiteralValue(node: Expression, includeBigInt = true): node is PrimitiveLiteral {
10653+
export function isPrimitiveLiteralValue(node: Expression): node is PrimitiveLiteral {
1065410654
Debug.type<PrimitiveLiteral>(node);
1065510655
switch (node.kind) {
1065610656
case SyntaxKind.TrueKeyword:
1065710657
case SyntaxKind.FalseKeyword:
1065810658
case SyntaxKind.NumericLiteral:
1065910659
case SyntaxKind.StringLiteral:
1066010660
case SyntaxKind.NoSubstitutionTemplateLiteral:
10661-
return true;
1066210661
case SyntaxKind.BigIntLiteral:
10663-
return includeBigInt;
10662+
return true;
1066410663
case SyntaxKind.PrefixUnaryExpression:
1066510664
if (node.operator === SyntaxKind.MinusToken) {
10666-
return isNumericLiteral(node.operand) || (includeBigInt && isBigIntLiteral(node.operand));
10665+
return isNumericLiteral(node.operand) || isBigIntLiteral(node.operand);
1066710666
}
1066810667
if (node.operator === SyntaxKind.PlusToken) {
1066910668
return isNumericLiteral(node.operand);
@@ -10674,40 +10673,3 @@ export function isPrimitiveLiteralValue(node: Expression, includeBigInt = true):
1067410673
return false;
1067510674
}
1067610675
}
10677-
10678-
/**
10679-
* @internal
10680-
*
10681-
* Clone literal value while normalizing it (converts octals/hex to base 10, uses double quotes strings)
10682-
*/
10683-
export function clonePrimitiveLiteralValue<T extends PrimitiveLiteral>(node: T): T;
10684-
export function clonePrimitiveLiteralValue(node: PrimitiveLiteral): PrimitiveLiteral {
10685-
switch (node.kind) {
10686-
case SyntaxKind.NumericLiteral:
10687-
return factory.createNumericLiteral(node.text);
10688-
case SyntaxKind.BigIntLiteral:
10689-
return factory.createBigIntLiteral({ negative: false, base10Value: parsePseudoBigInt(node.text) });
10690-
case SyntaxKind.StringLiteral:
10691-
case SyntaxKind.NoSubstitutionTemplateLiteral:
10692-
return factory.createStringLiteral(node.text);
10693-
case SyntaxKind.FalseKeyword:
10694-
return factory.createFalse();
10695-
case SyntaxKind.TrueKeyword:
10696-
return factory.createTrue();
10697-
case SyntaxKind.PrefixUnaryExpression:
10698-
Debug.assert(isNumericLiteral(node.operand) || isBigIntLiteral(node.operand));
10699-
if (node.operator === SyntaxKind.PlusToken) {
10700-
return clonePrimitiveLiteralValue(node.operand);
10701-
}
10702-
else if (node.operator === SyntaxKind.MinusToken) {
10703-
return factory.createPrefixUnaryExpression(
10704-
node.operator,
10705-
clonePrimitiveLiteralValue(node.operand),
10706-
);
10707-
}
10708-
Debug.fail(`Unable to clone prefixed unary expression with operator ${Debug.formatSyntaxKind(node.operator)}`);
10709-
break;
10710-
default:
10711-
Debug.assertNever(node, `Unable to clone unknown literal type.`);
10712-
}
10713-
}

tests/baselines/reference/ambientConstLiterals.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ declare enum E {
6161
}
6262
declare const c1 = "abc";
6363
declare const c2 = 123;
64-
declare const c3: "abc";
65-
declare const c4: 123;
66-
declare const c5: 123;
67-
declare const c6: -123;
64+
declare const c3 = "abc";
65+
declare const c4 = 123;
66+
declare const c5 = 123;
67+
declare const c6 = -123;
6868
declare const c7 = true;
69-
declare const c8: E.A;
70-
declare const c8b: (typeof E)["non identifier"];
69+
declare const c8 = E.A;
70+
declare const c8b = E["non identifier"];
7171
declare const c9: {
7272
x: string;
7373
};

tests/baselines/reference/classStaticBlock25(target=es2022).js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/classStaticBlock25(target=es2022).sourcemap.txt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,42 +214,48 @@ sourceFile:classStaticBlock25.ts
214214
2 >^^^^^^^^
215215
3 > ^^^^^^
216216
4 > ^
217-
5 > ^^^^
218-
6 > ^
219-
7 > ^->
217+
5 > ^^^
218+
6 > ^
219+
7 > ^
220+
8 > ^->
220221
1 >
221222
2 >
222223
3 > const
223224
4 > a
224-
5 > = 1
225-
6 > ;
225+
5 > =
226+
6 > 1
227+
7 > ;
226228
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
227229
2 >Emitted(1, 9) Source(1, 1) + SourceIndex(0)
228230
3 >Emitted(1, 15) Source(1, 7) + SourceIndex(0)
229231
4 >Emitted(1, 16) Source(1, 8) + SourceIndex(0)
230-
5 >Emitted(1, 20) Source(1, 12) + SourceIndex(0)
231-
6 >Emitted(1, 21) Source(1, 13) + SourceIndex(0)
232+
5 >Emitted(1, 19) Source(1, 11) + SourceIndex(0)
233+
6 >Emitted(1, 20) Source(1, 12) + SourceIndex(0)
234+
7 >Emitted(1, 21) Source(1, 13) + SourceIndex(0)
232235
---
233236
>>>declare const b = 2;
234237
1->
235238
2 >^^^^^^^^
236239
3 > ^^^^^^
237240
4 > ^
238-
5 > ^^^^
239-
6 > ^
241+
5 > ^^^
242+
6 > ^
243+
7 > ^
240244
1->
241245
>
242246
2 >
243247
3 > const
244248
4 > b
245-
5 > = 2
246-
6 > ;
249+
5 > =
250+
6 > 2
251+
7 > ;
247252
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0)
248253
2 >Emitted(2, 9) Source(2, 1) + SourceIndex(0)
249254
3 >Emitted(2, 15) Source(2, 7) + SourceIndex(0)
250255
4 >Emitted(2, 16) Source(2, 8) + SourceIndex(0)
251-
5 >Emitted(2, 20) Source(2, 12) + SourceIndex(0)
252-
6 >Emitted(2, 21) Source(2, 13) + SourceIndex(0)
256+
5 >Emitted(2, 19) Source(2, 11) + SourceIndex(0)
257+
6 >Emitted(2, 20) Source(2, 12) + SourceIndex(0)
258+
7 >Emitted(2, 21) Source(2, 13) + SourceIndex(0)
253259
---
254260
>>>declare class C {
255261
1 >

tests/baselines/reference/classStaticBlock25(target=esnext).js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/classStaticBlock25(target=esnext).sourcemap.txt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,42 +214,48 @@ sourceFile:classStaticBlock25.ts
214214
2 >^^^^^^^^
215215
3 > ^^^^^^
216216
4 > ^
217-
5 > ^^^^
218-
6 > ^
219-
7 > ^->
217+
5 > ^^^
218+
6 > ^
219+
7 > ^
220+
8 > ^->
220221
1 >
221222
2 >
222223
3 > const
223224
4 > a
224-
5 > = 1
225-
6 > ;
225+
5 > =
226+
6 > 1
227+
7 > ;
226228
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
227229
2 >Emitted(1, 9) Source(1, 1) + SourceIndex(0)
228230
3 >Emitted(1, 15) Source(1, 7) + SourceIndex(0)
229231
4 >Emitted(1, 16) Source(1, 8) + SourceIndex(0)
230-
5 >Emitted(1, 20) Source(1, 12) + SourceIndex(0)
231-
6 >Emitted(1, 21) Source(1, 13) + SourceIndex(0)
232+
5 >Emitted(1, 19) Source(1, 11) + SourceIndex(0)
233+
6 >Emitted(1, 20) Source(1, 12) + SourceIndex(0)
234+
7 >Emitted(1, 21) Source(1, 13) + SourceIndex(0)
232235
---
233236
>>>declare const b = 2;
234237
1->
235238
2 >^^^^^^^^
236239
3 > ^^^^^^
237240
4 > ^
238-
5 > ^^^^
239-
6 > ^
241+
5 > ^^^
242+
6 > ^
243+
7 > ^
240244
1->
241245
>
242246
2 >
243247
3 > const
244248
4 > b
245-
5 > = 2
246-
6 > ;
249+
5 > =
250+
6 > 2
251+
7 > ;
247252
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0)
248253
2 >Emitted(2, 9) Source(2, 1) + SourceIndex(0)
249254
3 >Emitted(2, 15) Source(2, 7) + SourceIndex(0)
250255
4 >Emitted(2, 16) Source(2, 8) + SourceIndex(0)
251-
5 >Emitted(2, 20) Source(2, 12) + SourceIndex(0)
252-
6 >Emitted(2, 21) Source(2, 13) + SourceIndex(0)
256+
5 >Emitted(2, 19) Source(2, 11) + SourceIndex(0)
257+
6 >Emitted(2, 20) Source(2, 12) + SourceIndex(0)
258+
7 >Emitted(2, 21) Source(2, 13) + SourceIndex(0)
253259
---
254260
>>>declare class C {
255261
1 >

tests/baselines/reference/computedEnumTypeWidening.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ declare enum E2 {
170170
D
171171
}
172172
declare function f4(): void;
173-
declare const c1: E.B;
173+
declare const c1 = E.B;
174174
declare const c2: E.B;
175175
declare let v1: E;
176176
declare let v2: E.B;
177177
declare class C {
178178
p1: E;
179179
p2: E.B;
180-
readonly p3: E.B;
180+
readonly p3 = E.B;
181181
readonly p4: E.B;
182182
}
183183
declare enum MyEnum {

tests/baselines/reference/declarationEmitClassMemberNameConflict2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var Foo = /** @class */ (function () {
4646

4747

4848
//// [declarationEmitClassMemberNameConflict2.d.ts]
49-
declare const Bar = "bar";
49+
declare const Bar = 'bar';
5050
declare enum Hello {
5151
World = 0
5252
}

tests/baselines/reference/declarationEmitConstantNoWidening.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exports.Bar = Bar;
2121

2222

2323
//// [declarationEmitConstantNoWidening.d.ts]
24-
export declare const FOO = "FOO";
24+
export declare const FOO = 'FOO';
2525
export declare class Bar {
26-
readonly type: "FOO";
26+
readonly type = "FOO";
2727
}

tests/baselines/reference/declarationEmitEnumReadonlyProperty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ declare enum E {
3333
B = "b"
3434
}
3535
declare class C {
36-
readonly type: E.A;
36+
readonly type = E.A;
3737
}
3838
declare let x: E.A;

tests/baselines/reference/enumDeclarationEmitInitializerHasImport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ export declare enum Enum {
3434
}
3535
//// [consumer.d.ts]
3636
import provider = require('./provider');
37-
export declare const value: provider.Enum.Value1;
37+
export declare const value = provider.Enum.Value1;

tests/baselines/reference/fakeInfinity1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ export type NegativeInfinity = -1e999;
4343
export type TypeOfInfinity = typeof Infinity;
4444
export type TypeOfNaN = typeof NaN;
4545
export type Oops = 123456789123456789123456789123456789123456789123456789;
46-
export declare const oops = 1.2345678912345678e+53;
46+
export declare const oops = 123456789123456789123456789123456789123456789123456789;

tests/baselines/reference/initializerWithThisPropertyAccess.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ declare class C {
110110
}
111111
declare class Foo {
112112
private bar;
113-
readonly barProp: false;
113+
readonly barProp = false;
114114
constructor();
115115
}
116116
declare class Bar {

tests/baselines/reference/nodeModulesTopLevelAwait(module=node16).js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ for await (const y of []) { }
3737

3838

3939
//// [index.d.ts]
40-
declare const x: 1;
40+
declare const x = 1;
4141
export { x };
4242
//// [index.d.ts]
43-
declare const x: 1;
43+
declare const x = 1;
4444
export { x };

tests/baselines/reference/nodeModulesTopLevelAwait(module=nodenext).js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ for await (const y of []) { }
3737

3838

3939
//// [index.d.ts]
40-
declare const x: 1;
40+
declare const x = 1;
4141
export { x };
4242
//// [index.d.ts]
43-
declare const x: 1;
43+
declare const x = 1;
4444
export { x };

tests/baselines/reference/strictFunctionTypes1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ declare function fo(x: Object): void;
8181
declare function fs(x: string): void;
8282
declare function fx(f: (x: "def") => void): void;
8383
declare const x1: (x: string) => void;
84-
declare const x2: "abc";
84+
declare const x2 = "abc";
8585
declare const x3: "def" | "abc";
8686
declare const x4: Func<string>;
8787
declare const never: never;

tests/baselines/reference/strictPropertyInitialization.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ declare class C11 {
351351
a: number;
352352
constructor();
353353
}
354-
declare const a = "a";
354+
declare const a = 'a';
355355
declare const b: unique symbol;
356356
declare class C12 {
357357
[a]: number;

0 commit comments

Comments
 (0)