forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noImplicitDependenciesRule.ts
179 lines (162 loc) · 6.07 KB
/
noImplicitDependenciesRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as builtins from "builtin-modules";
import * as fs from "fs";
import * as path from "path";
import { findImports, ImportKind } from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
interface Options {
dev: boolean;
optional: boolean;
whitelist: string[];
}
const OPTION_DEV = "dev";
const OPTION_OPTIONAL = "optional";
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-implicit-dependencies",
description: "Disallows importing modules that are not listed as dependency in the project's package.json",
descriptionDetails: Lint.Utils.dedent`
Disallows importing transient dependencies and modules installed above your package's root directory.
`,
optionsDescription: Lint.Utils.dedent`
By default the rule looks at \`"dependencies"\` and \`"peerDependencies"\`.
By adding the \`"${OPTION_DEV}"\` option the rule also looks at \`"devDependencies"\`.
By adding the \`"${OPTION_OPTIONAL}"\` option the rule also looks at \`"optionalDependencies"\`.
An array of whitelisted modules can be added to skip checking their existence in package.json.
`,
options: {
type: "array",
items: [
{
type: "string",
enum: [OPTION_DEV, OPTION_OPTIONAL],
},
{
type: "array",
},
],
minItems: 0,
maxItems: 3,
},
optionExamples: [
true,
[true, OPTION_DEV],
[true, OPTION_OPTIONAL],
[true, ["src", "app"]],
],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING_FACTORY(module: string) {
return `Module '${module}' is not listed as dependency in package.json`;
}
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
let whitelist = this.ruleArguments.find((arg) => Array.isArray(arg)) as string[];
if (whitelist === null || whitelist === undefined) {
whitelist = [];
}
return this.applyWithFunction(sourceFile, walk, {
dev: this.ruleArguments.indexOf(OPTION_DEV) !== - 1,
optional: this.ruleArguments.indexOf(OPTION_OPTIONAL) !== -1,
whitelist,
});
}
}
function walk(ctx: Lint.WalkContext<Options>) {
const {options} = ctx;
let dependencies: Set<string> | undefined;
const whitelist = new Set(options.whitelist);
for (const name of findImports(ctx.sourceFile, ImportKind.All)) {
if (!ts.isExternalModuleNameRelative(name.text)) {
const packageName = getPackageName(name.text);
if (!whitelist.has(packageName) && builtins.indexOf(packageName) === -1 && !hasDependency(packageName)) {
ctx.addFailureAtNode(name, Rule.FAILURE_STRING_FACTORY(packageName));
}
}
}
function hasDependency(module: string): boolean {
if (dependencies === undefined) {
dependencies = getDependencies(ctx.sourceFile.fileName, options);
}
return dependencies.has(module);
}
}
function getPackageName(name: string): string {
const parts = name.split(/\//g);
if (name[0] !== "@") {
return parts[0];
}
return `${parts[0]}/${parts[1]}`;
}
interface Dependencies extends Object {
[name: string]: any;
}
interface PackageJson {
dependencies?: Dependencies;
devDependencies?: Dependencies;
peerDependencies?: Dependencies;
optionalDependencies?: Dependencies;
}
function getDependencies(fileName: string, options: Options): Set<string> {
const result = new Set<string>();
const packageJsonPath = findPackageJson(path.resolve(path.dirname(fileName)));
if (packageJsonPath !== undefined) {
try {
// don't use require here to avoid caching
// remove BOM from file content before parsing
const content = JSON.parse(fs.readFileSync(packageJsonPath, "utf8").replace(/^\uFEFF/, "")) as PackageJson;
if (content.dependencies !== undefined) {
addDependencies(result, content.dependencies);
}
if (content.peerDependencies !== undefined) {
addDependencies(result, content.peerDependencies);
}
if (options.dev && content.devDependencies !== undefined) {
addDependencies(result, content.devDependencies);
}
if (options.optional && content.optionalDependencies !== undefined) {
addDependencies(result, content.optionalDependencies);
}
} catch {
// treat malformed package.json files as empty
}
}
return result;
}
function addDependencies(result: Set<string>, dependencies: Dependencies) {
for (const name in dependencies) {
if (dependencies.hasOwnProperty(name)) {
result.add(name);
}
}
}
function findPackageJson(current: string): string | undefined {
let prev: string;
do {
const fileName = path.join(current, "package.json");
if (fs.existsSync(fileName)) {
return fileName;
}
prev = current;
current = path.dirname(current);
} while (prev !== current);
return undefined;
}