Skip to content

Commit ce3adfa

Browse files
authored
fix(shadcn): Support single quote formatted registry files (#4870) (#4871)
1 parent 674807c commit ce3adfa

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

.changeset/wise-cheetahs-report.md

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+
support tw prefixes for registry files with single quote formatting

packages/shadcn/src/utils/transformers/transform-tw-prefix.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const transformTwPrefixes: Transformer = async ({
2222
if (defaultClassNames) {
2323
defaultClassNames.replaceWithText(
2424
`"${applyPrefix(
25-
defaultClassNames.getText()?.replace(/"/g, ""),
25+
defaultClassNames.getText()?.replace(/"|'/g, ""),
2626
config.tailwind.prefix
2727
)}"`
2828
)
@@ -46,7 +46,7 @@ export const transformTwPrefixes: Transformer = async ({
4646
if (classNames) {
4747
classNames?.replaceWithText(
4848
`"${applyPrefix(
49-
classNames.getText()?.replace(/"/g, ""),
49+
classNames.getText()?.replace(/"|'/g, ""),
5050
config.tailwind.prefix
5151
)}"`
5252
)
@@ -65,7 +65,7 @@ export const transformTwPrefixes: Transformer = async ({
6565
if (value) {
6666
value.replaceWithText(
6767
`"${applyPrefix(
68-
value.getText()?.replace(/"/g, ""),
68+
value.getText()?.replace(/"|'/g, ""),
6969
config.tailwind.prefix
7070
)}"`
7171
)
@@ -91,7 +91,7 @@ export const transformTwPrefixes: Transformer = async ({
9191
.forEach((node) => {
9292
node.replaceWithText(
9393
`"${applyPrefix(
94-
node.getText()?.replace(/"/g, ""),
94+
node.getText()?.replace(/"|'/g, ""),
9595
config.tailwind.prefix
9696
)}"`
9797
)
@@ -101,7 +101,7 @@ export const transformTwPrefixes: Transformer = async ({
101101
if (node.isKind(SyntaxKind.StringLiteral)) {
102102
node.replaceWithText(
103103
`"${applyPrefix(
104-
node.getText()?.replace(/"/g, ""),
104+
node.getText()?.replace(/"|'/g, ""),
105105
config.tailwind.prefix
106106
)}"`
107107
)
@@ -130,7 +130,7 @@ export const transformTwPrefixes: Transformer = async ({
130130
.forEach((node) => {
131131
node.replaceWithText(
132132
`"${applyPrefix(
133-
node.getText()?.replace(/"/g, ""),
133+
node.getText()?.replace(/"|'/g, ""),
134134
config.tailwind.prefix
135135
)}"`
136136
)
@@ -140,7 +140,7 @@ export const transformTwPrefixes: Transformer = async ({
140140
if (arg.isKind(SyntaxKind.StringLiteral)) {
141141
arg.replaceWithText(
142142
`"${applyPrefix(
143-
arg.getText()?.replace(/"/g, ""),
143+
arg.getText()?.replace(/"|'/g, ""),
144144
config.tailwind.prefix
145145
)}"`
146146
)
@@ -155,7 +155,7 @@ export const transformTwPrefixes: Transformer = async ({
155155
if (classNames) {
156156
classNames.replaceWithText(
157157
`"${applyPrefix(
158-
classNames.getText()?.replace(/"/g, ""),
158+
classNames.getText()?.replace(/"|'/g, ""),
159159
config.tailwind.prefix
160160
)}"`
161161
)

packages/shadcn/test/utils/__snapshots__/transform-tw-prefix.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ export function Foo() {
2525
`;
2626

2727
exports[`transform tailwind prefix 4`] = `
28+
"import * as React from "react"
29+
export function Foo() {
30+
return <div className={cn("tw-bg-background hover:tw-bg-muted", true && "tw-text-primary-foreground sm:focus:tw-text-accent-foreground")}>foo</div>
31+
}
32+
"
33+
`;
34+
35+
exports[`transform tailwind prefix 5`] = `
2836
"@tailwind base;
2937
@tailwind components;
3038
@tailwind utilities;

packages/shadcn/test/utils/transform-tw-prefix.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ export function Foo() {
7373
})
7474
).toMatchSnapshot()
7575

76+
expect(
77+
await transform({
78+
filename: "test.ts",
79+
raw: `import * as React from "react"
80+
export function Foo() {
81+
return <div className={cn('bg-background hover:bg-muted', true && 'text-primary-foreground sm:focus:text-accent-foreground')}>foo</div>
82+
}
83+
`,
84+
config: {
85+
tailwind: {
86+
baseColor: "stone",
87+
cssVariables: false,
88+
prefix: "tw-",
89+
},
90+
aliases: {
91+
components: "@/components",
92+
utils: "@/lib/utils",
93+
},
94+
},
95+
baseColor: stone,
96+
})
97+
).toMatchSnapshot()
98+
7699
expect(
77100
applyPrefixesCss(
78101
"@tailwind base;\n@tailwind components;\n@tailwind utilities;\n \n@layer base {\n :root {\n --background: 0 0% 100%;\n --foreground: 224 71.4% 4.1%;\n \n --muted: 220 14.3% 95.9%;\n --muted-foreground: 220 8.9% 46.1%;\n \n --popover: 0 0% 100%;\n --popover-foreground: 224 71.4% 4.1%;\n \n --card: 0 0% 100%;\n --card-foreground: 224 71.4% 4.1%;\n \n --border: 220 13% 91%;\n --input: 220 13% 91%;\n \n --primary: 220.9 39.3% 11%;\n --primary-foreground: 210 20% 98%;\n \n --secondary: 220 14.3% 95.9%;\n --secondary-foreground: 220.9 39.3% 11%;\n \n --accent: 220 14.3% 95.9%;\n --accent-foreground: 220.9 39.3% 11%;\n \n --destructive: 0 84.2% 60.2%;\n --destructive-foreground: 210 20% 98%;\n \n --ring: 217.9 10.6% 64.9%;\n \n --radius: 0.5rem;\n }\n \n .dark {\n --background: 224 71.4% 4.1%;\n --foreground: 210 20% 98%;\n \n --muted: 215 27.9% 16.9%;\n --muted-foreground: 217.9 10.6% 64.9%;\n \n --popover: 224 71.4% 4.1%;\n --popover-foreground: 210 20% 98%;\n \n --card: 224 71.4% 4.1%;\n --card-foreground: 210 20% 98%;\n \n --border: 215 27.9% 16.9%;\n --input: 215 27.9% 16.9%;\n \n --primary: 210 20% 98%;\n --primary-foreground: 220.9 39.3% 11%;\n \n --secondary: 215 27.9% 16.9%;\n --secondary-foreground: 210 20% 98%;\n \n --accent: 215 27.9% 16.9%;\n --accent-foreground: 210 20% 98%;\n \n --destructive: 0 62.8% 30.6%;\n --destructive-foreground: 0 85.7% 97.3%;\n \n --ring: 215 27.9% 16.9%;\n }\n}\n \n@layer base {\n * {\n @apply border-border;\n }\n body {\n @apply bg-background text-foreground;\n }\n}",

0 commit comments

Comments
 (0)