From 142a6f64200c671b31ecbc9a196dbbd43ccae5bc Mon Sep 17 00:00:00 2001 From: arusakov Date: Mon, 19 Dec 2016 11:36:20 +0300 Subject: [PATCH] Disallow old style octal literal types --- src/compiler/checker.ts | 9 +++++++-- src/compiler/diagnosticMessages.json | 6 +++++- src/compiler/utilities.ts | 10 ++++++++++ tests/baselines/reference/literals.errors.txt | 8 ++++---- .../reference/objectLiteralErrors.errors.txt | 4 ++-- .../reference/oldStyleOctalLiteralTypes.errors.txt | 12 ++++++++++++ .../baselines/reference/oldStyleOctalLiteralTypes.js | 8 ++++++++ .../reference/scannerNumericLiteral2.errors.txt | 4 ++-- .../reference/scannerNumericLiteral8.errors.txt | 4 ++-- tests/cases/compiler/oldStyleOctalLiteralTypes.ts | 3 +++ 10 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 tests/baselines/reference/oldStyleOctalLiteralTypes.errors.txt create mode 100644 tests/baselines/reference/oldStyleOctalLiteralTypes.js create mode 100644 tests/cases/compiler/oldStyleOctalLiteralTypes.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5246bbfc7001b..90b21506113be 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21845,8 +21845,13 @@ namespace ts { function checkGrammarNumericLiteral(node: NumericLiteral): boolean { // Grammar checking - if (node.isOctalLiteral && languageVersion >= ScriptTarget.ES5) { - return grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher); + if (node.isOctalLiteral) { + if (languageVersion >= ScriptTarget.ES5) { + return grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0o_0, node.text); + } + if (isChildOfLiteralType(node)) { + return grammarErrorOnNode(node, Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0o_0, node.text); + } } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b59921c29489b..30e2aa29b9126 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -227,7 +227,7 @@ "category": "Error", "code": 1084 }, - "Octal literals are not available when targeting ECMAScript 5 and higher.": { + "Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o{0}'.": { "category": "Error", "code": 1085 }, @@ -3234,5 +3234,9 @@ "Add {0} to existing import declaration from {1}": { "category": "Message", "code": 90015 + }, + "Octal literal types must use ES2015 syntax. Use the syntax '0o{0}'.": { + "category": "Error", + "code": 8017 } } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 6f491f6708fee..309236e01c1ad 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -732,6 +732,16 @@ namespace ts { return false; } + export function isChildOfLiteralType(node: Node): boolean { + while (node) { + if (node.kind === SyntaxKind.LiteralType) { + return true; + } + node = node.parent; + } + return false; + } + // Warning: This has the same semantics as the forEach family of functions, // in that traversal terminates in the event that 'visitor' supplies a truthy value. export function forEachReturnStatement(body: Block, visitor: (stmt: ReturnStatement) => T): T { diff --git a/tests/baselines/reference/literals.errors.txt b/tests/baselines/reference/literals.errors.txt index d43e89bb4db81..2920498bde478 100644 --- a/tests/baselines/reference/literals.errors.txt +++ b/tests/baselines/reference/literals.errors.txt @@ -2,8 +2,8 @@ tests/cases/conformance/expressions/literals/literals.ts(9,10): error TS2362: Th tests/cases/conformance/expressions/literals/literals.ts(9,17): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/literals/literals.ts(10,9): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. tests/cases/conformance/expressions/literals/literals.ts(10,21): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. -tests/cases/conformance/expressions/literals/literals.ts(20,9): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. -tests/cases/conformance/expressions/literals/literals.ts(25,10): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. +tests/cases/conformance/expressions/literals/literals.ts(20,9): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o1'. +tests/cases/conformance/expressions/literals/literals.ts(25,10): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o3'. ==== tests/cases/conformance/expressions/literals/literals.ts (6 errors) ==== @@ -36,14 +36,14 @@ tests/cases/conformance/expressions/literals/literals.ts(25,10): error TS1085: O var n = 1e4; var n = 001; // Error in ES5 ~~~ -!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. +!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o1'. var n = 0x1; var n = -1; var n = -1.0; var n = -1e-4; var n = -003; // Error in ES5 ~~~ -!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. +!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o3'. var n = -0x1; var s: string; diff --git a/tests/baselines/reference/objectLiteralErrors.errors.txt b/tests/baselines/reference/objectLiteralErrors.errors.txt index 234ac24a8fdc5..03b4b74eacd7b 100644 --- a/tests/baselines/reference/objectLiteralErrors.errors.txt +++ b/tests/baselines/reference/objectLiteralErrors.errors.txt @@ -12,7 +12,7 @@ tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(13,21) tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(14,19): error TS2300: Duplicate identifier '0'. tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(15,19): error TS2300: Duplicate identifier '0'. tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(16,19): error TS2300: Duplicate identifier '0x0'. -tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(17,19): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. +tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(17,19): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o0'. tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(17,19): error TS2300: Duplicate identifier '000'. tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(18,23): error TS2300: Duplicate identifier '1e2'. tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(19,22): error TS2300: Duplicate identifier '3.2e1'. @@ -125,7 +125,7 @@ tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(45,55) !!! error TS2300: Duplicate identifier '0x0'. var e14 = { 0: 0, 000: 0 }; ~~~ -!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. +!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o0'. ~~~ !!! error TS2300: Duplicate identifier '000'. var e15 = { "100": 0, 1e2: 0 }; diff --git a/tests/baselines/reference/oldStyleOctalLiteralTypes.errors.txt b/tests/baselines/reference/oldStyleOctalLiteralTypes.errors.txt new file mode 100644 index 0000000000000..4dc81f370a40b --- /dev/null +++ b/tests/baselines/reference/oldStyleOctalLiteralTypes.errors.txt @@ -0,0 +1,12 @@ +tests/cases/compiler/oldStyleOctalLiteralTypes.ts(1,8): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o10'. +tests/cases/compiler/oldStyleOctalLiteralTypes.ts(2,9): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o20'. + + +==== tests/cases/compiler/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/oldStyleOctalLiteralTypes.js b/tests/baselines/reference/oldStyleOctalLiteralTypes.js new file mode 100644 index 0000000000000..28b275774c3dd --- /dev/null +++ b/tests/baselines/reference/oldStyleOctalLiteralTypes.js @@ -0,0 +1,8 @@ +//// [oldStyleOctalLiteralTypes.ts] +let x: 010; +let y: -020; + + +//// [oldStyleOctalLiteralTypes.js] +var x; +var y; diff --git a/tests/baselines/reference/scannerNumericLiteral2.errors.txt b/tests/baselines/reference/scannerNumericLiteral2.errors.txt index 5860be78c1b2d..faf5429f8b754 100644 --- a/tests/baselines/reference/scannerNumericLiteral2.errors.txt +++ b/tests/baselines/reference/scannerNumericLiteral2.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral2.ts(1,1): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. +tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral2.ts(1,1): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o1'. ==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral2.ts (1 errors) ==== 01 ~~ -!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. \ No newline at end of file +!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o1'. \ No newline at end of file diff --git a/tests/baselines/reference/scannerNumericLiteral8.errors.txt b/tests/baselines/reference/scannerNumericLiteral8.errors.txt index 94916626fe660..5aad01449f283 100644 --- a/tests/baselines/reference/scannerNumericLiteral8.errors.txt +++ b/tests/baselines/reference/scannerNumericLiteral8.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral8.ts(1,2): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. +tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral8.ts(1,2): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o3'. ==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral8.ts (1 errors) ==== -03 ~~ -!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. \ No newline at end of file +!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o3'. \ No newline at end of file diff --git a/tests/cases/compiler/oldStyleOctalLiteralTypes.ts b/tests/cases/compiler/oldStyleOctalLiteralTypes.ts new file mode 100644 index 0000000000000..c5a5475e30029 --- /dev/null +++ b/tests/cases/compiler/oldStyleOctalLiteralTypes.ts @@ -0,0 +1,3 @@ +// @target: ES3 +let x: 010; +let y: -020;