|
| 1 | +import type { StylisticCustomizeOptions } from '@stylistic/eslint-plugin' |
| 2 | +import type { Linter } from 'eslint' |
| 3 | +import { isPackageExists } from 'local-pkg' |
| 4 | +import type { OptionsFormatters } from '../types' |
| 5 | +import { ensurePackages, interopDefault, parserPlain } from '../utils' |
| 6 | +import { GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_SVG, GLOB_XML } from '../globs' |
| 7 | + |
| 8 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 9 | +type VendoredPrettierOptions = any |
| 10 | + |
| 11 | +function mergePrettierOptions( |
| 12 | + options: VendoredPrettierOptions, |
| 13 | + overrides: VendoredPrettierOptions = {}, |
| 14 | +): VendoredPrettierOptions { |
| 15 | + return { |
| 16 | + ...options, |
| 17 | + ...overrides, |
| 18 | + plugins: [ |
| 19 | + ...(overrides.plugins || []), |
| 20 | + ...(options.plugins || []), |
| 21 | + ], |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +export async function formatters( |
| 26 | + options: OptionsFormatters | boolean = {}, |
| 27 | + stylistic: StylisticCustomizeOptions<true>, |
| 28 | +): Promise<Linter.Config[]> { |
| 29 | + if (!options) |
| 30 | + return [] |
| 31 | + |
| 32 | + if (options === true) { |
| 33 | + const isPrettierPluginXmlInScope = isPackageExists('@prettier/plugin-xml') |
| 34 | + options = { |
| 35 | + css: true, |
| 36 | + graphql: true, |
| 37 | + html: true, |
| 38 | + // Markdown is disabled by default as many Nuxt projects use MDC with @nuxt/content, |
| 39 | + // where Prettier doesn't fully understand. |
| 40 | + markdown: false, |
| 41 | + svg: isPrettierPluginXmlInScope, |
| 42 | + xml: isPrettierPluginXmlInScope, |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + await ensurePackages([ |
| 47 | + 'eslint-plugin-format', |
| 48 | + (options.xml || options.svg) ? '@prettier/plugin-xml' : undefined, |
| 49 | + ]) |
| 50 | + |
| 51 | + const { |
| 52 | + indent, |
| 53 | + quotes, |
| 54 | + semi, |
| 55 | + } = { |
| 56 | + indent: 2, |
| 57 | + quotes: 'single', |
| 58 | + semi: false, |
| 59 | + ...stylistic, |
| 60 | + } |
| 61 | + |
| 62 | + const prettierOptions: VendoredPrettierOptions = Object.assign( |
| 63 | + { |
| 64 | + endOfLine: 'auto', |
| 65 | + printWidth: 120, |
| 66 | + semi, |
| 67 | + singleQuote: quotes === 'single', |
| 68 | + tabWidth: typeof indent === 'number' ? indent : 2, |
| 69 | + trailingComma: 'all', |
| 70 | + useTabs: indent === 'tab', |
| 71 | + } satisfies VendoredPrettierOptions, |
| 72 | + options.prettierOptions || {}, |
| 73 | + ) |
| 74 | + |
| 75 | + const prettierXmlOptions: VendoredPrettierOptions = { |
| 76 | + xmlQuoteAttributes: 'double', |
| 77 | + xmlSelfClosingSpace: true, |
| 78 | + xmlSortAttributesByKey: false, |
| 79 | + xmlWhitespaceSensitivity: 'ignore', |
| 80 | + } |
| 81 | + |
| 82 | + const dprintOptions = Object.assign( |
| 83 | + { |
| 84 | + indentWidth: typeof indent === 'number' ? indent : 2, |
| 85 | + quoteStyle: quotes === 'single' ? 'preferSingle' : 'preferDouble', |
| 86 | + useTabs: indent === 'tab', |
| 87 | + }, |
| 88 | + options.dprintOptions || {}, |
| 89 | + ) |
| 90 | + |
| 91 | + const pluginFormat = await interopDefault(import('eslint-plugin-format')) |
| 92 | + |
| 93 | + const configs: Linter.Config[] = [ |
| 94 | + { |
| 95 | + name: 'nuxt/formatter/setup', |
| 96 | + plugins: { |
| 97 | + format: pluginFormat, |
| 98 | + }, |
| 99 | + }, |
| 100 | + ] |
| 101 | + |
| 102 | + if (options.css) { |
| 103 | + configs.push( |
| 104 | + { |
| 105 | + files: [GLOB_CSS, GLOB_POSTCSS], |
| 106 | + languageOptions: { |
| 107 | + parser: parserPlain, |
| 108 | + }, |
| 109 | + name: 'nuxt/formatter/css', |
| 110 | + rules: { |
| 111 | + 'format/prettier': [ |
| 112 | + 'error', |
| 113 | + mergePrettierOptions(prettierOptions, { |
| 114 | + parser: 'css', |
| 115 | + }), |
| 116 | + ], |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + files: [GLOB_SCSS], |
| 121 | + languageOptions: { |
| 122 | + parser: parserPlain, |
| 123 | + }, |
| 124 | + name: 'nuxt/formatter/scss', |
| 125 | + rules: { |
| 126 | + 'format/prettier': [ |
| 127 | + 'error', |
| 128 | + mergePrettierOptions(prettierOptions, { |
| 129 | + parser: 'scss', |
| 130 | + }), |
| 131 | + ], |
| 132 | + }, |
| 133 | + }, |
| 134 | + { |
| 135 | + files: [GLOB_LESS], |
| 136 | + languageOptions: { |
| 137 | + parser: parserPlain, |
| 138 | + }, |
| 139 | + name: 'nuxt/formatter/less', |
| 140 | + rules: { |
| 141 | + 'format/prettier': [ |
| 142 | + 'error', |
| 143 | + mergePrettierOptions(prettierOptions, { |
| 144 | + parser: 'less', |
| 145 | + }), |
| 146 | + ], |
| 147 | + }, |
| 148 | + }, |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + if (options.html) { |
| 153 | + configs.push({ |
| 154 | + files: [GLOB_HTML], |
| 155 | + languageOptions: { |
| 156 | + parser: parserPlain, |
| 157 | + }, |
| 158 | + name: 'nuxt/formatter/html', |
| 159 | + rules: { |
| 160 | + 'format/prettier': [ |
| 161 | + 'error', |
| 162 | + mergePrettierOptions(prettierOptions, { |
| 163 | + parser: 'html', |
| 164 | + }), |
| 165 | + ], |
| 166 | + }, |
| 167 | + }) |
| 168 | + } |
| 169 | + |
| 170 | + if (options.xml) { |
| 171 | + configs.push({ |
| 172 | + files: [GLOB_XML], |
| 173 | + languageOptions: { |
| 174 | + parser: parserPlain, |
| 175 | + }, |
| 176 | + name: 'nuxt/formatter/xml', |
| 177 | + rules: { |
| 178 | + 'format/prettier': [ |
| 179 | + 'error', |
| 180 | + mergePrettierOptions({ ...prettierXmlOptions, ...prettierOptions }, { |
| 181 | + parser: 'xml', |
| 182 | + plugins: [ |
| 183 | + '@prettier/plugin-xml', |
| 184 | + ], |
| 185 | + }), |
| 186 | + ], |
| 187 | + }, |
| 188 | + }) |
| 189 | + } |
| 190 | + if (options.svg) { |
| 191 | + configs.push({ |
| 192 | + files: [GLOB_SVG], |
| 193 | + languageOptions: { |
| 194 | + parser: parserPlain, |
| 195 | + }, |
| 196 | + name: 'nuxt/formatter/svg', |
| 197 | + rules: { |
| 198 | + 'format/prettier': [ |
| 199 | + 'error', |
| 200 | + mergePrettierOptions({ ...prettierXmlOptions, ...prettierOptions }, { |
| 201 | + parser: 'xml', |
| 202 | + plugins: [ |
| 203 | + '@prettier/plugin-xml', |
| 204 | + ], |
| 205 | + }), |
| 206 | + ], |
| 207 | + }, |
| 208 | + }) |
| 209 | + } |
| 210 | + |
| 211 | + if (options.markdown) { |
| 212 | + const formater = options.markdown === true |
| 213 | + ? 'prettier' |
| 214 | + : options.markdown |
| 215 | + |
| 216 | + configs.push({ |
| 217 | + files: [GLOB_MARKDOWN], |
| 218 | + languageOptions: { |
| 219 | + parser: parserPlain, |
| 220 | + }, |
| 221 | + name: 'nuxt/formatter/markdown', |
| 222 | + rules: { |
| 223 | + [`format/${formater}`]: [ |
| 224 | + 'error', |
| 225 | + formater === 'prettier' |
| 226 | + ? mergePrettierOptions(prettierOptions, { |
| 227 | + embeddedLanguageFormatting: 'off', |
| 228 | + parser: 'markdown', |
| 229 | + }) |
| 230 | + : { |
| 231 | + ...dprintOptions, |
| 232 | + language: 'markdown', |
| 233 | + }, |
| 234 | + ], |
| 235 | + }, |
| 236 | + }) |
| 237 | + } |
| 238 | + |
| 239 | + if (options.graphql) { |
| 240 | + configs.push({ |
| 241 | + files: [GLOB_GRAPHQL], |
| 242 | + languageOptions: { |
| 243 | + parser: parserPlain, |
| 244 | + }, |
| 245 | + name: 'nuxt/formatter/graphql', |
| 246 | + rules: { |
| 247 | + 'format/prettier': [ |
| 248 | + 'error', |
| 249 | + mergePrettierOptions(prettierOptions, { |
| 250 | + parser: 'graphql', |
| 251 | + }), |
| 252 | + ], |
| 253 | + }, |
| 254 | + }) |
| 255 | + } |
| 256 | + |
| 257 | + return configs |
| 258 | +} |
0 commit comments