fix(shadcn): fix transformRsc to account for ' (#5518)

* fix(shadcn): fix transformRsc to account for '

* chore: add changeset

---------

Co-authored-by: shadcn <m@shadcn.com>
This commit is contained in:
Braden Corbold
2024-10-23 00:33:11 -07:00
committed by GitHub
parent f02b412478
commit bf5a79c4d4
4 changed files with 80 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
---
"shadcn": patch
---
fix transofmrRsc for handling use client

View File

@@ -1,6 +1,8 @@
import { Transformer } from "@/src/utils/transformers"
import { SyntaxKind } from "ts-morph"
const directiveRegex = /^["']use client["']$/g
export const transformRsc: Transformer = async ({ sourceFile, config }) => {
if (config.rsc) {
return sourceFile
@@ -8,7 +10,7 @@ export const transformRsc: Transformer = async ({ sourceFile, config }) => {
// Remove "use client" from the top of the file.
const first = sourceFile.getFirstChildByKind(SyntaxKind.ExpressionStatement)
if (first?.getText() === `"use client"`) {
if (first && directiveRegex.test(first.getText())) {
first.remove()
}

View File

@@ -29,3 +29,27 @@ import { Foo } from "bar"
"use client"
"
`;
exports[`transform rsc 5`] = `
"'use client'
import * as React from 'react'
import { Foo } from 'bar'
"
`;
exports[`transform rsc 6`] = `
"import * as React from 'react'
import { Foo } from 'bar'
"
`;
exports[`transform rsc 7`] = `
"'use foo'
import * as React from 'react'
import { Foo } from 'bar'
'use client'
"
`;

View File

@@ -62,4 +62,52 @@ import { Foo } from "bar"
},
})
).toMatchSnapshot()
expect(
await transform({
filename: "test.ts",
raw: `'use client'
import * as React from 'react'
import { Foo } from 'bar'
`,
config: {
tsx: true,
rsc: true,
},
})
).toMatchSnapshot()
expect(
await transform({
filename: "test.ts",
raw: `'use client'
import * as React from 'react'
import { Foo } from 'bar'
`,
config: {
tsx: true,
rsc: false,
},
})
).toMatchSnapshot()
expect(
await transform({
filename: "test.ts",
raw: `'use foo'
import * as React from 'react'
import { Foo } from 'bar'
'use client'
`,
config: {
tsx: true,
rsc: false,
},
})
).toMatchSnapshot()
})