Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process and log results from eslint 🐗
Browse files Browse the repository at this point in the history
01taylop committed Jul 17, 2024
1 parent e3e5345 commit a2bf52c
Showing 2 changed files with 37 additions and 16 deletions.
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default [{
rules: {
quotes: [2, 'single'],
semi: [2, 'never'],
},
}]
47 changes: 31 additions & 16 deletions src/linters/eslint.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { ESLint } from 'eslint'

import { Linter, type LintReport, type ReportSummary } from '@Types'
import { Linter, RuleSeverity } from '@Types'
import colourLog from '@Utils/colourLog'
import { formatResult } from '@Utils/transform'

import type { LintReport, ReportResults, ReportSummary } from '@Types'

const lintFiles = async (files: Array<string>): Promise<LintReport> => {
try {
const eslint = new ESLint({
// @ts-expect-error
overrideConfigFile: true,
overrideConfig: {
rules: {
'quotes': [2, 'single'],
'no-console': 2,
'no-ternary': 1,
'no-unused-vars': 2,
},
},
cache: false,
fix: false,
})

const results = await eslint.lintFiles(files)
const results: Array<ESLint.LintResult> = await eslint.lintFiles(files)

const reportResults: ReportResults = {}

const reportSummary: ReportSummary = {
deprecatedRules: [],
@@ -29,21 +27,38 @@ const lintFiles = async (files: Array<string>): Promise<LintReport> => {
warningCount: 0,
}

results.forEach(({ errorCount, fixableErrorCount, fixableWarningCount, usedDeprecatedRules, warningCount }) => {
results.forEach(({ errorCount, filePath, fixableErrorCount, fixableWarningCount, messages, usedDeprecatedRules, warningCount }) => {
const file = filePath.replace(`${process.cwd()}/`, '')

reportSummary.deprecatedRules = [...new Set([...reportSummary.deprecatedRules, ...usedDeprecatedRules.map(({ ruleId }) => ruleId)])]
reportSummary.errorCount += errorCount
reportSummary.fixableErrorCount += fixableErrorCount
reportSummary.fixableWarningCount += fixableWarningCount
reportSummary.warningCount += warningCount

messages.forEach(({ column, line, message, ruleId, severity }) => {
if (!reportResults[file]) {
reportResults[file] = []
}

reportResults[file].push(formatResult({
column,
lineNumber: line || 0,
message: message.trim(),
rule: ruleId || 'core-error',
severity: severity === 1 ? RuleSeverity.WARNING : RuleSeverity.ERROR,
}))
})
})

return {
results: {},
results: reportResults,
summary: reportSummary,
}
} catch (error: any) {
console.error(error.stack)
throw error
colourLog.error('An error occurred while running eslint', error)
console.log()
process.exit(1)
}
}

0 comments on commit a2bf52c

Please sign in to comment.