Merge pull request #13127 from arusakov/old_style_literal_types_in_enums

Disallow old style octal literals in enums
This commit is contained in:
Mohamed Hegazy 2016-12-22 10:00:33 -08:00 committed by GitHub
commit e1872dfdaa
12 changed files with 91 additions and 3 deletions

View File

@ -18582,6 +18582,7 @@ namespace ts {
}
return undefined;
case SyntaxKind.NumericLiteral:
checkGrammarNumericLiteral(<NumericLiteral>e);
return +(<NumericLiteral>e).text;
case SyntaxKind.ParenthesizedExpression:
return evalConstant((<ParenthesizedExpression>e).expression);
@ -21885,9 +21886,12 @@ namespace ts {
if (languageVersion >= ScriptTarget.ES5) {
diagnosticMessage = Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0;
}
else if (isChildOfLiteralType(node)) {
else if (isChildOfNodeWithKind(node, SyntaxKind.LiteralType)) {
diagnosticMessage = Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0;
}
else if (isChildOfNodeWithKind(node, SyntaxKind.EnumMember)) {
diagnosticMessage = Diagnostics.Octal_literals_are_not_allowed_in_enums_members_initializer_Use_the_syntax_0;
}
if (diagnosticMessage) {
const withMinus = isPrefixUnaryExpression(node.parent) && node.parent.operator === SyntaxKind.MinusToken;
const literal = `${withMinus ? "-" : ""}0o${node.text}`;

View File

@ -3246,5 +3246,9 @@
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
"category": "Error",
"code": 8017
},
"Octal literals are not allowed in enums members initializer. Use the syntax '{0}'.": {
"category": "Error",
"code": 8018
}
}

View File

@ -732,9 +732,9 @@ namespace ts {
return false;
}
export function isChildOfLiteralType(node: Node): boolean {
export function isChildOfNodeWithKind(node: Node, kind: SyntaxKind): boolean {
while (node) {
if (node.kind === SyntaxKind.LiteralType) {
if (node.kind === kind) {
return true;
}
node = node.parent;

View File

@ -0,0 +1,13 @@
tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts(2,7): error TS8018: Octal literals are not allowed in enums members initializer. Use the syntax '-0o1'.
tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts(3,7): error TS8018: Octal literals are not allowed in enums members initializer. Use the syntax '0o2'.
==== tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts (2 errors) ====
enum E {
x = -01,
~~~
!!! error TS8018: Octal literals are not allowed in enums members initializer. Use the syntax '-0o1'.
y = 02,
~~
!!! error TS8018: Octal literals are not allowed in enums members initializer. Use the syntax '0o2'.
}

View File

@ -0,0 +1,12 @@
//// [es3-oldStyleOctalLiteralInEnums.ts]
enum E {
x = -01,
y = 02,
}
//// [es3-oldStyleOctalLiteralInEnums.js]
var E;
(function (E) {
E[E["x"] = -1] = "x";
E[E["y"] = 2] = "y";
})(E || (E = {}));

View File

@ -0,0 +1,12 @@
tests/cases/compiler/es3-oldStyleOctalLiteralTypes.ts(1,8): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o10'.
tests/cases/compiler/es3-oldStyleOctalLiteralTypes.ts(2,8): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '-0o20'.
==== tests/cases/compiler/es3-oldStyleOctalLiteralTypes.ts (2 errors) ====
let x: 010;
~~~
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o10'.
let y: -020;
~~~~
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '-0o20'.

View File

@ -0,0 +1,8 @@
//// [es3-oldStyleOctalLiteralTypes.ts]
let x: 010;
let y: -020;
//// [es3-oldStyleOctalLiteralTypes.js]
var x;
var y;

View File

@ -0,0 +1,13 @@
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'.
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'.
==== tests/cases/compiler/es5-oldStyleOctalLiteralInEnums.ts (2 errors) ====
enum E {
x = -01,
~~~
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '-0o1'.
y = 02,
~~
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o2'.
}

View File

@ -0,0 +1,12 @@
//// [es5-oldStyleOctalLiteralInEnums.ts]
enum E {
x = -01,
y = 02,
}
//// [es5-oldStyleOctalLiteralInEnums.js]
var E;
(function (E) {
E[E["x"] = -1] = "x";
E[E["y"] = 2] = "y";
})(E || (E = {}));

View File

@ -0,0 +1,5 @@
// @target: ES3
enum E {
x = -01,
y = 02,
}

View File

@ -0,0 +1,5 @@
// @target: ES5
enum E {
x = -01,
y = 02,
}