Skip to content

Commit da5b104

Browse files
Preserve initalizer nodes if they are primitive literals.
1 parent 75507b9 commit da5b104

File tree

79 files changed

+405
-269
lines changed

Some content is hidden

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

79 files changed

+405
-269
lines changed

src/compiler/transformers/declarations.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ import {
127127
isModifier,
128128
isModuleDeclaration,
129129
isOmittedExpression,
130+
isPrimitiveLiteralValue,
130131
isPrivateIdentifier,
131132
isSemicolonClassElement,
132133
isSetAccessorDeclaration,
@@ -208,6 +209,7 @@ import {
208209
TypeParameterDeclaration,
209210
TypeReferenceNode,
210211
unescapeLeadingUnderscores,
212+
unwrapParenthesizedExpression,
211213
VariableDeclaration,
212214
VariableDeclarationList,
213215
VariableStatement,
@@ -702,7 +704,8 @@ export function transformDeclarations(context: TransformationContext) {
702704

703705
function ensureNoInitializer(node: CanHaveLiteralInitializer) {
704706
if (shouldPrintWithInitializer(node)) {
705-
return resolver.createLiteralConstValue(getParseTreeNode(node) as CanHaveLiteralInitializer, symbolTracker); // TODO: Make safe
707+
const unwrappedInitializer = unwrapParenthesizedExpression(node.initializer);
708+
return isPrimitiveLiteralValue(unwrappedInitializer) ? unwrappedInitializer : resolver.createLiteralConstValue(getParseTreeNode(node, canHaveLiteralInitializer)!, symbolTracker);
706709
}
707710
return undefined;
708711
}

src/compiler/types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,15 @@ export type HasIllegalModifiers =
13761376
| MissingDeclaration
13771377
| NamespaceExportDeclaration;
13781378

1379+
/** @internal */
1380+
export type PrimitiveLiteral =
1381+
| BooleanLiteral
1382+
| NumericLiteral
1383+
| StringLiteral
1384+
| NoSubstitutionTemplateLiteral
1385+
| BigIntLiteral
1386+
| PrefixUnaryExpression;
1387+
13791388
/**
13801389
* Declarations that can contain other declarations. Corresponds with `ContainerFlags.IsContainer` in binder.ts.
13811390
*

src/compiler/utilities.ts

+43
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ import {
444444
Pattern,
445445
PostfixUnaryExpression,
446446
PrefixUnaryExpression,
447+
PrimitiveLiteral,
447448
PrinterOptions,
448449
PrintHandlers,
449450
PrivateIdentifier,
@@ -2343,6 +2344,14 @@ export function isLet(node: Node): boolean {
23432344
return (getCombinedNodeFlags(node) & NodeFlags.BlockScoped) === NodeFlags.Let;
23442345
}
23452346

2347+
/** @internal */
2348+
export function isVarConstLike(node: VariableDeclaration | VariableDeclarationList) {
2349+
const blockScopeKind = getCombinedNodeFlags(node) & NodeFlags.BlockScoped;
2350+
return blockScopeKind === NodeFlags.Const ||
2351+
blockScopeKind === NodeFlags.Using ||
2352+
blockScopeKind === NodeFlags.AwaitUsing;
2353+
}
2354+
23462355
/** @internal */
23472356
export function isSuperCall(n: Node): n is SuperCall {
23482357
return n.kind === SyntaxKind.CallExpression && (n as CallExpression).expression.kind === SyntaxKind.SuperKeyword;
@@ -10639,3 +10648,37 @@ export function replaceFirstStar(s: string, replacement: string): string {
1063910648
export function getNameFromImportAttribute(node: ImportAttribute) {
1064010649
return isIdentifier(node.name) ? node.name.escapedText : escapeLeadingUnderscores(node.name.text);
1064110650
}
10651+
10652+
/** @internal */
10653+
export function isPrimitiveLiteralValue(node: Expression): node is PrimitiveLiteral {
10654+
Debug.type<PrimitiveLiteral>(node);
10655+
switch (node.kind) {
10656+
case SyntaxKind.TrueKeyword:
10657+
case SyntaxKind.FalseKeyword:
10658+
case SyntaxKind.NumericLiteral:
10659+
case SyntaxKind.StringLiteral:
10660+
case SyntaxKind.NoSubstitutionTemplateLiteral:
10661+
case SyntaxKind.BigIntLiteral:
10662+
return true;
10663+
case SyntaxKind.PrefixUnaryExpression:
10664+
if (node.operator === SyntaxKind.MinusToken) {
10665+
return node.operand.kind === SyntaxKind.NumericLiteral ||
10666+
node.operand.kind === SyntaxKind.BigIntLiteral;
10667+
}
10668+
if (node.operator === SyntaxKind.PlusToken) {
10669+
return node.operand.kind === SyntaxKind.NumericLiteral;
10670+
}
10671+
return false;
10672+
default:
10673+
assertType<never>(node);
10674+
return false;
10675+
}
10676+
}
10677+
10678+
/** @internal */
10679+
export function unwrapParenthesizedExpression(o: Expression) {
10680+
while (o.kind === SyntaxKind.ParenthesizedExpression) {
10681+
o = (o as ParenthesizedExpression).expression;
10682+
}
10683+
return o;
10684+
}

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

+2-2
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

+19-13
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

+2-2
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

+19-13
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/declarationEmitClassMemberNameConflict2.js

+1-1
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

+1-1
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 {
2626
readonly type = "FOO";
2727
}

tests/baselines/reference/fakeInfinity1.js

+1-1
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/strictPropertyInitialization.js

+1-1
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)