Skip to content

Commit 8ae0376

Browse files
authored
Merge pull request #13016 from arusakov/disallow_old_style_octal_literal_types
Disallow old style octal literal types
2 parents 0649c22 + 142a6f6 commit 8ae0376

10 files changed

+55
-13
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21848,8 +21848,13 @@ namespace ts {
2184821848

2184921849
function checkGrammarNumericLiteral(node: NumericLiteral): boolean {
2185021850
// Grammar checking
21851-
if (node.isOctalLiteral && languageVersion >= ScriptTarget.ES5) {
21852-
return grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher);
21851+
if (node.isOctalLiteral) {
21852+
if (languageVersion >= ScriptTarget.ES5) {
21853+
return grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0o_0, node.text);
21854+
}
21855+
if (isChildOfLiteralType(node)) {
21856+
return grammarErrorOnNode(node, Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0o_0, node.text);
21857+
}
2185321858
}
2185421859
}
2185521860

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
"category": "Error",
228228
"code": 1084
229229
},
230-
"Octal literals are not available when targeting ECMAScript 5 and higher.": {
230+
"Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o{0}'.": {
231231
"category": "Error",
232232
"code": 1085
233233
},
@@ -3238,5 +3238,9 @@
32383238
"Add {0} to existing import declaration from {1}": {
32393239
"category": "Message",
32403240
"code": 90015
3241+
},
3242+
"Octal literal types must use ES2015 syntax. Use the syntax '0o{0}'.": {
3243+
"category": "Error",
3244+
"code": 8017
32413245
}
32423246
}

src/compiler/utilities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,16 @@ namespace ts {
732732
return false;
733733
}
734734

735+
export function isChildOfLiteralType(node: Node): boolean {
736+
while (node) {
737+
if (node.kind === SyntaxKind.LiteralType) {
738+
return true;
739+
}
740+
node = node.parent;
741+
}
742+
return false;
743+
}
744+
735745
// Warning: This has the same semantics as the forEach family of functions,
736746
// in that traversal terminates in the event that 'visitor' supplies a truthy value.
737747
export function forEachReturnStatement<T>(body: Block, visitor: (stmt: ReturnStatement) => T): T {

tests/baselines/reference/literals.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ tests/cases/conformance/expressions/literals/literals.ts(9,10): error TS2362: Th
22
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.
33
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.
44
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.
5-
tests/cases/conformance/expressions/literals/literals.ts(20,9): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
6-
tests/cases/conformance/expressions/literals/literals.ts(25,10): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
5+
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'.
6+
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'.
77

88

99
==== 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
3636
var n = 1e4;
3737
var n = 001; // Error in ES5
3838
~~~
39-
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
39+
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o1'.
4040
var n = 0x1;
4141
var n = -1;
4242
var n = -1.0;
4343
var n = -1e-4;
4444
var n = -003; // Error in ES5
4545
~~~
46-
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
46+
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o3'.
4747
var n = -0x1;
4848

4949
var s: string;

tests/baselines/reference/objectLiteralErrors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(13,21)
1212
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(14,19): error TS2300: Duplicate identifier '0'.
1313
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(15,19): error TS2300: Duplicate identifier '0'.
1414
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(16,19): error TS2300: Duplicate identifier '0x0'.
15-
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(17,19): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
15+
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'.
1616
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(17,19): error TS2300: Duplicate identifier '000'.
1717
tests/cases/conformance/expressions/objectLiterals/objectLiteralErrors.ts(18,23): error TS2300: Duplicate identifier '1e2'.
1818
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)
125125
!!! error TS2300: Duplicate identifier '0x0'.
126126
var e14 = { 0: 0, 000: 0 };
127127
~~~
128-
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
128+
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o0'.
129129
~~~
130130
!!! error TS2300: Duplicate identifier '000'.
131131
var e15 = { "100": 0, 1e2: 0 };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/oldStyleOctalLiteralTypes.ts(1,8): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o10'.
2+
tests/cases/compiler/oldStyleOctalLiteralTypes.ts(2,9): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o20'.
3+
4+
5+
==== tests/cases/compiler/oldStyleOctalLiteralTypes.ts (2 errors) ====
6+
let x: 010;
7+
~~~
8+
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o10'.
9+
let y: -020;
10+
~~~
11+
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o20'.
12+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [oldStyleOctalLiteralTypes.ts]
2+
let x: 010;
3+
let y: -020;
4+
5+
6+
//// [oldStyleOctalLiteralTypes.js]
7+
var x;
8+
var y;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral2.ts(1,1): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
1+
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'.
22

33

44
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral2.ts (1 errors) ====
55
01
66
~~
7-
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
7+
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o1'.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral8.ts(1,2): error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
1+
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'.
22

33

44
==== tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral8.ts (1 errors) ====
55
-03
66
~~
7-
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher.
7+
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o3'.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: ES3
2+
let x: 010;
3+
let y: -020;

0 commit comments

Comments
 (0)