forked from wearerequired/lint-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c8c7c2
commit f894f77
Showing
10 changed files
with
188 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const fs = require("fs"); | ||
const { sep } = require("path"); | ||
|
||
const commandExists = require("../../vendor/command-exists"); | ||
const { log, run } = require("../utils/action"); | ||
const { initLintResult } = require("../utils/lint-result"); | ||
|
||
const PARSE_REGEX = /^(.*):([0-9]+): (\w*): (.*)$/gm; | ||
|
||
/** | ||
* https://mypy.readthedocs.io/en/stable/ | ||
*/ | ||
class Mypy { | ||
static get name() { | ||
return "Mypy"; | ||
} | ||
|
||
/** | ||
* Verifies that all required programs are installed. Throws an error if programs are missing | ||
* @param {string} dir - Directory to run the linting program in | ||
*/ | ||
static async verifySetup(dir) { | ||
// Verify that Python is installed (required to execute Flake8) | ||
if (!(await commandExists("python"))) { | ||
throw new Error("Python is not installed"); | ||
} | ||
|
||
// Verify that mypy is installed | ||
if (!(await commandExists("mypy"))) { | ||
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 | ||
* @returns {{status: number, stdout: string, stderr: string}} - Output of the lint command | ||
*/ | ||
static lint(dir, extensions, args = "", fix = false) { | ||
if (fix) { | ||
log(`${this.name} does not support auto-fixing`, "warning"); | ||
} | ||
|
||
let specifiedPath = false; | ||
// Check if they passed a directory as an arg | ||
for (const arg of args.split(" ")) { | ||
if (fs.existsSync(arg)) { | ||
specifiedPath = true; | ||
break; | ||
} | ||
} | ||
let extraArgs = ""; | ||
if (!specifiedPath) { | ||
extraArgs = ` ${dir}`; | ||
} | ||
return run(`mypy ${args}${extraArgs}`, { | ||
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 {{isSuccess: boolean, warning: [], error: []}} - Parsed lint result | ||
*/ | ||
static parseOutput(dir, output) { | ||
const lintResult = initLintResult(); | ||
lintResult.isSuccess = output.status === 0; | ||
|
||
const matches = output.stdout.matchAll(PARSE_REGEX); | ||
for (const match of matches) { | ||
const [_, pathFull, line, level, text] = match; | ||
const leadingSep = `.${sep}`; | ||
let path = pathFull; | ||
if (path.startsWith(leadingSep)) { | ||
path = path.substring(2); // Remove "./" or ".\" from start of path | ||
} | ||
const lineNr = parseInt(line, 10); | ||
const result = { | ||
path, | ||
firstLine: lineNr, | ||
lastLine: lineNr, | ||
message: text, | ||
}; | ||
if (level === "error") { | ||
lintResult.error.push(result); | ||
} else if (level === "warning") { | ||
lintResult.warning.push(result); | ||
} | ||
} | ||
|
||
return lintResult; | ||
} | ||
} | ||
|
||
module.exports = Mypy; |
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,45 @@ | ||
const { EOL } = require("os"); | ||
|
||
const Mypy = require("../../../src/linters/mypy"); | ||
|
||
const testName = "mypy"; | ||
const linter = Mypy; | ||
const extensions = ["py"]; | ||
|
||
// Linting without auto-fixing | ||
function getLintParams(dir) { | ||
const stdoutPart1 = `file1.py:7: error: Dict entry 0 has incompatible type "str": "int"; expected "str": "str"`; | ||
const stdoutPart2 = `file1.py:11: error: Argument 1 to "main" has incompatible type "List[str]"; expected "str"`; | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 1, | ||
stdoutParts: [stdoutPart1, stdoutPart2], | ||
stdout: `${stdoutPart1}${EOL}${stdoutPart2}`, | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: false, | ||
warning: [], | ||
error: [ | ||
{ | ||
path: "file1.py", | ||
firstLine: 7, | ||
lastLine: 7, | ||
message: `Dict entry 0 has incompatible type "str": "int"; expected "str": "str"`, | ||
}, | ||
{ | ||
path: "file1.py", | ||
firstLine: 11, | ||
lastLine: 11, | ||
message: `Argument 1 to "main" has incompatible type "List[str]"; expected "str"`, | ||
}, | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
// Linting with auto-fixing | ||
const getFixParams = getLintParams; // Does not support auto-fixing -> option has no effect | ||
|
||
module.exports = [testName, linter, extensions, getLintParams, getFixParams]; |
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,11 @@ | ||
from file2 import helper | ||
|
||
|
||
def main(input_str: str): | ||
print(input_str) | ||
print(helper({ | ||
input_str: 42, | ||
})) | ||
|
||
|
||
main(["hello"]) |
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,5 @@ | ||
from typing import Mapping | ||
|
||
|
||
def helper(var: Mapping[str, str]): | ||
pass |
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 @@ | ||
mypy>=0.761 |