Blame view
node_modules/eslint/lib/ignored-paths.js
7.27 KB
f7563de62
|
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 |
/** * @fileoverview Responsible for loading ignore config files and managing ignore patterns * @author Jonathan Rajavuori */ "use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ const fs = require("fs"), path = require("path"), ignore = require("ignore"), shell = require("shelljs"), pathUtil = require("./util/path-util"); const debug = require("debug")("eslint:ignored-paths"); //------------------------------------------------------------------------------ // Constants //------------------------------------------------------------------------------ const ESLINT_IGNORE_FILENAME = ".eslintignore"; /** * Adds `"*"` at the end of `"node_modules/"`, * so that subtle directories could be re-included by .gitignore patterns * such as `"!node_modules/should_not_ignored"` */ const DEFAULT_IGNORE_DIRS = [ "/node_modules/*", "/bower_components/*" ]; const DEFAULT_OPTIONS = { dotfiles: false, cwd: process.cwd() }; //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ /** * Find an ignore file in the current directory. * @param {string} cwd Current working directory * @returns {string} Path of ignore file or an empty string. */ function findIgnoreFile(cwd) { cwd = cwd || DEFAULT_OPTIONS.cwd; const ignoreFilePath = path.resolve(cwd, ESLINT_IGNORE_FILENAME); return shell.test("-f", ignoreFilePath) ? ignoreFilePath : ""; } /** * Merge options with defaults * @param {Object} options Options to merge with DEFAULT_OPTIONS constant * @returns {Object} Merged options */ function mergeDefaultOptions(options) { options = (options || {}); return Object.assign({}, DEFAULT_OPTIONS, options); } //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ /** * IgnoredPaths class */ class IgnoredPaths { /** * @param {Object} options object containing 'ignore', 'ignorePath' and 'patterns' properties */ constructor(options) { options = mergeDefaultOptions(options); /** * add pattern to node-ignore instance * @param {Object} ig, instance of node-ignore * @param {string} pattern, pattern do add to ig * @returns {array} raw ignore rules */ function addPattern(ig, pattern) { return ig.addPattern(pattern); } /** * add ignore file to node-ignore instance * @param {Object} ig, instance of node-ignore * @param {string} filepath, file to add to ig * @returns {array} raw ignore rules */ function addIgnoreFile(ig, filepath) { ig.ignoreFiles.push(filepath); return ig.add(fs.readFileSync(filepath, "utf8")); } this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []); this.baseDir = options.cwd; this.ig = { custom: ignore(), default: ignore() }; // Add a way to keep track of ignored files. This was present in node-ignore // 2.x, but dropped for now as of 3.0.10. this.ig.custom.ignoreFiles = []; this.ig.default.ignoreFiles = []; if (options.dotfiles !== true) { /* * ignore files beginning with a dot, but not files in a parent or * ancestor directory (which in relative format will begin with `../`). */ addPattern(this.ig.default, [".*", "!../"]); } addPattern(this.ig.default, this.defaultPatterns); 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} Error: ${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"); this.options = options; } } if (ignorePath) { debug(`Adding ${ignorePath}`); this.baseDir = path.dirname(path.resolve(options.cwd, ignorePath)); addIgnoreFile(this.ig.custom, ignorePath); addIgnoreFile(this.ig.default, ignorePath); } if (options.ignorePattern) { addPattern(this.ig.custom, options.ignorePattern); addPattern(this.ig.default, options.ignorePattern); } } this.options = options; } /** * Determine whether a file path is included in the default or custom ignore patterns * @param {string} filepath Path to check * @param {string} [category=null] check 'default', 'custom' or both (null) * @returns {boolean} true if the file path matches one or more patterns, false otherwise */ contains(filepath, category) { let result = false; const absolutePath = path.resolve(this.options.cwd, filepath); const relativePath = pathUtil.getRelativePath(absolutePath, this.options.cwd); if ((typeof category === "undefined") || (category === "default")) { result = result || (this.ig.default.filter([relativePath]).length === 0); } if ((typeof category === "undefined") || (category === "custom")) { result = result || (this.ig.custom.filter([relativePath]).length === 0); } return result; } /** * Returns a list of dir patterns for glob to ignore * @returns {function()} method to check whether a folder should be ignored by glob. */ getIgnoredFoldersGlobChecker() { const ig = ignore().add(DEFAULT_IGNORE_DIRS); if (this.options.dotfiles !== true) { // Ignore hidden folders. (This cannot be ".*", or else it's not possible to unignore hidden files) ig.add([".*/*", "!../"]); } if (this.options.ignore) { ig.add(this.ig.custom); } const filter = ig.createFilter(); /** * TODO * 1. * Actually, it should be `this.options.baseDir`, which is the base dir of `ignore-path`, * as well as Line 177. * But doing this leads to a breaking change and fails tests. * Related to #6759 */ const base = this.options.cwd; return function(absolutePath) { const relative = pathUtil.getRelativePath(absolutePath, base); if (!relative) { return false; } return !filter(relative); }; } } module.exports = IgnoredPaths; |