Skip to content

Restrict leading zero before separator for decimal integers #1389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}

Expand Down Expand Up @@ -1553,7 +1558,6 @@ export class Tokenizer extends DiagnosticEmitter {
} else if (!isDecimalDigit(c)) {
break;
}

++pos;
}

Expand Down
9 changes: 8 additions & 1 deletion tests/parser/numeric-separators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
0_0e0_0; // 6188

0x_11_11; // 6188
0o_11_11; // 6188
0b_11_11; // 6188

00_01 // 1121
10 changes: 10 additions & 0 deletions tests/parser/numeric-separators.ts.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)