-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add deno lint
support
#728
Open
KomaKomaD
wants to merge
3
commits into
wearerequired:master
Choose a base branch
from
KomaKomaD:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,102 @@ | ||
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://docs.deno.com/runtime/manual/tools/linter | ||
*/ | ||
class DenoLint { | ||
static get name() { | ||
return "deno_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 deno is installed | ||
if (!(await commandExists("deno"))) { | ||
throw new Error("deno 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 = "", fix = false, prefix = "") { | ||
if (fix) { | ||
core.warning(`${this.name} does not support auto-fixing`); | ||
} | ||
|
||
let targets; | ||
switch (extensions.length) { | ||
case 0: | ||
targets = ""; | ||
break; | ||
case 1: | ||
if (extensions[0] === "*") { | ||
targets = ""; // all files supported by deno lint | ||
} else { | ||
targets = `./**/*.${extensions[0]}`; | ||
} | ||
break; | ||
default: | ||
targets = `./**/*.{${extensions.map((ext) => ext).join(",")}}`; | ||
break; | ||
} | ||
|
||
return run(`${prefix} deno lint ${targets} --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 new Error( | ||
`Error parsing ${this.name} JSON output: ${err.message}. Output: "${output.stdout}"`, | ||
); | ||
} | ||
|
||
for (const diagnostics of outputJson.diagnostics) { | ||
const { filename, range, code, message, hint } = diagnostics; | ||
const path = filename.substring(dir.length + 1); | ||
const entry = { | ||
path, | ||
firstLine: range.start.line, | ||
lastLine: range.end.line, | ||
message: `${removeTrailingPeriod(message)} (${code}, ${hint})`, | ||
}; | ||
lintResult.warning.push(entry); | ||
} | ||
return lintResult; | ||
} | ||
} | ||
|
||
module.exports = DenoLint; |
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,62 @@ | ||
const DenoLint = require("../../../src/linters/deno-lint"); | ||
const { joinDoubleBackslash } = require("../../test-utils"); | ||
|
||
const testName = "deno-lint"; | ||
const linter = DenoLint; | ||
const args = ""; | ||
const commandPrefex = ""; | ||
const extensions = ["*"]; | ||
|
||
// Linting without auto-fixing | ||
function getLintParams(dir) { | ||
const stdoutStr = `{ | ||
"diagnostics": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 1, | ||
"col": 31, | ||
"bytePos": 31 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"col": 35, | ||
"bytePos": 35 | ||
} | ||
}, | ||
"filename": "${joinDoubleBackslash(dir, "file.ts")}", | ||
"message": "\`arg1\` is never used", | ||
"code": "no-unused-vars", | ||
"hint": "If this is intentional, prefix it with an underscore like \`_arg1\`" | ||
} | ||
], | ||
"errors": [] | ||
}`; | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 1, | ||
stdOutParts: stdoutStr, | ||
stdout: stdoutStr, | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: false, | ||
warning: [ | ||
{ | ||
firstLine: 1, | ||
lastLine: 1, | ||
message: | ||
"`arg1` is never used (no-unused-vars, If this is intentional, prefix it with an underscore like `_arg1`)", | ||
path: "file.ts", | ||
}, | ||
], | ||
error: [], | ||
}, | ||
}; | ||
} | ||
|
||
// Linting with auto-fixing | ||
const getFixParams = getLintParams; // Does not support auto-fixing -> option has no effect | ||
|
||
module.exports = [testName, linter, commandPrefex, extensions, args, getLintParams, getFixParams]; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deno lint
checks all supported files if the[files]...
argument is not provided. This default value "*" is interpreted as not passing the[files]...
argument.