Skip to content
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

feat: enable numericSeparator parsing support #11863

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.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
48 changes: 19 additions & 29 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -1016,28 +1016,26 @@ export default class Tokenizer extends ParserErrors {
const code = this.input.charCodeAt(this.state.pos);
let val;

if (this.hasPlugin("numericSeparator")) {
if (code === charCodes.underscore) {
const prev = this.input.charCodeAt(this.state.pos - 1);
const next = this.input.charCodeAt(this.state.pos + 1);
if (allowedSiblings.indexOf(next) === -1) {
this.raise(this.state.pos, Errors.UnexpectedNumericSeparator);
} else if (
forbiddenSiblings.indexOf(prev) > -1 ||
forbiddenSiblings.indexOf(next) > -1 ||
Number.isNaN(next)
) {
this.raise(this.state.pos, Errors.UnexpectedNumericSeparator);
}

if (!allowNumSeparator) {
this.raise(this.state.pos, Errors.NumericSeparatorInEscapeSequence);
}
if (code === charCodes.underscore) {
const prev = this.input.charCodeAt(this.state.pos - 1);
const next = this.input.charCodeAt(this.state.pos + 1);
if (allowedSiblings.indexOf(next) === -1) {
this.raise(this.state.pos, Errors.UnexpectedNumericSeparator);
} else if (
forbiddenSiblings.indexOf(prev) > -1 ||
forbiddenSiblings.indexOf(next) > -1 ||
Number.isNaN(next)
) {
this.raise(this.state.pos, Errors.UnexpectedNumericSeparator);
}

// Ignore this _ character
++this.state.pos;
continue;
if (!allowNumSeparator) {
this.raise(this.state.pos, Errors.NumericSeparatorInEscapeSequence);
}

// Ignore this _ character
++this.state.pos;
continue;
}

if (code >= charCodes.lowercaseA) {
Expand Down Expand Up @@ -1088,10 +1086,6 @@ export default class Tokenizer extends ParserErrors {
}
const next = this.input.charCodeAt(this.state.pos);

if (next === charCodes.underscore) {
this.expectPlugin("numericSeparator", this.state.pos);
}

if (next === charCodes.lowercaseN) {
++this.state.pos;
isBigInt = true;
Expand Down Expand Up @@ -1129,7 +1123,7 @@ export default class Tokenizer extends ParserErrors {
const integer = this.input.slice(start, this.state.pos);
if (this.state.strict) {
this.raise(start, Errors.StrictOctalLiteral);
} else if (this.hasPlugin("numericSeparator")) {
} else {
// disallow numeric separators in non octal decimals and legacy octal likes
const underscorePos = integer.indexOf("_");
if (underscorePos > 0) {
Expand Down Expand Up @@ -1160,10 +1154,6 @@ export default class Tokenizer extends ParserErrors {
next = this.input.charCodeAt(this.state.pos);
}

if (next === charCodes.underscore) {
this.expectPlugin("numericSeparator", this.state.pos);
}

if (next === charCodes.lowercaseN) {
// disallow floats, legacy octal syntax and non octal decimals
// new style octal ("0o") is handled in this.readRadixNumber
Expand Down