-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathno-restricted-paths.js
246 lines (206 loc) · 7.94 KB
/
no-restricted-paths.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import path from 'path';
import { getPhysicalFilename } from 'eslint-module-utils/contextCompat';
import resolve from 'eslint-module-utils/resolve';
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
import isGlob from 'is-glob';
import { Minimatch } from 'minimatch';
import importType from '../core/importType';
import docsUrl from '../docsUrl';
const containsPath = (filepath, target) => {
const relative = path.relative(target, filepath);
return relative === '' || !relative.startsWith('..');
};
function isMatchingTargetPath(filename, targetPath) {
if (isGlob(targetPath)) {
const mm = new Minimatch(targetPath);
return mm.match(filename);
}
return containsPath(filename, targetPath);
}
module.exports = {
meta: {
type: 'problem',
docs: {
category: 'Static analysis',
description: 'Enforce which files can be imported in a given folder.',
url: docsUrl('no-restricted-paths'),
},
schema: [
{
type: 'object',
properties: {
zones: {
type: 'array',
minItems: 1,
items: {
type: 'object',
properties: {
target: {
anyOf: [
{ type: 'string' },
{
type: 'array',
items: { type: 'string' },
uniqueItems: true,
minLength: 1,
},
],
},
from: {
anyOf: [
{ type: 'string' },
{
type: 'array',
items: { type: 'string' },
uniqueItems: true,
minLength: 1,
},
],
},
except: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
},
message: { type: 'string' },
},
additionalProperties: false,
},
},
basePath: { type: 'string' },
},
additionalProperties: false,
},
],
},
create: function noRestrictedPaths(context) {
const options = context.options[0] || {};
const restrictedPaths = options.zones || [];
const basePath = options.basePath || process.cwd();
const currentFilename = getPhysicalFilename(context);
const matchingZones = restrictedPaths.filter(
(zone) => [].concat(zone.target)
.map((target) => path.resolve(basePath, target))
.some((targetPath) => isMatchingTargetPath(currentFilename, targetPath)),
);
function isValidExceptionPath(absoluteFromPath, absoluteExceptionPath) {
const relativeExceptionPath = path.relative(absoluteFromPath, absoluteExceptionPath);
return importType(relativeExceptionPath, context) !== 'parent';
}
function areBothGlobPatternAndAbsolutePath(areGlobPatterns) {
return areGlobPatterns.some((isGlob) => isGlob) && areGlobPatterns.some((isGlob) => !isGlob);
}
function reportInvalidExceptionPath(node) {
context.report({
node,
message: 'Restricted path exceptions must be descendants of the configured `from` path for that zone.',
});
}
function reportInvalidExceptionMixedGlobAndNonGlob(node) {
context.report({
node,
message: 'Restricted path `from` must contain either only glob patterns or none',
});
}
function reportInvalidExceptionGlob(node) {
context.report({
node,
message: 'Restricted path exceptions must be glob patterns when `from` contains glob patterns',
});
}
function computeMixedGlobAndAbsolutePathValidator() {
return {
isPathRestricted: () => true,
hasValidExceptions: false,
reportInvalidException: reportInvalidExceptionMixedGlobAndNonGlob,
};
}
function computeGlobPatternPathValidator(absoluteFrom, zoneExcept) {
let isPathException;
const mm = new Minimatch(absoluteFrom);
const isPathRestricted = (absoluteImportPath) => mm.match(absoluteImportPath);
const hasValidExceptions = zoneExcept.every(isGlob);
if (hasValidExceptions) {
const exceptionsMm = zoneExcept.map((except) => new Minimatch(except));
isPathException = (absoluteImportPath) => exceptionsMm.some((mm) => mm.match(absoluteImportPath));
}
const reportInvalidException = reportInvalidExceptionGlob;
return {
isPathRestricted,
hasValidExceptions,
isPathException,
reportInvalidException,
};
}
function computeAbsolutePathValidator(absoluteFrom, zoneExcept) {
let isPathException;
const isPathRestricted = (absoluteImportPath) => containsPath(absoluteImportPath, absoluteFrom);
const absoluteExceptionPaths = zoneExcept
.map((exceptionPath) => path.resolve(absoluteFrom, exceptionPath));
const hasValidExceptions = absoluteExceptionPaths
.every((absoluteExceptionPath) => isValidExceptionPath(absoluteFrom, absoluteExceptionPath));
if (hasValidExceptions) {
isPathException = (absoluteImportPath) => absoluteExceptionPaths.some(
(absoluteExceptionPath) => containsPath(absoluteImportPath, absoluteExceptionPath),
);
}
const reportInvalidException = reportInvalidExceptionPath;
return {
isPathRestricted,
hasValidExceptions,
isPathException,
reportInvalidException,
};
}
function reportInvalidExceptions(validators, node) {
validators.forEach((validator) => validator.reportInvalidException(node));
}
function reportImportsInRestrictedZone(validators, node, importPath, customMessage) {
validators.forEach(() => {
context.report({
node,
message: `Unexpected path "{{importPath}}" imported in restricted zone.${customMessage ? ` ${customMessage}` : ''}`,
data: { importPath },
});
});
}
const makePathValidators = (zoneFrom, zoneExcept = []) => {
const allZoneFrom = [].concat(zoneFrom);
const areGlobPatterns = allZoneFrom.map(isGlob);
if (areBothGlobPatternAndAbsolutePath(areGlobPatterns)) {
return [computeMixedGlobAndAbsolutePathValidator()];
}
const isGlobPattern = areGlobPatterns.every((isGlob) => isGlob);
return allZoneFrom.map((singleZoneFrom) => {
const absoluteFrom = path.resolve(basePath, singleZoneFrom);
if (isGlobPattern) {
return computeGlobPatternPathValidator(absoluteFrom, zoneExcept);
}
return computeAbsolutePathValidator(absoluteFrom, zoneExcept);
});
};
const validators = [];
function checkForRestrictedImportPath(importPath, node) {
const absoluteImportPath = resolve(importPath, context);
if (!absoluteImportPath) {
return;
}
matchingZones.forEach((zone, index) => {
if (!validators[index]) {
validators[index] = makePathValidators(zone.from, zone.except);
}
const applicableValidatorsForImportPath = validators[index].filter((validator) => validator.isPathRestricted(absoluteImportPath));
const validatorsWithInvalidExceptions = applicableValidatorsForImportPath.filter((validator) => !validator.hasValidExceptions);
reportInvalidExceptions(validatorsWithInvalidExceptions, node);
const applicableValidatorsForImportPathExcludingExceptions = applicableValidatorsForImportPath
.filter((validator) => validator.hasValidExceptions && !validator.isPathException(absoluteImportPath));
reportImportsInRestrictedZone(applicableValidatorsForImportPathExcludingExceptions, node, importPath, zone.message);
});
}
return moduleVisitor((source) => {
checkForRestrictedImportPath(source.value, source);
}, { commonjs: true });
},
};