This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noImportZonesRule.ts
170 lines (151 loc) · 6.42 KB
/
noImportZonesRule.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
// tslint:disable:object-literal-sort-keys
import * as Lint from "tslint";
import mm from "micromatch";
import path from "path";
import ts from "typescript";
import {findImports, ImportKind} from "tsutils";
const camelCasedRuleFileName = path.parse(__filename).name;
const dashedRuleFileName = camelToDash(camelCasedRuleFileName);
const dashedRuleName = dashedRuleFileName.replace(/-rule$/, "");
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: dashedRuleName,
description: `Forbids specific imports for the specified targets using "micromatch" patterns matching`,
optionsDescription: Lint.Utils.dedent`
* "basePath": by default "process.cwd()" is used as the base path, but custom "basePath"
property can be specified on the both zone/top and specific item levels. If values are specified on the
both levels, then specific item's value takes the priority.
* "patterns" object specifies:
* "target": file name pattern to which forbidding rule will be applied
* "from": file import pattern that will be from for the specified "target"
* "verbose": verbose output
`,
options: {
type: "object",
required: ["zones"],
additionalProperties: false,
properties: {
basePath: {type: ["string", "null"]},
zones: {
type: "array",
items: {
type: "object",
required: ["patterns"],
additionalProperties: false,
properties: {
basePath: {type: ["string", "null"]},
patterns: {
type: "array",
items: {
type: "object",
required: ["target", "from"],
additionalProperties: false,
properties: {
target: {
anyOf: [
{type: "string"},
{type: "array", items: {type: "string"}},
],
},
from: {
anyOf: [
{type: "string"},
{type: "array", items: {type: "string"}},
],
},
},
},
},
},
},
},
},
},
optionExamples: [
true,
[
{
basePath: "src",
zones: [
{
patterns: [
{
target: "test/**/*",
from: "lib/**/*",
},
],
},
],
},
],
],
type: "functionality",
typescriptOnly: false,
};
public isEnabled(): boolean {
return super.isEnabled() && this.ruleArguments.length > 0;
}
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction<Options[]>(sourceFile, walk, this.ruleArguments);
}
}
function walk(ctx: Lint.WalkContext<Options[]>) {
for (const importUnit of findImports(ctx.sourceFile, ImportKind.All)) {
for (const {basePath: topLevelBasePath, zones, verbose} of ctx.options) {
for (const {basePath: zoneBasePath, patterns} of zones) {
const basePath = zoneBasePath
? resolveBasePath(zoneBasePath)
: topLevelBasePath
? resolveBasePath(topLevelBasePath)
: path.resolve(process.cwd());
const srcFile = path.resolve(path.resolve(ctx.sourceFile.fileName));
const srcFileDir = path.parse(srcFile).dir;
const importFile = importUnit.text.startsWith(".")
? path.resolve(srcFileDir, importUnit.text)
: importUnit.text;
const relativeSrcFile = path.relative(basePath, srcFile);
const relativeImportFile = path.relative(basePath, importFile);
for (const {target, from} of patterns) {
if (mm([relativeSrcFile], target).length && mm([relativeImportFile], from).length) {
const zone: OptionsZoneError = {basePath, target, from};
const messages = [
`"${importUnit.text}" import is forbidden`,
];
if (verbose) {
messages.push(...[
`; rule: ${JSON.stringify(zone)}; resolved values: ${JSON.stringify({
file: relativeSrcFile,
import: relativeImportFile,
})}`,
]);
}
ctx.addFailure(importUnit.getStart(ctx.sourceFile) + 1, importUnit.end - 1, messages.join(""));
}
}
}
}
}
}
interface Options extends Partial<OptionsBasePath> {
verbose?: boolean;
zones: OptionsZone[];
}
interface OptionsZone extends Partial<OptionsBasePath> {
patterns: OptionsZonePattern[];
}
interface OptionsZonePattern {
target: string | string[];
from: string | string[];
}
interface OptionsZoneError extends OptionsZonePattern, OptionsBasePath {}
interface OptionsBasePath {
basePath: string;
}
function resolveBasePath(basePath: string) {
return path.isAbsolute(basePath)
? basePath
: path.relative(process.cwd(), basePath);
}
function camelToDash(str: string) {
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
}