Skip to content

Commit

Permalink
fix: use eslint linter instead of the cli engine (#34)
Browse files Browse the repository at this point in the history
* fix: use linter from eslint

* fix: do not report on unused ignore pragmas

* fix: check on the correct prop
  • Loading branch information
varl authored Apr 24, 2019
1 parent 9880fc2 commit e9e9331
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
49 changes: 25 additions & 24 deletions src/tools/js/eslint.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
12 changes: 8 additions & 4 deletions src/tools/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -40,8 +44,8 @@ function runTools(file, apply = false) {
return {
file,
messages,
fixed,
output: source,
modified: original !== source,
name: path.basename(file),
}
}
Expand Down Expand Up @@ -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('')
Expand All @@ -97,6 +100,7 @@ function print(report, violations) {
})

log.info('')
log.error(`${violations.length} file(s) violate the code standard.`)
}
}

Expand All @@ -105,7 +109,7 @@ function getViolations(report) {
}

function getAutoFixable(report) {
return report.filter(f => f.modified)
return report.filter(f => f.fixed)
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/tools/js/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = (file, text, apply = false) => {
const response = {
messages: [],
output: text,
fixed: false,
}

try {
Expand All @@ -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.',
Expand Down

0 comments on commit e9e9331

Please sign in to comment.