From ec86d413ba388a5e10f3b0b39cb04b797821e727 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Wed, 15 Jul 2020 23:01:17 +0300 Subject: [PATCH] fix: Restrict leading zero before decimal separator (#1389) --- src/tokenizer.ts | 22 +++++++++++-------- tests/parser/numeric-separators.ts | 9 +++++++- tests/parser/numeric-separators.ts.fixture.ts | 10 +++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/tokenizer.ts b/src/tokenizer.ts index 02f8dae7da..7d070b1967 100644 --- a/src/tokenizer.ts +++ b/src/tokenizer.ts @@ -1052,13 +1052,15 @@ export class Tokenizer extends DiagnosticEmitter { readIdentifier(): string { var text = this.source.text; - var start = this.pos; var end = this.end; + var pos = this.pos; + var start = pos; while ( - ++this.pos < end && - isIdentifierPart(text.charCodeAt(this.pos)) + ++pos < end && + isIdentifierPart(text.charCodeAt(pos)) ); - return text.substring(start, this.pos); + this.pos = pos; + return text.substring(start, pos); } readString(): string { @@ -1362,6 +1364,11 @@ export class Tokenizer extends DiagnosticEmitter { : DiagnosticCode.Multiple_consecutive_numeric_separators_are_not_permitted, this.range(pos) ); + } else if (pos - 1 == start && text.charCodeAt(pos - 1) == CharCode._0) { + this.error( + DiagnosticCode.Numeric_separators_are_not_allowed_here, + this.range(pos) + ); } sepEnd = pos + 1; } else { @@ -1497,9 +1504,7 @@ export class Tokenizer extends DiagnosticEmitter { var text = this.source.text; var end = this.end; var start = this.pos; - var sepCount = 0; - - sepCount += this.readDecimalFloatPartial(false); + var sepCount = this.readDecimalFloatPartial(false); if (this.pos < end && text.charCodeAt(this.pos) == CharCode.DOT) { ++this.pos; sepCount += this.readDecimalFloatPartial(); @@ -1518,7 +1523,7 @@ export class Tokenizer extends DiagnosticEmitter { } } let result = text.substring(start, this.pos); - if (sepCount > 0) result = result.replaceAll("_", ""); + if (sepCount) result = result.replaceAll("_", ""); return parseFloat(result); } @@ -1553,7 +1558,6 @@ export class Tokenizer extends DiagnosticEmitter { } else if (!isDecimalDigit(c)) { break; } - ++pos; } diff --git a/tests/parser/numeric-separators.ts b/tests/parser/numeric-separators.ts index 3341aef2fa..05037b0034 100644 --- a/tests/parser/numeric-separators.ts +++ b/tests/parser/numeric-separators.ts @@ -35,6 +35,13 @@ 1e2_; // 6188 1e-1__0; // 6189 +0_0; // 6188 0_0.0; // 6188 0_0.0_0; // 6188 -0_0e0_0; // 6188 \ No newline at end of file +0_0e0_0; // 6188 + +0x_11_11; // 6188 +0o_11_11; // 6188 +0b_11_11; // 6188 + +00_01 // 1121 diff --git a/tests/parser/numeric-separators.ts.fixture.ts b/tests/parser/numeric-separators.ts.fixture.ts index a0cb362767..acc3384c8f 100644 --- a/tests/parser/numeric-separators.ts.fixture.ts +++ b/tests/parser/numeric-separators.ts.fixture.ts @@ -28,6 +28,11 @@ 0; 0; 0; +0; +4369; +585; +15; +1; // ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(14,9+0) // ERROR 6189: "Multiple consecutive numeric separators are not permitted." in numeric-separators.ts(15,4+0) // ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(17,11+0) @@ -48,3 +53,8 @@ // ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(38,2+0) // ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(39,2+0) // ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(40,2+0) +// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(41,2+0) +// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(43,3+0) +// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(44,3+0) +// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(45,3+0) +// ERROR 1121: "Octal literals are not allowed in strict mode." in numeric-separators.ts(47,1+5)