Skip to content

Commit

Permalink
fix(config): include eslintrc.rules to validation
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 2, 2020
1 parent 07ac720 commit 556a09f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/config/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default, resolveConfigurationLocation } from './config';
export { validateEslintrcRules } from './validator';
39 changes: 39 additions & 0 deletions lib/config/validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk';
import { ESLint } from 'eslint';

import { Config, ResultParser } from './types';

Expand All @@ -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 {
Expand Down Expand Up @@ -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<void> {
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);
}
}
5 changes: 4 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,6 +23,9 @@ async function main() {
}
}

// Validate given ESLint rules
await validateEslintrcRules(config);

// Clear possible earlier results / initialize results folder
clearResults();

Expand Down

0 comments on commit 556a09f

Please sign in to comment.