Skip to content

Commit bf5a79c

Browse files
bcorboldshadcn
andauthored
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>
1 parent f02b412 commit bf5a79c

4 files changed

Lines changed: 80 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"shadcn": patch
3+
---
4+
5+
fix transofmrRsc for handling use client

packages/shadcn/src/utils/transformers/transform-rsc.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Transformer } from "@/src/utils/transformers"
22
import { SyntaxKind } from "ts-morph"
33

4+
const directiveRegex = /^["']use client["']$/g
5+
46
export const transformRsc: Transformer = async ({ sourceFile, config }) => {
57
if (config.rsc) {
68
return sourceFile
79
}
810

911
// Remove "use client" from the top of the file.
1012
const first = sourceFile.getFirstChildByKind(SyntaxKind.ExpressionStatement)
11-
if (first?.getText() === `"use client"`) {
13+
if (first && directiveRegex.test(first.getText())) {
1214
first.remove()
1315
}
1416

packages/shadcn/test/utils/__snapshots__/transform-rsc.test.ts.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,27 @@ import { Foo } from "bar"
2929
"use client"
3030
"
3131
`;
32+
33+
exports[`transform rsc 5`] = `
34+
"'use client'
35+
36+
import * as React from 'react'
37+
import { Foo } from 'bar'
38+
"
39+
`;
40+
41+
exports[`transform rsc 6`] = `
42+
"import * as React from 'react'
43+
import { Foo } from 'bar'
44+
"
45+
`;
46+
47+
exports[`transform rsc 7`] = `
48+
"'use foo'
49+
50+
import * as React from 'react'
51+
import { Foo } from 'bar'
52+
53+
'use client'
54+
"
55+
`;

packages/shadcn/test/utils/transform-rsc.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,52 @@ import { Foo } from "bar"
6262
},
6363
})
6464
).toMatchSnapshot()
65+
66+
67+
expect(
68+
await transform({
69+
filename: "test.ts",
70+
raw: `'use client'
71+
72+
import * as React from 'react'
73+
import { Foo } from 'bar'
74+
`,
75+
config: {
76+
tsx: true,
77+
rsc: true,
78+
},
79+
})
80+
).toMatchSnapshot()
81+
82+
expect(
83+
await transform({
84+
filename: "test.ts",
85+
raw: `'use client'
86+
87+
import * as React from 'react'
88+
import { Foo } from 'bar'
89+
`,
90+
config: {
91+
tsx: true,
92+
rsc: false,
93+
},
94+
})
95+
).toMatchSnapshot()
96+
97+
expect(
98+
await transform({
99+
filename: "test.ts",
100+
raw: `'use foo'
101+
102+
import * as React from 'react'
103+
import { Foo } from 'bar'
104+
105+
'use client'
106+
`,
107+
config: {
108+
tsx: true,
109+
rsc: false,
110+
},
111+
})
112+
).toMatchSnapshot()
65113
})

0 commit comments

Comments
 (0)