-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemstone-linter-js.js
67 lines (61 loc) · 2.47 KB
/
gemstone-linter-js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
** GemstoneJS -- Gemstone JavaScript Technology Stack
** Copyright (c) 2016-2019 Gemstone Project <http://gemstonejs.com>
** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>
*/
/* load external requirements */
const path = require("path")
const ESLint = require("eslint")
/* exported API function */
module.exports = async function (filenames, opts = {}, report = { sources: {}, findings: [] }) {
/* setup ESLint CLI engine */
const rules = {}
if (opts.env === "development")
rules["no-console"] = "off"
Object.assign(rules, opts.rules)
const engine = new ESLint.CLIEngine({
useEslintrc: false,
configFile: require.resolve("gemstone-config-eslint/eslint.yaml"),
rules: rules
})
/* interate over all source files */
let passed = true
if (typeof opts.progress === "function")
opts.progress(0.0, "linting JS: starting")
for (let i = 0; i < filenames.length; i++) {
/* indicate progress */
if (typeof opts.progress === "function")
opts.progress(i / filenames.length, `linting JS: ${filenames[i]}`)
/* execute ESLint on given source file */
const result = engine.executeOnFiles([ filenames[i] ])
/* report linting results */
if (result.errorCount > 0 || result.warningCount > 0) {
passed = false
result.results.forEach((file) => {
const filename = path.relative(process.cwd(), file.filePath)
report.sources[filename] = file.source
file.messages.forEach((msg) => {
let [ ruleProc, ruleId ] = [ "eslint", msg.ruleId ]
let message = msg.message
if (ruleId === null && message.match(/^Parsing error:/)) {
message = message.replace(/\n(?:.|\n)*$/, "")
ruleProc = "eslint-babel"
ruleId = "*"
}
report.findings.push({
ctx: "JS",
filename: filename,
line: msg.line,
column: msg.column,
message: message,
ruleProc: ruleProc,
ruleId: ruleId
})
})
})
}
}
if (typeof opts.progress === "function")
opts.progress(1.0, "")
return passed
}