123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
-
-
- "use strict";
-
-
-
-
-
- const fs = require("fs"),
- path = require("path"),
- ignore = require("ignore"),
- pathUtils = require("./util/path-utils");
-
- const debug = require("debug")("eslint:ignored-paths");
-
-
-
-
-
- const ESLINT_IGNORE_FILENAME = ".eslintignore";
-
-
- const DEFAULT_IGNORE_DIRS = [
- "/node_modules/*",
- "/bower_components/*"
- ];
- const DEFAULT_OPTIONS = {
- dotfiles: false,
- cwd: process.cwd()
- };
-
-
-
-
-
-
- function findFile(cwd, name) {
- const ignoreFilePath = path.resolve(cwd, name);
-
- return fs.existsSync(ignoreFilePath) && fs.statSync(ignoreFilePath).isFile() ? ignoreFilePath : "";
- }
-
-
- function findIgnoreFile(cwd) {
- return findFile(cwd, ESLINT_IGNORE_FILENAME);
- }
-
-
- function findPackageJSONFile(cwd) {
- return findFile(cwd, "package.json");
- }
-
-
- function mergeDefaultOptions(options) {
- return Object.assign({}, DEFAULT_OPTIONS, options);
- }
-
-
-
- const normalizePathSeps = path.sep === "/"
- ? (str => str)
- : ((seps, str) => str.replace(seps, "/")).bind(null, new RegExp(`\\${path.sep}`, "g"));
-
-
-
- function relativize(globPattern, relativePathToOldBaseDir) {
- if (relativePathToOldBaseDir === "") {
- return globPattern;
- }
-
- const prefix = globPattern.startsWith("!") ? "!" : "";
- const globWithoutPrefix = globPattern.replace(/^!/, "");
-
- if (globWithoutPrefix.startsWith("/")) {
- return `${prefix}/${normalizePathSeps(relativePathToOldBaseDir)}${globWithoutPrefix}`;
- }
-
- return globPattern;
- }
-
-
-
-
-
-
- class IgnoredPaths {
-
-
-
- constructor(providedOptions) {
- const options = mergeDefaultOptions(providedOptions);
-
- this.cache = {};
-
- this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []);
-
- this.ignoreFileDir = options.ignore !== false && options.ignorePath
- ? path.dirname(path.resolve(options.cwd, options.ignorePath))
- : options.cwd;
- this.options = options;
- this._baseDir = null;
-
- this.ig = {
- custom: ignore(),
- default: ignore()
- };
-
- this.defaultPatterns.forEach(pattern => this.addPatternRelativeToCwd(this.ig.default, pattern));
- if (options.dotfiles !== true) {
-
-
-
- this.addPatternRelativeToCwd(this.ig.default, ".*");
- this.addPatternRelativeToCwd(this.ig.default, "!../");
- }
-
-
-
- this.ig.custom.ignoreFiles = [];
- this.ig.default.ignoreFiles = [];
-
- if (options.ignore !== false) {
- let ignorePath;
-
- if (options.ignorePath) {
- debug("Using specific ignore file");
-
- try {
- fs.statSync(options.ignorePath);
- ignorePath = options.ignorePath;
- } catch (e) {
- e.message = `Cannot read ignore file: ${options.ignorePath}\nError: ${e.message}`;
- throw e;
- }
- } else {
- debug(`Looking for ignore file in ${options.cwd}`);
- ignorePath = findIgnoreFile(options.cwd);
-
- try {
- fs.statSync(ignorePath);
- debug(`Loaded ignore file ${ignorePath}`);
- } catch (e) {
- debug("Could not find ignore file in cwd");
- }
- }
-
- if (ignorePath) {
- debug(`Adding ${ignorePath}`);
- this.addIgnoreFile(this.ig.custom, ignorePath);
- this.addIgnoreFile(this.ig.default, ignorePath);
- } else {
- try {
-
-
- const packageJSONPath = findPackageJSONFile(options.cwd);
-
- if (packageJSONPath) {
- let packageJSONOptions;
-
- try {
- packageJSONOptions = JSON.parse(fs.readFileSync(packageJSONPath, "utf8"));
- } catch (e) {
- debug("Could not read package.json file to check eslintIgnore property");
- e.messageTemplate = "failed-to-read-json";
- e.messageData = {
- path: packageJSONPath,
- message: e.message
- };
- throw e;
- }
-
- if (packageJSONOptions.eslintIgnore) {
- if (Array.isArray(packageJSONOptions.eslintIgnore)) {
- packageJSONOptions.eslintIgnore.forEach(pattern => {
- this.addPatternRelativeToIgnoreFile(this.ig.custom, pattern);
- this.addPatternRelativeToIgnoreFile(this.ig.default, pattern);
- });
- } else {
- throw new TypeError("Package.json eslintIgnore property requires an array of paths");
- }
- }
- }
- } catch (e) {
- debug("Could not find package.json to check eslintIgnore property");
- throw e;
- }
- }
-
- if (options.ignorePattern) {
- this.addPatternRelativeToCwd(this.ig.custom, options.ignorePattern);
- this.addPatternRelativeToCwd(this.ig.default, options.ignorePattern);
- }
- }
- }
-
-
-
-
- addPatternRelativeToCwd(ig, pattern) {
- const baseDir = this.getBaseDir();
- const cookedPattern = baseDir === this.options.cwd
- ? pattern
- : relativize(pattern, path.relative(baseDir, this.options.cwd));
-
- ig.addPattern(cookedPattern);
- debug("addPatternRelativeToCwd:\n original = %j\n cooked = %j", pattern, cookedPattern);
- }
-
- addPatternRelativeToIgnoreFile(ig, pattern) {
- const baseDir = this.getBaseDir();
- const cookedPattern = baseDir === this.ignoreFileDir
- ? pattern
- : relativize(pattern, path.relative(baseDir, this.ignoreFileDir));
-
- ig.addPattern(cookedPattern);
- debug("addPatternRelativeToIgnoreFile:\n original = %j\n cooked = %j", pattern, cookedPattern);
- }
-
-
- getBaseDir() {
- if (!this._baseDir) {
- const a = path.resolve(this.options.cwd);
- const b = path.resolve(this.ignoreFileDir);
- let lastSepPos = 0;
-
-
- this._baseDir = a.length < b.length ? a : b;
-
-
- for (let i = 0; i < a.length && i < b.length; ++i) {
- if (a[i] !== b[i]) {
- this._baseDir = a.slice(0, lastSepPos);
- break;
- }
- if (a[i] === path.sep) {
- lastSepPos = i;
- }
- }
-
-
- if (/^[A-Z]:$/.test(this._baseDir)) {
- this._baseDir += "\\";
- }
-
- debug("baseDir = %j", this._baseDir);
- }
- return this._baseDir;
- }
-
-
-
- readIgnoreFile(filePath) {
- if (typeof this.cache[filePath] === "undefined") {
- this.cache[filePath] = fs.readFileSync(filePath, "utf8").split(/\r?\n/g).filter(Boolean);
- }
- return this.cache[filePath];
- }
-
-
-
- addIgnoreFile(ig, filePath) {
- ig.ignoreFiles.push(filePath);
- this
- .readIgnoreFile(filePath)
- .forEach(ignoreRule => this.addPatternRelativeToIgnoreFile(ig, ignoreRule));
- }
-
-
-
- contains(filepath, category) {
-
- let result = false;
- const absolutePath = path.resolve(this.options.cwd, filepath);
- const relativePath = pathUtils.getRelativePath(absolutePath, this.getBaseDir());
-
- if (typeof category === "undefined") {
- result = (this.ig.default.filter([relativePath]).length === 0) ||
- (this.ig.custom.filter([relativePath]).length === 0);
- } else {
- result = (this.ig[category].filter([relativePath]).length === 0);
- }
- debug("contains:");
- debug(" target = %j", filepath);
- debug(" result = %j", result);
-
- return result;
-
- }
-
-
-
- getIgnoredFoldersGlobChecker() {
- const baseDir = this.getBaseDir();
- const ig = ignore();
-
- DEFAULT_IGNORE_DIRS.forEach(ignoreDir => this.addPatternRelativeToCwd(ig, ignoreDir));
-
- if (this.options.dotfiles !== true) {
-
-
- ig.add([".*/*", "!../*"]);
- }
-
- if (this.options.ignore) {
- ig.add(this.ig.custom);
- }
-
- const filter = ig.createFilter();
-
- return function(absolutePath) {
- const relative = pathUtils.getRelativePath(absolutePath, baseDir);
-
- if (!relative) {
- return false;
- }
-
- return !filter(relative);
- };
- }
- }
-
- module.exports = IgnoredPaths;
|