Skip to content

Commit

Permalink
feat: add prettier-plugin-astro for eslint-config formatter (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaivanwong committed Mar 4, 2024
1 parent 74909bf commit 479c47f
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 6 deletions.
14 changes: 14 additions & 0 deletions fixtures/input/astro.astro
@@ -1,7 +1,21 @@
---
const isJsx = true
const content = "hi!";
---

<article>
<div>{content}</div>
<div>
{isJsx && (
<h1>{content}</h1>
)}
</div>
</article>


<script>
document.querySelector('h1')?.addEventListener('click', () => {
alert('hi!');
});

</script>
20 changes: 17 additions & 3 deletions fixtures/output/all/astro.astro
@@ -1,7 +1,21 @@
---
const isJsx = true
const content = 'hi!';
---

<article>
<div>{content}</div>
</article>
<article>
<div>{content}</div>
<div>
{isJsx && (
<h1>{content}</h1>
)}
</div>
</article>


<script>
document.querySelector('h1')?.addEventListener('click', () => {
alert('hi!');
});

</script>
21 changes: 21 additions & 0 deletions fixtures/output/with-formatters/astro.astro
@@ -0,0 +1,21 @@
---
const isJsx = true
const content = 'hi!';
---

<article>
<div>{content}</div>
<div>
{isJsx && (
<h1>{content}</h1>
)}
</div>
</article>


<script>
document.querySelector('h1')?.addEventListener('click', () => {
alert('hi!');
});

</script>
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -46,6 +46,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.4",
"eslint-plugin-svelte": "^2.34.1",
"prettier-plugin-astro": "^0.13.0",
"prettier-plugin-slidev": "^1.0.5",
"svelte-eslint-parser": "^0.33.1"
},
Expand Down Expand Up @@ -74,6 +75,9 @@
"eslint-plugin-svelte": {
"optional": true
},
"prettier-plugin-astro": {
"optional": true
},
"prettier-plugin-slidev": {
"optional": true
},
Expand Down Expand Up @@ -146,6 +150,7 @@
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"lint-staged": "^15.2.2",
"prettier-plugin-astro": "^0.13.0",
"prettier-plugin-slidev": "^1.0.5",
"rimraf": "^5.0.5",
"simple-git-hooks": "^2.9.0",
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/cli/constants.ts
Expand Up @@ -49,6 +49,7 @@ export const vscodeSettingsString = `
"json",
"jsonc",
"yaml",
"toml"
"toml",
"astro",
]
`
8 changes: 7 additions & 1 deletion src/configs/astro.ts
Expand Up @@ -43,7 +43,13 @@ export async function astro(
'astro/semi': 'off',

...stylistic
? {}
? {
'style/indent': 'off',
'style/jsx-closing-tag-location': 'off',
'style/jsx-indent': 'off',
'style/jsx-one-expression-per-line': 'off',
'style/no-multiple-empty-lines': 'off',
}
: {},

...overrides,
Expand Down
26 changes: 25 additions & 1 deletion src/configs/formatters.ts
@@ -1,5 +1,5 @@
import { isPackageExists } from 'local-pkg'
import { GLOB_CSS, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs'
import { GLOB_ASTRO, GLOB_CSS, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS } from '../globs'
import type { VendoredPrettierOptions } from '../vender/prettier-types'
import { ensurePackages, interopDefault, parserPlain } from '../utils'
import type { FlatConfigItem, OptionsFormatters, StylisticConfig } from '../types'
Expand All @@ -11,6 +11,7 @@ export async function formatters(
): Promise<FlatConfigItem[]> {
if (options === true) {
options = {
astro: isPackageExists('astro'),
css: true,
graphql: true,
html: true,
Expand All @@ -22,6 +23,7 @@ export async function formatters(
await ensurePackages([
'eslint-plugin-format',
options.markdown && options.slidev ? 'prettier-plugin-slidev' : undefined,
options.astro ? 'prettier-plugin-astro' : undefined,
])

if (options.slidev && options.markdown !== true && options.markdown !== 'prettier')
Expand Down Expand Up @@ -201,6 +203,28 @@ export async function formatters(
}
}

if (options.astro) {
configs.push({
files: [GLOB_ASTRO],
languageOptions: {
parser: parserPlain,
},
name: 'antfu:formatter:astro',
rules: {
'format/prettier': [
'error',
{
...prettierOptions,
parser: 'astro',
plugins: [
'prettier-plugin-astro',
],
},
],
},
})
}

if (options.graphql) {
configs.push({
files: ['**/*.graphql'],
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Expand Up @@ -152,6 +152,13 @@ export interface OptionsFormatters {
slidev?: boolean | {
files?: string[]
}

/**
* Enable formatting support for Astro.
*
* Currently only support Prettier.
*/
astro?: 'prettier' | boolean
}

export interface OptionsComponentExts {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.test.ts
Expand Up @@ -62,6 +62,7 @@ runWithConfig(
{
typescript: true,
vue: true,
astro: true,
formatters: true,
},
)
Expand Down

0 comments on commit 479c47f

Please sign in to comment.