index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. // A simple implementation of make-array
  2. function makeArray (subject) {
  3. return Array.isArray(subject)
  4. ? subject
  5. : [subject]
  6. }
  7. const EMPTY = ''
  8. const SPACE = ' '
  9. const ESCAPE = '\\'
  10. const REGEX_TEST_BLANK_LINE = /^\s+$/
  11. const REGEX_INVALID_TRAILING_BACKSLASH = /(?:[^\\]|^)\\$/
  12. const REGEX_REPLACE_LEADING_EXCAPED_EXCLAMATION = /^\\!/
  13. const REGEX_REPLACE_LEADING_EXCAPED_HASH = /^\\#/
  14. const REGEX_SPLITALL_CRLF = /\r?\n/g
  15. // /foo,
  16. // ./foo,
  17. // ../foo,
  18. // .
  19. // ..
  20. const REGEX_TEST_INVALID_PATH = /^\.*\/|^\.+$/
  21. const SLASH = '/'
  22. // Do not use ternary expression here, since "istanbul ignore next" is buggy
  23. let TMP_KEY_IGNORE = 'node-ignore'
  24. /* istanbul ignore else */
  25. if (typeof Symbol !== 'undefined') {
  26. TMP_KEY_IGNORE = Symbol.for('node-ignore')
  27. }
  28. const KEY_IGNORE = TMP_KEY_IGNORE
  29. const define = (object, key, value) =>
  30. Object.defineProperty(object, key, {value})
  31. const REGEX_REGEXP_RANGE = /([0-z])-([0-z])/g
  32. const RETURN_FALSE = () => false
  33. // Sanitize the range of a regular expression
  34. // The cases are complicated, see test cases for details
  35. const sanitizeRange = range => range.replace(
  36. REGEX_REGEXP_RANGE,
  37. (match, from, to) => from.charCodeAt(0) <= to.charCodeAt(0)
  38. ? match
  39. // Invalid range (out of order) which is ok for gitignore rules but
  40. // fatal for JavaScript regular expression, so eliminate it.
  41. : EMPTY
  42. )
  43. // See fixtures #59
  44. const cleanRangeBackSlash = slashes => {
  45. const {length} = slashes
  46. return slashes.slice(0, length - length % 2)
  47. }
  48. // > If the pattern ends with a slash,
  49. // > it is removed for the purpose of the following description,
  50. // > but it would only find a match with a directory.
  51. // > In other words, foo/ will match a directory foo and paths underneath it,
  52. // > but will not match a regular file or a symbolic link foo
  53. // > (this is consistent with the way how pathspec works in general in Git).
  54. // '`foo/`' will not match regular file '`foo`' or symbolic link '`foo`'
  55. // -> ignore-rules will not deal with it, because it costs extra `fs.stat` call
  56. // you could use option `mark: true` with `glob`
  57. // '`foo/`' should not continue with the '`..`'
  58. const REPLACERS = [
  59. [
  60. // remove BOM
  61. // TODO:
  62. // Other similar zero-width characters?
  63. /^\uFEFF/,
  64. () => EMPTY
  65. ],
  66. // > Trailing spaces are ignored unless they are quoted with backslash ("\")
  67. [
  68. // (a\ ) -> (a )
  69. // (a ) -> (a)
  70. // (a ) -> (a)
  71. // (a \ ) -> (a )
  72. /((?:\\\\)*?)(\\?\s+)$/,
  73. (_, m1, m2) => m1 + (
  74. m2.indexOf('\\') === 0
  75. ? SPACE
  76. : EMPTY
  77. )
  78. ],
  79. // replace (\ ) with ' '
  80. // (\ ) -> ' '
  81. // (\\ ) -> '\\ '
  82. // (\\\ ) -> '\\ '
  83. [
  84. /(\\+?)\s/g,
  85. (_, m1) => {
  86. const {length} = m1
  87. return m1.slice(0, length - length % 2) + SPACE
  88. }
  89. ],
  90. // Escape metacharacters
  91. // which is written down by users but means special for regular expressions.
  92. // > There are 12 characters with special meanings:
  93. // > - the backslash \,
  94. // > - the caret ^,
  95. // > - the dollar sign $,
  96. // > - the period or dot .,
  97. // > - the vertical bar or pipe symbol |,
  98. // > - the question mark ?,
  99. // > - the asterisk or star *,
  100. // > - the plus sign +,
  101. // > - the opening parenthesis (,
  102. // > - the closing parenthesis ),
  103. // > - and the opening square bracket [,
  104. // > - the opening curly brace {,
  105. // > These special characters are often called "metacharacters".
  106. [
  107. /[\\$.|*+(){^]/g,
  108. match => `\\${match}`
  109. ],
  110. [
  111. // > a question mark (?) matches a single character
  112. /(?!\\)\?/g,
  113. () => '[^/]'
  114. ],
  115. // leading slash
  116. [
  117. // > A leading slash matches the beginning of the pathname.
  118. // > For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
  119. // A leading slash matches the beginning of the pathname
  120. /^\//,
  121. () => '^'
  122. ],
  123. // replace special metacharacter slash after the leading slash
  124. [
  125. /\//g,
  126. () => '\\/'
  127. ],
  128. [
  129. // > A leading "**" followed by a slash means match in all directories.
  130. // > For example, "**/foo" matches file or directory "foo" anywhere,
  131. // > the same as pattern "foo".
  132. // > "**/foo/bar" matches file or directory "bar" anywhere that is directly
  133. // > under directory "foo".
  134. // Notice that the '*'s have been replaced as '\\*'
  135. /^\^*\\\*\\\*\\\//,
  136. // '**/foo' <-> 'foo'
  137. () => '^(?:.*\\/)?'
  138. ],
  139. // starting
  140. [
  141. // there will be no leading '/'
  142. // (which has been replaced by section "leading slash")
  143. // If starts with '**', adding a '^' to the regular expression also works
  144. /^(?=[^^])/,
  145. function startingReplacer () {
  146. // If has a slash `/` at the beginning or middle
  147. return !/\/(?!$)/.test(this)
  148. // > Prior to 2.22.1
  149. // > If the pattern does not contain a slash /,
  150. // > Git treats it as a shell glob pattern
  151. // Actually, if there is only a trailing slash,
  152. // git also treats it as a shell glob pattern
  153. // After 2.22.1 (compatible but clearer)
  154. // > If there is a separator at the beginning or middle (or both)
  155. // > of the pattern, then the pattern is relative to the directory
  156. // > level of the particular .gitignore file itself.
  157. // > Otherwise the pattern may also match at any level below
  158. // > the .gitignore level.
  159. ? '(?:^|\\/)'
  160. // > Otherwise, Git treats the pattern as a shell glob suitable for
  161. // > consumption by fnmatch(3)
  162. : '^'
  163. }
  164. ],
  165. // two globstars
  166. [
  167. // Use lookahead assertions so that we could match more than one `'/**'`
  168. /\\\/\\\*\\\*(?=\\\/|$)/g,
  169. // Zero, one or several directories
  170. // should not use '*', or it will be replaced by the next replacer
  171. // Check if it is not the last `'/**'`
  172. (_, index, str) => index + 6 < str.length
  173. // case: /**/
  174. // > A slash followed by two consecutive asterisks then a slash matches
  175. // > zero or more directories.
  176. // > For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
  177. // '/**/'
  178. ? '(?:\\/[^\\/]+)*'
  179. // case: /**
  180. // > A trailing `"/**"` matches everything inside.
  181. // #21: everything inside but it should not include the current folder
  182. : '\\/.+'
  183. ],
  184. // normal intermediate wildcards
  185. [
  186. // Never replace escaped '*'
  187. // ignore rule '\*' will match the path '*'
  188. // 'abc.*/' -> go
  189. // 'abc.*' -> skip this rule,
  190. // coz trailing single wildcard will be handed by [trailing wildcard]
  191. /(^|[^\\]+)(\\\*)+(?=.+)/g,
  192. // '*.js' matches '.js'
  193. // '*.js' doesn't match 'abc'
  194. (_, p1, p2) => {
  195. // 1.
  196. // > An asterisk "*" matches anything except a slash.
  197. // 2.
  198. // > Other consecutive asterisks are considered regular asterisks
  199. // > and will match according to the previous rules.
  200. const unescaped = p2.replace(/\\\*/g, '[^\\/]*')
  201. return p1 + unescaped
  202. }
  203. ],
  204. [
  205. // unescape, revert step 3 except for back slash
  206. // For example, if a user escape a '\\*',
  207. // after step 3, the result will be '\\\\\\*'
  208. /\\\\\\(?=[$.|*+(){^])/g,
  209. () => ESCAPE
  210. ],
  211. [
  212. // '\\\\' -> '\\'
  213. /\\\\/g,
  214. () => ESCAPE
  215. ],
  216. [
  217. // > The range notation, e.g. [a-zA-Z],
  218. // > can be used to match one of the characters in a range.
  219. // `\` is escaped by step 3
  220. /(\\)?\[([^\]/]*?)(\\*)($|\])/g,
  221. (match, leadEscape, range, endEscape, close) => leadEscape === ESCAPE
  222. // '\\[bar]' -> '\\\\[bar\\]'
  223. ? `\\[${range}${cleanRangeBackSlash(endEscape)}${close}`
  224. : close === ']'
  225. ? endEscape.length % 2 === 0
  226. // A normal case, and it is a range notation
  227. // '[bar]'
  228. // '[bar\\\\]'
  229. ? `[${sanitizeRange(range)}${endEscape}]`
  230. // Invalid range notaton
  231. // '[bar\\]' -> '[bar\\\\]'
  232. : '[]'
  233. : '[]'
  234. ],
  235. // ending
  236. [
  237. // 'js' will not match 'js.'
  238. // 'ab' will not match 'abc'
  239. /(?:[^*])$/,
  240. // WTF!
  241. // https://git-scm.com/docs/gitignore
  242. // changes in [2.22.1](https://git-scm.com/docs/gitignore/2.22.1)
  243. // which re-fixes #24, #38
  244. // > If there is a separator at the end of the pattern then the pattern
  245. // > will only match directories, otherwise the pattern can match both
  246. // > files and directories.
  247. // 'js*' will not match 'a.js'
  248. // 'js/' will not match 'a.js'
  249. // 'js' will match 'a.js' and 'a.js/'
  250. match => /\/$/.test(match)
  251. // foo/ will not match 'foo'
  252. ? `${match}$`
  253. // foo matches 'foo' and 'foo/'
  254. : `${match}(?=$|\\/$)`
  255. ],
  256. // trailing wildcard
  257. [
  258. /(\^|\\\/)?\\\*$/,
  259. (_, p1) => {
  260. const prefix = p1
  261. // '\^':
  262. // '/*' does not match EMPTY
  263. // '/*' does not match everything
  264. // '\\\/':
  265. // 'abc/*' does not match 'abc/'
  266. ? `${p1}[^/]+`
  267. // 'a*' matches 'a'
  268. // 'a*' matches 'aa'
  269. : '[^/]*'
  270. return `${prefix}(?=$|\\/$)`
  271. }
  272. ],
  273. ]
  274. // A simple cache, because an ignore rule only has only one certain meaning
  275. const regexCache = Object.create(null)
  276. // @param {pattern}
  277. const makeRegex = (pattern, ignoreCase) => {
  278. let source = regexCache[pattern]
  279. if (!source) {
  280. source = REPLACERS.reduce(
  281. (prev, [matcher, replacer]) =>
  282. prev.replace(matcher, replacer.bind(pattern)),
  283. pattern
  284. )
  285. regexCache[pattern] = source
  286. }
  287. return ignoreCase
  288. ? new RegExp(source, 'i')
  289. : new RegExp(source)
  290. }
  291. const isString = subject => typeof subject === 'string'
  292. // > A blank line matches no files, so it can serve as a separator for readability.
  293. const checkPattern = pattern => pattern
  294. && isString(pattern)
  295. && !REGEX_TEST_BLANK_LINE.test(pattern)
  296. && !REGEX_INVALID_TRAILING_BACKSLASH.test(pattern)
  297. // > A line starting with # serves as a comment.
  298. && pattern.indexOf('#') !== 0
  299. const splitPattern = pattern => pattern.split(REGEX_SPLITALL_CRLF)
  300. class IgnoreRule {
  301. constructor (
  302. origin,
  303. pattern,
  304. negative,
  305. regex
  306. ) {
  307. this.origin = origin
  308. this.pattern = pattern
  309. this.negative = negative
  310. this.regex = regex
  311. }
  312. }
  313. const createRule = (pattern, ignoreCase) => {
  314. const origin = pattern
  315. let negative = false
  316. // > An optional prefix "!" which negates the pattern;
  317. if (pattern.indexOf('!') === 0) {
  318. negative = true
  319. pattern = pattern.substr(1)
  320. }
  321. pattern = pattern
  322. // > Put a backslash ("\") in front of the first "!" for patterns that
  323. // > begin with a literal "!", for example, `"\!important!.txt"`.
  324. .replace(REGEX_REPLACE_LEADING_EXCAPED_EXCLAMATION, '!')
  325. // > Put a backslash ("\") in front of the first hash for patterns that
  326. // > begin with a hash.
  327. .replace(REGEX_REPLACE_LEADING_EXCAPED_HASH, '#')
  328. const regex = makeRegex(pattern, ignoreCase)
  329. return new IgnoreRule(
  330. origin,
  331. pattern,
  332. negative,
  333. regex
  334. )
  335. }
  336. const throwError = (message, Ctor) => {
  337. throw new Ctor(message)
  338. }
  339. const checkPath = (path, originalPath, doThrow) => {
  340. if (!isString(path)) {
  341. return doThrow(
  342. `path must be a string, but got \`${originalPath}\``,
  343. TypeError
  344. )
  345. }
  346. // We don't know if we should ignore EMPTY, so throw
  347. if (!path) {
  348. return doThrow(`path must not be empty`, TypeError)
  349. }
  350. // Check if it is a relative path
  351. if (checkPath.isNotRelative(path)) {
  352. const r = '`path.relative()`d'
  353. return doThrow(
  354. `path should be a ${r} string, but got "${originalPath}"`,
  355. RangeError
  356. )
  357. }
  358. return true
  359. }
  360. const isNotRelative = path => REGEX_TEST_INVALID_PATH.test(path)
  361. checkPath.isNotRelative = isNotRelative
  362. checkPath.convert = p => p
  363. class Ignore {
  364. constructor ({
  365. ignorecase = true,
  366. ignoreCase = ignorecase,
  367. allowRelativePaths = false
  368. } = {}) {
  369. define(this, KEY_IGNORE, true)
  370. this._rules = []
  371. this._ignoreCase = ignoreCase
  372. this._allowRelativePaths = allowRelativePaths
  373. this._initCache()
  374. }
  375. _initCache () {
  376. this._ignoreCache = Object.create(null)
  377. this._testCache = Object.create(null)
  378. }
  379. _addPattern (pattern) {
  380. // #32
  381. if (pattern && pattern[KEY_IGNORE]) {
  382. this._rules = this._rules.concat(pattern._rules)
  383. this._added = true
  384. return
  385. }
  386. if (checkPattern(pattern)) {
  387. const rule = createRule(pattern, this._ignoreCase)
  388. this._added = true
  389. this._rules.push(rule)
  390. }
  391. }
  392. // @param {Array<string> | string | Ignore} pattern
  393. add (pattern) {
  394. this._added = false
  395. makeArray(
  396. isString(pattern)
  397. ? splitPattern(pattern)
  398. : pattern
  399. ).forEach(this._addPattern, this)
  400. // Some rules have just added to the ignore,
  401. // making the behavior changed.
  402. if (this._added) {
  403. this._initCache()
  404. }
  405. return this
  406. }
  407. // legacy
  408. addPattern (pattern) {
  409. return this.add(pattern)
  410. }
  411. // | ignored : unignored
  412. // negative | 0:0 | 0:1 | 1:0 | 1:1
  413. // -------- | ------- | ------- | ------- | --------
  414. // 0 | TEST | TEST | SKIP | X
  415. // 1 | TESTIF | SKIP | TEST | X
  416. // - SKIP: always skip
  417. // - TEST: always test
  418. // - TESTIF: only test if checkUnignored
  419. // - X: that never happen
  420. // @param {boolean} whether should check if the path is unignored,
  421. // setting `checkUnignored` to `false` could reduce additional
  422. // path matching.
  423. // @returns {TestResult} true if a file is ignored
  424. _testOne (path, checkUnignored) {
  425. let ignored = false
  426. let unignored = false
  427. this._rules.forEach(rule => {
  428. const {negative} = rule
  429. if (
  430. unignored === negative && ignored !== unignored
  431. || negative && !ignored && !unignored && !checkUnignored
  432. ) {
  433. return
  434. }
  435. const matched = rule.regex.test(path)
  436. if (matched) {
  437. ignored = !negative
  438. unignored = negative
  439. }
  440. })
  441. return {
  442. ignored,
  443. unignored
  444. }
  445. }
  446. // @returns {TestResult}
  447. _test (originalPath, cache, checkUnignored, slices) {
  448. const path = originalPath
  449. // Supports nullable path
  450. && checkPath.convert(originalPath)
  451. checkPath(
  452. path,
  453. originalPath,
  454. this._allowRelativePaths
  455. ? RETURN_FALSE
  456. : throwError
  457. )
  458. return this._t(path, cache, checkUnignored, slices)
  459. }
  460. _t (path, cache, checkUnignored, slices) {
  461. if (path in cache) {
  462. return cache[path]
  463. }
  464. if (!slices) {
  465. // path/to/a.js
  466. // ['path', 'to', 'a.js']
  467. slices = path.split(SLASH)
  468. }
  469. slices.pop()
  470. // If the path has no parent directory, just test it
  471. if (!slices.length) {
  472. return cache[path] = this._testOne(path, checkUnignored)
  473. }
  474. const parent = this._t(
  475. slices.join(SLASH) + SLASH,
  476. cache,
  477. checkUnignored,
  478. slices
  479. )
  480. // If the path contains a parent directory, check the parent first
  481. return cache[path] = parent.ignored
  482. // > It is not possible to re-include a file if a parent directory of
  483. // > that file is excluded.
  484. ? parent
  485. : this._testOne(path, checkUnignored)
  486. }
  487. ignores (path) {
  488. return this._test(path, this._ignoreCache, false).ignored
  489. }
  490. createFilter () {
  491. return path => !this.ignores(path)
  492. }
  493. filter (paths) {
  494. return makeArray(paths).filter(this.createFilter())
  495. }
  496. // @returns {TestResult}
  497. test (path) {
  498. return this._test(path, this._testCache, true)
  499. }
  500. }
  501. const factory = options => new Ignore(options)
  502. const isPathValid = path =>
  503. checkPath(path && checkPath.convert(path), path, RETURN_FALSE)
  504. factory.isPathValid = isPathValid
  505. // Fixes typescript
  506. factory.default = factory
  507. module.exports = factory
  508. // Windows
  509. // --------------------------------------------------------------
  510. /* istanbul ignore if */
  511. if (
  512. // Detect `process` so that it can run in browsers.
  513. typeof process !== 'undefined'
  514. && (
  515. process.env && process.env.IGNORE_TEST_WIN32
  516. || process.platform === 'win32'
  517. )
  518. ) {
  519. /* eslint no-control-regex: "off" */
  520. const makePosix = str => /^\\\\\?\\/.test(str)
  521. || /["<>|\u0000-\u001F]+/u.test(str)
  522. ? str
  523. : str.replace(/\\/g, '/')
  524. checkPath.convert = makePosix
  525. // 'C:\\foo' <- 'C:\\foo' has been converted to 'C:/'
  526. // 'd:\\foo'
  527. const REGIX_IS_WINDOWS_PATH_ABSOLUTE = /^[a-z]:\//i
  528. checkPath.isNotRelative = path =>
  529. REGIX_IS_WINDOWS_PATH_ABSOLUTE.test(path)
  530. || isNotRelative(path)
  531. }