Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}`;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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'.
}
12 changes: 12 additions & 0 deletions tests/baselines/reference/es3-oldStyleOctalLiteralInEnums.js
Original file line number Diff line number Diff line change
@@ -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 = {}));
12 changes: 12 additions & 0 deletions tests/baselines/reference/es3-oldStyleOctalLiteralTypes.errors.txt
Original file line number Diff line number Diff line change
@@ -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'.

8 changes: 8 additions & 0 deletions tests/baselines/reference/es3-oldStyleOctalLiteralTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [es3-oldStyleOctalLiteralTypes.ts]
let x: 010;
let y: -020;


//// [es3-oldStyleOctalLiteralTypes.js]
var x;
var y;
Original file line number Diff line number Diff line change
@@ -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'.
}
12 changes: 12 additions & 0 deletions tests/baselines/reference/es5-oldStyleOctalLiteralInEnums.js
Original file line number Diff line number Diff line change
@@ -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 = {}));
5 changes: 5 additions & 0 deletions tests/cases/compiler/es3-oldStyleOctalLiteralInEnums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @target: ES3
enum E {
x = -01,
y = 02,
}
5 changes: 5 additions & 0 deletions tests/cases/compiler/es5-oldStyleOctalLiteralInEnums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @target: ES5
enum E {
x = -01,
y = 02,
}