1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const fs = require("fs"),
|
13 | path = require("path"),
|
14 | ignore = require("ignore");
|
15 |
|
16 | const debug = require("debug")("eslint:ignored-paths");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const ESLINT_IGNORE_FILENAME = ".eslintignore";
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const DEFAULT_IGNORE_DIRS = [
|
30 | "/node_modules/*",
|
31 | "/bower_components/*"
|
32 | ];
|
33 | const DEFAULT_OPTIONS = {
|
34 | dotfiles: false,
|
35 | cwd: process.cwd()
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function findFile(cwd, name) {
|
49 | const ignoreFilePath = path.resolve(cwd, name);
|
50 |
|
51 | return fs.existsSync(ignoreFilePath) && fs.statSync(ignoreFilePath).isFile() ? ignoreFilePath : "";
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function findIgnoreFile(cwd) {
|
60 | return findFile(cwd, ESLINT_IGNORE_FILENAME);
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function findPackageJSONFile(cwd) {
|
69 | return findFile(cwd, "package.json");
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | function mergeDefaultOptions(options) {
|
78 | return Object.assign({}, DEFAULT_OPTIONS, options);
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | const normalizePathSeps = path.sep === "/"
|
90 | ? (str => str)
|
91 | : ((seps, str) => str.replace(seps, "/")).bind(null, new RegExp(`\\${path.sep}`, "gu"));
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | function relativize(globPattern, relativePathToOldBaseDir) {
|
101 | if (relativePathToOldBaseDir === "") {
|
102 | return globPattern;
|
103 | }
|
104 |
|
105 | const prefix = globPattern.startsWith("!") ? "!" : "";
|
106 | const globWithoutPrefix = globPattern.replace(/^!/u, "");
|
107 |
|
108 | if (globWithoutPrefix.startsWith("/")) {
|
109 | return `${prefix}/${normalizePathSeps(relativePathToOldBaseDir)}${globWithoutPrefix}`;
|
110 | }
|
111 |
|
112 | return globPattern;
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | class IgnoredPaths {
|
123 |
|
124 | |
125 |
|
126 |
|
127 | constructor(providedOptions) {
|
128 | const options = mergeDefaultOptions(providedOptions);
|
129 |
|
130 | this.cache = {};
|
131 |
|
132 | this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []);
|
133 |
|
134 | this.ignoreFileDir = options.ignore !== false && options.ignorePath
|
135 | ? path.dirname(path.resolve(options.cwd, options.ignorePath))
|
136 | : options.cwd;
|
137 | this.options = options;
|
138 | this._baseDir = null;
|
139 |
|
140 | this.ig = {
|
141 | custom: ignore(),
|
142 | default: ignore()
|
143 | };
|
144 |
|
145 | this.defaultPatterns.forEach(pattern => this.addPatternRelativeToCwd(this.ig.default, pattern));
|
146 | if (options.dotfiles !== true) {
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 | this.addPatternRelativeToCwd(this.ig.default, ".*");
|
153 | this.addPatternRelativeToCwd(this.ig.default, "!../");
|
154 | }
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 | this.ig.custom.ignoreFiles = [];
|
161 | this.ig.default.ignoreFiles = [];
|
162 |
|
163 | if (options.ignore !== false) {
|
164 | let ignorePath;
|
165 |
|
166 | if (options.ignorePath) {
|
167 | debug("Using specific ignore file");
|
168 |
|
169 | try {
|
170 | const stat = fs.statSync(options.ignorePath);
|
171 |
|
172 | if (!stat.isFile()) {
|
173 | throw new Error(`${options.ignorePath} is not a file`);
|
174 | }
|
175 | ignorePath = options.ignorePath;
|
176 | } catch (e) {
|
177 | e.message = `Cannot read ignore file: ${options.ignorePath}\nError: ${e.message}`;
|
178 | throw e;
|
179 | }
|
180 | } else {
|
181 | debug(`Looking for ignore file in ${options.cwd}`);
|
182 | ignorePath = findIgnoreFile(options.cwd);
|
183 |
|
184 | try {
|
185 | fs.statSync(ignorePath);
|
186 | debug(`Loaded ignore file ${ignorePath}`);
|
187 | } catch (e) {
|
188 | debug("Could not find ignore file in cwd");
|
189 | }
|
190 | }
|
191 |
|
192 | if (ignorePath) {
|
193 | debug(`Adding ${ignorePath}`);
|
194 | this.addIgnoreFile(this.ig.custom, ignorePath);
|
195 | this.addIgnoreFile(this.ig.default, ignorePath);
|
196 | } else {
|
197 | try {
|
198 |
|
199 |
|
200 | const packageJSONPath = findPackageJSONFile(options.cwd);
|
201 |
|
202 | if (packageJSONPath) {
|
203 | let packageJSONOptions;
|
204 |
|
205 | try {
|
206 | packageJSONOptions = JSON.parse(fs.readFileSync(packageJSONPath, "utf8"));
|
207 | } catch (e) {
|
208 | debug("Could not read package.json file to check eslintIgnore property");
|
209 | e.messageTemplate = "failed-to-read-json";
|
210 | e.messageData = {
|
211 | path: packageJSONPath,
|
212 | message: e.message
|
213 | };
|
214 | throw e;
|
215 | }
|
216 |
|
217 | if (packageJSONOptions.eslintIgnore) {
|
218 | if (Array.isArray(packageJSONOptions.eslintIgnore)) {
|
219 | packageJSONOptions.eslintIgnore.forEach(pattern => {
|
220 | this.addPatternRelativeToIgnoreFile(this.ig.custom, pattern);
|
221 | this.addPatternRelativeToIgnoreFile(this.ig.default, pattern);
|
222 | });
|
223 | } else {
|
224 | throw new TypeError("Package.json eslintIgnore property requires an array of paths");
|
225 | }
|
226 | }
|
227 | }
|
228 | } catch (e) {
|
229 | debug("Could not find package.json to check eslintIgnore property");
|
230 | throw e;
|
231 | }
|
232 | }
|
233 |
|
234 | if (options.ignorePattern) {
|
235 | this.addPatternRelativeToCwd(this.ig.custom, options.ignorePattern);
|
236 | this.addPatternRelativeToCwd(this.ig.default, options.ignorePattern);
|
237 | }
|
238 | }
|
239 | }
|
240 |
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | addPatternRelativeToCwd(ig, pattern) {
|
249 | const baseDir = this.getBaseDir();
|
250 | const cookedPattern = baseDir === this.options.cwd
|
251 | ? pattern
|
252 | : relativize(pattern, path.relative(baseDir, this.options.cwd));
|
253 |
|
254 | ig.addPattern(cookedPattern);
|
255 | debug("addPatternRelativeToCwd:\n original = %j\n cooked = %j", pattern, cookedPattern);
|
256 | }
|
257 |
|
258 | addPatternRelativeToIgnoreFile(ig, pattern) {
|
259 | const baseDir = this.getBaseDir();
|
260 | const cookedPattern = baseDir === this.ignoreFileDir
|
261 | ? pattern
|
262 | : relativize(pattern, path.relative(baseDir, this.ignoreFileDir));
|
263 |
|
264 | ig.addPattern(cookedPattern);
|
265 | debug("addPatternRelativeToIgnoreFile:\n original = %j\n cooked = %j", pattern, cookedPattern);
|
266 | }
|
267 |
|
268 |
|
269 | getBaseDir() {
|
270 | if (!this._baseDir) {
|
271 | const a = path.resolve(this.options.cwd);
|
272 | const b = path.resolve(this.ignoreFileDir);
|
273 | let lastSepPos = 0;
|
274 |
|
275 |
|
276 | this._baseDir = a.length < b.length ? a : b;
|
277 |
|
278 |
|
279 | for (let i = 0; i < a.length && i < b.length; ++i) {
|
280 | if (a[i] !== b[i]) {
|
281 | this._baseDir = a.slice(0, lastSepPos);
|
282 | break;
|
283 | }
|
284 | if (a[i] === path.sep) {
|
285 | lastSepPos = i;
|
286 | }
|
287 | }
|
288 |
|
289 |
|
290 | if (/^[A-Z]:$/u.test(this._baseDir)) {
|
291 | this._baseDir += "\\";
|
292 | }
|
293 |
|
294 | debug("baseDir = %j", this._baseDir);
|
295 | }
|
296 | return this._baseDir;
|
297 | }
|
298 |
|
299 | |
300 |
|
301 |
|
302 |
|
303 |
|
304 | readIgnoreFile(filePath) {
|
305 | if (typeof this.cache[filePath] === "undefined") {
|
306 | this.cache[filePath] = fs.readFileSync(filePath, "utf8").split(/\r?\n/gu).filter(Boolean);
|
307 | }
|
308 | return this.cache[filePath];
|
309 | }
|
310 |
|
311 | |
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | addIgnoreFile(ig, filePath) {
|
318 | ig.ignoreFiles.push(filePath);
|
319 | this
|
320 | .readIgnoreFile(filePath)
|
321 | .forEach(ignoreRule => this.addPatternRelativeToIgnoreFile(ig, ignoreRule));
|
322 | }
|
323 |
|
324 | |
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | contains(filepath, category) {
|
331 | const isDir = filepath.endsWith(path.sep) ||
|
332 | (path.sep === "\\" && filepath.endsWith("/"));
|
333 | let result = false;
|
334 | const basePath = this.getBaseDir();
|
335 | const absolutePath = path.resolve(this.options.cwd, filepath);
|
336 | let relativePath = path.relative(basePath, absolutePath);
|
337 |
|
338 | if (relativePath) {
|
339 | if (isDir) {
|
340 | relativePath += path.sep;
|
341 | }
|
342 | if (typeof category === "undefined") {
|
343 | result =
|
344 | (this.ig.default.filter([relativePath]).length === 0) ||
|
345 | (this.ig.custom.filter([relativePath]).length === 0);
|
346 | } else {
|
347 | result =
|
348 | (this.ig[category].filter([relativePath]).length === 0);
|
349 | }
|
350 | }
|
351 | debug("contains:");
|
352 | debug(" target = %j", filepath);
|
353 | debug(" base = %j", basePath);
|
354 | debug(" relative = %j", relativePath);
|
355 | debug(" result = %j", result);
|
356 |
|
357 | return result;
|
358 |
|
359 | }
|
360 | }
|
361 |
|
362 | module.exports = { IgnoredPaths };
|