Skip to content

Commit

Permalink
Restore numeric seaprators support in @babel/standalone (#14427)
Browse files Browse the repository at this point in the history
* Add failing test

* Don't spread sets, since we compile `@babel/parser` in `@babel/standalone` with `iterableIsArray`
  • Loading branch information
nicolo-ribaudo committed Apr 6, 2022
1 parent 84336bb commit 7a473ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 44 deletions.
61 changes: 17 additions & 44 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -69,49 +69,22 @@ const forbiddenNumericSeparatorSiblings = {
]),
};

const allowedNumericSeparatorSiblings = {};
allowedNumericSeparatorSiblings.bin = new Set([
const isAllowedNumericSeparatorSibling = {
// 0 - 1
charCodes.digit0,
charCodes.digit1,
]);
allowedNumericSeparatorSiblings.oct = new Set([
bin: ch => ch === charCodes.digit0 || ch === charCodes.digit1,

// 0 - 7
...allowedNumericSeparatorSiblings.bin,

charCodes.digit2,
charCodes.digit3,
charCodes.digit4,
charCodes.digit5,
charCodes.digit6,
charCodes.digit7,
]);
allowedNumericSeparatorSiblings.dec = new Set([
// 0 - 9
...allowedNumericSeparatorSiblings.oct,
oct: ch => ch >= charCodes.digit0 && ch <= charCodes.digit7,

charCodes.digit8,
charCodes.digit9,
]);
// 0 - 9
dec: ch => ch >= charCodes.digit0 && ch <= charCodes.digit9,

allowedNumericSeparatorSiblings.hex = new Set([
// 0 - 9, A - F, a - f,
...allowedNumericSeparatorSiblings.dec,

charCodes.uppercaseA,
charCodes.uppercaseB,
charCodes.uppercaseC,
charCodes.uppercaseD,
charCodes.uppercaseE,
charCodes.uppercaseF,

charCodes.lowercaseA,
charCodes.lowercaseB,
charCodes.lowercaseC,
charCodes.lowercaseD,
charCodes.lowercaseE,
charCodes.lowercaseF,
]);
hex: ch =>
(ch >= charCodes.digit0 && ch <= charCodes.digit9) ||
(ch >= charCodes.uppercaseA && ch <= charCodes.uppercaseF) ||
(ch >= charCodes.lowercaseA && ch <= charCodes.lowercaseF),
};

// Object type used to represent tokens. Note that normally, tokens
// simply exist as properties on the parser object. This is only
Expand Down Expand Up @@ -1180,14 +1153,14 @@ export default class Tokenizer extends CommentsParser {
radix === 16
? forbiddenNumericSeparatorSiblings.hex
: forbiddenNumericSeparatorSiblings.decBinOct;
const allowedSiblings =
const isAllowedSibling =
radix === 16
? allowedNumericSeparatorSiblings.hex
? isAllowedNumericSeparatorSibling.hex
: radix === 10
? allowedNumericSeparatorSiblings.dec
? isAllowedNumericSeparatorSibling.dec
: radix === 8
? allowedNumericSeparatorSiblings.oct
: allowedNumericSeparatorSiblings.bin;
? isAllowedNumericSeparatorSibling.oct
: isAllowedNumericSeparatorSibling.bin;
let invalid = false;
let total = 0;
Expand All @@ -1206,7 +1179,7 @@ export default class Tokenizer extends CommentsParser {
});
} else if (
Number.isNaN(next) ||
!allowedSiblings.has(next) ||
!isAllowedSibling(next) ||
forbiddenSiblings.has(prev) ||
forbiddenSiblings.has(next)
) {
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-standalone/test/babel.js
Expand Up @@ -256,6 +256,9 @@ const require = createRequire(import.meta.url);
}),
).not.toThrow();
});
it("#14425 - numeric separators should be parsed correctly", () => {
expect(() => Babel.transform("1_1", {})).not.toThrow();
});
});
},
);

0 comments on commit 7a473ed

Please sign in to comment.