Skip to content

Commit db1fda5

Browse files
committed
Improve diagnostic message for negative old style literals
1 parent 4cbb50a commit db1fda5

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
@@ -21857,11 +21857,17 @@ namespace ts {
2185721857
function checkGrammarNumericLiteral(node: NumericLiteral): boolean {
2185821858
// Grammar checking
2185921859
if (node.isOctalLiteral) {
21860+
let diagnosticMessage: DiagnosticMessage | undefined;
2186021861
if (languageVersion >= ScriptTarget.ES5) {
21861-
return grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0o_0, node.text);
21862+
diagnosticMessage = Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0;
2186221863
}
21863-
if (isChildOfLiteralType(node)) {
21864-
return grammarErrorOnNode(node, Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0o_0, node.text);
21864+
else if (isChildOfLiteralType(node)) {
21865+
diagnosticMessage = Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0;
21866+
}
21867+
if (diagnosticMessage) {
21868+
const withMinus = isMinusPrefixUnaryExpression(node.parent);
21869+
const literal = `${withMinus ? "-" : ""}0o${node.text}`;
21870+
return grammarErrorOnNode(withMinus ? node.parent : node, diagnosticMessage, literal);
2186521871
}
2186621872
}
2186721873
}

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
},
@@ -2952,7 +2952,7 @@
29522952
"Resolution for module '{0}' was found in cache": {
29532953
"category": "Message",
29542954
"code": 6147
2955-
},
2955+
},
29562956
"Variable '{0}' implicitly has an '{1}' type.": {
29572957
"category": "Error",
29582958
"code": 7005
@@ -3243,7 +3243,7 @@
32433243
"category": "Message",
32443244
"code": 90015
32453245
},
3246-
"Octal literal types must use ES2015 syntax. Use the syntax '0o{0}'.": {
3246+
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
32473247
"category": "Error",
32483248
"code": 8017
32493249
}

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)