From 344e5c0ee652a32c5775c20d9e5c99858d412906 Mon Sep 17 00:00:00 2001 From: Pionxzh Date: Thu, 14 Dec 2023 02:47:16 +0800 Subject: [PATCH] perf(unminify) : improve performance of `prettier` and `lebab` --- packages/ast-utils/package.json | 3 ++- .../ast-utils/src/wrapStringTransformation.ts | 18 ++++++++++++++++++ .../__tests__/un-parameters.spec.ts | 13 +++++++++++++ packages/unminify/src/transformations/lebab.ts | 9 ++++----- .../unminify/src/transformations/prettier.ts | 10 +++------- .../src/transformations/un-parameters.ts | 4 ++-- 6 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 packages/ast-utils/src/wrapStringTransformation.ts diff --git a/packages/ast-utils/package.json b/packages/ast-utils/package.json index 2cf82aeb..afe16885 100644 --- a/packages/ast-utils/package.json +++ b/packages/ast-utils/package.json @@ -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", diff --git a/packages/ast-utils/src/wrapStringTransformation.ts b/packages/ast-utils/src/wrapStringTransformation.ts new file mode 100644 index 00000000..8f0c68d9 --- /dev/null +++ b/packages/ast-utils/src/wrapStringTransformation.ts @@ -0,0 +1,18 @@ +import type { Options, Transform } from 'jscodeshift' + +export interface StringTransformation { + (code: string, params: Params): string | void +} + +export function wrapStringTransformation( + transformAST: StringTransformation, +): 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 +} diff --git a/packages/unminify/src/transformations/__tests__/un-parameters.spec.ts b/packages/unminify/src/transformations/__tests__/un-parameters.spec.ts index 3254de29..3c7eb3d2 100644 --- a/packages/unminify/src/transformations/__tests__/un-parameters.spec.ts +++ b/packages/unminify/src/transformations/__tests__/un-parameters.spec.ts @@ -208,3 +208,16 @@ function test(a = 1, b) { } `, ) + +inlineTest('lebab', + ` +function test() { + console.log(arguments); +} +`, + ` +function test(...args) { + console.log(args); +} +`, +) diff --git a/packages/unminify/src/transformations/lebab.ts b/packages/unminify/src/transformations/lebab.ts index 90c14939..900a6c4f 100644 --- a/packages/unminify/src/transformations/lebab.ts +++ b/packages/unminify/src/transformations/lebab.ts @@ -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' /** @@ -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)) diff --git a/packages/unminify/src/transformations/prettier.ts b/packages/unminify/src/transformations/prettier.ts index 054689db..bab04261 100644 --- a/packages/unminify/src/transformations/prettier.ts +++ b/packages/unminify/src/transformations/prettier.ts @@ -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) +}) diff --git a/packages/unminify/src/transformations/un-parameters.ts b/packages/unminify/src/transformations/un-parameters.ts index e9f86156..2ce6c842 100644 --- a/packages/unminify/src/transformations/un-parameters.ts +++ b/packages/unminify/src/transformations/un-parameters.ts @@ -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' }), {}) } /**