Error on octal literals in ES5 and strict mode

This commit is contained in:
Jason Freeman
2014-08-06 15:41:11 -07:00
parent 60e7f0850a
commit 72cf78f681
9 changed files with 39 additions and 77 deletions

View File

@@ -105,6 +105,7 @@ module ts {
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." },
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." },
An_export_assignment_cannot_have_modifiers: { code: 1120, category: DiagnosticCategory.Error, key: "An export assignment cannot have modifiers." },
Octal_literals_are_not_allowed_in_strict_mode: { code: 1121, category: DiagnosticCategory.Error, key: "Octal literals are not allowed in strict mode." },
Duplicate_identifier_0: { code: 2000, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
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}'." },
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}'." },

View File

@@ -412,6 +412,10 @@
"category": "Error",
"code": 1120
},
"Octal literals are not allowed in strict mode.": {
"category": "Error",
"code": 1121
},
"Duplicate identifier '{0}'.": {
"category": "Error",
"code": 2000

View File

@@ -1056,7 +1056,23 @@ module ts {
var node = <LiteralExpression>createNode(token);
node.text = scanner.getTokenValue();
nextToken();
return finishNode(node);
finishNode(node);
// Octal literals are not allowed in strict mode or ES5
if (node.kind === SyntaxKind.NumericLiteral && (isInStrictMode || languageVersion >= ScriptTarget.ES5)) {
var numberLiteralSource = getSourceTextOfNodeFromSourceText(sourceText, node);
// This regex checks if the number is written in octal
if (/0[0-7]+/.test(numberLiteralSource)) {
if (isInStrictMode) {
grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode);
}
else {
grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher);
}
}
}
return node;
}
function parseStringLiteral(): LiteralExpression {

View File

@@ -1,4 +1,4 @@
==== tests/cases/conformance/expressions/literals/literals.ts (4 errors) ====
==== tests/cases/conformance/expressions/literals/literals.ts (6 errors) ====
//typeof null is Null
//typeof true is Boolean
@@ -27,11 +27,15 @@
var n = 1.0;
var n = 1e4;
var n = 001; // Error in ES5
~~~
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
var n = 0x1;
var n = -1;
var n = -1.0;
var n = -1e-4;
var n = -003; // Error in ES5
~~~
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
var n = -0x1;
var s: string;

View File

@@ -1,68 +0,0 @@
//// [literals.ts]
//typeof null is Null
//typeof true is Boolean
//typeof false is Boolean
//typeof numeric literal is Number
//typeof string literal is String
//typeof regex literal is Regex
var nu = null / null;
var u = undefined / undefined;
var b: boolean;
var b = true;
var b = false;
var n: number;
var n = 1;
var n = 1.0;
var n = 1e4;
var n = 001; // Error in ES5
var n = 0x1;
var n = -1;
var n = -1.0;
var n = -1e-4;
var n = -003; // Error in ES5
var n = -0x1;
var s: string;
var s = '';
var s = "";
var s = 'foo\
bar';
var s = "foo\
bar";
var r: RegExp;
var r = /what/;
var r = /\\\\/;
//// [literals.js]
var nu = null / null;
var u = undefined / undefined;
var b;
var b = true;
var b = false;
var n;
var n = 1;
var n = 1.0;
var n = 1e4;
var n = 001;
var n = 0x1;
var n = -1;
var n = -1.0;
var n = -1e-4;
var n = -003;
var n = -0x1;
var s;
var s = '';
var s = "";
var s = 'foo\
bar';
var s = "foo\
bar";
var r;
var r = /what/;
var r = /\\\\/;

View File

@@ -1,4 +1,4 @@
==== tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts (59 errors) ====
==== tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts (61 errors) ====
// Multiple properties with the same name
var e1 = { a: 0, a: 0 };
@@ -45,6 +45,8 @@
!!! Duplicate identifier '0x0'.
var e14 = { 0: 0, 000: 0 };
~~~
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
~~~
!!! Duplicate identifier '000'.
var e15 = { "100": 0, 1e2: 0 };
~~~
@@ -129,6 +131,8 @@
!!! Duplicate identifier '0x0'.
var f14 = { 0: 0, get 000() { return 0; } };
~~~
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
~~~
!!! An object literal cannot have property and accessor with the same name.
~~~
!!! Duplicate identifier '000'.

View File

@@ -0,0 +1,4 @@
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral2.ts (1 errors) ====
01
~~
!!! Octal literals are not available when targeting ECMAScript 5 and higher.

View File

@@ -1,5 +0,0 @@
//// [scannerNumericLiteral2.ts]
01
//// [scannerNumericLiteral2.js]
01;

View File

@@ -1,4 +1,6 @@
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral3.ts (1 errors) ====
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral3.ts (2 errors) ====
01.0
~~
!!! Octal literals are not available when targeting ECMAScript 5 and higher.
~~
!!! ';' expected.