Skip to content

Commit e1872df

Browse files
authored
Merge pull request #13127 from arusakov/old_style_literal_types_in_enums
Disallow old style octal literals in enums
2 parents c563e83 + 31abc59 commit e1872df

12 files changed

+91
-3
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18582,6 +18582,7 @@ namespace ts {
1858218582
}
1858318583
return undefined;
1858418584
case SyntaxKind.NumericLiteral:
18585+
checkGrammarNumericLiteral(<NumericLiteral>e);
1858518586
return +(<NumericLiteral>e).text;
1858618587
case SyntaxKind.ParenthesizedExpression:
1858718588
return evalConstant((<ParenthesizedExpression>e).expression);
@@ -21885,9 +21886,12 @@ namespace ts {
2188521886
if (languageVersion >= ScriptTarget.ES5) {
2188621887
diagnosticMessage = Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0;
2188721888
}
21888-
else if (isChildOfLiteralType(node)) {
21889+
else if (isChildOfNodeWithKind(node, SyntaxKind.LiteralType)) {
2188921890
diagnosticMessage = Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0;
2189021891
}
21892+
else if (isChildOfNodeWithKind(node, SyntaxKind.EnumMember)) {
21893+
diagnosticMessage = Diagnostics.Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0;
21894+
}
2189121895
if (diagnosticMessage) {
2189221896
const withMinus = isPrefixUnaryExpression(node.parent) && node.parent.operator === SyntaxKind.MinusToken;
2189321897
const literal = `${withMinus ? "-" : ""}0o${node.text}`;

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3246,5 +3246,9 @@
32463246
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
32473247
"category": "Error",
32483248
"code": 8017
3249+
},
3250+
"Octal literals are not allowed in enums members initializer. Use the syntax '{0}'.": {
3251+
"category": "Error",
3252+
"code": 8018
32493253
}
32503254
}

src/compiler/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,9 @@ namespace ts {
732732
return false;
733733
}
734734

735-
export function isChildOfLiteralType(node: Node): boolean {
735+
export function isChildOfNodeWithKind(node: Node, kind: SyntaxKind): boolean {
736736
while (node) {
737-
if (node.kind === SyntaxKind.LiteralType) {
737+
if (node.kind === kind) {
738738
return true;
739739
}
740740
node = node.parent;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts(2,7): error TS8018: Octal literals are not allowed in enums members initializer. Use the syntax '-0o1'.
2+
tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts(3,7): error TS8018: Octal literals are not allowed in enums members initializer. Use the syntax '0o2'.
3+
4+
5+
==== tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts (2 errors) ====
6+
enum E {
7+
x = -01,
8+
~~~
9+
!!! error TS8018: Octal literals are not allowed in enums members initializer. Use the syntax '-0o1'.
10+
y = 02,
11+
~~
12+
!!! error TS8018: Octal literals are not allowed in enums members initializer. Use the syntax '0o2'.
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [es3-oldStyleOctalLiteralInEnums.ts]
2+
enum E {
3+
x = -01,
4+
y = 02,
5+
}
6+
7+
//// [es3-oldStyleOctalLiteralInEnums.js]
8+
var E;
9+
(function (E) {
10+
E[E["x"] = -1] = "x";
11+
E[E["y"] = 2] = "y";
12+
})(E || (E = {}));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/es3-oldStyleOctalLiteralTypes.ts(1,8): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o10'.
2+
tests/cases/compiler/es3-oldStyleOctalLiteralTypes.ts(2,8): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '-0o20'.
3+
4+
5+
==== tests/cases/compiler/es3-oldStyleOctalLiteralTypes.ts (2 errors) ====
6+
let x: 010;
7+
~~~
8+
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o10'.
9+
let y: -020;
10+
~~~~
11+
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '-0o20'.
12+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [es3-oldStyleOctalLiteralTypes.ts]
2+
let x: 010;
3+
let y: -020;
4+
5+
6+
//// [es3-oldStyleOctalLiteralTypes.js]
7+
var x;
8+
var y;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/es5-oldStyleOctalLiteralInEnums.ts(2,7): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '-0o1'.
2+
tests/cases/compiler/es5-oldStyleOctalLiteralInEnums.ts(3,7): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o2'.
3+
4+
5+
==== tests/cases/compiler/es5-oldStyleOctalLiteralInEnums.ts (2 errors) ====
6+
enum E {
7+
x = -01,
8+
~~~
9+
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '-0o1'.
10+
y = 02,
11+
~~
12+
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o2'.
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [es5-oldStyleOctalLiteralInEnums.ts]
2+
enum E {
3+
x = -01,
4+
y = 02,
5+
}
6+
7+
//// [es5-oldStyleOctalLiteralInEnums.js]
8+
var E;
9+
(function (E) {
10+
E[E["x"] = -1] = "x";
11+
E[E["y"] = 2] = "y";
12+
})(E || (E = {}));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @target: ES3
2+
enum E {
3+
x = -01,
4+
y = 02,
5+
}

0 commit comments

Comments
 (0)