From 7a473ed0c6e7bf4fb9f79d72ade0c3753efefac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 6 Apr 2022 17:06:09 +0200 Subject: [PATCH] Restore numeric seaprators support in `@babel/standalone` (#14427) * Add failing test * Don't spread sets, since we compile `@babel/parser` in `@babel/standalone` with `iterableIsArray` --- packages/babel-parser/src/tokenizer/index.js | 61 ++++++-------------- packages/babel-standalone/test/babel.js | 3 + 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index ad93c11f724d..93132e902e18 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -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 @@ -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; @@ -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) ) { diff --git a/packages/babel-standalone/test/babel.js b/packages/babel-standalone/test/babel.js index 8af895cc4149..8589d1f83a7a 100644 --- a/packages/babel-standalone/test/babel.js +++ b/packages/babel-standalone/test/babel.js @@ -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(); + }); }); }, );