diff --git a/README.md b/README.md index df16ee4..8c66f6a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index f3f1ce8..e3c797b 100644 --- a/action.yml +++ b/action.yml @@ -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" diff --git a/dist/index.js b/dist/index.js index c4c19e8..74e822d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -26176,7 +26176,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.EslintResult = void 0; +exports.ESLintResult = void 0; const core = __importStar(__nccwpck_require__(2186)); const git_utils_1 = __nccwpck_require__(5601); var Severity; @@ -26184,7 +26184,7 @@ var Severity; Severity[Severity["Warning"] = 1] = "Warning"; Severity[Severity["Error"] = 2] = "Error"; })(Severity || (Severity = {})); -class EslintResult { +class ESLintResult { relevantWarningCount = 0; relevantErrorCount = 0; resultObject; @@ -26192,7 +26192,7 @@ class EslintResult { relevantMessages = []; static async for(resultObject, compareSha) { const changeRanges = await (0, git_utils_1.generateChangeRanges)(resultObject.filePath, compareSha); - return new EslintResult(resultObject, changeRanges); + return new ESLintResult(resultObject, changeRanges); } constructor(resultObject, changeRanges) { this.resultObject = resultObject; @@ -26247,7 +26247,7 @@ class EslintResult { return this.resultObject.filePath.replace(absoluteFolderPath, ""); } } -exports.EslintResult = EslintResult; +exports.ESLintResult = ESLintResult; /***/ }), @@ -26363,13 +26363,21 @@ async function run() { changedFiles = await (0, git_utils_1.detectChangedFiles)(compareSha); } core.debug(`Changed files: ${changedFiles}`); - let { stdout: eslintOut, exitCode } = await (0, exec_1.getExecOutput)("npx eslint --format=json", 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 (0, exec_1.getExecOutput)("npx eslint --format=json", 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 = eslintJson.map((resultObject) => eslint_result_1.EslintResult.for(resultObject, compareSha)); + let promises = eslintJson.map((resultObject) => eslint_result_1.ESLintResult.for(resultObject, compareSha)); let eslintResults = await Promise.all(promises); core.debug("Eslint results ->"); eslintResults.forEach((result) => core.debug(JSON.stringify(result))); diff --git a/src/eslint_result.ts b/src/eslint_result.ts index 281cfb1..62c7b4e 100644 --- a/src/eslint_result.ts +++ b/src/eslint_result.ts @@ -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 @@ -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 { + ): Promise { const changeRanges = await generateChangeRanges( resultObject.filePath, compareSha, ) - return new EslintResult(resultObject, changeRanges) + return new ESLintResult(resultObject, changeRanges) } constructor(resultObject: ResultObject, changeRanges: ChangeRange[]) { diff --git a/src/index.ts b/src/index.ts index b13f017..cd80156 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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") @@ -34,9 +34,22 @@ 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 }, @@ -44,8 +57,8 @@ async function run() { let eslintJson = JSON.parse(eslintOut) core.debug(`Eslint exit code: ${exitCode}`) - let promises: Array> = eslintJson.map( - (resultObject: ResultObject) => EslintResult.for(resultObject, compareSha), + let promises: Array> = eslintJson.map( + (resultObject: ResultObject) => ESLintResult.for(resultObject, compareSha), ) let eslintResults = await Promise.all(promises) diff --git a/test/pull_request_event_payload.json b/test/pull_request_event_payload.json index dde140b..904c5b5 100644 --- a/test/pull_request_event_payload.json +++ b/test/pull_request_event_payload.json @@ -1 +1 @@ -{ "pull_request": { "base": { "sha": "c2eaebd173390cb17efa35fce4d4941ccd5b3b5c" } } } +{ "pull_request": { "base": { "sha": "54de0dbcb922b6544025d1604d46146fcbdbda81" } } } diff --git a/test/snapshots/updates/some_file_that_eslint_should_ignore b/test/snapshots/updates/some_file_that_eslint_should_ignore index e69de29..27921aa 100644 --- a/test/snapshots/updates/some_file_that_eslint_should_ignore +++ b/test/snapshots/updates/some_file_that_eslint_should_ignore @@ -0,0 +1,4 @@ +class EslintShouldIgnoreThis + def evenThoughItsWeird (*) + end + end diff --git a/test/v6/some_file_that_eslint_should_ignore b/test/v6/some_file_that_eslint_should_ignore index e69de29..27921aa 100644 --- a/test/v6/some_file_that_eslint_should_ignore +++ b/test/v6/some_file_that_eslint_should_ignore @@ -0,0 +1,4 @@ +class EslintShouldIgnoreThis + def evenThoughItsWeird (*) + end + end diff --git a/test/v7/some_file_that_eslint_should_ignore b/test/v7/some_file_that_eslint_should_ignore index e69de29..27921aa 100644 --- a/test/v7/some_file_that_eslint_should_ignore +++ b/test/v7/some_file_that_eslint_should_ignore @@ -0,0 +1,4 @@ +class EslintShouldIgnoreThis + def evenThoughItsWeird (*) + end + end diff --git a/test/v8/some_file_that_eslint_should_ignore b/test/v8/some_file_that_eslint_should_ignore index e69de29..27921aa 100644 --- a/test/v8/some_file_that_eslint_should_ignore +++ b/test/v8/some_file_that_eslint_should_ignore @@ -0,0 +1,4 @@ +class EslintShouldIgnoreThis + def evenThoughItsWeird (*) + end + end diff --git a/test/v9/some_file_that_eslint_should_ignore b/test/v9/some_file_that_eslint_should_ignore index e69de29..27921aa 100644 --- a/test/v9/some_file_that_eslint_should_ignore +++ b/test/v9/some_file_that_eslint_should_ignore @@ -0,0 +1,4 @@ +class EslintShouldIgnoreThis + def evenThoughItsWeird (*) + end + end