-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
features.formatters
to format CSS and HTML (#520)
- Loading branch information
Showing
12 changed files
with
595 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
import type { StylisticCustomizeOptions } from '@stylistic/eslint-plugin' | ||
import type { Linter } from 'eslint' | ||
import { isPackageExists } from 'local-pkg' | ||
import type { OptionsFormatters } from '../types' | ||
import { ensurePackages, interopDefault, parserPlain } from '../utils' | ||
import { GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_SVG, GLOB_XML } from '../globs' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type VendoredPrettierOptions = any | ||
|
||
function mergePrettierOptions( | ||
options: VendoredPrettierOptions, | ||
overrides: VendoredPrettierOptions = {}, | ||
): VendoredPrettierOptions { | ||
return { | ||
...options, | ||
...overrides, | ||
plugins: [ | ||
...(overrides.plugins || []), | ||
...(options.plugins || []), | ||
], | ||
} | ||
} | ||
|
||
export async function formatters( | ||
options: OptionsFormatters | boolean = {}, | ||
stylistic: StylisticCustomizeOptions<true>, | ||
): Promise<Linter.Config[]> { | ||
if (!options) | ||
return [] | ||
|
||
if (options === true) { | ||
const isPrettierPluginXmlInScope = isPackageExists('@prettier/plugin-xml') | ||
options = { | ||
css: true, | ||
graphql: true, | ||
html: true, | ||
// Markdown is disabled by default as many Nuxt projects use MDC with @nuxt/content, | ||
// where Prettier doesn't fully understand. | ||
markdown: false, | ||
svg: isPrettierPluginXmlInScope, | ||
xml: isPrettierPluginXmlInScope, | ||
} | ||
} | ||
|
||
await ensurePackages([ | ||
'eslint-plugin-format', | ||
(options.xml || options.svg) ? '@prettier/plugin-xml' : undefined, | ||
]) | ||
|
||
const { | ||
indent, | ||
quotes, | ||
semi, | ||
} = { | ||
indent: 2, | ||
quotes: 'single', | ||
semi: false, | ||
...stylistic, | ||
} | ||
|
||
const prettierOptions: VendoredPrettierOptions = Object.assign( | ||
{ | ||
endOfLine: 'auto', | ||
printWidth: 120, | ||
semi, | ||
singleQuote: quotes === 'single', | ||
tabWidth: typeof indent === 'number' ? indent : 2, | ||
trailingComma: 'all', | ||
useTabs: indent === 'tab', | ||
} satisfies VendoredPrettierOptions, | ||
options.prettierOptions || {}, | ||
) | ||
|
||
const prettierXmlOptions: VendoredPrettierOptions = { | ||
xmlQuoteAttributes: 'double', | ||
xmlSelfClosingSpace: true, | ||
xmlSortAttributesByKey: false, | ||
xmlWhitespaceSensitivity: 'ignore', | ||
} | ||
|
||
const dprintOptions = Object.assign( | ||
{ | ||
indentWidth: typeof indent === 'number' ? indent : 2, | ||
quoteStyle: quotes === 'single' ? 'preferSingle' : 'preferDouble', | ||
useTabs: indent === 'tab', | ||
}, | ||
options.dprintOptions || {}, | ||
) | ||
|
||
const pluginFormat = await interopDefault(import('eslint-plugin-format')) | ||
|
||
const configs: Linter.Config[] = [ | ||
{ | ||
name: 'nuxt/formatter/setup', | ||
plugins: { | ||
format: pluginFormat, | ||
}, | ||
}, | ||
] | ||
|
||
if (options.css) { | ||
configs.push( | ||
{ | ||
files: [GLOB_CSS, GLOB_POSTCSS], | ||
languageOptions: { | ||
parser: parserPlain, | ||
}, | ||
name: 'nuxt/formatter/css', | ||
rules: { | ||
'format/prettier': [ | ||
'error', | ||
mergePrettierOptions(prettierOptions, { | ||
parser: 'css', | ||
}), | ||
], | ||
}, | ||
}, | ||
{ | ||
files: [GLOB_SCSS], | ||
languageOptions: { | ||
parser: parserPlain, | ||
}, | ||
name: 'nuxt/formatter/scss', | ||
rules: { | ||
'format/prettier': [ | ||
'error', | ||
mergePrettierOptions(prettierOptions, { | ||
parser: 'scss', | ||
}), | ||
], | ||
}, | ||
}, | ||
{ | ||
files: [GLOB_LESS], | ||
languageOptions: { | ||
parser: parserPlain, | ||
}, | ||
name: 'nuxt/formatter/less', | ||
rules: { | ||
'format/prettier': [ | ||
'error', | ||
mergePrettierOptions(prettierOptions, { | ||
parser: 'less', | ||
}), | ||
], | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
if (options.html) { | ||
configs.push({ | ||
files: [GLOB_HTML], | ||
languageOptions: { | ||
parser: parserPlain, | ||
}, | ||
name: 'nuxt/formatter/html', | ||
rules: { | ||
'format/prettier': [ | ||
'error', | ||
mergePrettierOptions(prettierOptions, { | ||
parser: 'html', | ||
}), | ||
], | ||
}, | ||
}) | ||
} | ||
|
||
if (options.xml) { | ||
configs.push({ | ||
files: [GLOB_XML], | ||
languageOptions: { | ||
parser: parserPlain, | ||
}, | ||
name: 'nuxt/formatter/xml', | ||
rules: { | ||
'format/prettier': [ | ||
'error', | ||
mergePrettierOptions({ ...prettierXmlOptions, ...prettierOptions }, { | ||
parser: 'xml', | ||
plugins: [ | ||
'@prettier/plugin-xml', | ||
], | ||
}), | ||
], | ||
}, | ||
}) | ||
} | ||
if (options.svg) { | ||
configs.push({ | ||
files: [GLOB_SVG], | ||
languageOptions: { | ||
parser: parserPlain, | ||
}, | ||
name: 'nuxt/formatter/svg', | ||
rules: { | ||
'format/prettier': [ | ||
'error', | ||
mergePrettierOptions({ ...prettierXmlOptions, ...prettierOptions }, { | ||
parser: 'xml', | ||
plugins: [ | ||
'@prettier/plugin-xml', | ||
], | ||
}), | ||
], | ||
}, | ||
}) | ||
} | ||
|
||
if (options.markdown) { | ||
const formater = options.markdown === true | ||
? 'prettier' | ||
: options.markdown | ||
|
||
configs.push({ | ||
files: [GLOB_MARKDOWN], | ||
languageOptions: { | ||
parser: parserPlain, | ||
}, | ||
name: 'nuxt/formatter/markdown', | ||
rules: { | ||
[`format/${formater}`]: [ | ||
'error', | ||
formater === 'prettier' | ||
? mergePrettierOptions(prettierOptions, { | ||
embeddedLanguageFormatting: 'off', | ||
parser: 'markdown', | ||
}) | ||
: { | ||
...dprintOptions, | ||
language: 'markdown', | ||
}, | ||
], | ||
}, | ||
}) | ||
} | ||
|
||
if (options.graphql) { | ||
configs.push({ | ||
files: [GLOB_GRAPHQL], | ||
languageOptions: { | ||
parser: parserPlain, | ||
}, | ||
name: 'nuxt/formatter/graphql', | ||
rules: { | ||
'format/prettier': [ | ||
'error', | ||
mergePrettierOptions(prettierOptions, { | ||
parser: 'graphql', | ||
}), | ||
], | ||
}, | ||
}) | ||
} | ||
|
||
return configs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.