UNPKG

@code-pushup/eslint-plugin

Version:

Code PushUp plugin for detecting problems in source code using ESLint.📋

75 lines • 3.32 kB
import path from 'node:path'; import { fileExists, toArray } from '@code-pushup/utils'; const ESLINT_CONFIG_EXTENSIONS = { // https://eslint.org/docs/latest/use/configure/configuration-files#configuration-file-formats flat: ['js', 'mjs', 'cjs'], // https://eslint.org/docs/latest/use/configure/configuration-files-deprecated legacy: ['json', 'js', 'cjs', 'yml', 'yaml'], }; const ESLINT_CONFIG_NAMES = { // https://eslint.org/docs/latest/use/configure/configuration-files#configuration-file-formats flat: ['eslint.config'], // https://eslint.org/docs/latest/use/configure/configuration-files-deprecated legacy: ['.eslintrc'], }; const CP_ESLINT_CONFIG_NAMES = { flat: [ 'code-pushup.eslint.config', 'eslint.code-pushup.config', 'eslint.config.code-pushup', 'eslint.strict.config', 'eslint.config.strict', ], legacy: ['code-pushup.eslintrc', '.eslintrc.code-pushup', '.eslintrc.strict'], }; export async function findCodePushupEslintConfig(project, format) { return findProjectFile(project, { names: CP_ESLINT_CONFIG_NAMES[format], extensions: ESLINT_CONFIG_EXTENSIONS[format], }); } export async function findEslintConfig(project, format) { const options = project.targets?.['lint']?.options; return (options?.eslintConfig ?? (await findProjectFile(project, { names: ESLINT_CONFIG_NAMES[format], extensions: ESLINT_CONFIG_EXTENSIONS[format], }))); } export function getLintFilePatterns(project, format) { const options = project.targets?.['lint']?.options; // lintFilePatterns defaults to ["{projectRoot}"] - https://github.com/nrwl/nx/pull/20313 const defaultPatterns = format === 'legacy' ? `${project.root}/**/*` // files not folder needed for legacy because rules detected with ESLint.calculateConfigForFile : project.root; const patterns = options?.lintFilePatterns == null ? [defaultPatterns] : toArray(options.lintFilePatterns); if (format === 'legacy') { return [ ...patterns, // HACK: ESLint.calculateConfigForFile won't find rules included only for subsets of *.ts when globs used // so we explicitly provide additional patterns used by @code-pushup/eslint-config to ensure those rules are included // this workaround is only necessary for legacy configs (rules are detected more reliably in flat configs) `${project.root}/*.spec.ts`, // jest/* and vitest/* rules `${project.root}/*.cy.ts`, // cypress/* rules `${project.root}/*.stories.ts`, // storybook/* rules `${project.root}/.storybook/main.ts`, // storybook/no-uninstalled-addons rule ]; } return patterns; } async function findProjectFile(project, file) { // eslint-disable-next-line functional/no-loop-statements for (const name of file.names) { // eslint-disable-next-line functional/no-loop-statements for (const ext of file.extensions) { const filename = `./${project.root}/${name}.${ext}`; if (await fileExists(path.join(process.cwd(), filename))) { return filename; } } } return undefined; } //# sourceMappingURL=utils.js.map