Skip to content

Commit 9c33d96

Browse files
authored
Merge pull request #47 from ony3000/support-css
Support css, scss, and less
2 parents f7c9e75 + b217327 commit 9c33d96

22 files changed

Lines changed: 436 additions & 6 deletions

global.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ declare global {
3131
| 'angular'
3232
| 'html'
3333
| 'vue'
34+
| 'css'
35+
| 'scss'
36+
| 'less'
3437
| 'oxc'
3538
| 'oxc-ts'
3639
| 'astro'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"esbuild": "0.25.0",
4444
"prettier": "3.8.1",
4545
"prettier-plugin-astro": "0.11.0",
46-
"prettier-plugin-brace-style": "0.9.0",
46+
"prettier-plugin-brace-style": "0.10.0",
4747
"prettier-plugin-classnames": "0.9.0",
4848
"prettier-plugin-space-before-function-paren": "0.0.7",
4949
"prettier-plugin-svelte": "3.0.0",

pnpm-lock.yaml

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

src/parsers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import type { Parser, ParserOptions, Plugin } from 'prettier';
22
import { format } from 'prettier';
33
import { parsers as babelParsers } from 'prettier/plugins/babel';
44
import { parsers as htmlParsers } from 'prettier/plugins/html';
5+
import { parsers as postcssParsers } from 'prettier/plugins/postcss';
56
import { parsers as typescriptParsers } from 'prettier/plugins/typescript';
67

78
import type { SubstitutePatch } from './core-parts';
89
import { makePatches, applyPatches } from './core-parts';
910

1011
const EOL = '\n';
1112

13+
/**
14+
* @deprecated This function is deprecated and will be removed in version 0.12.0. There are still no plans to support the `markdown` and `mdx` parsers. I just thought it would be better to guide users to override Prettier's configuration rather than branching inside this plugin.
15+
*/
1216
async function formatAsCodeblock(text: string, options: ParserOptions, plugins?: Plugin[]) {
1317
let codeblockStart = '```';
1418
const codeblockEnd = '```';
@@ -183,6 +187,15 @@ export const parsers: { [parserName: string]: Parser } = {
183187
vue: transformParser('vue', {
184188
defaultParser: htmlParsers.vue,
185189
}),
190+
css: transformParser('css', {
191+
defaultParser: postcssParsers.css,
192+
}),
193+
scss: transformParser('scss', {
194+
defaultParser: postcssParsers.scss,
195+
}),
196+
less: transformParser('less', {
197+
defaultParser: postcssParsers.less,
198+
}),
186199
oxc: transformParser('oxc', {
187200
defaultParser: null,
188201
externalPluginName: '@prettier/plugin-oxc',
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`'two plugins whose formatting regions …' > expectation 1`] = `
4+
".callout
5+
{
6+
@apply rounded-xl border border-zinc-400/30 bg-gray-100/50 px-4 py-4 dark:border-neutral-500/30 dark:bg-neutral-900/50;
7+
}
8+
"
9+
`;
10+
11+
exports[`'two plugins whose formatting regions …' > expectation 2`] = `
12+
".callout
13+
{
14+
@apply rounded-xl border border-zinc-400/30 bg-gray-100/50 px-4 py-4 dark:border-neutral-500/30 dark:bg-neutral-900/50;
15+
}
16+
"
17+
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`'brace-style plugin - standalone use' > expectation 1`] = `
4+
".callout
5+
{
6+
@apply bg-gray-100/50 border border-zinc-400/30 dark:bg-neutral-900/50 dark:border-neutral-500/30 px-4 py-4 rounded-xl;
7+
}
8+
"
9+
`;
10+
11+
exports[`'tailwindcss plugin - standalone use' > expectation 1`] = `
12+
".callout {
13+
@apply rounded-xl border border-zinc-400/30 bg-gray-100/50 px-4 py-4 dark:border-neutral-500/30 dark:bg-neutral-900/50;
14+
}
15+
"
16+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`'no plugins' > expectation 1`] = `
4+
".callout {
5+
@apply bg-gray-100/50 border border-zinc-400/30 dark:bg-neutral-900/50 dark:border-neutral-500/30 px-4 py-4 rounded-xl;
6+
}
7+
"
8+
`;

tests/css/multiple-plugin.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { thisPlugin, braceStylePluginOptions, testSnapshotEach } from '../adaptor';
2+
import type { Fixture } from '../settings';
3+
import { baseOptions } from '../settings';
4+
5+
const options = {
6+
...baseOptions,
7+
parser: 'css',
8+
};
9+
10+
const fixtures: Omit<Fixture, 'output'>[] = [
11+
{
12+
name: 'two plugins whose formatting regions are disjoint (1) - brace-style -> tailwindcss',
13+
input: `
14+
.callout {
15+
@apply bg-gray-100/50 border border-zinc-400/30 dark:bg-neutral-900/50 dark:border-neutral-500/30 px-4 py-4 rounded-xl;
16+
}
17+
`,
18+
options: {
19+
plugins: ['prettier-plugin-brace-style', 'prettier-plugin-tailwindcss', thisPlugin],
20+
...braceStylePluginOptions,
21+
},
22+
},
23+
{
24+
name: 'two plugins whose formatting regions are disjoint (2) - tailwindcss -> brace-style',
25+
input: `
26+
.callout {
27+
@apply bg-gray-100/50 border border-zinc-400/30 dark:bg-neutral-900/50 dark:border-neutral-500/30 px-4 py-4 rounded-xl;
28+
}
29+
`,
30+
options: {
31+
plugins: ['prettier-plugin-tailwindcss', 'prettier-plugin-brace-style', thisPlugin],
32+
...braceStylePluginOptions,
33+
},
34+
},
35+
];
36+
37+
testSnapshotEach(fixtures, options);

tests/css/single-plugin.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { braceStylePluginOptions, testSnapshotEach } from '../adaptor';
2+
import type { Fixture } from '../settings';
3+
import { baseOptions } from '../settings';
4+
5+
const options = {
6+
...baseOptions,
7+
parser: 'css',
8+
};
9+
10+
const fixtures: Omit<Fixture, 'output'>[] = [
11+
{
12+
name: 'brace-style plugin - standalone use',
13+
input: `
14+
.callout {
15+
@apply bg-gray-100/50 border border-zinc-400/30 dark:bg-neutral-900/50 dark:border-neutral-500/30 px-4 py-4 rounded-xl;
16+
}
17+
`,
18+
options: {
19+
plugins: ['prettier-plugin-brace-style'],
20+
...braceStylePluginOptions,
21+
},
22+
},
23+
{
24+
name: 'tailwindcss plugin - standalone use',
25+
input: `
26+
.callout {
27+
@apply bg-gray-100/50 border border-zinc-400/30 dark:bg-neutral-900/50 dark:border-neutral-500/30 px-4 py-4 rounded-xl;
28+
}
29+
`,
30+
options: {
31+
plugins: ['prettier-plugin-tailwindcss'],
32+
},
33+
},
34+
];
35+
36+
testSnapshotEach(fixtures, options);

tests/css/zero-plugin.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { testSnapshotEach } from '../adaptor';
2+
import type { Fixture } from '../settings';
3+
import { baseOptions } from '../settings';
4+
5+
const options = {
6+
...baseOptions,
7+
parser: 'css',
8+
};
9+
10+
const fixtures: Omit<Fixture, 'output'>[] = [
11+
{
12+
name: 'no plugins',
13+
input: `
14+
.callout {
15+
@apply bg-gray-100/50 border border-zinc-400/30 dark:bg-neutral-900/50 dark:border-neutral-500/30 px-4 py-4 rounded-xl;
16+
}
17+
`,
18+
options: {
19+
plugins: [],
20+
},
21+
},
22+
];
23+
24+
testSnapshotEach(fixtures, options);

0 commit comments

Comments
 (0)