-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(unminify): transform typeof undefined
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/webcrack/src/unminify/test/typeof-undefined.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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";`, | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/webcrack/src/unminify/transforms/typeof-undefined.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |