Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
Loading