Skip to content

Commit 73d8afb

Browse files
committed
fix: "Maximum call stack size exceeded" when flipping comparisons
1 parent 241f946 commit 73d8afb

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

packages/webcrack/src/unminify/test/yoda.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ test('ignore other operators', () =>
6363

6464
test('ignore when right side is a literal', () =>
6565
expectJS('1 === 2').toMatchInlineSnapshot('1 === 2;'));
66+
67+
test('ignore when both sides are pure values', () =>
68+
expectJS('NaN == Infinity').toMatchInlineSnapshot(`NaN == Infinity;`));

packages/webcrack/src/unminify/transforms/yoda.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,23 @@ export default {
2323
name: 'yoda',
2424
tags: ['safe'],
2525
visitor: () => {
26+
const pureValue = m.or(
27+
m.stringLiteral(),
28+
m.numericLiteral(),
29+
m.unaryExpression(
30+
'-',
31+
m.or(m.numericLiteral(), m.identifier('Infinity')),
32+
),
33+
m.booleanLiteral(),
34+
m.nullLiteral(),
35+
m.identifier('undefined'),
36+
m.identifier('NaN'),
37+
m.identifier('Infinity'),
38+
);
2639
const matcher = m.binaryExpression(
2740
m.or(...Object.values(FLIPPED_OPERATORS)),
28-
m.or(
29-
m.stringLiteral(),
30-
m.numericLiteral(),
31-
m.unaryExpression(
32-
'-',
33-
m.or(m.numericLiteral(), m.identifier('Infinity')),
34-
),
35-
m.booleanLiteral(),
36-
m.nullLiteral(),
37-
m.identifier('undefined'),
38-
m.identifier('NaN'),
39-
m.identifier('Infinity'),
40-
),
41-
m.matcher((node) => !t.isLiteral(node)),
41+
pureValue,
42+
m.matcher((node) => !pureValue.match(node)),
4243
);
4344

4445
return {

0 commit comments

Comments
 (0)