Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate default parser to babylon #31

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions packages/ast-utils/src/exports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isString } from './isPrimitive'
import { isTopLevel } from './isTopLevel'
import type { ASTNode, AssignmentExpression, Collection, Identifier, JSCodeshift, MemberExpression, VariableDeclarator } from 'jscodeshift'

Expand Down Expand Up @@ -51,23 +50,19 @@ export class ExportManager {
}

if (path.node.specifiers) {
const source = j.Literal.check(path.node.source) && isString(path.node.source.value)
const source = j.StringLiteral.check(path.node.source)
? path.node.source.value
: null

path.node.specifiers.forEach((specifier) => {
const exported = j.Identifier.check(specifier.exported)
? specifier.exported.name
: isString(specifier.exported)
? specifier.exported
: null
: null
if (!exported) return

const local = j.Identifier.check(specifier.local)
? specifier.local.name
: isString(specifier.local)
? specifier.local
: exported
: exported

this.addNamedExport(exported, local)
if (source) this.addExportFrom(local, source)
Expand Down
30 changes: 15 additions & 15 deletions packages/ast-utils/src/imports.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { MultiMap } from '@wakaru/ds'
import { isNumber, isString } from './isPrimitive'
import { isTopLevel } from './isTopLevel'
import type { NodePath } from 'ast-types/lib/node-path'
import type { ASTNode, CallExpression, Collection, ImportDeclaration, JSCodeshift, Literal, VariableDeclaration, VariableDeclarator } from 'jscodeshift'
import type { ASTNode, CallExpression, Collection, ImportDeclaration, JSCodeshift, StringLiteral, VariableDeclaration, VariableDeclarator } from 'jscodeshift'

type Source = string
type Imported = string
Expand Down Expand Up @@ -164,7 +163,7 @@ export class ImportManager {
.find(j.ImportDeclaration)
.forEach((path) => {
const { specifiers, source } = path.node
if (!j.Literal.check(source) || typeof source.value !== 'string') return
if (!j.StringLiteral.check(source)) return

const sourceValue = source.value
this.addImportOrder(sourceValue)
Expand Down Expand Up @@ -239,8 +238,8 @@ export class ImportManager {
? firstDeclaration.init.object as CallExpression
: firstDeclaration.init as CallExpression

const sourceLiteral = init.arguments[0] as Literal
const source = sourceLiteral.value as string
const sourceLiteral = init.arguments[0] as StringLiteral
const source = sourceLiteral.value

if (j.Identifier.check(id)) {
const local = id.name
Expand All @@ -253,7 +252,7 @@ export class ImportManager {
*/
if (j.ObjectPattern.check(id)) {
id.properties.forEach((property) => {
if (j.Property.check(property)
if (j.ObjectProperty.check(property)
&& j.Identifier.check(property.key)
&& j.Identifier.check(property.value)
) {
Expand Down Expand Up @@ -300,13 +299,13 @@ export class ImportManager {
...(firstDefaultImport ? [j.importDefaultSpecifier(j.identifier(firstDefaultImport.name))] : []),
...namedImports.map(info => j.importSpecifier(j.identifier(info.name), j.identifier(info.local))),
]
const importDeclaration = j.importDeclaration(importSpecifiers, j.literal(source))
const importDeclaration = j.importDeclaration(importSpecifiers, j.stringLiteral(source))
importStatements.push(importDeclaration)

if (restDefaultImports.length > 0) {
const restImportDeclaration = restDefaultImports.map(info => j.importDeclaration(
[j.importDefaultSpecifier(j.identifier(info.name))],
j.literal(source),
j.stringLiteral(source),
))
importStatements.push(...restImportDeclaration)
}
Expand All @@ -316,7 +315,7 @@ export class ImportManager {
const first = namespaceImports[0]
const importDeclaration = j.importDeclaration(
[j.importNamespaceSpecifier(j.identifier(first.name!))],
j.literal(source),
j.stringLiteral(source),
)
importStatements.push(importDeclaration)

Expand All @@ -336,7 +335,7 @@ export class ImportManager {
* Bare import is not needed if there are other imports
*/
if (bareImports.length > 0) {
const importDeclaration = j.importDeclaration([], j.literal(source))
const importDeclaration = j.importDeclaration([], j.stringLiteral(source))
importStatements.push(importDeclaration)
}

Expand Down Expand Up @@ -365,10 +364,11 @@ function isRequireCall(j: JSCodeshift, node: ASTNode) {
type: 'Identifier',
name: 'require',
},
arguments: [{
type: 'Literal' as const,
// @ts-expect-error
value: (value: unknown) => isString(value) || isNumber(value),
}],
// @ts-expect-error
arguments: (args) => {
if (args.length !== 1) return false
return j.StringLiteral.check(args[0])
|| j.NumericLiteral.check(args[0])
},
})
}
1 change: 0 additions & 1 deletion packages/ast-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ export { wrapAstTransformation } from './wrapAstTransformation'

export * from './imports'
export * from './exports'
export * from './isPrimitive'
export * from './types'
export * from './reference'
5 changes: 0 additions & 5 deletions packages/ast-utils/src/isPrimitive.ts

This file was deleted.

20 changes: 14 additions & 6 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ export function defineInlineTestWithOptions(transform: Transform) {
expectedOutput: string,
) {
it(testName, () => {
runInlineTest(transform, options, {
source: input,
}, expectedOutput)
runInlineTest(
transform,
options,
{ source: input },
expectedOutput,
{ parser: 'babylon' },
)
})
}
}
Expand Down Expand Up @@ -52,9 +56,13 @@ export function defineInlineTest(transforms: Transform | Transform[]) {
const itFn = modifier ? it[modifier] : it
itFn(testName, () => {
try {
runInlineTest(reducedTransform, {}, {
source: input,
}, expectedOutput)
runInlineTest(
reducedTransform,
{},
{ source: input },
expectedOutput,
{ parser: 'babylon' },
)
}
catch (err) {
/**
Expand Down
5 changes: 2 additions & 3 deletions packages/unminify/docs/Terser.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ This is the tracking list of support status of the reversion of [compressor opti
- [ ] arguments
- TODO: priority `low`.
- We can do the same thing this rule does as some obfuscator might use arguments to obfuscate.
- [ ] arrows
- TODO: priority `low`.
- We can improve `function-to-arrow` rule to follow the same strategy.
- [X] arrows
- `lebab`'s `arrow` rule.
- [X] ~~booleans_as_integers~~
- `a === true` will be converted to `a == 1`.
- It is risky to reverse a loose equality.
Expand Down
5 changes: 1 addition & 4 deletions packages/unminify/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import jscodeshift from 'jscodeshift'

// @ts-expect-error - no types
import getParser from 'jscodeshift/src/getParser'
import { transformationMap } from './transformations'
import { arraify } from './utils/arraify'
import type { MaybeArray } from './utils/types'
Expand All @@ -19,8 +17,7 @@ export function runTransformations<P extends Record<string, any>>(
) {
const { path } = fileInfo

const parser = getParser()
const j = jscodeshift.withParser(parser)
const j = jscodeshift.withParser('babylon')
const api = {
j,
jscodeshift: j,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ inlineTest('remove \'use strict\' with comments',
// another comment
'use strict'
function foo(str) {
'use strict'
return str === 'use strict'
'use strict'
return str === 'use strict'
}
`,
`
// comment
// another comment
function foo(str) {
return str === 'use strict'
return str === 'use strict'
}
`,
)
45 changes: 0 additions & 45 deletions packages/unminify/src/transformations/function-to-arrow.ts

This file was deleted.

10 changes: 4 additions & 6 deletions packages/unminify/src/transformations/module-mapping.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import wrap from '../wrapAstTransformation'
import type { ASTTransformation } from '../wrapAstTransformation'
import type { ModuleMapping } from '@wakaru/ast-utils'
import type { Literal } from 'jscodeshift'
import type { NumericLiteral, StringLiteral } from 'jscodeshift'

/**
* // params: { 29: 'index.js' }
Expand All @@ -22,15 +22,13 @@ export const transformAST: ASTTransformation<Params> = (context, params = { modu
type: 'Identifier',
name: 'require',
},
arguments: args => args.length === 1 && j.Literal.check(args[0]),
arguments: args => args.length === 1 && (j.StringLiteral.check(args[0]) || j.NumericLiteral.check(args[0])),
})
.forEach((p) => {
const { value } = p.node.arguments[0] as Literal
if (typeof value !== 'number' && typeof value !== 'string') return

const { value } = p.node.arguments[0] as StringLiteral | NumericLiteral
const replacement = moduleMapping[value]
if (replacement) {
p.node.arguments[0] = j.literal(replacement)
p.node.arguments[0] = j.stringLiteral(replacement)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findReferences, isNumber } from '@wakaru/ast-utils'
import { findReferences } from '@wakaru/ast-utils'
import { findHelperLocals, removeHelperImport } from '../../../utils/import'
import { isHelperFunctionCall } from '../../../utils/isHelperFunctionCall'
import wrap from '../../../wrapAstTransformation'
Expand Down Expand Up @@ -49,7 +49,7 @@ export const transformAST: ASTTransformation<SharedParams> = (context, params) =

if (argLength === 2) {
const secondArg = path.node.arguments[1]
return j.Literal.check(secondArg) && isNumber(secondArg.value)
return j.NumericLiteral.check(secondArg)
}

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import wrap from '../../../wrapAstTransformation'
import type { SharedParams } from '../../../utils/types'
import type { ASTTransformation } from '../../../wrapAstTransformation'
import type { Scope } from 'ast-types/lib/scope'
import type { ASTPath, AssignmentExpression, CallExpression, Identifier, Literal, MemberExpression, SequenceExpression, VariableDeclarator } from 'jscodeshift'
import type { ASTPath, AssignmentExpression, CallExpression, Identifier, MemberExpression, NumericLiteral, SequenceExpression, VariableDeclarator } from 'jscodeshift'

/**
* Restores default import from `@babel/runtime/helpers/interopRequireDefault` helper.
Expand Down Expand Up @@ -92,8 +92,8 @@ export const transformAST: ASTTransformation<SharedParams> = (context, params) =
if (
j.SequenceExpression.check(seq?.node)
&& seq.node.expressions.length === 2
&& j.Literal.check(seq.node.expressions[0])
&& (seq.node.expressions[0] as Literal).value === 0
&& j.NumericLiteral.check(seq.node.expressions[0])
&& (seq.node.expressions[0] as NumericLiteral).value === 0
&& j.MemberExpression.check(seq.node.expressions[1])
&& seq.node.expressions[1].object === idReference.node
&& j.Identifier.check(seq.node.expressions[1].property)
Expand All @@ -118,7 +118,7 @@ export const transformAST: ASTTransformation<SharedParams> = (context, params) =
if (
j.MemberExpression.check(idReference.parent?.node)
&& idReference.parent.node.object === idReference.node
&& j.Literal.check(idReference.parent.node.property)
&& j.StringLiteral.check(idReference.parent.node.property)
&& idReference.parent.node.property.value === 'default'
) {
idReference.parent.replace(idReference.node)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { findReferences, isNumber } from '@wakaru/ast-utils'
import { findReferences } from '@wakaru/ast-utils'
import { findHelperLocals, removeHelperImport } from '../../../utils/import'
import { isHelperFunctionCall } from '../../../utils/isHelperFunctionCall'
import wrap from '../../../wrapAstTransformation'
import type { SharedParams } from '../../../utils/types'
import type { ASTTransformation } from '../../../wrapAstTransformation'
import type { ExpressionKind } from 'ast-types/lib/gen/kinds'
import type { Scope } from 'ast-types/lib/scope'
import type { CallExpression, Identifier, Literal, VariableDeclarator } from 'jscodeshift'
import type { CallExpression, Identifier, NumericLiteral, VariableDeclarator } from 'jscodeshift'

/**
* Restores array destructuring from `@babel/runtime/helpers/slicedToArray` helper.
Expand Down Expand Up @@ -48,15 +48,14 @@ export const transformAST: ASTTransformation<SharedParams> = (context, params) =
init: (init) => {
return isHelperFunctionCall(j, init, helperLocal)
&& init.arguments.length === 2
&& j.Literal.check(init.arguments[1])
&& isNumber(init.arguments[1].value)
&& j.NumericLiteral.check(init.arguments[1])
},
})
.forEach((path) => {
const decl = path.node as VariableDeclarator
const tempVariable = decl.id as Identifier
const wrappedExpression = (decl.init as CallExpression).arguments[0] as ExpressionKind
const length = ((decl.init as CallExpression).arguments[1] as Literal).value as number
const length = ((decl.init as CallExpression).arguments[1] as NumericLiteral).value as number

if (length === 0) {
// var [] = wrappedExpression
Expand Down
Loading