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

Restore numeric seaprators support in @babel/standalone #14427

Merged
merged 2 commits into from Apr 6, 2022
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
Jump to file
Failed to load files.
Diff view
Diff view
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();
});
});
},
);