Skip to content

Commit 72cf78f

Browse files
committed
Error on octal literals in ES5 and strict mode
1 parent 60e7f08 commit 72cf78f

9 files changed

+39
-77
lines changed

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ module ts {
105105
An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name: { code: 1118, category: DiagnosticCategory.Error, key: "An object literal cannot have multiple get/set accessors with the same name." },
106106
An_object_literal_cannot_have_property_and_accessor_with_the_same_name: { code: 1119, category: DiagnosticCategory.Error, key: "An object literal cannot have property and accessor with the same name." },
107107
An_export_assignment_cannot_have_modifiers: { code: 1120, category: DiagnosticCategory.Error, key: "An export assignment cannot have modifiers." },
108+
Octal_literals_are_not_allowed_in_strict_mode: { code: 1121, category: DiagnosticCategory.Error, key: "Octal literals are not allowed in strict mode." },
108109
Duplicate_identifier_0: { code: 2000, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
109110
Extends_clause_of_exported_class_0_has_or_is_using_private_name_1: { code: 2018, category: DiagnosticCategory.Error, key: "Extends clause of exported class '{0}' has or is using private name '{1}'." },
110111
Implements_clause_of_exported_class_0_has_or_is_using_private_name_1: { code: 2019, category: DiagnosticCategory.Error, key: "Implements clause of exported class '{0}' has or is using private name '{1}'." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@
412412
"category": "Error",
413413
"code": 1120
414414
},
415+
"Octal literals are not allowed in strict mode.": {
416+
"category": "Error",
417+
"code": 1121
418+
},
415419
"Duplicate identifier '{0}'.": {
416420
"category": "Error",
417421
"code": 2000

src/compiler/parser.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,23 @@ module ts {
10561056
var node = <LiteralExpression>createNode(token);
10571057
node.text = scanner.getTokenValue();
10581058
nextToken();
1059-
return finishNode(node);
1059+
finishNode(node);
1060+
1061+
// Octal literals are not allowed in strict mode or ES5
1062+
if (node.kind === SyntaxKind.NumericLiteral && (isInStrictMode || languageVersion >= ScriptTarget.ES5)) {
1063+
var numberLiteralSource = getSourceTextOfNodeFromSourceText(sourceText, node);
1064+
// This regex checks if the number is written in octal
1065+
if (/0[0-7]+/.test(numberLiteralSource)) {
1066+
if (isInStrictMode) {
1067+
grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode);
1068+
}
1069+
else {
1070+
grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher);
1071+
}
1072+
}
1073+
}
1074+
1075+
return node;
10601076
}
10611077

10621078
function parseStringLiteral(): LiteralExpression {

tests/baselines/reference/literals.errors.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
==== tests/cases/conformance/expressions/literals/literals.ts (4 errors) ====
1+
==== tests/cases/conformance/expressions/literals/literals.ts (6 errors) ====
22

33
//typeof null is Null
44
//typeof true is Boolean
@@ -27,11 +27,15 @@
2727
var n = 1.0;
2828
var n = 1e4;
2929
var n = 001; // Error in ES5
30+
~~~
31+
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
3032
var n = 0x1;
3133
var n = -1;
3234
var n = -1.0;
3335
var n = -1e-4;
3436
var n = -003; // Error in ES5
37+
~~~
38+
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
3539
var n = -0x1;
3640

3741
var s: string;

tests/baselines/reference/literals.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

tests/baselines/reference/objectLiteralErrors.errors.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
==== tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts (59 errors) ====
1+
==== tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts (61 errors) ====
22

33
// Multiple properties with the same name
44
var e1 = { a: 0, a: 0 };
@@ -45,6 +45,8 @@
4545
!!! Duplicate identifier '0x0'.
4646
var e14 = { 0: 0, 000: 0 };
4747
~~~
48+
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
49+
~~~
4850
!!! Duplicate identifier '000'.
4951
var e15 = { "100": 0, 1e2: 0 };
5052
~~~
@@ -129,6 +131,8 @@
129131
!!! Duplicate identifier '0x0'.
130132
var f14 = { 0: 0, get 000() { return 0; } };
131133
~~~
134+
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
135+
~~~
132136
!!! An object literal cannot have property and accessor with the same name.
133137
~~~
134138
!!! Duplicate identifier '000'.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral2.ts (1 errors) ====
2+
01
3+
~~
4+
!!! Octal literals are not available when targeting ECMAScript 5 and higher.

tests/baselines/reference/scannerNumericLiteral2.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral3.ts (1 errors) ====
1+
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral3.ts (2 errors) ====
22
01.0
3+
~~
4+
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
35
~~
46
!!! ';' expected.

0 commit comments

Comments
 (0)