.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noImplicitDependenciesRule.js
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         var whitelist = this.ruleArguments.find(function (arg) { return Array.isArray(arg); });
39         if (whitelist === null || whitelist === undefined) {
40             whitelist = [];
41         }
42         return this.applyWithFunction(sourceFile, walk, {
43             dev: this.ruleArguments.indexOf(OPTION_DEV) !== -1,
44             optional: this.ruleArguments.indexOf(OPTION_OPTIONAL) !== -1,
45             whitelist: whitelist,
46         });
47     };
48     /* tslint:disable:object-literal-sort-keys */
49     Rule.metadata = {
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),
54         options: {
55             type: "array",
56             items: [
57                 {
58                     type: "string",
59                     enum: [OPTION_DEV, OPTION_OPTIONAL],
60                 },
61                 {
62                     type: "array",
63                 },
64             ],
65             minItems: 0,
66             maxItems: 3,
67         },
68         optionExamples: [true, [true, OPTION_DEV], [true, OPTION_OPTIONAL], [true, ["src", "app"]]],
69         type: "functionality",
70         typescriptOnly: false,
71     };
72     return Rule;
73 }(Lint.Rules.AbstractRule));
74 exports.Rule = Rule;
75 function walk(ctx) {
76     var options = ctx.options;
77     var dependencies;
78     var whitelist = new Set(options.whitelist);
79     for (var _i = 0, _a = tsutils_1.findImports(ctx.sourceFile, 63 /* All */); _i < _a.length; _i++) {
80         var name = _a[_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));
87             }
88         }
89     }
90     function hasDependency(module) {
91         if (dependencies === undefined) {
92             dependencies = getDependencies(ctx.sourceFile.fileName, options);
93         }
94         return dependencies.has(module);
95     }
96 }
97 function getPackageName(name, whitelist) {
98     var parts = name.split(/\//g);
99     if (name[0] !== "@" || whitelist.has(parts[0])) {
100         return parts[0];
101     }
102     if (whitelist.has(name)) {
103         return name;
104     }
105     return parts[0] + "/" + parts[1];
106 }
107 function getDependencies(fileName, options) {
108     var result = new Set();
109     var packageJsonPath = findPackageJson(path.resolve(path.dirname(fileName)));
110     if (packageJsonPath !== undefined) {
111         try {
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);
117             }
118             if (content.peerDependencies !== undefined) {
119                 addDependencies(result, content.peerDependencies);
120             }
121             if (options.dev && content.devDependencies !== undefined) {
122                 addDependencies(result, content.devDependencies);
123             }
124             if (options.optional && content.optionalDependencies !== undefined) {
125                 addDependencies(result, content.optionalDependencies);
126             }
127         }
128         catch (_a) {
129             // treat malformed package.json files as empty
130         }
131     }
132     return result;
133 }
134 function addDependencies(result, dependencies) {
135     for (var _i = 0, _a = Object.keys(dependencies); _i < _a.length; _i++) {
136         var name = _a[_i];
137         result.add(name);
138     }
139 }
140 function findPackageJson(current) {
141     var prev;
142     do {
143         var fileName = path.join(current, "package.json");
144         if (fs.existsSync(fileName)) {
145             return fileName;
146         }
147         prev = current;
148         current = path.dirname(current);
149     } while (prev !== current);
150     return undefined;
151 }
152 var templateObject_1, templateObject_2;