-
Notifications
You must be signed in to change notification settings - Fork 139
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
Showing
10 changed files
with
199 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,91 @@ | ||
const { sep } = require("path"); | ||
|
||
const { run } = require("../utils/action"); | ||
const commandExists = require("../utils/command-exists"); | ||
const { initLintResult } = require("../utils/lint-result"); | ||
const { capitalizeFirstLetter } = require("../utils/string"); | ||
|
||
const PARSE_REGEX = /^(.*):([0-9]+):[0-9]+: (\w*) (.*)$/gm; | ||
|
||
/** @typedef {import('../utils/lint-result').LintResult} LintResult */ | ||
|
||
/** | ||
* https://beta.ruff.rs | ||
*/ | ||
class Ruff { | ||
static get name() { | ||
return "Ruff"; | ||
} | ||
|
||
/** | ||
* 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 Python is installed (required to execute Ruff) | ||
if (!(await commandExists("python"))) { | ||
throw new Error("Python is not installed"); | ||
} | ||
|
||
// Verify that Ruff is installed | ||
try { | ||
run(`${prefix} ruff --version`, { 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 = "", fix = false, prefix = "") { | ||
if (extensions.length !== 1 || extensions[0] !== "py") { | ||
throw new Error(`${this.name} error: File extensions are not configurable`); | ||
} | ||
const fixArg = fix ? "--fix-only --exit-non-zero-on-fix" : ""; | ||
return run(`${prefix} ruff check --quiet ${fixArg} ${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; | ||
|
||
const matches = output.stdout.matchAll(PARSE_REGEX); | ||
for (const match of matches) { | ||
const [_, pathFull, line, rule, 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); | ||
lintResult.error.push({ | ||
path, | ||
firstLine: lineNr, | ||
lastLine: lineNr, | ||
message: `${capitalizeFirstLetter(text)} (${rule})`, | ||
}); | ||
} | ||
|
||
return lintResult; | ||
} | ||
} | ||
|
||
module.exports = Ruff; |
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,67 @@ | ||
const { EOL } = require("os"); | ||
|
||
const Ruff = require("../../../src/linters/ruff"); | ||
|
||
const testName = "ruff"; | ||
const linter = Ruff; | ||
const args = ""; | ||
const commandPrefix = ""; | ||
const extensions = ["py"]; | ||
|
||
// Linting without auto-fixing | ||
function getLintParams(dir) { | ||
const stdoutFile1 = `file1.py:3:8: F401 [*] \`os\` imported but unused`; | ||
const stdoutFile2 = `file2.py:1:4: F821 Undefined name \`a\`${EOL}file2.py:1:5: E701 Multiple statements on one line (colon)`; | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 1, | ||
stdoutParts: [stdoutFile1, stdoutFile2], | ||
stdout: `${stdoutFile1}${EOL}${stdoutFile2}`, | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: false, | ||
warning: [], | ||
error: [ | ||
{ | ||
path: "file1.py", | ||
firstLine: 3, | ||
lastLine: 3, | ||
message: "[*] `os` imported but unused (F401)", | ||
}, | ||
{ | ||
path: "file2.py", | ||
firstLine: 1, | ||
lastLine: 1, | ||
message: "Undefined name `a` (F821)", | ||
}, | ||
{ | ||
path: "file2.py", | ||
firstLine: 1, | ||
lastLine: 1, | ||
message: "Multiple statements on one line (colon) (E701)", | ||
}, | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
// Linting with auto-fixing | ||
function getFixParams(dir) { | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 1, | ||
stdout: "", | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: false, | ||
warning: [], | ||
error: [], | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = [testName, linter, commandPrefix, extensions, args, 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,8 @@ | ||
from typing import List | ||
|
||
import os | ||
|
||
|
||
def sum_even_numbers(numbers: List[int]) -> int: | ||
"""Given a list of integers, return the sum of all even numbers in the list.""" | ||
return sum(num for num in numbers if num % 2 == 0) |
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 @@ | ||
if a: a = False |
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 @@ | ||
ruff>=0.0.257 |