From 556a09f30b598a5a14658d253c1f29e884d86a7f Mon Sep 17 00:00:00 2001 From: Ari Perkkio Date: Mon, 30 Nov 2020 19:27:15 +0200 Subject: [PATCH] fix(config): include eslintrc.rules to validation --- lib/config/index.ts | 1 + lib/config/validator.ts | 39 +++++++++++++++++++++++++++++++++++++++ lib/index.ts | 5 ++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/config/index.ts b/lib/config/index.ts index a2b29ab2..4e5b3e97 100644 --- a/lib/config/index.ts +++ b/lib/config/index.ts @@ -1 +1,2 @@ export { default, resolveConfigurationLocation } from './config'; +export { validateEslintrcRules } from './validator'; diff --git a/lib/config/validator.ts b/lib/config/validator.ts index f9ef0938..7aa504d6 100644 --- a/lib/config/validator.ts +++ b/lib/config/validator.ts @@ -1,4 +1,5 @@ import chalk from 'chalk'; +import { ESLint } from 'eslint'; import { Config, ResultParser } from './types'; @@ -8,6 +9,8 @@ const DEFAULT_RESULT_PARSER_CI: ResultParser = 'plaintext'; const DEFAULT_CONCURRENT_TASKS = 5; const DEFAULT_MAX_FILE_SIZE_BYTES = 2000000; +const UNKNOWN_RULE_REGEXP = /^Definition for rule (.*) was not found.$/; + export default function constructAndValidateConfiguration( configToValidate: Config ): Config { @@ -112,3 +115,39 @@ export default function constructAndValidateConfiguration( return config; } + +/** + * Validate given rules of `config.eslintrc.rules` + * - When unknown rules are defined, or known ones are mispelled they are not + * reported during linting. We need to specifically look for them. + * - Separate method from `constructAndValidateConfiguration` due to async + * implementation of `linter.lintText` + */ +export async function validateEslintrcRules(config: Config): Promise { + const linter = new ESLint({ + useEslintrc: false, + overrideConfig: config.eslintrc, + }); + + const results = await linter.lintText(''); + const errors = []; + + for (const result of results) { + for (const resultMessage of result.messages) { + if (UNKNOWN_RULE_REGEXP.test(resultMessage.message)) { + errors.push(resultMessage.message); + } + } + } + + if (errors.length) { + console.log( + chalk.red( + `Configuration validation errors at eslintrc.rules: \n- ${errors.join( + '\n- ' + )}` + ) + ); + process.exit(1); + } +} diff --git a/lib/index.ts b/lib/index.ts index f7532e72..6bbfa16f 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node import { renderApplication } from '@ui'; -import config from '@config'; +import config, { validateEslintrcRules } from '@config'; import engine, { WorkerMessage } from '@engine'; import { writeResults, clearResults } from '@file-client'; import logger from '@progress-logger'; @@ -23,6 +23,9 @@ async function main() { } } + // Validate given ESLint rules + await validateEslintrcRules(config); + // Clear possible earlier results / initialize results folder clearResults();