Skip to content

Commit 42eb9b1

Browse files
committed
Update based on microsoft#57686
1 parent baacbe8 commit 42eb9b1

File tree

5 files changed

+39
-51
lines changed

5 files changed

+39
-51
lines changed

src/compiler/checker.ts

+2-38
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ import {
719719
isStringOrNumericLiteralLike,
720720
isSuperCall,
721721
isSuperProperty,
722+
isSyntacticallyNumeric,
723+
isSyntacticallyString,
722724
isTaggedTemplateExpression,
723725
isTemplateSpan,
724726
isThisContainerOrFunctionBlock,
@@ -45595,44 +45597,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4559545597
return value;
4559645598
}
4559745599

45598-
function isSyntacticallyNumeric(expr: Expression): boolean {
45599-
switch (expr.kind) {
45600-
case SyntaxKind.PrefixUnaryExpression:
45601-
return isSyntacticallyNumeric((expr as PrefixUnaryExpression).operand);
45602-
case SyntaxKind.BinaryExpression:
45603-
return isSyntacticallyNumeric((expr as BinaryExpression).left) && isSyntacticallyNumeric((expr as BinaryExpression).right);
45604-
case SyntaxKind.ParenthesizedExpression:
45605-
return isSyntacticallyNumeric((expr as ParenthesizedExpression).expression);
45606-
case SyntaxKind.NumericLiteral:
45607-
return true;
45608-
}
45609-
return false;
45610-
}
45611-
45612-
function isSyntacticallyString(expr: Expression): boolean {
45613-
switch (expr.kind) {
45614-
case SyntaxKind.BinaryExpression:
45615-
const left = (expr as BinaryExpression).left;
45616-
const right = (expr as BinaryExpression).right;
45617-
const leftIsNumeric = isSyntacticallyNumeric(left);
45618-
const rightIsNumeric = isSyntacticallyNumeric(right);
45619-
return (
45620-
!(leftIsNumeric && rightIsNumeric) &&
45621-
(isSyntacticallyString(left) || leftIsNumeric) &&
45622-
(isSyntacticallyString(right) || rightIsNumeric) &&
45623-
(expr as BinaryExpression).operatorToken.kind === SyntaxKind.PlusToken
45624-
);
45625-
case SyntaxKind.TemplateExpression:
45626-
return (expr as TemplateExpression).templateSpans.every(span => isSyntacticallyString(span.expression));
45627-
case SyntaxKind.ParenthesizedExpression:
45628-
return isSyntacticallyString((expr as ParenthesizedExpression).expression);
45629-
case SyntaxKind.StringLiteral:
45630-
case SyntaxKind.NoSubstitutionTemplateLiteral:
45631-
return true;
45632-
}
45633-
return false;
45634-
}
45635-
4563645600
function evaluate(expr: Expression, location?: Declaration): string | number | undefined {
4563745601
switch (expr.kind) {
4563845602
case SyntaxKind.PrefixUnaryExpression:

src/compiler/utilities.ts

+14
Original file line numberDiff line numberDiff line change
@@ -10657,3 +10657,17 @@ export function isSyntacticallyString(expr: Expression): boolean {
1065710657
}
1065810658
return false;
1065910659
}
10660+
10661+
/** @internal */
10662+
export function isSyntacticallyNumeric(expr: Expression): boolean {
10663+
expr = skipOuterExpressions(expr);
10664+
switch (expr.kind) {
10665+
case SyntaxKind.PrefixUnaryExpression:
10666+
return isSyntacticallyNumeric((expr as PrefixUnaryExpression).operand);
10667+
case SyntaxKind.BinaryExpression:
10668+
return isSyntacticallyNumeric((expr as BinaryExpression).left) && isSyntacticallyNumeric((expr as BinaryExpression).right);
10669+
case SyntaxKind.NumericLiteral:
10670+
return true;
10671+
}
10672+
return false;
10673+
}

tests/baselines/reference/enumWithNonLiteralStringInitializer.errors.txt

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ bad.ts(3,8): error TS18055: A string member initializer in a enum declaration ca
33

44
==== ./helpers.ts (0 errors) ====
55
export const foo = 2;
6+
export const bar = "bar";
67

78
==== ./bad.ts (1 errors) ====
8-
import { foo } from "./helpers";
9+
import { bar } from "./helpers";
910
enum A {
10-
a = `${foo}`
11-
~~~~~~~~
11+
a = bar,
12+
~~~
1213
!!! error TS18055: A string member initializer in a enum declaration can only use constant expressions when 'isolatedModules' is enabled.
1314
}
1415

1516
==== ./good.ts (0 errors) ====
17+
import { foo } from "./helpers";
1618
enum A {
17-
a = `${"foo"}`,
19+
a = `${foo}`,
1820
b = "" + 2,
1921
c = 2 + "",
2022
d = ("foo"),

tests/baselines/reference/enumWithNonLiteralStringInitializer.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
//// [helpers.ts]
44
export const foo = 2;
5+
export const bar = "bar";
56

67
//// [bad.ts]
7-
import { foo } from "./helpers";
8+
import { bar } from "./helpers";
89
enum A {
9-
a = `${foo}`
10+
a = bar,
1011
}
1112

1213
//// [good.ts]
14+
import { foo } from "./helpers";
1315
enum A {
14-
a = `${"foo"}`,
16+
a = `${foo}`,
1517
b = "" + 2,
1618
c = 2 + "",
1719
d = ("foo"),
@@ -21,20 +23,24 @@ enum A {
2123
//// [helpers.js]
2224
"use strict";
2325
Object.defineProperty(exports, "__esModule", { value: true });
24-
exports.foo = void 0;
26+
exports.bar = exports.foo = void 0;
2527
exports.foo = 2;
28+
exports.bar = "bar";
2629
//// [bad.js]
2730
"use strict";
2831
Object.defineProperty(exports, "__esModule", { value: true });
2932
var helpers_1 = require("./helpers");
3033
var A;
3134
(function (A) {
32-
A["a"] = "2";
35+
A["a"] = "bar";
3336
})(A || (A = {}));
3437
//// [good.js]
38+
"use strict";
39+
Object.defineProperty(exports, "__esModule", { value: true });
40+
var helpers_1 = require("./helpers");
3541
var A;
3642
(function (A) {
37-
A["a"] = "foo";
43+
A["a"] = "2";
3844
A["b"] = "2";
3945
A["c"] = "2";
4046
A["d"] = "foo";

tests/cases/compiler/enumWithNonLiteralStringInitializer.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33

44
// @filename: ./helpers.ts
55
export const foo = 2;
6+
export const bar = "bar";
67

78
// @filename: ./bad.ts
8-
import { foo } from "./helpers";
9+
import { bar } from "./helpers";
910
enum A {
10-
a = `${foo}`
11+
a = bar,
1112
}
1213

1314
// @filename: ./good.ts
15+
import { foo } from "./helpers";
1416
enum A {
15-
a = `${"foo"}`,
17+
a = `${foo}`,
1618
b = "" + 2,
1719
c = 2 + "",
1820
d = ("foo"),

0 commit comments

Comments
 (0)