Skip to content

Commit 479c47f

Browse files
author
Kaivan Wong
authored
feat: add prettier-plugin-astro for eslint-config formatter (#413)
1 parent 74909bf commit 479c47f

File tree

10 files changed

+135
-6
lines changed

10 files changed

+135
-6
lines changed

fixtures/input/astro.astro

+14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
---
2+
const isJsx = true
23
const content = "hi!";
34
---
45

56
<article>
67
<div>{content}</div>
8+
<div>
9+
{isJsx && (
10+
<h1>{content}</h1>
11+
)}
12+
</div>
713
</article>
14+
15+
16+
<script>
17+
document.querySelector('h1')?.addEventListener('click', () => {
18+
alert('hi!');
19+
});
20+
21+
</script>

fixtures/output/all/astro.astro

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
---
2+
const isJsx = true
23
const content = 'hi!';
34
---
45

5-
<article>
6-
<div>{content}</div>
7-
</article>
6+
<article>
7+
<div>{content}</div>
8+
<div>
9+
{isJsx && (
10+
<h1>{content}</h1>
11+
)}
12+
</div>
13+
</article>
14+
15+
16+
<script>
17+
document.querySelector('h1')?.addEventListener('click', () => {
18+
alert('hi!');
19+
});
20+
21+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
const isJsx = true
3+
const content = 'hi!';
4+
---
5+
6+
<article>
7+
<div>{content}</div>
8+
<div>
9+
{isJsx && (
10+
<h1>{content}</h1>
11+
)}
12+
</div>
13+
</article>
14+
15+
16+
<script>
17+
document.querySelector('h1')?.addEventListener('click', () => {
18+
alert('hi!');
19+
});
20+
21+
</script>

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"eslint-plugin-react-hooks": "^4.6.0",
4747
"eslint-plugin-react-refresh": "^0.4.4",
4848
"eslint-plugin-svelte": "^2.34.1",
49+
"prettier-plugin-astro": "^0.13.0",
4950
"prettier-plugin-slidev": "^1.0.5",
5051
"svelte-eslint-parser": "^0.33.1"
5152
},
@@ -74,6 +75,9 @@
7475
"eslint-plugin-svelte": {
7576
"optional": true
7677
},
78+
"prettier-plugin-astro": {
79+
"optional": true
80+
},
7781
"prettier-plugin-slidev": {
7882
"optional": true
7983
},
@@ -146,6 +150,7 @@
146150
"fast-glob": "^3.3.2",
147151
"fs-extra": "^11.2.0",
148152
"lint-staged": "^15.2.2",
153+
"prettier-plugin-astro": "^0.13.0",
149154
"prettier-plugin-slidev": "^1.0.5",
150155
"rimraf": "^5.0.5",
151156
"simple-git-hooks": "^2.9.0",

pnpm-lock.yaml

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/constants.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const vscodeSettingsString = `
4949
"json",
5050
"jsonc",
5151
"yaml",
52-
"toml"
52+
"toml",
53+
"astro",
5354
]
5455
`

src/configs/astro.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ export async function astro(
4343
'astro/semi': 'off',
4444

4545
...stylistic
46-
? {}
46+
? {
47+
'style/indent': 'off',
48+
'style/jsx-closing-tag-location': 'off',
49+
'style/jsx-indent': 'off',
50+
'style/jsx-one-expression-per-line': 'off',
51+
'style/no-multiple-empty-lines': 'off',
52+
}
4753
: {},
4854

4955
...overrides,

src/configs/formatters.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isPackageExists } from 'local-pkg'
2-
import { GLOB_CSS, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs'
2+
import { GLOB_ASTRO, GLOB_CSS, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs'
33
import type { VendoredPrettierOptions } from '../vender/prettier-types'
44
import { ensurePackages, interopDefault, parserPlain } from '../utils'
55
import type { FlatConfigItem, OptionsFormatters, StylisticConfig } from '../types'
@@ -11,6 +11,7 @@ export async function formatters(
1111
): Promise<FlatConfigItem[]> {
1212
if (options === true) {
1313
options = {
14+
astro: isPackageExists('astro'),
1415
css: true,
1516
graphql: true,
1617
html: true,
@@ -22,6 +23,7 @@ export async function formatters(
2223
await ensurePackages([
2324
'eslint-plugin-format',
2425
options.markdown && options.slidev ? 'prettier-plugin-slidev' : undefined,
26+
options.astro ? 'prettier-plugin-astro' : undefined,
2527
])
2628

2729
if (options.slidev && options.markdown !== true && options.markdown !== 'prettier')
@@ -201,6 +203,28 @@ export async function formatters(
201203
}
202204
}
203205

206+
if (options.astro) {
207+
configs.push({
208+
files: [GLOB_ASTRO],
209+
languageOptions: {
210+
parser: parserPlain,
211+
},
212+
name: 'antfu:formatter:astro',
213+
rules: {
214+
'format/prettier': [
215+
'error',
216+
{
217+
...prettierOptions,
218+
parser: 'astro',
219+
plugins: [
220+
'prettier-plugin-astro',
221+
],
222+
},
223+
],
224+
},
225+
})
226+
}
227+
204228
if (options.graphql) {
205229
configs.push({
206230
files: ['**/*.graphql'],

src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ export interface OptionsFormatters {
152152
slidev?: boolean | {
153153
files?: string[]
154154
}
155+
156+
/**
157+
* Enable formatting support for Astro.
158+
*
159+
* Currently only support Prettier.
160+
*/
161+
astro?: 'prettier' | boolean
155162
}
156163

157164
export interface OptionsComponentExts {

test/fixtures.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ runWithConfig(
6262
{
6363
typescript: true,
6464
vue: true,
65+
astro: true,
6566
formatters: true,
6667
},
6768
)

0 commit comments

Comments
 (0)