Skip to content

Commit

Permalink
perf(unminify) : improve performance of prettier and lebab
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Dec 14, 2023
1 parent 771b639 commit 344e5c0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/ast-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"./reference": "./src/reference.ts",
"./scope": "./src/scope.ts",
"./types": "./src/types.ts",
"./wrapAstTransformation": "./src/wrapAstTransformation.ts"
"./wrapAstTransformation": "./src/wrapAstTransformation.ts",
"./wrapStringTransformation": "./src/wrapStringTransformation.ts"
},
"files": [
"dist",
Expand Down
18 changes: 18 additions & 0 deletions packages/ast-utils/src/wrapStringTransformation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Options, Transform } from 'jscodeshift'

export interface StringTransformation<Params = object> {
(code: string, params: Params): string | void
}

export function wrapStringTransformation<Params extends Options>(
transformAST: StringTransformation<Params>,
): Transform {
// @ts-expect-error - jscodeshift is not happy
const transform: Transform = (file, api, options: Params) => {
const code = file.source
const result = transformAST(code, options)
return result ?? code
}

return transform
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,16 @@ function test(a = 1, b) {
}
`,
)

inlineTest('lebab',
`
function test() {
console.log(arguments);
}
`,
`
function test(...args) {
console.log(args);
}
`,
)
9 changes: 4 additions & 5 deletions packages/unminify/src/transformations/lebab.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { wrapAstTransformation } from '@wakaru/ast-utils/wrapAstTransformation'
import { wrapStringTransformation } from '@wakaru/ast-utils/wrapStringTransformation'
import { transform } from 'lebab'
import type { ASTTransformation } from '@wakaru/ast-utils/wrapAstTransformation'
import type { StringTransformation } from '@wakaru/ast-utils/wrapStringTransformation'
import type { LebabRule } from 'lebab'

/**
Expand Down Expand Up @@ -32,9 +32,8 @@ function transformLebab(input: string, rules: LebabRule[]) {
return { code, warnings }
}

export const transformASTWithRules = (rules: LebabRule[]): ASTTransformation => (context) => {
const code = context.root.toSource()
export const transformASTWithRules = (rules: LebabRule[]): StringTransformation => (code) => {
return transformLebab(code, rules).code
}

export default wrapAstTransformation(transformASTWithRules(allLebabRules))
export default wrapStringTransformation(transformASTWithRules(allLebabRules))
10 changes: 3 additions & 7 deletions packages/unminify/src/transformations/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { wrapAstTransformation } from '@wakaru/ast-utils/wrapAstTransformation'
import { wrapStringTransformation } from '@wakaru/ast-utils/wrapStringTransformation'
import babelParser from 'prettier/parser-babel'
import prettier from 'prettier/standalone'
import type { ASTTransformation } from '@wakaru/ast-utils/wrapAstTransformation'

/**
* @url https://prettier.io
*/
export const transformAST: ASTTransformation = (context) => {
const code = context.root.toSource()
export default wrapStringTransformation((code) => {
return prettier.format(code, {
parser: 'babel',
plugins: [babelParser],
})
}

export default wrapAstTransformation(transformAST)
})
4 changes: 2 additions & 2 deletions packages/unminify/src/transformations/un-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ export const transformAST: ASTTransformation = (context) => {
handleBody(j, path)
})

transformASTWithRules([
return transformASTWithRules([
// 'default-param',
// 'destruct-param',
'arg-spread',
'arg-rest',
])(context, {})
])(root.toSource({ lineTerminator: '\n' }), {})
}

/**
Expand Down

0 comments on commit 344e5c0

Please sign in to comment.