diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a27e8ba524d..6a1edbf3cc6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -18582,6 +18582,7 @@ namespace ts { } return undefined; case SyntaxKind.NumericLiteral: + checkGrammarNumericLiteral(e); return +(e).text; case SyntaxKind.ParenthesizedExpression: return evalConstant((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}`; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7b2208e4ca1..8cf1313264f 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 } } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index edfbe639d25..3c64c4d4a34 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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; diff --git a/tests/baselines/reference/es3-oldStyleOctalLiteralInEnums.errors.txt b/tests/baselines/reference/es3-oldStyleOctalLiteralInEnums.errors.txt new file mode 100644 index 00000000000..137b76f660d --- /dev/null +++ b/tests/baselines/reference/es3-oldStyleOctalLiteralInEnums.errors.txt @@ -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'. + } \ No newline at end of file diff --git a/tests/baselines/reference/es3-oldStyleOctalLiteralInEnums.js b/tests/baselines/reference/es3-oldStyleOctalLiteralInEnums.js new file mode 100644 index 00000000000..0098d841f2a --- /dev/null +++ b/tests/baselines/reference/es3-oldStyleOctalLiteralInEnums.js @@ -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 = {})); diff --git a/tests/baselines/reference/es3-oldStyleOctalLiteralTypes.errors.txt b/tests/baselines/reference/es3-oldStyleOctalLiteralTypes.errors.txt new file mode 100644 index 00000000000..5fc9843989c --- /dev/null +++ b/tests/baselines/reference/es3-oldStyleOctalLiteralTypes.errors.txt @@ -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'. + \ No newline at end of file diff --git a/tests/baselines/reference/es3-oldStyleOctalLiteralTypes.js b/tests/baselines/reference/es3-oldStyleOctalLiteralTypes.js new file mode 100644 index 00000000000..5ac915400cd --- /dev/null +++ b/tests/baselines/reference/es3-oldStyleOctalLiteralTypes.js @@ -0,0 +1,8 @@ +//// [es3-oldStyleOctalLiteralTypes.ts] +let x: 010; +let y: -020; + + +//// [es3-oldStyleOctalLiteralTypes.js] +var x; +var y; diff --git a/tests/baselines/reference/es5-oldStyleOctalLiteralInEnums.errors.txt b/tests/baselines/reference/es5-oldStyleOctalLiteralInEnums.errors.txt new file mode 100644 index 00000000000..a504568ce20 --- /dev/null +++ b/tests/baselines/reference/es5-oldStyleOctalLiteralInEnums.errors.txt @@ -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'. + } \ No newline at end of file diff --git a/tests/baselines/reference/es5-oldStyleOctalLiteralInEnums.js b/tests/baselines/reference/es5-oldStyleOctalLiteralInEnums.js new file mode 100644 index 00000000000..71f718054fe --- /dev/null +++ b/tests/baselines/reference/es5-oldStyleOctalLiteralInEnums.js @@ -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 = {})); diff --git a/tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts b/tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts new file mode 100644 index 00000000000..93489ee4820 --- /dev/null +++ b/tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts @@ -0,0 +1,5 @@ +// @target: ES3 +enum E { + x = -01, + y = 02, +} \ No newline at end of file diff --git a/tests/cases/compiler/oldStyleOctalLiteralTypes.ts b/tests/cases/compiler/es3-oldStyleOctalLiteralTypes.ts similarity index 100% rename from tests/cases/compiler/oldStyleOctalLiteralTypes.ts rename to tests/cases/compiler/es3-oldStyleOctalLiteralTypes.ts diff --git a/tests/cases/compiler/es5-oldStyleOctalLiteralInEnums.ts b/tests/cases/compiler/es5-oldStyleOctalLiteralInEnums.ts new file mode 100644 index 00000000000..42a95eff2a2 --- /dev/null +++ b/tests/cases/compiler/es5-oldStyleOctalLiteralInEnums.ts @@ -0,0 +1,5 @@ +// @target: ES5 +enum E { + x = -01, + y = 02, +} \ No newline at end of file