Format various languages with formatters in ESLint
Format various languages with formatters in ESLint. Supports Prettier and dprint. Side-effects-free and fully configurable.
npm i -D eslint-plugin-format
This plugin does not do language detection or reading configure files, you need to specify the language for each file type you want to format along with other formatting options. We recommend using ESLint’s Flat Config format.
// eslint.config.js
import format from 'eslint-plugin-format'
export default [
// ...other flat configs
// use Prettier to format CSS
{
files: ['**/*.css'],
languageOptions: {
parser: format.parserPlain,
},
plugins: {
format,
},
rules: {
'format/prettier': ['error', { parser: 'css', tabWidth: 2 }],
},
},
// use dprint to format TOML
{
files: ['**/*.toml'],
languageOptions: {
parser: format.parserPlain,
},
plugins: {
format,
},
rules: {
'format/dprint': ['error', { language: 'toml', languageOptions: { indentWidth: 2 } }],
},
},
// use dprint to format HTML and CSS in style tags
{
files: ['**/*.html'],
languageOptions: {
parser: format.parserPlain,
},
plugins: {
format,
},
rules: {
'format/dprint': ['error', {
plugins: [
{
plugin: 'node_modules/dprint-plugin-malva/plugin.wasm',
options: {},
},
{
plugin: 'node_modules/dprint-plugin-markup/plugin.wasm',
options: {
scriptIndent: true,
styleIndent: true,
},
},
],
useTabs: false,
indentWidth: 2,
}],
},
},
]
format/prettierUse Prettier to format files.
parser (required) - the language to format, Supported languagesformat/dprintUse dprint to format files.
Either:
language (required) - the language to format, or can be a filepath or URL to the WASM binary. Supported languageslanguageOptions - the options for the languageOr:
plugins (required) - Array of plugins, defined as an object containing plugin (same as language above) and options, which is the same as languageOptions above.eslint-plugin-prettier?While this plugin provides Prettier as one of the formatters, the main difference is that eslint-plugin-prettier is much more opinionated toward the Prettier CLI ecosystem. While this plugin only treats Prettier as the side-effects-free formatter and gives you full control in ESLint.
Thanks to the existing works for references and inspiration.
MIT License © 2023-PRESENT Anthony Fu
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.