ignore-pattern.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * @fileoverview `IgnorePattern` class.
  3. *
  4. * `IgnorePattern` class has the set of glob patterns and the base path.
  5. *
  6. * It provides two static methods.
  7. *
  8. * - `IgnorePattern.createDefaultIgnore(cwd)`
  9. * Create the default predicate function.
  10. * - `IgnorePattern.createIgnore(ignorePatterns)`
  11. * Create the predicate function from multiple `IgnorePattern` objects.
  12. *
  13. * It provides two properties and a method.
  14. *
  15. * - `patterns`
  16. * The glob patterns that ignore to lint.
  17. * - `basePath`
  18. * The base path of the glob patterns. If absolute paths existed in the
  19. * glob patterns, those are handled as relative paths to the base path.
  20. * - `getPatternsRelativeTo(basePath)`
  21. * Get `patterns` as modified for a given base path. It modifies the
  22. * absolute paths in the patterns as prepending the difference of two base
  23. * paths.
  24. *
  25. * `ConfigArrayFactory` creates `IgnorePattern` objects when it processes
  26. * `ignorePatterns` properties.
  27. *
  28. * @author Toru Nagashima <https://github.com/mysticatea>
  29. */
  30. "use strict";
  31. //------------------------------------------------------------------------------
  32. // Requirements
  33. //------------------------------------------------------------------------------
  34. const assert = require("assert");
  35. const path = require("path");
  36. const ignore = require("ignore");
  37. const debug = require("debug")("eslint:ignore-pattern");
  38. /** @typedef {ReturnType<import("ignore").default>} Ignore */
  39. //------------------------------------------------------------------------------
  40. // Helpers
  41. //------------------------------------------------------------------------------
  42. /**
  43. * Get the path to the common ancestor directory of given paths.
  44. * @param {string[]} sourcePaths The paths to calculate the common ancestor.
  45. * @returns {string} The path to the common ancestor directory.
  46. */
  47. function getCommonAncestorPath(sourcePaths) {
  48. let result = sourcePaths[0];
  49. for (let i = 1; i < sourcePaths.length; ++i) {
  50. const a = result;
  51. const b = sourcePaths[i];
  52. // Set the shorter one (it's the common ancestor if one includes the other).
  53. result = a.length < b.length ? a : b;
  54. // Set the common ancestor.
  55. for (let j = 0, lastSepPos = 0; j < a.length && j < b.length; ++j) {
  56. if (a[j] !== b[j]) {
  57. result = a.slice(0, lastSepPos);
  58. break;
  59. }
  60. if (a[j] === path.sep) {
  61. lastSepPos = j;
  62. }
  63. }
  64. }
  65. return result || path.sep;
  66. }
  67. /**
  68. * Make relative path.
  69. * @param {string} from The source path to get relative path.
  70. * @param {string} to The destination path to get relative path.
  71. * @returns {string} The relative path.
  72. */
  73. function relative(from, to) {
  74. const relPath = path.relative(from, to);
  75. if (path.sep === "/") {
  76. return relPath;
  77. }
  78. return relPath.split(path.sep).join("/");
  79. }
  80. /**
  81. * Get the trailing slash if existed.
  82. * @param {string} filePath The path to check.
  83. * @returns {string} The trailing slash if existed.
  84. */
  85. function dirSuffix(filePath) {
  86. const isDir = (
  87. filePath.endsWith(path.sep) ||
  88. (process.platform === "win32" && filePath.endsWith("/"))
  89. );
  90. return isDir ? "/" : "";
  91. }
  92. const DefaultPatterns = Object.freeze(["/node_modules/*", "/bower_components/*"]);
  93. const DotPatterns = Object.freeze([".*", "!../"]);
  94. //------------------------------------------------------------------------------
  95. // Public
  96. //------------------------------------------------------------------------------
  97. class IgnorePattern {
  98. /**
  99. * The default patterns.
  100. * @type {string[]}
  101. */
  102. static get DefaultPatterns() {
  103. return DefaultPatterns;
  104. }
  105. /**
  106. * Create the default predicate function.
  107. * @param {string} cwd The current working directory.
  108. * @returns {((filePath:string, dot:boolean) => boolean) & {basePath:string; patterns:string[]}}
  109. * The preficate function.
  110. * The first argument is an absolute path that is checked.
  111. * The second argument is the flag to not ignore dotfiles.
  112. * If the predicate function returned `true`, it means the path should be ignored.
  113. */
  114. static createDefaultIgnore(cwd) {
  115. return this.createIgnore([new IgnorePattern(DefaultPatterns, cwd)]);
  116. }
  117. /**
  118. * Create the predicate function from multiple `IgnorePattern` objects.
  119. * @param {IgnorePattern[]} ignorePatterns The list of ignore patterns.
  120. * @returns {((filePath:string, dot?:boolean) => boolean) & {basePath:string; patterns:string[]}}
  121. * The preficate function.
  122. * The first argument is an absolute path that is checked.
  123. * The second argument is the flag to not ignore dotfiles.
  124. * If the predicate function returned `true`, it means the path should be ignored.
  125. */
  126. static createIgnore(ignorePatterns) {
  127. debug("Create with: %o", ignorePatterns);
  128. const basePath = getCommonAncestorPath(ignorePatterns.map(p => p.basePath));
  129. const patterns = [].concat(
  130. ...ignorePatterns.map(p => p.getPatternsRelativeTo(basePath))
  131. );
  132. const ig = ignore().add([...DotPatterns, ...patterns]);
  133. const dotIg = ignore().add(patterns);
  134. debug(" processed: %o", { basePath, patterns });
  135. return Object.assign(
  136. (filePath, dot = false) => {
  137. assert(path.isAbsolute(filePath), "'filePath' should be an absolute path.");
  138. const relPathRaw = relative(basePath, filePath);
  139. const relPath = relPathRaw && (relPathRaw + dirSuffix(filePath));
  140. const adoptedIg = dot ? dotIg : ig;
  141. const result = relPath !== "" && adoptedIg.ignores(relPath);
  142. debug("Check", { filePath, dot, relativePath: relPath, result });
  143. return result;
  144. },
  145. { basePath, patterns }
  146. );
  147. }
  148. /**
  149. * Initialize a new `IgnorePattern` instance.
  150. * @param {string[]} patterns The glob patterns that ignore to lint.
  151. * @param {string} basePath The base path of `patterns`.
  152. */
  153. constructor(patterns, basePath) {
  154. assert(path.isAbsolute(basePath), "'basePath' should be an absolute path.");
  155. /**
  156. * The glob patterns that ignore to lint.
  157. * @type {string[]}
  158. */
  159. this.patterns = patterns;
  160. /**
  161. * The base path of `patterns`.
  162. * @type {string}
  163. */
  164. this.basePath = basePath;
  165. /**
  166. * If `true` then patterns which don't start with `/` will match the paths to the outside of `basePath`. Defaults to `false`.
  167. *
  168. * It's set `true` for `.eslintignore`, `package.json`, and `--ignore-path` for backward compatibility.
  169. * It's `false` as-is for `ignorePatterns` property in config files.
  170. * @type {boolean}
  171. */
  172. this.loose = false;
  173. }
  174. /**
  175. * Get `patterns` as modified for a given base path. It modifies the
  176. * absolute paths in the patterns as prepending the difference of two base
  177. * paths.
  178. * @param {string} newBasePath The base path.
  179. * @returns {string[]} Modifired patterns.
  180. */
  181. getPatternsRelativeTo(newBasePath) {
  182. assert(path.isAbsolute(newBasePath), "'newBasePath' should be an absolute path.");
  183. const { basePath, loose, patterns } = this;
  184. if (newBasePath === basePath) {
  185. return patterns;
  186. }
  187. const prefix = `/${relative(newBasePath, basePath)}`;
  188. return patterns.map(pattern => {
  189. const negative = pattern.startsWith("!");
  190. const head = negative ? "!" : "";
  191. const body = negative ? pattern.slice(1) : pattern;
  192. if (body.startsWith("/") || body.startsWith("../")) {
  193. return `${head}${prefix}${body}`;
  194. }
  195. return loose ? pattern : `${head}${prefix}/**/${body}`;
  196. });
  197. }
  198. }
  199. module.exports = { IgnorePattern };