diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 55dd45df789..cc789bcca11 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -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}'." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 30e3489374f..d60fd2e5c4f 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 929e676c305..57e23531398 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1056,7 +1056,23 @@ module ts { var node = 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 { diff --git a/tests/baselines/reference/literals.errors.txt b/tests/baselines/reference/literals.errors.txt index edfdad8c279..3361dfd5c67 100644 --- a/tests/baselines/reference/literals.errors.txt +++ b/tests/baselines/reference/literals.errors.txt @@ -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; diff --git a/tests/baselines/reference/literals.js b/tests/baselines/reference/literals.js deleted file mode 100644 index da11b6cef49..00000000000 --- a/tests/baselines/reference/literals.js +++ /dev/null @@ -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 = /\\\\/; diff --git a/tests/baselines/reference/objectLiteralErrors.errors.txt b/tests/baselines/reference/objectLiteralErrors.errors.txt index 8e1b64f1758..6a36b93edbb 100644 --- a/tests/baselines/reference/objectLiteralErrors.errors.txt +++ b/tests/baselines/reference/objectLiteralErrors.errors.txt @@ -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'. diff --git a/tests/baselines/reference/scannerNumericLiteral2.errors.txt b/tests/baselines/reference/scannerNumericLiteral2.errors.txt new file mode 100644 index 00000000000..c9160bffdf2 --- /dev/null +++ b/tests/baselines/reference/scannerNumericLiteral2.errors.txt @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/scannerNumericLiteral2.js b/tests/baselines/reference/scannerNumericLiteral2.js deleted file mode 100644 index 382f1e6e4df..00000000000 --- a/tests/baselines/reference/scannerNumericLiteral2.js +++ /dev/null @@ -1,5 +0,0 @@ -//// [scannerNumericLiteral2.ts] -01 - -//// [scannerNumericLiteral2.js] -01; diff --git a/tests/baselines/reference/scannerNumericLiteral3.errors.txt b/tests/baselines/reference/scannerNumericLiteral3.errors.txt index 6f82581759b..ac751dd9089 100644 --- a/tests/baselines/reference/scannerNumericLiteral3.errors.txt +++ b/tests/baselines/reference/scannerNumericLiteral3.errors.txt @@ -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. \ No newline at end of file