.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / ruleLoader.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 Palantir Technologies, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
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             // Perf: don't bother finding the rule if it's disabled.
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     return rules;
64 }
65 exports.loadRules = loadRules;
66 /** @internal private API */
67 function findRule(name, rulesDirectories) {
68     var camelizedName = transformName(name);
69     // first check for core rules
70     var Rule = loadCachedRule(CORE_RULES_DIRECTORY, camelizedName);
71     return Rule !== undefined
72         ? Rule
73         : // then check for rules within the first level of rulesDirectory
74             utils_1.find(utils_1.arrayify(rulesDirectories), function (dir) { return loadCachedRule(dir, camelizedName, true); });
75 }
76 exports.findRule = findRule;
77 function transformName(name) {
78     // camelize strips out leading and trailing underscores and dashes, so make sure they aren't passed to camelize
79     // the regex matches the groups (leading underscores and dashes)(other characters)(trailing underscores and dashes)
80     var nameMatch = name.match(/^([-_]*)(.*?)([-_]*)$/);
81     if (nameMatch === null) {
82         return name + "Rule";
83     }
84     return "" + nameMatch[1] + utils_1.camelize(nameMatch[2]) + nameMatch[3] + "Rule";
85 }
86 /**
87  * @param directory - An absolute path to a directory of rules
88  * @param ruleName - A name of a rule in filename format. ex) "someLintRule"
89  */
90 function loadRule(directory, ruleName) {
91     var ruleFullPath;
92     try {
93         // Resolve using node's path resolution to allow developers to write custom rules in TypeScript which can be loaded by TS-Node
94         ruleFullPath = require.resolve(path.join(directory, ruleName));
95     }
96     catch (_a) {
97         return "not-found";
98     }
99     return require(ruleFullPath).Rule;
100 }
101 function loadCachedRule(directory, ruleName, isCustomPath) {
102     // use cached value if available
103     var fullPath = path.join(directory, ruleName);
104     var cachedRule = cachedRules.get(fullPath);
105     if (cachedRule !== undefined) {
106         return cachedRule === "not-found" ? undefined : cachedRule;
107     }
108     // treat directory as a relative path (which needs to be resolved) if it's a custom rule directory
109     var absolutePath = directory;
110     if (isCustomPath) {
111         absolutePath = path.resolve(directory);
112         if (!fs.existsSync(absolutePath)) {
113             throw new error_1.FatalError("Could not find custom rule directory: " + absolutePath);
114         }
115     }
116     var Rule = loadRule(absolutePath, ruleName);
117     cachedRules.set(fullPath, Rule);
118     return Rule === "not-found" ? undefined : Rule;
119 }
120 var templateObject_1, templateObject_2;