Improve diagnostic message for negative old style literals

This commit is contained in:
Alexander Rusakov
2016-12-20 17:55:14 +03:00
committed by arusakov
parent 4cbb50a00c
commit db1fda5e77
6 changed files with 26 additions and 15 deletions

View File

@@ -21857,11 +21857,17 @@ namespace ts {
function checkGrammarNumericLiteral(node: NumericLiteral): boolean {
// Grammar checking
if (node.isOctalLiteral) {
let diagnosticMessage: DiagnosticMessage | undefined;
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);
diagnosticMessage = Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher_Use_the_syntax_0;
}
if (isChildOfLiteralType(node)) {
return grammarErrorOnNode(node, Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0o_0, node.text);
else if (isChildOfLiteralType(node)) {
diagnosticMessage = Diagnostics.Octal_literal_types_must_use_ES2015_syntax_Use_the_syntax_0;
}
if (diagnosticMessage) {
const withMinus = isMinusPrefixUnaryExpression(node.parent);
const literal = `${withMinus ? "-" : ""}0o${node.text}`;
return grammarErrorOnNode(withMinus ? node.parent : node, diagnosticMessage, literal);
}
}
}

View File

@@ -227,7 +227,7 @@
"category": "Error",
"code": 1084
},
"Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '0o{0}'.": {
"Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '{0}'.": {
"category": "Error",
"code": 1085
},
@@ -2952,7 +2952,7 @@
"Resolution for module '{0}' was found in cache": {
"category": "Message",
"code": 6147
},
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005
@@ -3243,7 +3243,7 @@
"category": "Message",
"code": 90015
},
"Octal literal types must use ES2015 syntax. Use the syntax '0o{0}'.": {
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
"category": "Error",
"code": 8017
}

View File

@@ -742,6 +742,11 @@ namespace ts {
return false;
}
export function isMinusPrefixUnaryExpression(node: Node): boolean {
return node.kind === SyntaxKind.PrefixUnaryExpression &&
(node as PrefixUnaryExpression).operator === SyntaxKind.MinusToken;
}
// 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<T>(body: Block, visitor: (stmt: ReturnStatement) => T): T {