Skip to content

Commit e995cf2

Browse files
committed
Use prettier to format CSS
1 parent 3d760f1 commit e995cf2

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

package-lock.json

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tailwindcss-language-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"postcss": "8.4.31",
6767
"postcss-load-config": "3.0.1",
6868
"postcss-selector-parser": "6.0.2",
69-
"prettier": "2.3.0",
69+
"prettier": "^2.8.8",
7070
"resolve": "1.20.0",
7171
"rimraf": "3.0.2",
7272
"stack-trace": "0.0.10",

packages/tailwindcss-language-server/src/util/v4/design-system.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { DesignSystem } from 'tailwindcss-language-service/src/util/v4'
22

33
import postcss from 'postcss'
44
import postcssImport from 'postcss-import'
5+
import { format } from 'prettier'
56

67
const resolveImports = postcss([postcssImport()])
78

@@ -23,7 +24,7 @@ export async function isMaybeV4(css: string): Promise<boolean> {
2324
export async function loadDesignSystem(
2425
tailwindcss: any,
2526
filepath: string,
26-
css: string
27+
css: string,
2728
): Promise<DesignSystem | null> {
2829
// This isn't a v4 project
2930
if (!tailwindcss.__unstable__loadDesignSystem) return null
@@ -48,11 +49,31 @@ export async function loadDesignSystem(
4849
},
4950

5051
compile(classes: string[]): postcss.Root {
51-
return postcss.parse(design.candidatesToCss(classes))
52+
let css = design.candidatesToCss(classes).join('\n')
53+
54+
// Downlevel syntax
55+
// TODO: Either don't downlevel nesting or make `recordClassDetails` more robust
56+
// try {
57+
// css = tailwindcss.optimizeCss(css)
58+
// } catch {}
59+
60+
// Reformat with Prettier
61+
try {
62+
css = format(css, {
63+
parser: 'css',
64+
singleQuote: true,
65+
trailingComma: 'all',
66+
filepath: 'input.css',
67+
}).trim()
68+
} catch {}
69+
70+
return postcss.parse(css)
5271
},
5372

5473
toCss(nodes: postcss.Root | postcss.Node[]): string {
55-
return Array.isArray(nodes) ? postcss.root({ nodes }).toString() : nodes.toString()
74+
return Array.isArray(nodes)
75+
? postcss.root({ nodes }).toString().trim()
76+
: nodes.toString().trim()
5677
},
5778
})
5879

packages/tailwindcss-language-server/tests/completions/completions.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,10 @@ withFixture('v4/basic', (c) => {
439439

440440
expect(resolved).toEqual({
441441
...item,
442-
detail: 'text-transform:uppercase',
442+
detail: 'text-transform: uppercase',
443443
documentation: {
444444
kind: 'markdown',
445-
value: '```css\n.uppercase{text-transform:uppercase;}\n```',
445+
value: '```css\n.uppercase {\n text-transform: uppercase;\n}\n```',
446446
},
447447
})
448448
})

packages/tailwindcss-language-server/tests/hover/hover.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ withFixture('v4/basic', (c) => {
111111
testHover('hover', {
112112
text: '<div class="bg-red-500">',
113113
position: { line: 0, character: 13 },
114-
expected: '.bg-red-500{background-color:#ef4444;}',
114+
expected: '.bg-red-500 {\n background-color: #ef4444;\n}',
115115
expectedRange: {
116116
start: { line: 0, character: 12 },
117117
end: { line: 0, character: 22 },
@@ -121,7 +121,7 @@ withFixture('v4/basic', (c) => {
121121
testHover('arbitrary value', {
122122
text: '<div class="p-[3px]">',
123123
position: { line: 0, character: 13 },
124-
expected: '.p-\\[3px\\]{padding:3px;}',
124+
expected: '.p-\\[3px\\] {\n padding: 3px;\n}',
125125
expectedRange: {
126126
start: { line: 0, character: 12 },
127127
end: { line: 0, character: 19 },
@@ -142,7 +142,7 @@ withFixture('v4/basic', (c) => {
142142
testHover('arbitrary property', {
143143
text: '<div class="[text-wrap:balance]">',
144144
position: { line: 0, character: 13 },
145-
expected: '.\\[text-wrap\\:balance\\]{text-wrap:balance;}',
145+
expected: '.\\[text-wrap\\:balance\\] {\n text-wrap: balance;\n}',
146146
expectedRange: {
147147
start: { line: 0, character: 12 },
148148
end: { line: 0, character: 31 },

0 commit comments

Comments
 (0)