Skip to content

Commit faf0bfe

Browse files
committed
Improve diagnostic message for negative old style literals
1 parent 8ae0376 commit faf0bfe

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21849,11 +21849,17 @@ namespace ts {
2184921849
function checkGrammarNumericLiteral(node: NumericLiteral): boolean {
2185021850
// Grammar checking
2185121851
if (node.isOctalLiteral) {
21852+
let diagnosticMessage: DiagnosticMessage | undefined;
2185221853
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+
diagnosticMessage = Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0;
2185421855
}
21855-
if (isChildOfLiteralType(node)) {
21856-
return grammarErrorOnNode(node, Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0o_0, node.text);
21856+
else if (isChildOfLiteralType(node)) {
21857+
diagnosticMessage = Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0;
21858+
}
21859+
if (diagnosticMessage) {
21860+
const withMinus = isMinusPrefixUnaryExpression(node.parent);
21861+
const literal = `${withMinus ? "-" : ""}0o${node.text}`;
21862+
return grammarErrorOnNode(withMinus ? node.parent : node, diagnosticMessage, literal);
2185721863
}
2185821864
}
2185921865
}

src/compiler/diagnosticMessages.json

Lines changed: 3 additions & 3 deletions
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. Use the syntax '0o{0}'.": {
230+
"Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '{0}'.": {
231231
"category": "Error",
232232
"code": 1085
233233
},
@@ -2948,7 +2948,7 @@
29482948
"Resolution for module '{0}' was found in cache": {
29492949
"category": "Message",
29502950
"code": 6147
2951-
},
2951+
},
29522952
"Variable '{0}' implicitly has an '{1}' type.": {
29532953
"category": "Error",
29542954
"code": 7005
@@ -3239,7 +3239,7 @@
32393239
"category": "Message",
32403240
"code": 90015
32413241
},
3242-
"Octal literal types must use ES2015 syntax. Use the syntax '0o{0}'.": {
3242+
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
32433243
"category": "Error",
32443244
"code": 8017
32453245
}

src/compiler/utilities.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,11 @@ namespace ts {
742742
return false;
743743
}
744744

745+
export function isMinusPrefixUnaryExpression(node: Node): boolean {
746+
return node.kind === SyntaxKind.PrefixUnaryExpression &&
747+
(node as PrefixUnaryExpression).operator === SyntaxKind.MinusToken;
748+
}
749+
745750
// Warning: This has the same semantics as the forEach family of functions,
746751
// in that traversal terminates in the event that 'visitor' supplies a truthy value.
747752
export function forEachReturnStatement<T>(body: Block, visitor: (stmt: ReturnStatement) => T): T {

tests/baselines/reference/literals.errors.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tests/cases/conformance/expressions/literals/literals.ts(9,17): error TS2363: Th
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.
55
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'.
6+
tests/cases/conformance/expressions/literals/literals.ts(25,9): 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) ====
@@ -42,8 +42,8 @@ tests/cases/conformance/expressions/literals/literals.ts(25,10): error TS1085: O
4242
var n = -1.0;
4343
var n = -1e-4;
4444
var n = -003; // Error in ES5
45-
~~~
46-
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o3'.
45+
~~~~
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;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
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'.
2+
tests/cases/compiler/oldStyleOctalLiteralTypes.ts(2,8): error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '-0o20'.
33

44

55
==== tests/cases/compiler/oldStyleOctalLiteralTypes.ts (2 errors) ====
66
let x: 010;
77
~~~
88
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o10'.
99
let y: -020;
10-
~~~
11-
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '0o20'.
10+
~~~~
11+
!!! error TS8017: Octal literal types must use ES2015 syntax. Use the syntax '-0o20'.
1212

Lines changed: 3 additions & 3 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. Use the syntax '0o3'.
1+
tests/cases/conformance/scanner/ecmascript5/scannerNumericLiteral8.ts(1,1): 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
6-
~~
7-
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o3'.
6+
~~~
7+
!!! error TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '-0o3'.

0 commit comments

Comments
 (0)