-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemstone-linter-ts.js
58 lines (52 loc) · 2.29 KB
/
gemstone-linter-ts.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
/*
** 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 fs = require("mz/fs")
const path = require("path")
const TSLint = require("tslint")
/* exported API function */
module.exports = async function (filenames, opts = {}, report = { sources: {}, findings: [] }) {
/* setup TSLint CLI engine */
const rules = {}
Object.assign(rules, opts.rules)
const linter = new TSLint.Linter({})
const linterConfigFile = require.resolve("gemstone-config-tslint/tslint.json")
/* interate over all source files */
let passed = true
if (typeof opts.progress === "function")
opts.progress(0.0, "linting TS: starting")
for (let i = 0; i < filenames.length; i++) {
/* indicate progress */
if (typeof opts.progress === "function")
opts.progress(i / filenames.length, `linting TS: ${filenames[i]}`)
/* execute TSLint on given source file */
const content = await fs.readFile(filenames[i], "utf8")
const configuration = TSLint.Configuration.findConfiguration(linterConfigFile, filenames[i]).results
linter.lint(filenames[i], content, configuration)
const result = linter.getResult()
/* report linting results */
if (result.errorCount > 0 || result.warningCount > 0) {
passed = false
result.failures.forEach((failure) => {
const filename = path.relative(process.cwd(), failure.fileName)
report.sources[filename] = failure.rawLines
const [ ruleProc, ruleId ] = [ "tslint", failure.ruleName ]
report.findings.push({
ctx: "TS",
filename: filename,
line: failure.startPosition.lineAndCharacter.line + 1,
column: failure.startPosition.lineAndCharacter.character + 1,
message: failure.failure,
ruleProc: ruleProc,
ruleId: ruleId
})
})
}
}
if (typeof opts.progress === "function")
opts.progress(1.0, "")
return passed
}