Skip to content

Commit

Permalink
Add [linter]_args option
Browse files Browse the repository at this point in the history
Closes #11
  • Loading branch information
samuelmeuli committed Jan 8, 2020
1 parent 388ac8f commit 8209f87
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 66 deletions.
100 changes: 70 additions & 30 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,131 +18,171 @@ inputs:
description: Enable or disable stylelint checks
required: false
default: false
stylelint_extensions:
description: Extensions of files to check with stylelint
stylelint_args:
description: Additional arguments to pass to the linter
required: false
default: "css,sass,scss"
default: ""
stylelint_dir:
description: Directory where the stylelint command should be run
required: false
stylelint_extensions:
description: Extensions of files to check with stylelint
required: false
default: "css,sass,scss"

# Go

gofmt:
description: Enable or disable gofmt checks
required: false
default: false
gofmt_extensions:
description: Extensions of files to check with gofmt
gofmt_args:
description: Additional arguments to pass to the linter
required: false
default: "go"
default: ""
gofmt_dir:
description: Directory where the gofmt command should be run
required: false
gofmt_extensions:
description: Extensions of files to check with gofmt
required: false
default: "go"

golint:
description: Enable or disable golint checks
required: false
default: false
golint_extensions:
description: Extensions of files to check with golint
golint_args:
description: Additional arguments to pass to the linter
required: false
default: "go"
default: ""
golint_dir:
description: Directory where the golint command should be run
required: false
golint_extensions:
description: Extensions of files to check with golint
required: false
default: "go"

# JavaScript

eslint:
description: Enable or disable ESLint checks
required: false
default: false
eslint_extensions:
description: Extensions of files to check with ESLint
eslint_args:
description: Additional arguments to pass to the linter
required: false
default: "js"
default: ""
eslint_dir:
description: Directory where the ESLint command should be run
required: false
eslint_extensions:
description: Extensions of files to check with ESLint
required: false
default: "js"

prettier:
description: Enable or disable Prettier checks
required: false
default: false
prettier_extensions:
description: Extensions of files to check with Prettier
prettier_args:
description: Additional arguments to pass to the linter
required: false
default: "css,html,js,json,jsx,md,sass,scss,ts,tsx,vue,yaml,yml"
default: ""
prettier_dir:
description: Directory where the Prettier command should be run
required: false
prettier_extensions:
description: Extensions of files to check with Prettier
required: false
default: "css,html,js,json,jsx,md,sass,scss,ts,tsx,vue,yaml,yml"

# Python

black:
description: Enable or disable Black checks
required: false
default: false
black_extensions:
description: Extensions of files to check with Black
black_args:
description: Additional arguments to pass to the linter
required: false
default: "py"
default: ""
black_dir:
description: Directory where the Black command should be run
required: false
black_extensions:
description: Extensions of files to check with Black
required: false
default: "py"

flake8:
description: Enable or disable Flake8 checks
required: false
default: false
flake8_extensions:
description: Extensions of files to check with Flake8
flake8_args:
description: Additional arguments to pass to the linter
required: false
default: "py"
default: ""
flake8_dir:
description: Directory where the Flake8 command should be run
required: false
flake8_extensions:
description: Extensions of files to check with Flake8
required: false
default: "py"

# Ruby

rubocop:
description: Enable or disable RuboCop checks
required: false
default: false
rubocop_extensions:
description: Extensions of files to check with RuboCop
rubocop_args:
description: Additional arguments to pass to the linter
required: false
default: "rb"
default: ""
rubocop_dir:
description: Directory where the RuboCop command should be run
required: false
rubocop_extensions:
description: Extensions of files to check with RuboCop
required: false
default: "rb"

# Swift

swiftformat:
description: Enable or disable SwiftFormat checks
required: false
default: false
swiftformat_extensions:
description: Extensions of files to check with SwiftFormat
swiftformat_args:
description: Additional arguments to pass to the linter
required: false
default: "swift"
default: ""
swiftformat_dir:
description: Directory where the SwiftFormat command should be run
required: false
swiftformat_extensions:
description: Extensions of files to check with SwiftFormat
required: false
default: "swift"

swiftlint:
description: Enable or disable SwiftLint checks
required: false
default: false
swiftlint_extensions:
description: Extensions of files to check with SwiftLint
swiftlint_args:
description: Additional arguments to pass to the linter
required: false
default: "swift"
default: ""
swiftlint_dir:
description: Directory where the SwiftLint command should be run
required: false
swiftlint_extensions:
description: Extensions of files to check with SwiftLint
required: false
default: "swift"

runs:
using: node12
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async function runAction() {
// Determine whether the linter should be executed on the commit
if (getInput(linterId) === "true") {
const fileExtensions = getInput(`${linterId}_extensions`, true);
const args = getInput(`${linterId}_args`) || "";
const lintDirRel = getInput(`${linterId}_dir`) || ".";
const lintDirAbs = join(context.workspace, lintDirRel);

Expand All @@ -57,7 +58,7 @@ async function runAction() {
log(
`Linting ${autoFix ? "and auto-fixing " : ""}files in ${lintDirAbs} with ${linter.name}…`,
);
const lintOutput = linter.lint(lintDirAbs, fileExtList, autoFix);
const lintOutput = linter.lint(lintDirAbs, fileExtList, args, autoFix);

// Parse output of linting command
const lintResult = linter.parseOutput(context.workspace, lintOutput);
Expand Down
16 changes: 8 additions & 8 deletions src/linters/black.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ class Black {
* 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
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command
*/
static lint(dir, extensions, fix = false) {
return run(
`black ${fix ? "" : "--check --diff"} --include "^.*\\.(${extensions.join("|")})$" "."`,
{
dir,
ignoreErrors: true,
},
);
static lint(dir, extensions, args = "", fix = false) {
const files = `^.*\\.(${extensions.join("|")})$`;
const fixArg = fix ? "" : "--check --diff";
return run(`black ${fixArg} --include "${files}" ${args} "."`, {
dir,
ignoreErrors: true,
});
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/linters/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ class ESLint {
* 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
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command
*/
static lint(dir, extensions, fix = false) {
static lint(dir, extensions, args = "", fix = false) {
const extensionsArg = extensions.map(ext => `.${ext}`).join(",");
const fixArg = fix ? "--fix" : "";
return run(
`npx --no-install eslint --ext ${extensions.map(ext => `.${ext}`).join(",")} ${
fix ? "--fix" : ""
} --no-color --format json "."`,
`npx --no-install eslint --ext ${extensionsArg} ${fixArg} --no-color --format json ${args} "."`,
{
dir,
ignoreErrors: true,
Expand Down
7 changes: 5 additions & 2 deletions src/linters/flake8.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ class Flake8 {
* 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
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command
*/
static lint(dir, extensions, fix = false) {
static lint(dir, extensions, args = "", fix = false) {
if (fix) {
log(`${this.name} does not support auto-fixing`, "warning");
}
return run(`flake8 --filename ${extensions.map(ext => `"**${sep}*.${ext}"`).join(",")}`, {

const files = extensions.map(ext => `"**${sep}*.${ext}"`).join(",");
return run(`flake8 --filename ${files} ${args}`, {
dir,
ignoreErrors: true,
});
Expand Down
6 changes: 4 additions & 2 deletions src/linters/gofmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class Gofmt {
* 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
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command
*/
static lint(dir, extensions, fix = false) {
static lint(dir, extensions, args = "", fix = false) {
if (extensions.length !== 1 || extensions[0] !== "go") {
throw new Error(`${this.name} error: File extensions are not configurable`);
}
Expand All @@ -38,7 +39,8 @@ class Gofmt {
// -e: Report all errors (not just the first 10 on different lines)
// -s: Simplify code
// -w: Write result to (source) file instead of stdout
return run(`gofmt -s ${fix ? "-w" : "-d -e"} "."`, {
const fixArg = fix ? "-w" : "-d -e";
return run(`gofmt -s ${fixArg} ${args} "."`, {
dir,
ignoreErrors: true,
});
Expand Down
5 changes: 3 additions & 2 deletions src/linters/golint.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ class Golint {
* 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
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command
*/
static lint(dir, extensions, fix = false) {
static lint(dir, extensions, args = "", fix = false) {
if (extensions.length !== 1 || extensions[0] !== "go") {
throw new Error(`${this.name} error: File extensions are not configurable`);
}
if (fix) {
log(`${this.name} does not support auto-fixing`, "warning");
}

return run(`golint -set_exit_status "."`, {
return run(`golint -set_exit_status ${args} "."`, {
dir,
ignoreErrors: true,
});
Expand Down
15 changes: 7 additions & 8 deletions src/linters/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,18 @@ class Prettier {
* 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
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command
*/
static lint(dir, extensions, fix = false) {
static lint(dir, extensions, args = "", fix = false) {
const files =
extensions.length === 1 ? `**/*.${extensions[0]}` : `**/*.{${extensions.join(",")}}`;
return run(
`npx --no-install prettier ${fix ? "--write" : "--list-different"} --no-color "${files}"`,
{
dir,
ignoreErrors: true,
},
);
const fixArg = fix ? "--write" : "--list-different";
return run(`npx --no-install prettier ${fixArg} --no-color ${args} "${files}"`, {
dir,
ignoreErrors: true,
});
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/linters/rubocop.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ class RuboCop {
* 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
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command
*/
static lint(dir, extensions, fix = false) {
static lint(dir, extensions, args = "", fix = false) {
if (extensions.length !== 1 || extensions[0] !== "rb") {
throw new Error(`${this.name} error: File extensions are not configurable`);
}

return run(`rubocop --format json ${fix ? "--auto-correct" : ""} "."`, {
const fixArg = fix ? "--auto-correct" : "";
return run(`rubocop --format json ${fixArg} ${args} "."`, {
dir,
ignoreErrors: true,
});
Expand Down
6 changes: 4 additions & 2 deletions src/linters/stylelint.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ class Stylelint {
* 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
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command
*/
static lint(dir, extensions, fix = false) {
static lint(dir, extensions, args = "", fix = false) {
const files =
extensions.length === 1 ? `**/*.${extensions[0]}` : `**/*.{${extensions.join(",")}}`;
const fixArg = fix ? "--fix" : "";
return run(
`npx --no-install stylelint --no-color --formatter json ${fix ? "--fix" : ""} "${files}"`,
`npx --no-install stylelint --no-color --formatter json ${fixArg} ${args} "${files}"`,
{
dir,
ignoreErrors: true,
Expand Down
Loading

0 comments on commit 8209f87

Please sign in to comment.