Skip to content

Commit

Permalink
refactor: remove transformationMap, use transformationRules instead
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Jan 1, 2024
1 parent 52cb975 commit 1cc68e5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 66 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ for (const mod of modules) {
### `@wakaru/unminify`

```ts
import { runDefaultTransformation, runTransformations } from '@wakaru/unminify';
import { runDefaultTransformation, runTransformationIds } from '@wakaru/unminify';

const file = {
source: '...', // source code
Expand All @@ -134,7 +134,7 @@ const rules = [
'un-esm',
...
]
const { code } = await runTransformations(file, rules);
const { code } = await runTransformationIds(file, rules);
```

You can check all the rules at [/unminify/src/transformations/index.ts](https://github.com/pionxzh/wakaru/blob/main/packages/unminify/src/transformations/index.ts).
Expand Down
4 changes: 2 additions & 2 deletions benches/un-undefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { add, complete, cycle, save, suite } from 'benny'
import { runTransformations, transformationMap } from '../packages/unminify/src/index'
import { runTransformations, transformationRules } from '../packages/unminify/src/index'

const __dirname = dirname(fileURLToPath(import.meta.url))

const title = 'un-undefined'
const rule = transformationMap[title]
const rule = transformationRules.find(rule => rule.name === title)!.transform
const snippet = `
void 0;
void 99;
Expand Down
19 changes: 10 additions & 9 deletions packages/unminify/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import jscodeshift from 'jscodeshift'

import { transformationMap, transformationRules } from './transformations'
import { transformationRules } from './transformations'
import { arraify } from './utils/arraify'
import type { MaybeArray } from './utils/arraify'
import type { FileInfo, Transform } from 'jscodeshift'

export function runDefaultTransformation<P extends Record<string, any>>(fileInfo: FileInfo, params: P = {} as any) {
const transforms = Object.values(transformationMap)
export * from './transformations'

export function runDefaultTransformation<P extends Record<string, any>>(
fileInfo: FileInfo,
params: P = {} as any,
) {
const transforms = transformationRules.map(rule => rule.transform)
return runTransformations(fileInfo, transforms, params)
}

export function runTransformationIds<P extends Record<string, any>>(
fileInfo: FileInfo,
ids: string[],
params: P = {} as any) {
params: P = {} as any,
) {
const transforms = ids.map(id => transformationRules.find(rule => rule.id === id)?.transform).filter(Boolean) as Transform[]
return runTransformations(fileInfo, transforms, params)
}
Expand Down Expand Up @@ -78,8 +84,3 @@ export function runTransformations<P extends Record<string, any>>(
code,
}
}

export {
transformationMap,
transformationRules,
} from './transformations'
53 changes: 0 additions & 53 deletions packages/unminify/src/transformations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,59 +37,6 @@ import unWhileLoop from './un-while-loop'
import type { Transform } from 'jscodeshift'
import type { ZodSchema } from 'zod'

export const transformationMap: {
[name: string]: Transform
} = {
// first stage - basically prettify the code
prettier,
'module-mapping': moduleMapping,
'un-curly-braces': unCurlyBraces, // add curly braces so that other transformations can works easier, but generally this is not required
'un-sequence-expression1': unSequenceExpression, // curly braces can bring out return sequence expression, so it runs before this
'un-variable-merging': unVariableMerging,
'un-assignment-merging': unAssignmentMerging,

// second stage - prepare the code for unminify
'un-runtime-helper': unRuntimeHelper, // eliminate helpers as early as possible
'un-esm': unEsm, // relies on `un-runtime-helper` to eliminate helpers around `require` calls, relies on `un-assignment-merging` to split exports
'un-enum': unEnum, // relies on `un-sequence-expression`

// third stage - mostly one-to-one transformation
lebab,
'un-export-rename': unExportRename, // relies on `un-esm` to give us the export statements, and this can break some rules from `lebab`
'un-use-strict': unUseStrict,
'un-esmodule-flag': unEsModuleFlag,
'un-boolean': unBoolean,
'un-undefined': unUndefined,
'un-infinity': unInfinity,
'un-typeof': unTypeof,
'un-numeric-literal': unNumericLiteral,
'un-template-literal': unTemplateLiteral,
'un-bracket-notation': unBracketNotation,
'un-return': unReturn,
'un-while-loop': unWhileLoop,
'un-indirect-call': unIndirectCall,
'un-type-constructor': unTypeConstructor,
'un-builtin-prototype': unBuiltinPrototype,
'un-sequence-expression2': unSequenceExpression,
'un-flip-comparisons': unFlipComparisons,

// advanced syntax upgrade
'smart-inline': smartInline, // relies on `lebab`'s `let` to `const` and `un-sequence-expression`
'smart-rename': smartRename, // partially relies on `un-indirect-call` to work
'un-optional-chaining': unOptionalChaining,
'un-nullish-coalescing': unNullishCoalescing,
'un-conditionals': unConditionals, // need to run after `un-optional-chaining` and `un-nullish-coalescing`
'un-sequence-expression3': unSequenceExpression, // split sequence expressions introduced by `un-conditionals`
'un-parameters': unParameters, // relies on `un-flip-comparisons` to work
'un-jsx': unJsx,
'un-iife': unIife,
'un-es6-class': unES6Class,
'un-async-await': unAsyncAwait,

// last stage - prettify the code again after we finish all the transformations
'prettier-last': prettier,
}

export interface TransformationRule {
id: string
/**
Expand Down

0 comments on commit 1cc68e5

Please sign in to comment.