Better error message for invalid bigint literals ending with n

Helps with the cases of decimals and exponentials, though not with numbers incorrectly starting with `0`.

Fixes #28887.
This commit is contained in:
Josh Goldberg
2018-12-07 00:40:13 -08:00
parent 71286e3d49
commit 773c50f05a
3 changed files with 29 additions and 9 deletions
+8
View File
@@ -1015,6 +1015,14 @@
"category": "Error",
"code": 1351
},
"A bigint literal cannot use exponential notation.": {
"category": "Error",
"code": 1352
},
"A bigint literal must be an integer.": {
"category": "Error",
"code": 1353
},
"Duplicate identifier '{0}'.": {
"category": "Error",
+15 -3
View File
@@ -976,7 +976,7 @@ namespace ts {
}
if (decimalFragment !== undefined || tokenFlags & TokenFlags.Scientific) {
checkForIdentifierStartAfterNumericLiteral();
checkForIdentifierStartAfterNumericLiteral(decimalFragment === undefined && !!(tokenFlags & TokenFlags.Scientific));
return {
type: SyntaxKind.NumericLiteral,
value: "" + +result // if value is not an integer, it can be safely coerced to a number
@@ -990,14 +990,26 @@ namespace ts {
}
}
function checkForIdentifierStartAfterNumericLiteral() {
function checkForIdentifierStartAfterNumericLiteral(isScientific?: boolean) {
if (!isIdentifierStart(text.charCodeAt(pos), languageVersion)) {
return;
}
const identifierStart = pos;
const { length } = scanIdentifierParts();
error(Diagnostics.An_identifier_or_keyword_cannot_immediately_follow_a_numeric_literal, identifierStart, length);
if (length === 1 && text[identifierStart] === "n") {
if (isScientific) {
error(Diagnostics.A_bigint_literal_cannot_use_exponential_notation, identifierStart, length);
}
else {
error(Diagnostics.A_bigint_literal_must_be_an_integer, identifierStart, length);
}
}
else {
error(Diagnostics.An_identifier_or_keyword_cannot_immediately_follow_a_numeric_literal, identifierStart, length);
}
pos = identifierStart;
}