noImplicitDependenciesRule.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2017 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 builtins = require("builtin-modules");
  21. var fs = require("fs");
  22. var path = require("path");
  23. var tsutils_1 = require("tsutils");
  24. var ts = require("typescript");
  25. var Lint = require("../index");
  26. var OPTION_DEV = "dev";
  27. var OPTION_OPTIONAL = "optional";
  28. var Rule = /** @class */ (function (_super) {
  29. tslib_1.__extends(Rule, _super);
  30. function Rule() {
  31. return _super !== null && _super.apply(this, arguments) || this;
  32. }
  33. /* tslint:enable:object-literal-sort-keys */
  34. Rule.FAILURE_STRING_FACTORY = function (module) {
  35. return "Module '" + module + "' is not listed as dependency in package.json";
  36. };
  37. Rule.prototype.apply = function (sourceFile) {
  38. return this.applyWithFunction(sourceFile, walk, {
  39. dev: this.ruleArguments.indexOf(OPTION_DEV) !== -1,
  40. optional: this.ruleArguments.indexOf(OPTION_OPTIONAL) !== -1,
  41. });
  42. };
  43. /* tslint:disable:object-literal-sort-keys */
  44. Rule.metadata = {
  45. ruleName: "no-implicit-dependencies",
  46. description: "Disallows importing modules that are not listed as dependency in the project's package.json",
  47. descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows importing transient dependencies and modules installed above your package's root directory.\n "], ["\n Disallows importing transient dependencies and modules installed above your package's root directory.\n "]))),
  48. optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n By default the rule looks at `\"dependencies\"` and `\"peerDependencies\"`.\n By adding the `\"", "\"` option the rule looks at `\"devDependencies\"` instead of `\"peerDependencies\"`.\n By adding the `\"", "\"` option the rule also looks at `\"optionalDependencies\"`.\n "], ["\n By default the rule looks at \\`\"dependencies\"\\` and \\`\"peerDependencies\"\\`.\n By adding the \\`\"", "\"\\` option the rule looks at \\`\"devDependencies\"\\` instead of \\`\"peerDependencies\"\\`.\n By adding the \\`\"", "\"\\` option the rule also looks at \\`\"optionalDependencies\"\\`.\n "])), OPTION_DEV, OPTION_OPTIONAL),
  49. options: {
  50. type: "array",
  51. items: {
  52. type: "string",
  53. enum: [OPTION_DEV, OPTION_OPTIONAL],
  54. },
  55. minItems: 0,
  56. maxItems: 2,
  57. },
  58. optionExamples: [true, [true, OPTION_DEV], [true, OPTION_OPTIONAL]],
  59. type: "functionality",
  60. typescriptOnly: false,
  61. };
  62. return Rule;
  63. }(Lint.Rules.AbstractRule));
  64. exports.Rule = Rule;
  65. function walk(ctx) {
  66. var options = ctx.options;
  67. var dependencies;
  68. for (var _i = 0, _a = tsutils_1.findImports(ctx.sourceFile, 31 /* All */); _i < _a.length; _i++) {
  69. var name = _a[_i];
  70. if (!ts.isExternalModuleNameRelative(name.text)) {
  71. var packageName = getPackageName(name.text);
  72. if (builtins.indexOf(packageName) === -1 && !hasDependency(packageName)) {
  73. ctx.addFailureAtNode(name, Rule.FAILURE_STRING_FACTORY(packageName));
  74. }
  75. }
  76. }
  77. function hasDependency(module) {
  78. if (dependencies === undefined) {
  79. dependencies = getDependencies(ctx.sourceFile.fileName, options);
  80. }
  81. return dependencies.has(module);
  82. }
  83. }
  84. function getPackageName(name) {
  85. var parts = name.split(/\//g);
  86. if (name[0] !== "@") {
  87. return parts[0];
  88. }
  89. return parts[0] + "/" + parts[1];
  90. }
  91. function getDependencies(fileName, options) {
  92. var result = new Set();
  93. var packageJsonPath = findPackageJson(path.resolve(path.dirname(fileName)));
  94. if (packageJsonPath !== undefined) {
  95. try {
  96. // don't use require here to avoid caching
  97. // remove BOM from file content before parsing
  98. var content = JSON.parse(fs.readFileSync(packageJsonPath, "utf8").replace(/^\uFEFF/, ""));
  99. if (content.dependencies !== undefined) {
  100. addDependencies(result, content.dependencies);
  101. }
  102. if (!options.dev && content.peerDependencies !== undefined) {
  103. addDependencies(result, content.peerDependencies);
  104. }
  105. if (options.dev && content.devDependencies !== undefined) {
  106. addDependencies(result, content.devDependencies);
  107. }
  108. if (options.optional && content.optionalDependencies !== undefined) {
  109. addDependencies(result, content.optionalDependencies);
  110. }
  111. }
  112. catch (_a) {
  113. // treat malformed package.json files as empty
  114. }
  115. }
  116. return result;
  117. }
  118. function addDependencies(result, dependencies) {
  119. for (var name in dependencies) {
  120. if (dependencies.hasOwnProperty(name)) {
  121. result.add(name);
  122. }
  123. }
  124. }
  125. function findPackageJson(current) {
  126. var prev;
  127. do {
  128. var fileName = path.join(current, "package.json");
  129. if (fs.existsSync(fileName)) {
  130. return fileName;
  131. }
  132. prev = current;
  133. current = path.dirname(current);
  134. } while (prev !== current);
  135. return undefined;
  136. }
  137. var templateObject_1, templateObject_2;