Skip to content

Commit

Permalink
fix(shadcn): fix transformRsc to account for ' (#5518)
Browse files Browse the repository at this point in the history
* fix(shadcn): fix transformRsc to account for '

* chore: add changeset

---------

Co-authored-by: shadcn <m@shadcn.com>
  • Loading branch information
bcorbold and shadcn authored Oct 23, 2024
1 parent f02b412 commit bf5a79c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/friendly-mangos-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": patch
---

fix transofmrRsc for handling use client
4 changes: 3 additions & 1 deletion packages/shadcn/src/utils/transformers/transform-rsc.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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
}

// 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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
"
`;
48 changes: 48 additions & 0 deletions packages/shadcn/test/utils/transform-rsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit bf5a79c

Please sign in to comment.