fix: prevent transforming importDeclaration

This commit is contained in:
zernonia 2024-06-18 16:49:17 +08:00
parent 0f551c85e6
commit 386c77c9c6
3 changed files with 24 additions and 2 deletions

View File

@ -31,6 +31,11 @@ export const transformTwPrefixes: Transformer = async ({
return sourceFile
const value = node.getText()
// Do not parse imported packages/files
if (node.getParent().getKind() === SyntaxKind.ImportDeclaration)
return
const hasClosingDoubleQuote = value.match(/"/g)?.length === 2
const hasFunction = value.startsWith('"cn(')
if (value.search('\'') === -1 && hasClosingDoubleQuote && !hasFunction) {

View File

@ -1,7 +1,8 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`transform tailwind prefix 1`] = `
"const testVariants = cva(
"import { cva } from "class-variance-authority";
export const testVariants = cva(
"tw-bg-background hover:tw-bg-muted tw-text-primary-foreground sm:focus:tw-text-accent-foreground",
{
variants: {
@ -54,6 +55,13 @@ exports[`transform tailwind prefix 4`] = `
)
"
:class="cn(buttonVariants({ variant, size }), props.class)"
:class="
cn(
buttonVariants({ 'outline' }),
props.class,
'tw-bg-background'
)
"
>
foo
</div>

View File

@ -6,7 +6,9 @@ it('transform tailwind prefix', async () => {
expect(
await transform({
filename: 'test.ts',
raw: `const testVariants = cva(
raw: `import { cva } from "class-variance-authority"
export const testVariants = cva(
'bg-background hover:bg-muted text-primary-foreground sm:focus:text-accent-foreground',
{
variants: {
@ -88,6 +90,13 @@ it('transform tailwind prefix', async () => {
id="testing" v-bind="props" @click="handleSomething" :data-test="true"
:class="cn('bg-background hover:bg-muted', true && 'text-primary-foreground sm:focus:text-accent-foreground')"
:class="cn(buttonVariants({ variant, size }), props.class)"
:class="
cn(
buttonVariants({ 'outline' }),
props.class,
'bg-background'
)
"
>
foo
</div>