Skip to content

Commit

Permalink
feat(unminify): transform typeof undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Dec 17, 2023
1 parent 6c8b722 commit 220f0a6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions apps/docs/src/concepts/unminify.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ if (a) { // [!code ++]
} // [!code ++]
```

## typeof-undefined

```js
typeof a > "u" // [!code --]
typeof a === "undefined" // [!code ++]
```

```js
typeof a < "u" // [!code --]
typeof a !== "undefined" // [!code ++]
```

## unminify-booleans

```js
Expand Down
15 changes: 15 additions & 0 deletions packages/webcrack/src/unminify/test/typeof-undefined.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from 'vitest';
import { testTransform } from '../../../test';
import { typeofUndefined } from '../transforms';

const expectJS = testTransform(typeofUndefined);

test('typeof greater than', () =>
expectJS('typeof a > "u"').toMatchInlineSnapshot(
`typeof a === "undefined";`,
));

test('typeof less than', () =>
expectJS('typeof a < "u"').toMatchInlineSnapshot(
`typeof a !== "undefined";`,
));
1 change: 1 addition & 0 deletions packages/webcrack/src/unminify/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { default as sequence } from './sequence';
export { default as splitVariableDeclarations } from './split-variable-declarations';
export { default as templateLiterals } from './template-literals';
export { default as ternaryToIf } from './ternary-to-if';
export { default as typeofUndefined } from './typeof-undefined';
export { default as unaryExpressions } from './unary-expressions';
export { default as unminifyBooleans } from './unminify-booleans';
export { default as voidToUndefined } from './void-to-undefined';
Expand Down
37 changes: 37 additions & 0 deletions packages/webcrack/src/unminify/transforms/typeof-undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as t from '@babel/types';
import * as m from '@codemod/matchers';
import { Transform } from '../../ast-utils';

const OPERATOR_MAP = {
'>': '===',
'<': '!==',
} as const;

export default {
name: 'typeof-undefined',
tags: ['safe'],
visitor() {
const operator = m.capture(m.or('>' as const, '<' as const));
const argument = m.capture(m.anyExpression());
const matcher = m.binaryExpression(
operator,
m.unaryExpression('typeof', argument),
m.stringLiteral('u'),
);
return {
BinaryExpression: {
exit(path) {
if (!matcher.match(path.node)) return;
path.replaceWith(
t.binaryExpression(
OPERATOR_MAP[operator.current!],
t.unaryExpression('typeof', argument.current!),
t.stringLiteral('undefined'),
),
);
this.changes++;
},
},
};
},
} satisfies Transform;

0 comments on commit 220f0a6

Please sign in to comment.