|
| 1 | +import type { Options } from '@cspell/eslint-plugin' |
| 2 | +import cspellRecommended from '@cspell/eslint-plugin/recommended' |
| 3 | +import js from '@eslint/js' |
| 4 | +import react from '@eslint-react/eslint-plugin' |
| 5 | +import { Command } from 'commander' |
| 6 | +import { merge } from 'es-toolkit/compat' |
| 7 | +import { ESLint } from 'eslint' |
| 8 | +import * as mdx from 'eslint-plugin-mdx' |
| 9 | +import tseslint from 'typescript-eslint' |
| 10 | + |
| 11 | +import type { GlobalCliOptions } from '../types.js' |
| 12 | + |
| 13 | +import { parseTerms } from './helpers.js' |
| 14 | +import { loadConfig } from './load-config.js' |
| 15 | + |
| 16 | +export const lintCommand = new Command('lint') |
| 17 | + .description('Lint the documentation') |
| 18 | + .argument('[root]', 'Root directory of the documentation') |
| 19 | + .action(async function (root?: string) { |
| 20 | + const globalOptions = this.optsWithGlobals<GlobalCliOptions>() |
| 21 | + |
| 22 | + const { config } = await loadConfig(root, globalOptions) |
| 23 | + |
| 24 | + const docsDir = config.root! |
| 25 | + |
| 26 | + const parsedTerms = await parseTerms() |
| 27 | + |
| 28 | + const eslint = new ESLint({ |
| 29 | + cwd: docsDir, |
| 30 | + // @ts-expect-error -- stronger types |
| 31 | + baseConfig: tseslint.config([ |
| 32 | + { |
| 33 | + extends: [ |
| 34 | + js.configs.recommended, |
| 35 | + react.configs.recommended, |
| 36 | + mdx.configs.flat, |
| 37 | + ], |
| 38 | + }, |
| 39 | + { |
| 40 | + files: ['**/docs/en/**/*.{js,jsx,md,mdx,ts,tsx}'], |
| 41 | + extends: [cspellRecommended], |
| 42 | + rules: { |
| 43 | + '@cspell/spellchecker': [ |
| 44 | + 'error', |
| 45 | + merge( |
| 46 | + { |
| 47 | + autoFix: true, |
| 48 | + cspell: { |
| 49 | + words: parsedTerms.map((it) => it.en), |
| 50 | + flagWords: parsedTerms.flatMap( |
| 51 | + ({ badCases }) => badCases?.en ?? [], |
| 52 | + ), |
| 53 | + }, |
| 54 | + } satisfies Partial<Options>, |
| 55 | + config.lint?.cspellOptions, |
| 56 | + ), |
| 57 | + ], |
| 58 | + }, |
| 59 | + }, |
| 60 | + // https://github.com/eslint/eslint/issues/19722 |
| 61 | + // { |
| 62 | + // files: ['**/*.{ts,tsx}'], |
| 63 | + // extends: [ |
| 64 | + // tseslint.configs.recommendedTypeChecked, |
| 65 | + // react.configs['recommended-typescript'], |
| 66 | + // ], |
| 67 | + // rules: { |
| 68 | + // '@typescript-eslint/no-misused-promises': 'off', |
| 69 | + // '@typescript-eslint/no-non-null-assertion': 'off', |
| 70 | + // '@typescript-eslint/restrict-template-expressions': [ |
| 71 | + // 'error', |
| 72 | + // { allowNumber: true }, |
| 73 | + // ], |
| 74 | + // 'prefer-const': ['error', { destructuring: 'all' }], |
| 75 | + // }, |
| 76 | + // languageOptions: { |
| 77 | + // parser: tseslint.parser, |
| 78 | + // parserOptions: { |
| 79 | + // projectService: true, |
| 80 | + // }, |
| 81 | + // }, |
| 82 | + // }, |
| 83 | + ]), |
| 84 | + }) |
| 85 | + |
| 86 | + const results = await eslint.lintFiles('**/*.{js,jsx,ts,tsx,md,mdx}') |
| 87 | + |
| 88 | + if (!results.length) { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + process.exitCode = 1 |
| 93 | + |
| 94 | + const formatter = await eslint.loadFormatter('stylish') |
| 95 | + |
| 96 | + process.stderr.write(await formatter.format(results)) |
| 97 | + }) |
0 commit comments