Skip to content

Commit

Permalink
Merge pull request #13 from alexandervalencia/av/support-eslint-7-api
Browse files Browse the repository at this point in the history
🆕  Feature: Add support for ESLint's new v7 API
  • Loading branch information
alexandervalencia authored Sep 14, 2022
2 parents 5b484b8 + 9701f3f commit ee016f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Feature: Support ESLint's new v7+ API (eslint.CLIEngine() has been removed as of v8)

## v0.5 (2022-03-06)

- Feature: Allow specifying lowest failure level with `failureLevel`
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ branding:
inputs:
extensions:
description: "A comma separated list of extensions to run on"
require: false
required: false
default: "js"
conclusionLevel:
description: 'Which check run conclusion type to use when annotations are created ("neutral" or "failure" are most common)'
Expand Down
42 changes: 37 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const io = require('@actions/io')
const semver = require('semver')
const { easyExec, setOutput } = require('./utils')
const { generateChangeRanges } = require('./git_utils')
const CheckRun = require('./check_run')
Expand All @@ -13,6 +14,8 @@ const {
const event = require(process.env.GITHUB_EVENT_PATH)
const checkName = 'ESLint'

let eslintVersionSevenOrGreater = null
let linter = null
let yarnOutput = null

async function getYarn () {
Expand Down Expand Up @@ -68,21 +71,49 @@ async function installEslintPackagesAsync () {
}
}

async function setupEslintVersionAndLinter () {
const eslintVersion =
require(`${GITHUB_WORKSPACE}/node_modules/eslint/package.json`).version
eslintVersionSevenOrGreater = semver.gte(eslintVersion, '7.0.0')

const eslint = require(`${GITHUB_WORKSPACE}/node_modules/eslint`)
linter = eslintVersionSevenOrGreater
? new eslint.ESLint()
: new eslint.CLIEngine()
}

function gatherReportForEslintSixOrLower (paths) {
return linter.executeOnFiles(paths)
}

async function gatherReportForEslintSevenOrGreater (paths) {
const report = await linter.lintFiles(paths)

return report.reduce(
({ results, errorCount, warningCount }, result) => ({
results: [...results, result],
errorCount: errorCount + result.errorCount,
warningCount: warningCount + result.warningCount
}),
{ results: [], errorCount: 0, warningCount: 0 }
)
}

async function runEslint () {
const compareSha = event.pull_request.base.sha

const { output } = await easyExec(
`git diff --name-only --diff-filter AM ${compareSha}`
)

const eslint = require(`${GITHUB_WORKSPACE}/node_modules/eslint`)
const cli = new eslint.CLIEngine()
const extensions = INPUT_EXTENSIONS.split(',')

const paths = output
.split('\n')
.filter(path => extensions.some(e => path.endsWith(`.${e}`)))
const report = cli.executeOnFiles(paths)

const report = eslintVersionSevenOrGreater
? await gatherReportForEslintSevenOrGreater(paths)
: gatherReportForEslintSixOrLower(paths)

const { results, errorCount, warningCount } = report

Expand All @@ -95,7 +126,7 @@ async function runEslint () {
const { filePath, messages } = result
const path = filePath.substring(GITHUB_WORKSPACE.length + 1)

if (cli.isPathIgnored(path)) continue
if (await linter.isPathIgnored(path)) continue

const changeRanges = await generateChangeRanges(path, { compareSha })

Expand Down Expand Up @@ -140,6 +171,7 @@ async function run () {
try {
process.chdir(GITHUB_WORKSPACE)
await installEslintPackagesAsync()
await setupEslintVersionAndLinter()
report = await runEslint()
} catch (e) {
report = {
Expand Down

0 comments on commit ee016f0

Please sign in to comment.