-
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
1 parent
ba7834b
commit a1f4ef1
Showing
12 changed files
with
1,048 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const { run } = require("../utils/action"); | ||
const commandExists = require("../utils/command-exists"); | ||
const { initLintResult } = require("../utils/lint-result"); | ||
|
||
const PARSE_REGEX = /^(.*)\(([0-9]+),([0-9]+)\): (warning|error) (.*) \[.*$/gm; | ||
|
||
/** @typedef {import('../utils/lint-result').LintResult} LintResult */ | ||
|
||
/** | ||
* https://github.com/dotnet/format | ||
*/ | ||
class DotnetFormat { | ||
static get name() { | ||
return "dotnet_format"; | ||
} | ||
|
||
/** | ||
* 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 dotnet is installed (required to execute dotnet format) | ||
if (!(await commandExists("dotnet"))) { | ||
throw new Error(".NET SDK is not installed"); | ||
} | ||
|
||
// Verify that dotnet-format is installed | ||
try { | ||
run(`${prefix} dotnet format --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] !== "cs") { | ||
throw new Error(`${this.name} error: File extensions are not configurable`); | ||
} | ||
|
||
const fixArg = fix ? "" : "--verify-no-changes"; | ||
return run(`${prefix} dotnet format ${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.stderr.matchAll(PARSE_REGEX); | ||
for (const match of matches) { | ||
const [_line, pathFull, line, _column, level, message] = match; | ||
const path = pathFull.substring(dir.length + 1); | ||
const lineNr = parseInt(line, 10); | ||
lintResult[level].push({ | ||
path, | ||
firstLine: lineNr, | ||
lastLine: lineNr, | ||
message: `${message}`, | ||
}); | ||
} | ||
|
||
return lintResult; | ||
} | ||
} | ||
|
||
module.exports = DotnetFormat; |
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,84 @@ | ||
const { EOL } = require("os"); | ||
const { join } = require("path"); | ||
|
||
const DotnetFormat = require("../../../src/linters/dotnet-format"); | ||
|
||
const testName = "dotnet-format"; | ||
const linter = DotnetFormat; | ||
const commandPrefix = ""; | ||
const extensions = ["cs"]; | ||
|
||
// Linting without auto-fixing | ||
function getLintParams(dir) { | ||
const stderrPart1 = `${join( | ||
dir, | ||
"file2.cs", | ||
)}(20,31): error WHITESPACE: Fix whitespace formatting. Delete 1 characters. [${join( | ||
dir, | ||
"dotnet-format.csproj", | ||
)}`; | ||
const stderrPart2 = `${join(dir, "file2.cs")}(1,1): error IMPORTS: Fix imports ordering. [${join( | ||
dir, | ||
"dotnet-format.csproj", | ||
)}`; | ||
const stderrPart3 = `${join( | ||
dir, | ||
"file1.cs", | ||
)}(1,1): warning IDE0073: A source file is missing a required header. [${join( | ||
dir, | ||
"dotnet-format.csproj", | ||
)}`; | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 2, | ||
stderrParts: [stderrPart1, stderrPart2, stderrPart3], | ||
stderr: `${stderrPart1}${EOL}${stderrPart2}${EOL}${stderrPart3}`, | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: false, | ||
warning: [ | ||
{ | ||
path: "file1.cs", | ||
firstLine: 1, | ||
lastLine: 1, | ||
message: `IDE0073: A source file is missing a required header.`, | ||
}, | ||
], | ||
error: [ | ||
{ | ||
path: "file2.cs", | ||
firstLine: 20, | ||
lastLine: 20, | ||
message: `WHITESPACE: Fix whitespace formatting. Delete 1 characters.`, | ||
}, | ||
{ | ||
path: "file2.cs", | ||
firstLine: 1, | ||
lastLine: 1, | ||
message: `IMPORTS: Fix imports ordering.`, | ||
}, | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
// Linting with auto-fixing | ||
function getFixParams(dir) { | ||
return { | ||
// Expected output of the linting function | ||
cmdOutput: { | ||
status: 0, | ||
stderr: ``, | ||
}, | ||
// Expected output of the parsing function | ||
lintResult: { | ||
isSuccess: true, | ||
warning: [], | ||
error: [], | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = [testName, linter, commandPrefix, extensions, getLintParams, getFixParams]; |
Oops, something went wrong.