4 * Copyright 2017 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
31 return _super !== null && _super.apply(this, arguments) || this;
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";
37 Rule.prototype.apply = function (sourceFile) {
38 var whitelist = this.ruleArguments.find(function (arg) { return Array.isArray(arg); });
39 if (whitelist === null || whitelist === undefined) {
42 return this.applyWithFunction(sourceFile, walk, {
43 dev: this.ruleArguments.indexOf(OPTION_DEV) !== -1,
44 optional: this.ruleArguments.indexOf(OPTION_OPTIONAL) !== -1,
48 /* tslint:disable:object-literal-sort-keys */
50 ruleName: "no-implicit-dependencies",
51 description: "Disallows importing modules that are not listed as dependency in the project's package.json",
52 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 "]))),
53 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 also looks at `\"devDependencies\"`.\n By adding the `\"", "\"` option the rule also looks at `\"optionalDependencies\"`.\n An array of whitelisted modules can be added to skip checking their existence in package.json.\n "], ["\n By default the rule looks at \\`\"dependencies\"\\` and \\`\"peerDependencies\"\\`.\n By adding the \\`\"", "\"\\` option the rule also looks at \\`\"devDependencies\"\\`.\n By adding the \\`\"", "\"\\` option the rule also looks at \\`\"optionalDependencies\"\\`.\n An array of whitelisted modules can be added to skip checking their existence in package.json.\n "])), OPTION_DEV, OPTION_OPTIONAL),
59 enum: [OPTION_DEV, OPTION_OPTIONAL],
68 optionExamples: [true, [true, OPTION_DEV], [true, OPTION_OPTIONAL], [true, ["src", "app"]]],
69 type: "functionality",
70 typescriptOnly: false,
73 }(Lint.Rules.AbstractRule));
76 var options = ctx.options;
78 var whitelist = new Set(options.whitelist);
79 for (var _i = 0, _a = tsutils_1.findImports(ctx.sourceFile, 63 /* All */); _i < _a.length; _i++) {
81 if (!ts.isExternalModuleNameRelative(name.text)) {
82 var packageName = getPackageName(name.text, whitelist);
83 if (!whitelist.has(packageName) &&
84 builtins.indexOf(packageName) === -1 &&
85 !hasDependency(packageName)) {
86 ctx.addFailureAtNode(name, Rule.FAILURE_STRING_FACTORY(packageName));
90 function hasDependency(module) {
91 if (dependencies === undefined) {
92 dependencies = getDependencies(ctx.sourceFile.fileName, options);
94 return dependencies.has(module);
97 function getPackageName(name, whitelist) {
98 var parts = name.split(/\//g);
99 if (name[0] !== "@" || whitelist.has(parts[0])) {
102 if (whitelist.has(name)) {
105 return parts[0] + "/" + parts[1];
107 function getDependencies(fileName, options) {
108 var result = new Set();
109 var packageJsonPath = findPackageJson(path.resolve(path.dirname(fileName)));
110 if (packageJsonPath !== undefined) {
112 // don't use require here to avoid caching
113 // remove BOM from file content before parsing
114 var content = JSON.parse(fs.readFileSync(packageJsonPath, "utf8").replace(/^\uFEFF/, ""));
115 if (content.dependencies !== undefined) {
116 addDependencies(result, content.dependencies);
118 if (content.peerDependencies !== undefined) {
119 addDependencies(result, content.peerDependencies);
121 if (options.dev && content.devDependencies !== undefined) {
122 addDependencies(result, content.devDependencies);
124 if (options.optional && content.optionalDependencies !== undefined) {
125 addDependencies(result, content.optionalDependencies);
129 // treat malformed package.json files as empty
134 function addDependencies(result, dependencies) {
135 for (var _i = 0, _a = Object.keys(dependencies); _i < _a.length; _i++) {
140 function findPackageJson(current) {
143 var fileName = path.join(current, "package.json");
144 if (fs.existsSync(fileName)) {
148 current = path.dirname(current);
149 } while (prev !== current);
152 var templateObject_1, templateObject_2;