Skip to content

Commit ec86d41

Browse files
authored
fix: Restrict leading zero before decimal separator (#1389)
1 parent 8941652 commit ec86d41

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

Diff for: src/tokenizer.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -1052,13 +1052,15 @@ export class Tokenizer extends DiagnosticEmitter {
10521052

10531053
readIdentifier(): string {
10541054
var text = this.source.text;
1055-
var start = this.pos;
10561055
var end = this.end;
1056+
var pos = this.pos;
1057+
var start = pos;
10571058
while (
1058-
++this.pos < end &&
1059-
isIdentifierPart(text.charCodeAt(this.pos))
1059+
++pos < end &&
1060+
isIdentifierPart(text.charCodeAt(pos))
10601061
);
1061-
return text.substring(start, this.pos);
1062+
this.pos = pos;
1063+
return text.substring(start, pos);
10621064
}
10631065

10641066
readString(): string {
@@ -1362,6 +1364,11 @@ export class Tokenizer extends DiagnosticEmitter {
13621364
: DiagnosticCode.Multiple_consecutive_numeric_separators_are_not_permitted,
13631365
this.range(pos)
13641366
);
1367+
} else if (pos - 1 == start && text.charCodeAt(pos - 1) == CharCode._0) {
1368+
this.error(
1369+
DiagnosticCode.Numeric_separators_are_not_allowed_here,
1370+
this.range(pos)
1371+
);
13651372
}
13661373
sepEnd = pos + 1;
13671374
} else {
@@ -1497,9 +1504,7 @@ export class Tokenizer extends DiagnosticEmitter {
14971504
var text = this.source.text;
14981505
var end = this.end;
14991506
var start = this.pos;
1500-
var sepCount = 0;
1501-
1502-
sepCount += this.readDecimalFloatPartial(false);
1507+
var sepCount = this.readDecimalFloatPartial(false);
15031508
if (this.pos < end && text.charCodeAt(this.pos) == CharCode.DOT) {
15041509
++this.pos;
15051510
sepCount += this.readDecimalFloatPartial();
@@ -1518,7 +1523,7 @@ export class Tokenizer extends DiagnosticEmitter {
15181523
}
15191524
}
15201525
let result = text.substring(start, this.pos);
1521-
if (sepCount > 0) result = result.replaceAll("_", "");
1526+
if (sepCount) result = result.replaceAll("_", "");
15221527
return parseFloat(result);
15231528
}
15241529

@@ -1553,7 +1558,6 @@ export class Tokenizer extends DiagnosticEmitter {
15531558
} else if (!isDecimalDigit(c)) {
15541559
break;
15551560
}
1556-
15571561
++pos;
15581562
}
15591563

Diff for: tests/parser/numeric-separators.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
1e2_; // 6188
3636
1e-1__0; // 6189
3737

38+
0_0; // 6188
3839
0_0.0; // 6188
3940
0_0.0_0; // 6188
40-
0_0e0_0; // 6188
41+
0_0e0_0; // 6188
42+
43+
0x_11_11; // 6188
44+
0o_11_11; // 6188
45+
0b_11_11; // 6188
46+
47+
00_01 // 1121

Diff for: tests/parser/numeric-separators.ts.fixture.ts

+10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
0;
2929
0;
3030
0;
31+
0;
32+
4369;
33+
585;
34+
15;
35+
1;
3136
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(14,9+0)
3237
// ERROR 6189: "Multiple consecutive numeric separators are not permitted." in numeric-separators.ts(15,4+0)
3338
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(17,11+0)
@@ -48,3 +53,8 @@
4853
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(38,2+0)
4954
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(39,2+0)
5055
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(40,2+0)
56+
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(41,2+0)
57+
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(43,3+0)
58+
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(44,3+0)
59+
// ERROR 6188: "Numeric separators are not allowed here." in numeric-separators.ts(45,3+0)
60+
// ERROR 1121: "Octal literals are not allowed in strict mode." in numeric-separators.ts(47,1+5)

0 commit comments

Comments
 (0)