From e9e93312d2c060c78440f8f048c4dc3602f1ca63 Mon Sep 17 00:00:00 2001 From: Viktor Varland Date: Wed, 24 Apr 2019 15:05:39 +0200 Subject: [PATCH] fix: use eslint linter instead of the cli engine (#34) * fix: use linter from eslint * fix: do not report on unused ignore pragmas * fix: check on the correct prop --- src/tools/js/eslint.js | 49 ++++++++++++++++++++-------------------- src/tools/js/index.js | 12 ++++++---- src/tools/js/prettier.js | 5 +++- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/tools/js/eslint.js b/src/tools/js/eslint.js index a066baa3..74d6e14a 100644 --- a/src/tools/js/eslint.js +++ b/src/tools/js/eslint.js @@ -1,16 +1,18 @@ const path = require('path') const eslint = require('eslint') +const linter = new eslint.Linter() const log = require('@dhis2/cli-helpers-engine').reporter const { readFile, writeFile } = require('../../files.js') -const eslintConfig = path.join(__dirname, '../../../config/eslint.config.js') +const eslintConfig = require('../../../config/eslint.config.js') + log.debug('ESLint configuration file', eslintConfig) /** - * This a checker used by {run-js} and needs to follow a specific - * format. + * This a checker used by {tools/js/index.js} and needs to follow a + * specific format. * * @param {string} file File path * @param {string} text Content of File @@ -21,32 +23,31 @@ module.exports = (file, text, apply = false) => { const response = { messages: [], output: text, + fixed: false, } try { - const cli = new eslint.CLIEngine({ - configFile: eslintConfig, - useEslintrc: false, - fix: apply, - allowInlineConfig: true, - ignore: false, - }) - const report = cli.executeOnText(text) - - // when using `executeOnText` the results array always has a - // single element - const result = report.results[0] - - if (result.output) { - response.output = result.output - } - - for (const message of result.messages) { + const { messages, fixed, output } = linter.verifyAndFix( + text, + eslintConfig, + { + filename: path.basename(file), + allowInlineConfig: true, + reportUnusedDisableDirectives: false, + } + ) + + response.fixed = fixed + response.output = output + + for (const message of messages) { + let rule = '' + if (message.ruleId) { + rule = `(${message.ruleId})` + } response.messages.push({ checker: 'eslint', - message: `Line ${message.line}: ${message.message} (${ - message.ruleId - })`, + message: `Line ${message.line}: ${message.message} ${rule}`, }) } } catch (error) { diff --git a/src/tools/js/index.js b/src/tools/js/index.js index 2f364d6c..2b4619f3 100644 --- a/src/tools/js/index.js +++ b/src/tools/js/index.js @@ -26,12 +26,16 @@ function runTools(file, apply = false) { let messages = [] let source = original + let fixed = false perf.start('exec-file') for (const tool of tools) { const result = tool(file, source, apply) - source = result.output + if (result.fixed) { + source = result.output + fixed = result.fixed + } messages = messages.concat(result.messages) } @@ -40,8 +44,8 @@ function runTools(file, apply = false) { return { file, messages, + fixed, output: source, - modified: original !== source, name: path.basename(file), } } @@ -88,7 +92,6 @@ function print(report, violations) { log.info(`${report.length} file(s) checked.`) if (violations.length > 0) { - log.error(`${violations.length} file(s) violate the code standards:`) violations.forEach(f => { const p = path.relative(process.cwd(), f.file) log.info('') @@ -97,6 +100,7 @@ function print(report, violations) { }) log.info('') + log.error(`${violations.length} file(s) violate the code standard.`) } } @@ -105,7 +109,7 @@ function getViolations(report) { } function getAutoFixable(report) { - return report.filter(f => f.modified) + return report.filter(f => f.fixed) } /** diff --git a/src/tools/js/prettier.js b/src/tools/js/prettier.js index 253a18dc..8a16edcb 100644 --- a/src/tools/js/prettier.js +++ b/src/tools/js/prettier.js @@ -24,6 +24,7 @@ module.exports = (file, text, apply = false) => { const response = { messages: [], output: text, + fixed: false, } try { @@ -48,7 +49,9 @@ module.exports = (file, text, apply = false) => { }) } - if (!apply && text !== response.output) { + response.fixed = text !== response.output + + if (!apply && response.fixed) { response.messages.push({ checker: 'prettier', message: 'Not formatted according to standards.',