1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const fs = require("fs").promises;
|
9 | const readdirSync = require("fs").readdirSync;
|
10 | const tslint = require("tslint");
|
11 | const SEVERITY = require("../severity");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const configure = function () {
|
19 | let config = {};
|
20 | for (const file of readdirSync(".")) {
|
21 | if ("tslint.json" === file) {
|
22 | config = "../" + file;
|
23 | break;
|
24 | }
|
25 | }
|
26 | return {
|
27 | "patterns": "*.ts",
|
28 | "linters": { "tslint": config }
|
29 | };
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const wrapper = async function (file, level, options) {
|
43 | if (SEVERITY.ERROR > level) {
|
44 | return [];
|
45 | }
|
46 |
|
47 | const source = await fs.readFile(file, "utf-8");
|
48 | const configuration = tslint.Configuration.parseConfigFile(options);
|
49 | const linter = new tslint.Linter({ "fix": false });
|
50 | linter.lint(file, source, configuration);
|
51 | return linter.getResult().failures
|
52 | .map((result) => {
|
53 | const severity = "warning" === result.getRuleSeverity()
|
54 | ? SEVERITY.WARN
|
55 | : SEVERITY.ERROR;
|
56 | const start = result.getStartPosition().getLineAndCharacter();
|
57 | const end = result.getEndPosition().getLineAndCharacter();
|
58 |
|
59 | return {
|
60 | "file": file,
|
61 | "linter": "tslint",
|
62 | "rule": result.getRuleName(),
|
63 | "severity": severity,
|
64 | "message": result.getFailure(),
|
65 | "locations": [{
|
66 |
|
67 |
|
68 | "line": start.line + 1,
|
69 | "column": start.character + 1,
|
70 | "lineEnd": end.line + 1,
|
71 | "columnEnd": end.character + 1
|
72 | }]
|
73 | };
|
74 | }).filter((n) => level >= n.severity);
|
75 | };
|
76 |
|
77 | module.exports = { configure, wrapper };
|