tslint.js
5.32 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"use strict";
var ts = require("typescript");
var configuration_1 = require("./configuration");
var enableDisableRules_1 = require("./enableDisableRules");
var formatterLoader_1 = require("./formatterLoader");
var typedRule_1 = require("./language/rule/typedRule");
var utils_1 = require("./language/utils");
var ruleLoader_1 = require("./ruleLoader");
var utils_2 = require("./utils");
var Linter = (function () {
function Linter(fileName, source, options, program) {
this.fileName = fileName;
this.source = source;
this.program = program;
this.options = this.computeFullOptions(options);
}
Linter.createProgram = function (configFile, projectDirectory) {
if (projectDirectory === undefined) {
var lastSeparator = configFile.lastIndexOf("/");
if (lastSeparator < 0) {
projectDirectory = ".";
}
else {
projectDirectory = configFile.substring(0, lastSeparator + 1);
}
}
var config = ts.readConfigFile(configFile, ts.sys.readFile).config;
var parsed = ts.parseJsonConfigFileContent(config, { readDirectory: ts.sys.readDirectory }, projectDirectory);
var host = ts.createCompilerHost(parsed.options, true);
var program = ts.createProgram(parsed.fileNames, parsed.options, host);
return program;
};
Linter.getFileNames = function (program) {
return program.getSourceFiles().map(function (s) { return s.fileName; }).filter(function (l) { return l.substr(-5) !== ".d.ts"; });
};
Linter.prototype.lint = function () {
var failures = [];
var sourceFile;
if (this.program) {
sourceFile = this.program.getSourceFile(this.fileName);
if (!("resolvedModules" in sourceFile)) {
throw new Error("Program must be type checked before linting");
}
}
else {
sourceFile = utils_1.getSourceFile(this.fileName, this.source);
}
if (sourceFile === undefined) {
throw new Error("Invalid source file: " + this.fileName + ". Ensure that the files supplied to lint have a .ts or .tsx extension.");
}
var rulesWalker = new enableDisableRules_1.EnableDisableRulesWalker(sourceFile, {
disabledIntervals: [],
ruleName: "",
});
rulesWalker.walk(sourceFile);
var enableDisableRuleMap = rulesWalker.enableDisableRuleMap;
var rulesDirectories = this.options.rulesDirectory;
var configuration = this.options.configuration.rules;
var configuredRules = ruleLoader_1.loadRules(configuration, enableDisableRuleMap, rulesDirectories);
var enabledRules = configuredRules.filter(function (r) { return r.isEnabled(); });
for (var _i = 0, enabledRules_1 = enabledRules; _i < enabledRules_1.length; _i++) {
var rule = enabledRules_1[_i];
var ruleFailures = [];
if (this.program && rule instanceof typedRule_1.TypedRule) {
ruleFailures = rule.applyWithProgram(sourceFile, this.program);
}
else {
ruleFailures = rule.apply(sourceFile);
}
for (var _a = 0, ruleFailures_1 = ruleFailures; _a < ruleFailures_1.length; _a++) {
var ruleFailure = ruleFailures_1[_a];
if (!this.containsRule(failures, ruleFailure)) {
failures.push(ruleFailure);
}
}
}
var formatter;
var formattersDirectory = configuration_1.getRelativePath(this.options.formattersDirectory);
var Formatter = formatterLoader_1.findFormatter(this.options.formatter, formattersDirectory);
if (Formatter) {
formatter = new Formatter();
}
else {
throw new Error("formatter '" + this.options.formatter + "' not found");
}
var output = formatter.format(failures);
return {
failureCount: failures.length,
failures: failures,
format: this.options.formatter,
output: output,
};
};
Linter.prototype.containsRule = function (rules, rule) {
return rules.some(function (r) { return r.equals(rule); });
};
Linter.prototype.computeFullOptions = function (options) {
if (options === void 0) { options = {}; }
if (typeof options !== "object") {
throw new Error("Unknown Linter options type: " + typeof options);
}
var configuration = options.configuration, formatter = options.formatter, formattersDirectory = options.formattersDirectory, rulesDirectory = options.rulesDirectory;
return {
configuration: configuration || configuration_1.DEFAULT_CONFIG,
formatter: formatter || "prose",
formattersDirectory: formattersDirectory,
rulesDirectory: utils_2.arrayify(rulesDirectory).concat(utils_2.arrayify(configuration.rulesDirectory)),
};
};
Linter.VERSION = "3.15.1";
Linter.findConfiguration = configuration_1.findConfiguration;
Linter.findConfigurationPath = configuration_1.findConfigurationPath;
Linter.getRulesDirectories = configuration_1.getRulesDirectories;
Linter.loadConfigurationFromPath = configuration_1.loadConfigurationFromPath;
return Linter;
}());
module.exports = Linter;