Skip to content

Commit

Permalink
support replacement as a function (#76)
Browse files Browse the repository at this point in the history
* support replacement as a function

* fix typo

* Create rotten-seahorses-occur.md

---------

Co-authored-by: Oleksandr Fediashov <[email protected]>
  • Loading branch information
siriwatknp and layershifter authored Apr 25, 2024
1 parent 99a3634 commit 9096ba1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-seahorses-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wyw-in-js/processor-utils": patch
---

feat: support `replacement` as a function
4 changes: 2 additions & 2 deletions packages/processor-utils/src/BaseProcessor.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions packages/transform/src/utils/getTagProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__');
}
Expand Down

0 comments on commit 9096ba1

Please sign in to comment.