Skip to content

Commit

Permalink
feat: support async transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Jan 13, 2024
1 parent eca0c6e commit 9e37839
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 24 deletions.
4 changes: 2 additions & 2 deletions benches/un-undefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const main = async () => {
title,
...([10, 100, 1000, 5000].map((count) => {
const source = snippet.repeat(count)
return add(`items=${count}`, () => {
runTransformationRules({ path: '', source }, [title])
return add(`items=${count}`, async () => {
await runTransformationRules({ path: '', source }, [title])
})
})),
cycle(),
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/unminify.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function unminify(data?: UnminifyWorkerParams) {
const source = await fsa.readFile(inputPath, 'utf-8')
const fileInfo = { path: inputPath, source }

const { code, timing } = runDefaultTransformationRules(fileInfo, { moduleMeta, moduleMapping })
const { code, timing } = await runDefaultTransformationRules(fileInfo, { moduleMeta, moduleMapping })
await fsa.ensureFile(outputPath)
await fsa.writeFile(outputPath, code, 'utf-8')

Expand Down
4 changes: 2 additions & 2 deletions packages/playground/src/unminify.worker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { runTransformationRules } from '@wakaru/unminify'
import type { CodeModParams, TransformedModule } from './types'

onmessage = (
onmessage = async (
msg: MessageEvent<CodeModParams>,
) => {
try {
const { name, module, transformationRuleIds, moduleMeta, moduleMapping } = msg.data
const fileInfo = { path: name, source: module.code }
const params = { moduleMeta, moduleMapping }
const { code } = runTransformationRules(fileInfo, transformationRuleIds, params)
const { code } = await runTransformationRules(fileInfo, transformationRuleIds, params)
const transformedDep: TransformedModule = { ...module, transformed: code }
postMessage(transformedDep)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/shared/src/jscodeshiftRule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jscodeshiftWithParser as j, printSourceWithErrorLoc, toSource } from './jscodeshift'
import type { BaseTransformationRule, SharedParams } from './rule'
import type { Collection, JSCodeshift, Transform } from 'jscodeshift'
import type { BaseTransformationRule, JSCodeshiftTransform, SharedParams } from './rule'
import type { Collection, JSCodeshift } from 'jscodeshift'
import type { ZodSchema, z } from 'zod'

export interface Context {
Expand Down Expand Up @@ -61,8 +61,8 @@ export class JSCodeshiftTransformationRule<Schema extends ZodSchema = ZodSchema>
/**
* Generate a jscodeshift compatible transform
*/
toJSCodeshiftTransform(): Transform {
const transform: Transform = (file, api, options) => {
toJSCodeshiftTransform(): JSCodeshiftTransform {
const transform: JSCodeshiftTransform = (file, api, options) => {
const root = api.jscodeshift(file.source)

this.execute({
Expand Down
25 changes: 20 additions & 5 deletions packages/shared/src/rule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { JSCodeshiftTransformationRule } from './jscodeshiftRule'
import type { StringTransformationRule } from './stringRule'
import type { ModuleMapping, ModuleMeta } from './types'
import type { Transform } from 'jscodeshift'
import type { API, FileInfo, Options } from 'jscodeshift'
import type { ZodSchema } from 'zod'

export * from './jscodeshiftRule'
Expand All @@ -12,6 +12,21 @@ export interface SharedParams {
moduleMeta?: ModuleMeta
}

/**
* `Transform` from `jscodeshift`. The type in `jscodeshift` is not accurate,
* so we have to re-define it here.
*
* Async support has been added in https://github.com/facebook/jscodeshift/pull/237
*/
export interface JSCodeshiftTransform {
/**
* If a string is returned and it is different from passed source, the transform is considered to be successful.
* If a string is returned but it's the same as the source, the transform is considered to be unsuccessful.
* If nothing is returned, the file is not supposed to be transformed (which is ok).
*/
(file: FileInfo, api: API, options: Options): Promise<string | null | undefined | void> | string | null | undefined | void
}

export interface BaseTransformationRule {
type: 'jscodeshift' | 'string' | 'rule-set'
/**
Expand All @@ -34,7 +49,7 @@ export interface BaseTransformationRule {
/**
* convert to jscodeshift compatible transform
*/
toJSCodeshiftTransform(): Transform
toJSCodeshiftTransform(): JSCodeshiftTransform
}

export type TransformationRule<Schema extends ZodSchema = ZodSchema> =
Expand Down Expand Up @@ -71,13 +86,13 @@ export class MergedTransformationRule implements BaseTransformationRule {
this.rules = rules
}

toJSCodeshiftTransform(): Transform {
toJSCodeshiftTransform(): JSCodeshiftTransform {
const rules = this.rules
return function mergedTransform(file, api, options) {
return async function mergedTransform(file, api, options) {
let source = file.source
for (const rule of rules) {
const transform = rule.toJSCodeshiftTransform()
const newResult = transform({ ...file, source }, api, options)
const newResult = await transform({ ...file, source }, api, options)
if (newResult) source = newResult
}
return source
Expand Down
18 changes: 10 additions & 8 deletions packages/shared/src/stringRule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { BaseTransformationRule } from './rule'
import type { Transform } from 'jscodeshift'
import type { BaseTransformationRule, JSCodeshiftTransform } from './rule'
import type { ZodSchema, z } from 'zod'

export type StringTransformation<Schema extends ZodSchema = ZodSchema> = (code: string, params: z.infer<Schema>) => string | void
export type StringTransformation<Schema extends ZodSchema = ZodSchema> = (
code: string,
params: z.infer<Schema>
) => Promise<string | void> | string | void

export class StringTransformationRule<Schema extends ZodSchema = ZodSchema> implements BaseTransformationRule {
type = 'string' as const
Expand Down Expand Up @@ -33,27 +35,27 @@ export class StringTransformationRule<Schema extends ZodSchema = ZodSchema> impl
this.schema = schema
}

execute({
async execute({
source, filename, params,
}: {
source: string
filename: string
params: z.infer<Schema>
}) {
try {
return this.transform(source, params)
return await this.transform(source, params)
}
catch (err: any) {
console.error(`\nError running rule ${this.name} on ${filename}`, err)
}
}

toJSCodeshiftTransform(): Transform {
const transform: Transform = (file, _api, options) => {
toJSCodeshiftTransform(): JSCodeshiftTransform {
const transform: JSCodeshiftTransform = async (file, _api, options) => {
const { source } = file
const params = options as z.infer<Schema>
try {
const newSource = this.transform(source, params)
const newSource = await this.transform(source, params)
return newSource ?? source
}
catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions packages/unminify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function runTransformationRules<P extends Record<string, any>>(
return executeTransformationRules(fileInfo.source, fileInfo.path, rules, params)
}

function executeTransformationRules<P extends Record<string, any>>(
async function executeTransformationRules<P extends Record<string, any>>(
/** The source code */
source: string,
/** The file path */
Expand Down Expand Up @@ -84,7 +84,7 @@ function executeTransformationRules<P extends Record<string, any>>(

try {
const stopMeasure2 = timing.startMeasure(filePath, rule.id)
currentSource = rule.execute({
currentSource = await rule.execute({
source: currentSource,
filename: filePath,
params,
Expand Down

0 comments on commit 9e37839

Please sign in to comment.