123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- "use strict";
- const assert = require("assert");
- const path = require("path");
- const ignore = require("ignore");
- const debug = require("debug")("eslintrc:ignore-pattern");
- function getCommonAncestorPath(sourcePaths) {
- let result = sourcePaths[0];
- for (let i = 1; i < sourcePaths.length; ++i) {
- const a = result;
- const b = sourcePaths[i];
-
- result = a.length < b.length ? a : b;
-
- for (let j = 0, lastSepPos = 0; j < a.length && j < b.length; ++j) {
- if (a[j] !== b[j]) {
- result = a.slice(0, lastSepPos);
- break;
- }
- if (a[j] === path.sep) {
- lastSepPos = j;
- }
- }
- }
- let resolvedResult = result || path.sep;
-
- if (resolvedResult && resolvedResult.endsWith(":") && process.platform === "win32") {
- resolvedResult += path.sep;
- }
- return resolvedResult;
- }
- function relative(from, to) {
- const relPath = path.relative(from, to);
- if (path.sep === "/") {
- return relPath;
- }
- return relPath.split(path.sep).join("/");
- }
- function dirSuffix(filePath) {
- const isDir = (
- filePath.endsWith(path.sep) ||
- (process.platform === "win32" && filePath.endsWith("/"))
- );
- return isDir ? "/" : "";
- }
- const DefaultPatterns = Object.freeze(["/**/node_modules/*"]);
- const DotPatterns = Object.freeze([".*", "!.eslintrc.*", "!../"]);
- class IgnorePattern {
-
- static get DefaultPatterns() {
- return DefaultPatterns;
- }
-
- static createDefaultIgnore(cwd) {
- return this.createIgnore([new IgnorePattern(DefaultPatterns, cwd)]);
- }
-
- static createIgnore(ignorePatterns) {
- debug("Create with: %o", ignorePatterns);
- const basePath = getCommonAncestorPath(ignorePatterns.map(p => p.basePath));
- const patterns = [].concat(
- ...ignorePatterns.map(p => p.getPatternsRelativeTo(basePath))
- );
- const ig = ignore().add([...DotPatterns, ...patterns]);
- const dotIg = ignore().add(patterns);
- debug(" processed: %o", { basePath, patterns });
- return Object.assign(
- (filePath, dot = false) => {
- assert(path.isAbsolute(filePath), "'filePath' should be an absolute path.");
- const relPathRaw = relative(basePath, filePath);
- const relPath = relPathRaw && (relPathRaw + dirSuffix(filePath));
- const adoptedIg = dot ? dotIg : ig;
- const result = relPath !== "" && adoptedIg.ignores(relPath);
- debug("Check", { filePath, dot, relativePath: relPath, result });
- return result;
- },
- { basePath, patterns }
- );
- }
-
- constructor(patterns, basePath) {
- assert(path.isAbsolute(basePath), "'basePath' should be an absolute path.");
-
- this.patterns = patterns;
-
- this.basePath = basePath;
-
- this.loose = false;
- }
-
- getPatternsRelativeTo(newBasePath) {
- assert(path.isAbsolute(newBasePath), "'newBasePath' should be an absolute path.");
- const { basePath, loose, patterns } = this;
- if (newBasePath === basePath) {
- return patterns;
- }
- const prefix = `/${relative(newBasePath, basePath)}`;
- return patterns.map(pattern => {
- const negative = pattern.startsWith("!");
- const head = negative ? "!" : "";
- const body = negative ? pattern.slice(1) : pattern;
- if (body.startsWith("/") || body.startsWith("../")) {
- return `${head}${prefix}${body}`;
- }
- return loose ? pattern : `${head}${prefix}/**/${body}`;
- });
- }
- }
- module.exports = { IgnorePattern };
|