-
Notifications
You must be signed in to change notification settings - Fork 142
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
4fe8fbc
commit 574b45d
Showing
8 changed files
with
180 additions
and
13 deletions.
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,77 @@ | ||
const commandExists = require("../../vendor/command-exists"); | ||
const { run } = require("../utils/action"); | ||
|
||
const PARSE_REGEX = /^(.*):([0-9]+):[0-9]+: \w+: \((\w+)\) (.*)\.$/gm; | ||
|
||
/** | ||
* https://github.com/nicklockwood/SwiftFormat | ||
*/ | ||
class SwiftFormat { | ||
static get name() { | ||
return "SwiftFormat"; | ||
} | ||
|
||
/** | ||
* Verifies that all required programs are installed. Exits the GitHub action if one of the | ||
* programs is missing | ||
* | ||
* @param {string} dir: Directory to run the linting program in | ||
*/ | ||
static async verifySetup(dir) { | ||
// Verify that SwiftFormat is installed | ||
if (!(await commandExists("swiftformat"))) { | ||
throw new Error(`${this.name} is not installed`); | ||
} | ||
} | ||
|
||
/** | ||
* Runs the linting program and returns the command output | ||
* | ||
* @param {string} dir: Directory to run the linting program in | ||
* @param {string[]} extensions: Array of file extensions which should be linted | ||
* @param {boolean} fix: Whether the linter should attempt to fix code style issues automatically | ||
* @returns {string}: Results of the linting process | ||
*/ | ||
static lint(dir, extensions, fix = false) { | ||
if (extensions.length !== 1 || extensions[0] !== "swift") { | ||
throw new Error(`${this.name} error: File extensions are not configurable`); | ||
} | ||
|
||
return run(`swiftformat ${fix ? "" : "--lint"} "."`, { | ||
dir, | ||
ignoreErrors: true, | ||
}).stderr; | ||
} | ||
|
||
/** | ||
* Parses the results of the linting process and returns it as a processable array | ||
* | ||
* @param {string} dir: Directory in which the linting program has been run | ||
* @param {string} results: Results of the linting process | ||
* @returns {object[]}: Parsed results | ||
*/ | ||
static parseResults(dir, results) { | ||
const matches = results.matchAll(PARSE_REGEX); | ||
|
||
// Parsed results: [notices, warnings, failures] | ||
const resultsParsed = [[], [], []]; | ||
|
||
for (const match of matches) { | ||
const [_, pathFull, line, rule, message] = match; | ||
const path = pathFull.substring(dir.length + 1); | ||
const lineNr = parseInt(line, 10); | ||
// SwiftFormat only seems to use the "warning" level, which this action will therefore | ||
// categorize as errors | ||
resultsParsed[2].push({ | ||
path, | ||
firstLine: lineNr, | ||
lastLine: lineNr, | ||
message: `${message} (${rule})`, | ||
}); | ||
} | ||
|
||
return resultsParsed; | ||
} | ||
} | ||
|
||
module.exports = SwiftFormat; |
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,65 @@ | ||
const { join } = require("path"); | ||
const SwiftFormat = require("../../../src/linters/swiftformat"); | ||
|
||
const testName = "swiftformat"; | ||
const linter = SwiftFormat; | ||
const extensions = ["swift"]; | ||
|
||
// Testing input/output for the Linter.lint function, with auto-fixing disabled | ||
function getLintParams(dir) { | ||
const warning1 = `${join( | ||
dir, | ||
"file1.swift", | ||
)}:3:1: warning: (consecutiveBlankLines) Replace consecutive blank lines with a single blank line.`; | ||
const warning2 = `${join( | ||
dir, | ||
"file1.swift", | ||
)}:7:1: warning: (indent) Indent code in accordance with the scope level.`; | ||
const warning3 = `${join(dir, "file2.swift")}:2:1: warning: (semicolons) Remove semicolons.`; | ||
return { | ||
// Strings that must be contained in the stdout of the lint command | ||
stdoutParts: [warning1, warning2, warning3], | ||
// Example output of the lint command, used to test the parsing function | ||
parseInput: `Running SwiftFormat...\n(lint mode - no files will be changed.)\n${warning1}\n${warning2}\n${warning3}\nwarning: No swift version was specified, so some formatting features were disabled. Specify the version of swift you are using with the --swiftversion command line option, or by adding a .swift-version file to your project.\nSwiftFormat completed in 0.01s.\nSource input did not pass lint check.\n2/2 files require formatting.`, | ||
// Expected output of the parsing function | ||
parseResult: [ | ||
[], | ||
[], | ||
[ | ||
{ | ||
path: "file1.swift", | ||
firstLine: 3, | ||
lastLine: 3, | ||
message: | ||
"Replace consecutive blank lines with a single blank line (consecutiveBlankLines)", | ||
}, | ||
{ | ||
path: "file1.swift", | ||
firstLine: 7, | ||
lastLine: 7, | ||
message: "Indent code in accordance with the scope level (indent)", | ||
}, | ||
{ | ||
path: "file2.swift", | ||
firstLine: 2, | ||
lastLine: 2, | ||
message: "Remove semicolons (semicolons)", | ||
}, | ||
], | ||
], | ||
}; | ||
} | ||
|
||
// Testing input/output for the Linter.lint function, with auto-fixing enabled | ||
function getFixParams(dir) { | ||
return { | ||
// stdout of the lint command | ||
stdout: "", | ||
// Example output of the lint command, used to test the parsing function | ||
parseInput: "", | ||
// Expected output of the parsing function | ||
parseResult: [[], [], []], | ||
}; | ||
} | ||
|
||
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,10 @@ | ||
let str = "world" | ||
|
||
// "consecutiveBlankLines" warning | ||
|
||
|
||
func main() { | ||
print("hello \(str)") // "indent" warning | ||
} | ||
|
||
main() |
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,2 @@ | ||
// "semicolons" warning | ||
print("hello \(str)"); |