1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | var tslib_1 = require("tslib");
|
20 | var fs = require("fs");
|
21 | var path = require("path");
|
22 | var error_1 = require("./error");
|
23 | var utils_1 = require("./utils");
|
24 | var CORE_RULES_DIRECTORY = path.resolve(__dirname, "rules");
|
25 | var cachedRules = new Map();
|
26 | function loadRules(ruleOptionsList, rulesDirectories, isJs) {
|
27 | if (isJs === void 0) { isJs = false; }
|
28 | var rules = [];
|
29 | var notFoundRules = [];
|
30 | var notAllowedInJsRules = [];
|
31 | for (var _i = 0, ruleOptionsList_1 = ruleOptionsList; _i < ruleOptionsList_1.length; _i++) {
|
32 | var ruleOptions = ruleOptionsList_1[_i];
|
33 | if (ruleOptions.ruleSeverity === "off") {
|
34 |
|
35 | continue;
|
36 | }
|
37 | var ruleName = ruleOptions.ruleName;
|
38 | var Rule = findRule(ruleName, rulesDirectories);
|
39 | if (Rule === undefined) {
|
40 | notFoundRules.push(ruleName);
|
41 | }
|
42 | else if (isJs && Rule.metadata !== undefined && Rule.metadata.typescriptOnly) {
|
43 | notAllowedInJsRules.push(ruleName);
|
44 | }
|
45 | else {
|
46 | var rule = new Rule(ruleOptions);
|
47 | if (rule.isEnabled()) {
|
48 | rules.push(rule);
|
49 | }
|
50 | if (Rule.metadata !== undefined && Boolean(Rule.metadata.deprecationMessage)) {
|
51 | error_1.showWarningOnce(Rule.metadata.ruleName + " is deprecated. " + Rule.metadata.deprecationMessage);
|
52 | }
|
53 | }
|
54 | }
|
55 | if (notFoundRules.length > 0) {
|
56 | var warning = utils_1.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Could not find implementations for the following rules specified in the configuration:\n ", "\n Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.\n If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.\n "], ["\n Could not find implementations for the following rules specified in the configuration:\n ", "\n Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.\n If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.\n "])), notFoundRules.join("\n "));
|
57 | error_1.showWarningOnce(warning);
|
58 | }
|
59 | if (notAllowedInJsRules.length > 0) {
|
60 | var warning = utils_1.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Following rules specified in configuration couldn't be applied to .js or .jsx files:\n ", "\n Make sure to exclude them from \"jsRules\" section of your tslint.json.\n "], ["\n Following rules specified in configuration couldn't be applied to .js or .jsx files:\n ", "\n Make sure to exclude them from \"jsRules\" section of your tslint.json.\n "])), notAllowedInJsRules.join("\n "));
|
61 | error_1.showWarningOnce(warning);
|
62 | }
|
63 | if (rules.length === 0) {
|
64 | var fileType = isJs ? "JavaScript" : "TypeScript";
|
65 | error_1.showWarningOnce("No valid rules have been specified for " + fileType + " files");
|
66 | }
|
67 | return rules;
|
68 | }
|
69 | exports.loadRules = loadRules;
|
70 |
|
71 | function findRule(name, rulesDirectories) {
|
72 | var camelizedName = transformName(name);
|
73 |
|
74 | var Rule = loadCachedRule(CORE_RULES_DIRECTORY, camelizedName);
|
75 | return Rule !== undefined
|
76 | ? Rule
|
77 | :
|
78 | utils_1.find(utils_1.arrayify(rulesDirectories), function (dir) { return loadCachedRule(dir, camelizedName, true); });
|
79 | }
|
80 | exports.findRule = findRule;
|
81 | function transformName(name) {
|
82 |
|
83 |
|
84 | var nameMatch = name.match(/^([-_]*)(.*?)([-_]*)$/);
|
85 | if (nameMatch === null) {
|
86 | return name + "Rule";
|
87 | }
|
88 | return "" + nameMatch[1] + utils_1.camelize(nameMatch[2]) + nameMatch[3] + "Rule";
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function loadRule(directory, ruleName) {
|
95 | var ruleFullPath;
|
96 | try {
|
97 |
|
98 | ruleFullPath = require.resolve(path.join(directory, ruleName));
|
99 | }
|
100 | catch (_a) {
|
101 | return "not-found";
|
102 | }
|
103 | return require(ruleFullPath).Rule;
|
104 | }
|
105 | function loadCachedRule(directory, ruleName, isCustomPath) {
|
106 |
|
107 | var fullPath = path.join(directory, ruleName);
|
108 | var cachedRule = cachedRules.get(fullPath);
|
109 | if (cachedRule !== undefined) {
|
110 | return cachedRule === "not-found" ? undefined : cachedRule;
|
111 | }
|
112 |
|
113 | var absolutePath = directory;
|
114 | if (isCustomPath) {
|
115 | absolutePath = path.resolve(directory);
|
116 | if (!fs.existsSync(absolutePath)) {
|
117 | throw new error_1.FatalError("Could not find custom rule directory: " + absolutePath);
|
118 | }
|
119 | }
|
120 | var Rule = loadRule(absolutePath, ruleName);
|
121 | cachedRules.set(fullPath, Rule);
|
122 | return Rule === "not-found" ? undefined : Rule;
|
123 | }
|
124 | var templateObject_1, templateObject_2;
|