-
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
50f23e4
commit ddb234a
Showing
9 changed files
with
177 additions
and
2 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
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,78 @@ | ||
const commandExists = require("../../vendor/command-exists"); | ||
const { log, run } = require("../utils/action"); | ||
const { capitalizeFirstLetter } = require("../utils/string"); | ||
|
||
const PARSE_REGEX = /^(.+):([0-9]+):([0-9]+): (.+)$/gm; | ||
|
||
/** | ||
* https://github.com/golang/lint | ||
*/ | ||
class Golint { | ||
static get name() { | ||
return "golint"; | ||
} | ||
|
||
/** | ||
* 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 golint is installed | ||
if (!(await commandExists("golint"))) { | ||
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] !== "go") { | ||
throw new Error(`${this.name} error: File extensions are not configurable`); | ||
} | ||
|
||
if (fix) { | ||
log(`${this.name} does not support auto-fixing`, "warning"); | ||
} | ||
return run(`golint "."`, { | ||
dir, | ||
ignoreErrors: true, | ||
}).stdout; | ||
} | ||
|
||
/** | ||
* 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 [_str, path, line, _column, text] = match; | ||
const lineNr = parseInt(line, 10); | ||
resultsParsed[2].push({ | ||
path, | ||
firstLine: lineNr, | ||
lastLine: lineNr, | ||
message: capitalizeFirstLetter(text), | ||
}); | ||
} | ||
|
||
return resultsParsed; | ||
} | ||
} | ||
|
||
module.exports = Golint; |
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,46 @@ | ||
const Golint = require("../../../src/linters/golint"); | ||
|
||
const testName = "golint"; | ||
const linter = Golint; | ||
const extensions = ["go"]; | ||
|
||
const getLintResults = dir => { | ||
const resultsFile1 = | ||
"file1.go:14:9: if block ends with a return statement, so drop this else and outdent its block"; | ||
const resultsFile2 = | ||
'file2.go:3:1: comment on exported function Divide should be of the form "Divide ..."'; | ||
return [`${resultsFile1}\n${resultsFile2}`, `${resultsFile2}\n${resultsFile1}`]; | ||
}; | ||
|
||
const parsedLintResults = [ | ||
[], | ||
[], | ||
[ | ||
{ | ||
path: "file1.go", | ||
firstLine: 14, | ||
lastLine: 14, | ||
message: `If block ends with a return statement, so drop this else and outdent its block`, | ||
}, | ||
{ | ||
path: "file2.go", | ||
firstLine: 3, | ||
lastLine: 3, | ||
message: `Comment on exported function Divide should be of the form "Divide ..."`, | ||
}, | ||
], | ||
]; | ||
|
||
const getFixResults = getLintResults; | ||
|
||
const parsedFixResults = parsedLintResults; | ||
|
||
module.exports = [ | ||
testName, | ||
linter, | ||
extensions, | ||
getLintResults, | ||
getFixResults, | ||
parsedLintResults, | ||
parsedFixResults, | ||
]; |
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,18 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
var str = "world" | ||
|
||
func main() { | ||
fmt.Println("hello " + doSomething(str)) | ||
} | ||
|
||
func doSomething(str string) string { | ||
if str == "" { | ||
return "default" | ||
} else { | ||
// Error for unnecessary "else" statement | ||
return str | ||
} | ||
} |
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,6 @@ | ||
package main | ||
|
||
// Error for incorrect comment format | ||
func Divide(num1 int, num2 int) int { | ||
return num1 / num2 | ||
} |