X Tutup
Skip to content

Commit 7a473ed

Browse files
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`
1 parent 84336bb commit 7a473ed

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

packages/babel-parser/src/tokenizer/index.js

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,49 +69,22 @@ const forbiddenNumericSeparatorSiblings = {
6969
]),
7070
};
7171

72-
const allowedNumericSeparatorSiblings = {};
73-
allowedNumericSeparatorSiblings.bin = new Set([
72+
const isAllowedNumericSeparatorSibling = {
7473
// 0 - 1
75-
charCodes.digit0,
76-
charCodes.digit1,
77-
]);
78-
allowedNumericSeparatorSiblings.oct = new Set([
74+
bin: ch => ch === charCodes.digit0 || ch === charCodes.digit1,
75+
7976
// 0 - 7
80-
...allowedNumericSeparatorSiblings.bin,
81-
82-
charCodes.digit2,
83-
charCodes.digit3,
84-
charCodes.digit4,
85-
charCodes.digit5,
86-
charCodes.digit6,
87-
charCodes.digit7,
88-
]);
89-
allowedNumericSeparatorSiblings.dec = new Set([
90-
// 0 - 9
91-
...allowedNumericSeparatorSiblings.oct,
77+
oct: ch => ch >= charCodes.digit0 && ch <= charCodes.digit7,
9278

93-
charCodes.digit8,
94-
charCodes.digit9,
95-
]);
79+
// 0 - 9
80+
dec: ch => ch >= charCodes.digit0 && ch <= charCodes.digit9,
9681

97-
allowedNumericSeparatorSiblings.hex = new Set([
9882
// 0 - 9, A - F, a - f,
99-
...allowedNumericSeparatorSiblings.dec,
100-
101-
charCodes.uppercaseA,
102-
charCodes.uppercaseB,
103-
charCodes.uppercaseC,
104-
charCodes.uppercaseD,
105-
charCodes.uppercaseE,
106-
charCodes.uppercaseF,
107-
108-
charCodes.lowercaseA,
109-
charCodes.lowercaseB,
110-
charCodes.lowercaseC,
111-
charCodes.lowercaseD,
112-
charCodes.lowercaseE,
113-
charCodes.lowercaseF,
114-
]);
83+
hex: ch =>
84+
(ch >= charCodes.digit0 && ch <= charCodes.digit9) ||
85+
(ch >= charCodes.uppercaseA && ch <= charCodes.uppercaseF) ||
86+
(ch >= charCodes.lowercaseA && ch <= charCodes.lowercaseF),
87+
};
11588

11689
// Object type used to represent tokens. Note that normally, tokens
11790
// simply exist as properties on the parser object. This is only
@@ -1180,14 +1153,14 @@ export default class Tokenizer extends CommentsParser {
11801153
radix === 16
11811154
? forbiddenNumericSeparatorSiblings.hex
11821155
: forbiddenNumericSeparatorSiblings.decBinOct;
1183-
const allowedSiblings =
1156+
const isAllowedSibling =
11841157
radix === 16
1185-
? allowedNumericSeparatorSiblings.hex
1158+
? isAllowedNumericSeparatorSibling.hex
11861159
: radix === 10
1187-
? allowedNumericSeparatorSiblings.dec
1160+
? isAllowedNumericSeparatorSibling.dec
11881161
: radix === 8
1189-
? allowedNumericSeparatorSiblings.oct
1190-
: allowedNumericSeparatorSiblings.bin;
1162+
? isAllowedNumericSeparatorSibling.oct
1163+
: isAllowedNumericSeparatorSibling.bin;
11911164

11921165
let invalid = false;
11931166
let total = 0;
@@ -1206,7 +1179,7 @@ export default class Tokenizer extends CommentsParser {
12061179
});
12071180
} else if (
12081181
Number.isNaN(next) ||
1209-
!allowedSiblings.has(next) ||
1182+
!isAllowedSibling(next) ||
12101183
forbiddenSiblings.has(prev) ||
12111184
forbiddenSiblings.has(next)
12121185
) {

packages/babel-standalone/test/babel.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ const require = createRequire(import.meta.url);
256256
}),
257257
).not.toThrow();
258258
});
259+
it("#14425 - numeric separators should be parsed correctly", () => {
260+
expect(() => Babel.transform("1_1", {})).not.toThrow();
261+
});
259262
});
260263
},
261264
);

0 commit comments

Comments
 (0)
X Tutup