|
| 1 | +import { includeIgnoreFile } from "@eslint/compat"; |
| 2 | +import { BaseCommand } from "@yarnpkg/cli"; |
| 3 | +import { Configuration, Project } from "@yarnpkg/core"; |
| 4 | +import { Option } from "clipanion"; |
| 5 | +import type { Linter } from "eslint"; |
| 6 | +import { ESLint } from "eslint"; |
| 7 | +import { spawnSync } from "node:child_process"; |
| 8 | +import * as fs from "node:fs"; |
| 9 | +import * as path from "node:path"; |
| 10 | + |
| 11 | +type LinterConfigs = Linter.Config<Linter.RulesRecord>[]; |
| 12 | + |
| 13 | +export class Lint extends BaseCommand { |
| 14 | + static override paths = [["rnx-lint"]]; |
| 15 | + |
| 16 | + fix = Option.Boolean("--fix", false, { |
| 17 | + description: "Automatically fix problems", |
| 18 | + }); |
| 19 | + |
| 20 | + patterns = Option.Rest({ name: "file.js" }); |
| 21 | + |
| 22 | + async execute(): Promise<number | void> { |
| 23 | + const cwd = this.context.cwd; |
| 24 | + const configuration = await Configuration.find(cwd, this.context.plugins); |
| 25 | + const { project } = await Project.find(configuration, cwd); |
| 26 | + |
| 27 | + const [overrideConfigFile, overrideConfig] = await this.loadConfig(cwd); |
| 28 | + |
| 29 | + const eslint = new ESLint({ |
| 30 | + cwd, |
| 31 | + ignorePatterns: this.ignorePatterns(project), |
| 32 | + warnIgnored: false, |
| 33 | + overrideConfigFile, |
| 34 | + overrideConfig, |
| 35 | + fix: this.fix, |
| 36 | + }); |
| 37 | + |
| 38 | + const results = await eslint.lintFiles(this.filePatterns()); |
| 39 | + await ESLint.outputFixes(results); |
| 40 | + |
| 41 | + const formatter = await this.loadFormatter(); |
| 42 | + const output = formatter.format(results); |
| 43 | + |
| 44 | + if (output) { |
| 45 | + if (ESLint.getErrorResults(results).length > 0) { |
| 46 | + console.error(output); |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + console.log(output); |
| 51 | + } |
| 52 | + |
| 53 | + return 0; |
| 54 | + } |
| 55 | + |
| 56 | + private filePatterns() { |
| 57 | + if (this.patterns.length > 0) { |
| 58 | + return this.patterns; |
| 59 | + } |
| 60 | + |
| 61 | + const args = [ |
| 62 | + "ls-files", |
| 63 | + "*.cjs", |
| 64 | + "*.js", |
| 65 | + "*.jsx", |
| 66 | + "*.mjs", |
| 67 | + "*.ts", |
| 68 | + "*.tsx", |
| 69 | + ]; |
| 70 | + const { stdout } = spawnSync("git", args); |
| 71 | + return stdout.toString().trim().split("\n"); |
| 72 | + } |
| 73 | + |
| 74 | + private ignorePatterns(project: Project): string[] { |
| 75 | + const patterns: string[] = []; |
| 76 | + |
| 77 | + const locations = [project.cwd, this.context.cwd]; |
| 78 | + for (const location of locations) { |
| 79 | + const gitignore = path.join(location, ".gitignore"); |
| 80 | + if (fs.existsSync(gitignore)) { |
| 81 | + const { ignores } = includeIgnoreFile(gitignore); |
| 82 | + if (ignores) { |
| 83 | + patterns.push(...ignores); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return patterns; |
| 89 | + } |
| 90 | + |
| 91 | + private async loadConfig( |
| 92 | + cwd: string |
| 93 | + ): Promise<[boolean | string, LinterConfigs | undefined]> { |
| 94 | + const eslintConfigPath = path.join(cwd, "eslint.config.js"); |
| 95 | + const overrideConfigFile = fs.existsSync(eslintConfigPath) |
| 96 | + ? eslintConfigPath |
| 97 | + : true; |
| 98 | + |
| 99 | + if (overrideConfigFile !== true) { |
| 100 | + return [eslintConfigPath, undefined]; |
| 101 | + } |
| 102 | + |
| 103 | + const rnx = await import("@rnx-kit/eslint-plugin"); |
| 104 | + const config = [ |
| 105 | + ...rnx.configs.strict, |
| 106 | + ...rnx.configs.stylistic, |
| 107 | + { |
| 108 | + rules: { |
| 109 | + "@typescript-eslint/consistent-type-definitions": ["error", "type"], |
| 110 | + }, |
| 111 | + }, |
| 112 | + ]; |
| 113 | + |
| 114 | + return [true, config]; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * ESLint will try to dynamically import a formatter and fail. We bundle our |
| 119 | + * own formatter to bypass this. |
| 120 | + */ |
| 121 | + private async loadFormatter() { |
| 122 | + const { default: format } = await import("eslint-formatter-pretty"); |
| 123 | + return { format }; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// eslint-disable-next-line no-restricted-exports |
| 128 | +export default { |
| 129 | + name: "@rnx-kit/yarn-plugin-eslint", |
| 130 | + commands: [Lint], |
| 131 | +}; |
0 commit comments