Skip to content

Commit cc7ca33

Browse files
committed
Simplify checking for octal literals in parser
1 parent 92f7c98 commit cc7ca33

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/compiler/parser.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,25 +1055,25 @@ module ts {
10551055
function parseLiteralNode(): LiteralExpression {
10561056
var node = <LiteralExpression>createNode(token);
10571057
node.text = scanner.getTokenValue();
1058+
var tokenPos = scanner.getTokenPos();
10581059
nextToken();
10591060
finishNode(node);
10601061

10611062
// Octal literals are not allowed in strict mode or ES5
1062-
if (node.kind === SyntaxKind.NumericLiteral && (isInStrictMode || languageVersion >= ScriptTarget.ES5)) {
1063-
var numberLiteralSource = getSourceTextOfNodeFromSourceText(sourceText, node);
1064-
// This regex checks if the number is written in octal
1065-
// Note that theoretically would match literals like 009, which is not octal. But because
1066-
// of how the scanner separates the tokens, we would never get a token like this. Instead,
1067-
// we would get 00 and 9 as two separate tokens.
1068-
// We also do not need to check for negatives because any prefix operator would be part of a
1069-
// parent unary expression.
1070-
if (/0[0-7]+/.test(numberLiteralSource)) {
1071-
if (isInStrictMode) {
1072-
grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode);
1073-
}
1074-
else {
1075-
grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher);
1076-
}
1063+
// Note that theoretically the following condition would hold true literals like 009,
1064+
// which is not octal.But because of how the scanner separates the tokens, we would
1065+
// never get a token like this.Instead, we would get 00 and 9 as two separate tokens.
1066+
// We also do not need to check for negatives because any prefix operator would be part of a
1067+
// parent unary expression.
1068+
if (node.kind === SyntaxKind.NumericLiteral
1069+
&& sourceText.charCodeAt(tokenPos) === CharacterCodes._0
1070+
&& isOctalDigit(sourceText.charCodeAt(tokenPos + 1))) {
1071+
1072+
if (isInStrictMode) {
1073+
grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode);
1074+
}
1075+
else if (languageVersion >= ScriptTarget.ES5) {
1076+
grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher);
10771077
}
10781078
}
10791079

src/compiler/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ module ts {
300300
return ch >= CharacterCodes._0 && ch <= CharacterCodes._9;
301301
}
302302

303-
function isOctalDigit(ch: number): boolean {
303+
export function isOctalDigit(ch: number): boolean {
304304
return ch >= CharacterCodes._0 && ch <= CharacterCodes._7;
305305
}
306306

0 commit comments

Comments
 (0)