Skip to content

Commit

Permalink
fix: disbale type-aware in astro, handle formatter overrides with sty…
Browse files Browse the repository at this point in the history
…listic (#493)

Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
tinchoz49 and antfu committed Jun 8, 2024
1 parent 0582c44 commit e32301a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
15 changes: 6 additions & 9 deletions fixtures/output/with-formatters/astro.astro
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
---
const isJsx = true
const content = 'hi!';
const content = 'hi!'
---

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


<script>
const { log: logger } = console
document.querySelector('h1')?.addEventListener('click', () => {
logger('clicked')
})
const { log: logger } = console
document.querySelector('h1')?.addEventListener('click', () => {
logger('clicked')
})
</script>
18 changes: 16 additions & 2 deletions src/configs/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isPackageExists } from 'local-pkg'
import { GLOB_ASTRO, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_XML } from '../globs'
import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_XML } from '../globs'
import type { VendoredPrettierOptions } from '../vender/prettier-types'
import { ensurePackages, interopDefault, parserPlain } from '../utils'
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
Expand All @@ -11,7 +11,7 @@ export async function formatters(
): Promise<TypedFlatConfigItem[]> {
if (options === true) {
options = {
astro: isPackageExists('astro'),
astro: isPackageExists('prettier-plugin-astro'),
css: true,
graphql: true,
html: true,
Expand Down Expand Up @@ -255,6 +255,20 @@ export async function formatters(
],
},
})

configs.push({
files: [GLOB_ASTRO, GLOB_ASTRO_TS],
name: 'antfu/formatter/astro/disables',
rules: {
'style/arrow-parens': 'off',
'style/block-spacing': 'off',
'style/comma-dangle': 'off',
'style/indent': 'off',
'style/no-multi-spaces': 'off',
'style/quotes': 'off',
'style/semi': 'off',
},
})
}

if (options.graphql) {
Expand Down
20 changes: 0 additions & 20 deletions src/configs/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,6 @@ export async function markdown(
'unused-imports/no-unused-imports': 'off',
'unused-imports/no-unused-vars': 'off',

// Type aware rules
...{
'ts/await-thenable': 'off',
'ts/dot-notation': 'off',
'ts/no-floating-promises': 'off',
'ts/no-for-in-array': 'off',
'ts/no-implied-eval': 'off',
'ts/no-misused-promises': 'off',
'ts/no-throw-literal': 'off',
'ts/no-unnecessary-type-assertion': 'off',
'ts/no-unsafe-argument': 'off',
'ts/no-unsafe-assignment': 'off',
'ts/no-unsafe-call': 'off',
'ts/no-unsafe-member-access': 'off',
'ts/no-unsafe-return': 'off',
'ts/restrict-plus-operands': 'off',
'ts/restrict-template-expressions': 'off',
'ts/unbound-method': 'off',
},

...overrides,
},
},
Expand Down
13 changes: 10 additions & 3 deletions src/configs/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'node:process'
import { GLOB_TS, GLOB_TSX } from '../globs'
import { GLOB_ASTRO_TS, GLOB_MARKDOWN, GLOB_TS, GLOB_TSX } from '../globs'
import type { OptionsComponentExts, OptionsFiles, OptionsOverrides, OptionsTypeScriptParserOptions, OptionsTypeScriptWithTypes, TypedFlatConfigItem } from '../types'
import { pluginAntfu } from '../plugins'
import { interopDefault, renameRules, toArray } from '../utils'
Expand All @@ -20,6 +20,10 @@ export async function typescript(
]

const filesTypeAware = options.filesTypeAware ?? [GLOB_TS, GLOB_TSX]
const ignoresTypeAware = options.ignoresTypeAware ?? [
`${GLOB_MARKDOWN}/**`,
GLOB_ASTRO_TS,
]
const tsconfigPath = options?.tsconfigPath
? toArray(options.tsconfigPath)
: undefined
Expand Down Expand Up @@ -90,10 +94,12 @@ export async function typescript(
// assign type-aware parser for type-aware files and type-unaware parser for the rest
...isTypeAware
? [
makeParser(true, filesTypeAware),
makeParser(true, filesTypeAware, ignoresTypeAware),
makeParser(false, files, filesTypeAware),
]
: [makeParser(false, files)],
: [
makeParser(false, files),
],
{
files,
name: 'antfu/typescript/rules',
Expand Down Expand Up @@ -138,6 +144,7 @@ export async function typescript(
...isTypeAware
? [{
files: filesTypeAware,
ignores: ignoresTypeAware,
name: 'antfu/typescript/rules-type-aware',
rules: {
...tsconfigPath ? typeAwareRules : {},
Expand Down
1 change: 1 addition & 0 deletions src/globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const GLOB_TOML = '**/*.toml'
export const GLOB_XML = '**/*.xml'
export const GLOB_HTML = '**/*.htm?(l)'
export const GLOB_ASTRO = '**/*.astro'
export const GLOB_ASTRO_TS = '**/*.astro/*.ts'
export const GLOB_GRAPHQL = '**/*.{g,graph}ql'

export const GLOB_MARKDOWN_CODE = `${GLOB_MARKDOWN}/${GLOB_SRC}`
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ export interface OptionsTypeScriptParserOptions {
* @default ['**\/*.{ts,tsx}']
*/
filesTypeAware?: string[]

/**
* Glob patterns for files that should not be type aware.
* @default ['**\/*.md\/**', '**\/*.astro/*.ts']
*/
ignoresTypeAware?: string[]
}

export interface OptionsTypeScriptWithTypes {
Expand Down

0 comments on commit e32301a

Please sign in to comment.