Skip to content

Commit

Permalink
feat: enable numericSeparator parsing support (#11863)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 22, 2020
1 parent 8f3f175 commit 475c397
Show file tree
Hide file tree
Showing 358 changed files with 19 additions and 58 deletions.
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

0 comments on commit 475c397

Please sign in to comment.