-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Paint by numbers copying rubocop * Build and update comments * Add debug print for args * Only warn, and include linter rather than rules * Add erblint tests * Add erb-lint to test action * Switch to using GITHUB_TOKEN * [auto] Update compiled version * Skip most setups * Uncomment other tests * Fix linter errors * Run format:fix * [auto] Update compiled version * Cleanup code after review * [auto] Update compiled version * Add Missed Import * [auto] Update compiled version * Fix tests after swapping error for test * Revert build to use BUILD_ACTION_GITHUB_TOKEN * Remove default . arg * [auto] Update compiled version * Restore default . arg * [auto] Update compiled version * Remove arg param and lint all files with erblint * Fix arg order for erblint * [auto] Update compiled version * Make --lint all default for erblint * [auto] Update compiled version * Remove commandArgs from testing Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1cd86d9
commit 8170c90
Showing
16 changed files
with
429 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const core = require("@actions/core"); | ||
|
||
const { run } = require("../utils/action"); | ||
const commandExists = require("../utils/command-exists"); | ||
const { initLintResult } = require("../utils/lint-result"); | ||
const { removeTrailingPeriod } = require("../utils/string"); | ||
|
||
/** @typedef {import('../utils/lint-result').LintResult} LintResult */ | ||
|
||
/** | ||
* https://https://github.com/Shopify/erb-lint | ||
*/ | ||
class Erblint { | ||
static get name() { | ||
return "ERB Lint"; | ||
} | ||
|
||
/** | ||
* Verifies that all required programs are installed. Throws an error if programs are missing | ||
* @param {string} dir - Directory to run the linting program in | ||
* @param {string} prefix - Prefix to the lint command | ||
*/ | ||
static async verifySetup(dir, prefix = "") { | ||
// Verify that Ruby is installed (required to execute erblint) | ||
if (!(await commandExists("ruby"))) { | ||
throw new Error("Ruby is not installed"); | ||
} | ||
// Verify that erblint is installed | ||
try { | ||
run(`${prefix} erblint -v`, { dir }); | ||
} catch (err) { | ||
throw new Error(`${this.name} is not installed`); | ||
} | ||
} | ||
|
||
/** | ||
* Runs the linting program and returns the command output | ||
* @param {string} dir - Directory to run the linter in | ||
* @param {string[]} extensions - File extensions which should be linted | ||
* @param {string} args - Additional arguments to pass to the linter | ||
* @param {boolean} fix - Whether the linter should attempt to fix code style issues automatically | ||
* @param {string} prefix - Prefix to the lint command | ||
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command | ||
*/ | ||
static lint(dir, extensions, args = "--lint-all", fix = false, prefix = "") { | ||
if (extensions.length !== 1 || extensions[0] !== "erb") { | ||
throw new Error(`${this.name} error: File extensions are not configurable`); | ||
} | ||
if (fix) { | ||
core.warning(`${this.name} does not support auto-fixing`); | ||
} | ||
|
||
return run(`${prefix} erblint --format json ${args}`, { | ||
dir, | ||
ignoreErrors: true, | ||
}); | ||
} | ||
|
||
/** | ||
* Parses the output of the lint command. Determines the success of the lint process and the | ||
* severity of the identified code style violations | ||
* @param {string} dir - Directory in which the linter has been run | ||
* @param {{status: number, stdout: string, stderr: string}} output - Output of the lint command | ||
* @returns {LintResult} - Parsed lint result | ||
*/ | ||
static parseOutput(dir, output) { | ||
const lintResult = initLintResult(); | ||
lintResult.isSuccess = output.status === 0; | ||
|
||
let outputJson; | ||
try { | ||
outputJson = JSON.parse(output.stdout); | ||
} catch (err) { | ||
throw Error( | ||
`Error parsing ${this.name} JSON output: ${err.message}. Output: "${output.stdout}"`, | ||
); | ||
} | ||
|
||
for (const file of outputJson.files) { | ||
const { path, offenses } = file; | ||
for (const offense of offenses) { | ||
const { message, linter, corrected, location } = offense; | ||
if (!corrected) { | ||
// ERB Lint does not provide severities in its JSON output | ||
lintResult.error.push({ | ||
path, | ||
firstLine: location.start_line, | ||
lastLine: location.last_line, | ||
message: `${removeTrailingPeriod(message)} (${linter})`, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return lintResult; | ||
} | ||
} | ||
|
||
module.exports = Erblint; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.