Skip to content

Commit

Permalink
Merge pull request #36 from planningcenter/ns/norubylint
Browse files Browse the repository at this point in the history
fix: Use extensions input to filter files passed to ESLint
  • Loading branch information
sheck authored Oct 10, 2024
2 parents 8c1391e + 61837c7 commit 3496f8f
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
| `failure-level` | The lowest annotation level to fail on ("warning or "error"") | no | `"error"` |
| `conclusion-level` | Action conclusion ("success" or "failure") if annotations of the failure-level were created. | no | `"success"` |
| `working-directory` | Which directory to run the action in | no | `"."` |
| `extensions` | A comma separated list of extensions to run ESLint on. | no | `".js,.ts,.jsx,.tsx,.mjs,.cjs"` |

## Outputs

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inputs:
description: Which directory to run the action in
required: false
default: "."
extensions:
description: A comma separated list of extensions to run ESLint on.
required: false
default: ".js,.ts,.jsx,.tsx,.mjs,.cjs"
outputs:
warning-count:
description: "Number of relevant warnings found"
Expand Down
20 changes: 14 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/eslint_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ChangeRange, generateChangeRanges } from "./git_utils"

export type ResultObject = {
filePath: string
messages: EslintMessage[]
messages: ESLintMessage[]
}

type EslintMessage = {
type ESLintMessage = {
ruleId: string
severity: Severity
message: string
Expand All @@ -24,22 +24,22 @@ enum Severity {
Error = 2,
}

export class EslintResult {
export class ESLintResult {
public relevantWarningCount: number = 0
public relevantErrorCount: number = 0
private resultObject: ResultObject
private changeRanges: ChangeRange[]
private relevantMessages: EslintMessage[] = []
private relevantMessages: ESLintMessage[] = []

static async for(
resultObject: ResultObject,
compareSha: string,
): Promise<EslintResult> {
): Promise<ESLintResult> {
const changeRanges = await generateChangeRanges(
resultObject.filePath,
compareSha,
)
return new EslintResult(resultObject, changeRanges)
return new ESLintResult(resultObject, changeRanges)
}

constructor(resultObject: ResultObject, changeRanges: ChangeRange[]) {
Expand Down
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from "@actions/core"
import { detectChangedFiles, detectChangedFilesInFolder } from "./git_utils"
import { getExecOutput } from "@actions/exec"
import { ResultObject, EslintResult } from "./eslint_result"
import { ResultObject, ESLintResult } from "./eslint_result"

async function run() {
let workingDirectory = core.getInput("working-directory")
Expand Down Expand Up @@ -34,18 +34,31 @@ async function run() {

core.debug(`Changed files: ${changedFiles}`)

let extensions = core.getInput("extensions").split(",")
core.debug(`Extensions: ${extensions}`)
let changedFilesMatchingExtensions = changedFiles.filter((file) =>
extensions.some((ext) => file.endsWith(ext)),
)
core.debug(
`Changed files matching extensions: ${changedFilesMatchingExtensions}`,
)

// Bail out early if the file list is empty (older ESLint versions will
// complain if the list is empty)
if (changedFilesMatchingExtensions.length === 0) return

let { stdout: eslintOut, exitCode } = await getExecOutput(
"npx eslint --format=json",
changedFiles,
changedFilesMatchingExtensions,
// Eslint will return exit code 1 if it finds linting problems, but that is
// expected and we don't want to stop execution because of it.
{ ignoreReturnCode: true },
)
let eslintJson = JSON.parse(eslintOut)
core.debug(`Eslint exit code: ${exitCode}`)

let promises: Array<Promise<EslintResult>> = eslintJson.map(
(resultObject: ResultObject) => EslintResult.for(resultObject, compareSha),
let promises: Array<Promise<ESLintResult>> = eslintJson.map(
(resultObject: ResultObject) => ESLintResult.for(resultObject, compareSha),
)
let eslintResults = await Promise.all(promises)

Expand Down
2 changes: 1 addition & 1 deletion test/pull_request_event_payload.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "pull_request": { "base": { "sha": "c2eaebd173390cb17efa35fce4d4941ccd5b3b5c" } } }
{ "pull_request": { "base": { "sha": "54de0dbcb922b6544025d1604d46146fcbdbda81" } } }
4 changes: 4 additions & 0 deletions test/snapshots/updates/some_file_that_eslint_should_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class EslintShouldIgnoreThis
def evenThoughItsWeird (*)
end
end
4 changes: 4 additions & 0 deletions test/v6/some_file_that_eslint_should_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class EslintShouldIgnoreThis
def evenThoughItsWeird (*)
end
end
4 changes: 4 additions & 0 deletions test/v7/some_file_that_eslint_should_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class EslintShouldIgnoreThis
def evenThoughItsWeird (*)
end
end
4 changes: 4 additions & 0 deletions test/v8/some_file_that_eslint_should_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class EslintShouldIgnoreThis
def evenThoughItsWeird (*)
end
end
4 changes: 4 additions & 0 deletions test/v9/some_file_that_eslint_should_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class EslintShouldIgnoreThis
def evenThoughItsWeird (*)
end
end

0 comments on commit 3496f8f

Please sign in to comment.