diff --git a/.changeset/rotten-seahorses-occur.md b/.changeset/rotten-seahorses-occur.md new file mode 100644 index 00000000..269273fc --- /dev/null +++ b/.changeset/rotten-seahorses-occur.md @@ -0,0 +1,5 @@ +--- +"@wyw-in-js/processor-utils": patch +--- + +feat: support `replacement` as a function diff --git a/packages/processor-utils/src/BaseProcessor.ts b/packages/processor-utils/src/BaseProcessor.ts index af537cc4..62c44c08 100644 --- a/packages/processor-utils/src/BaseProcessor.ts +++ b/packages/processor-utils/src/BaseProcessor.ts @@ -1,5 +1,5 @@ /* eslint-disable class-methods-use-this */ -import type { types as t } from '@babel/core'; +import type { NodePath, types as t } from '@babel/core'; import generator from '@babel/generator'; import type { Expression, @@ -61,7 +61,7 @@ export abstract class BaseProcessor { }, public readonly location: SourceLocation | null, protected readonly replacer: ( - replacement: Expression, + replacement: Expression | ((tagPath: NodePath) => Expression), isPure: boolean ) => void, public readonly displayName: string, diff --git a/packages/transform/src/utils/getTagProcessor.ts b/packages/transform/src/utils/getTagProcessor.ts index 34954d51..198c365e 100644 --- a/packages/transform/src/utils/getTagProcessor.ts +++ b/packages/transform/src/utils/getTagProcessor.ts @@ -245,9 +245,14 @@ function getBuilderForIdentifier( break; } - const replacer = (replacement: Expression, isPure: boolean) => { + const replacer = ( + replacement: Expression | ((tagPath: NodePath) => Expression), + isPure: boolean + ) => { mutate(prev, (p) => { - p.replaceWith(replacement); + p.replaceWith( + typeof replacement === 'function' ? replacement(p) : replacement + ); if (isPure) { p.addComment('leading', '#__PURE__'); }