Skip to content

Commit

Permalink
fix: "Maximum call stack size exceeded" when flipping comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Aug 14, 2024
1 parent 241f946 commit 73d8afb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
3 changes: 3 additions & 0 deletions packages/webcrack/src/unminify/test/yoda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ test('ignore other operators', () =>

test('ignore when right side is a literal', () =>
expectJS('1 === 2').toMatchInlineSnapshot('1 === 2;'));

test('ignore when both sides are pure values', () =>
expectJS('NaN == Infinity').toMatchInlineSnapshot(`NaN == Infinity;`));
29 changes: 15 additions & 14 deletions packages/webcrack/src/unminify/transforms/yoda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ export default {
name: 'yoda',
tags: ['safe'],
visitor: () => {
const pureValue = m.or(
m.stringLiteral(),
m.numericLiteral(),
m.unaryExpression(
'-',
m.or(m.numericLiteral(), m.identifier('Infinity')),
),
m.booleanLiteral(),
m.nullLiteral(),
m.identifier('undefined'),
m.identifier('NaN'),
m.identifier('Infinity'),
);
const matcher = m.binaryExpression(
m.or(...Object.values(FLIPPED_OPERATORS)),
m.or(
m.stringLiteral(),
m.numericLiteral(),
m.unaryExpression(
'-',
m.or(m.numericLiteral(), m.identifier('Infinity')),
),
m.booleanLiteral(),
m.nullLiteral(),
m.identifier('undefined'),
m.identifier('NaN'),
m.identifier('Infinity'),
),
m.matcher((node) => !t.isLiteral(node)),
pureValue,
m.matcher((node) => !pureValue.match(node)),
);

return {
Expand Down

0 comments on commit 73d8afb

Please sign in to comment.