123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. 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. /** @internal private API */
  71. function findRule(name, rulesDirectories) {
  72. var camelizedName = transformName(name);
  73. // first check for core rules
  74. var Rule = loadCachedRule(CORE_RULES_DIRECTORY, camelizedName);
  75. return Rule !== undefined ? Rule :
  76. // then check for rules within the first level of rulesDirectory
  77. utils_1.find(utils_1.arrayify(rulesDirectories), function (dir) { return loadCachedRule(dir, camelizedName, true); });
  78. }
  79. exports.findRule = findRule;
  80. function transformName(name) {
  81. // camelize strips out leading and trailing underscores and dashes, so make sure they aren't passed to camelize
  82. // the regex matches the groups (leading underscores and dashes)(other characters)(trailing underscores and dashes)
  83. var nameMatch = name.match(/^([-_]*)(.*?)([-_]*)$/);
  84. if (nameMatch === null) {
  85. return name + "Rule";
  86. }
  87. return "" + nameMatch[1] + utils_1.camelize(nameMatch[2]) + nameMatch[3] + "Rule";
  88. }
  89. /**
  90. * @param directory - An absolute path to a directory of rules
  91. * @param ruleName - A name of a rule in filename format. ex) "someLintRule"
  92. */
  93. function loadRule(directory, ruleName) {
  94. var ruleFullPath;
  95. try {
  96. // Resolve using node's path resolution to allow developers to write custom rules in TypeScript which can be loaded by TS-Node
  97. ruleFullPath = require.resolve(path.join(directory, ruleName));
  98. }
  99. catch (_a) {
  100. return "not-found";
  101. }
  102. return require(ruleFullPath).Rule;
  103. }
  104. function loadCachedRule(directory, ruleName, isCustomPath) {
  105. // use cached value if available
  106. var fullPath = path.join(directory, ruleName);
  107. var cachedRule = cachedRules.get(fullPath);
  108. if (cachedRule !== undefined) {
  109. return cachedRule === "not-found" ? undefined : cachedRule;
  110. }
  111. // treat directory as a relative path (which needs to be resolved) if it's a custom rule directory
  112. var absolutePath = directory;
  113. if (isCustomPath) {
  114. absolutePath = path.resolve(directory);
  115. if (!fs.existsSync(absolutePath)) {
  116. throw new error_1.FatalError("Could not find custom rule directory: " + absolutePath);
  117. }
  118. }
  119. var Rule = loadRule(absolutePath, ruleName);
  120. cachedRules.set(fullPath, Rule);
  121. return Rule === "not-found" ? undefined : Rule;
  122. }
  123. var templateObject_1, templateObject_2;