Skip to content

Commit 052a722

Browse files
committed
#75: main, README.md, test, ts: new options.allowRelativePaths to bring backward compatibility
1 parent 92e83e2 commit 052a722

5 files changed

Lines changed: 65 additions & 19 deletions

File tree

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,19 @@ interface TestResult {
298298
- `{ignored: false, unignored: true}`: the `pathname` is unignored
299299
- `{ignored: false, unignored: false}`: the `pathname` is never matched by any ignore rules.
300300

301-
## `options.ignorecase` since 4.0.0
301+
## static `ignore.isPathValid(pathname): boolean` since 5.0.0
302+
303+
Check whether the `pathname` is an valid `path.relative()`d path according to the [convention](#1-pathname-should-be-a-pathrelatived-pathname).
304+
305+
This method is **NOT** used to check if an ignore pattern is valid.
306+
307+
```js
308+
ignore.isPathValid('./foo') // false
309+
```
310+
311+
## ignore(options)
312+
313+
### `options.ignorecase` since 4.0.0
302314

303315
Similar as the `core.ignorecase` option of [git-config](https://git-scm.com/docs/git-config), `node-ignore` will be case insensitive if `options.ignorecase` is set to `true` (the default value), otherwise case sensitive.
304316

@@ -312,14 +324,20 @@ ig.add('*.png')
312324
ig.ignores('*.PNG') // false
313325
```
314326

315-
## static `ignore.isPathValid(pathname): boolean` since 5.0.0
327+
### `options.ignoreCase?: boolean` since 5.2.0
316328

317-
Check whether the `pathname` is an valid `path.relative()`d path according to the [convention](#1-pathname-should-be-a-pathrelatived-pathname).
329+
Which is alternative to `options.ignoreCase`
318330

319-
This method is **NOT** used to check if an ignore pattern is valid.
331+
### `options.allowRelativePaths?: boolean` since 5.2.0
332+
333+
This option brings backward compatibility with projects which based on `[email protected]`
334+
335+
However, passing a relative path to test if it is ignored or not is not a good practise, which might lead to unexpected behavior
320336

321337
```js
322-
ignore.isPathValid('./foo') // false
338+
ignore({
339+
allowRelativePaths: true
340+
}).ignores('../foo/bar.js') // And it will not throw
323341
```
324342

325343
****
@@ -328,7 +346,9 @@ ignore.isPathValid('./foo') // false
328346

329347
## Upgrade 4.x -> 5.x
330348

331-
Since `5.0.0`, if an invalid `Pathname` passed into `ig.ignores()`, an error will be thrown, while `ignore < 5.0.0` did not make sure what the return value was, as well as
349+
Since `5.0.0`, if an invalid `Pathname` passed into `ig.ignores()`, an error will be thrown, unless `options.allowRelative = true` is passed to the `Ignore` factory.
350+
351+
While `ignore < 5.0.0` did not make sure what the return value was, as well as
332352

333353
```ts
334354
.ignores(pathname: Pathname): boolean

index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface Ignore {
2020
* @returns The filtered array of paths
2121
*/
2222
filter(pathnames: readonly Pathname[]): Pathname[]
23-
23+
2424
/**
2525
* Creates a filter function which could filter
2626
* an array of paths with Array.prototype.filter.
@@ -44,6 +44,9 @@ export interface Ignore {
4444

4545
interface Options {
4646
ignorecase?: boolean
47+
// For compatibility
48+
ignoreCase?: boolean
49+
allowRelativePaths?: boolean
4750
}
4851

4952
/**

index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const define = (object, key, value) =>
3030

3131
const REGEX_REGEXP_RANGE = /([0-z])-([0-z])/g
3232

33+
const RETURN_FALSE = () => false
34+
3335
// Sanitize the range of a regular expression
3436
// The cases are complicated, see test cases for details
3537
const sanitizeRange = range => range.replace(
@@ -288,7 +290,7 @@ const REPLACERS = [
288290
const regexCache = Object.create(null)
289291

290292
// @param {pattern}
291-
const makeRegex = (pattern, ignorecase) => {
293+
const makeRegex = (pattern, ignoreCase) => {
292294
let source = regexCache[pattern]
293295

294296
if (!source) {
@@ -299,7 +301,7 @@ const makeRegex = (pattern, ignorecase) => {
299301
regexCache[pattern] = source
300302
}
301303

302-
return ignorecase
304+
return ignoreCase
303305
? new RegExp(source, 'i')
304306
: new RegExp(source)
305307
}
@@ -330,7 +332,7 @@ class IgnoreRule {
330332
}
331333
}
332334

333-
const createRule = (pattern, ignorecase) => {
335+
const createRule = (pattern, ignoreCase) => {
334336
const origin = pattern
335337
let negative = false
336338

@@ -348,7 +350,7 @@ const createRule = (pattern, ignorecase) => {
348350
// > begin with a hash.
349351
.replace(REGEX_REPLACE_LEADING_EXCAPED_HASH, '#')
350352

351-
const regex = makeRegex(pattern, ignorecase)
353+
const regex = makeRegex(pattern, ignoreCase)
352354

353355
return new IgnoreRule(
354356
origin,
@@ -394,12 +396,15 @@ checkPath.convert = p => p
394396

395397
class Ignore {
396398
constructor ({
397-
ignorecase = true
399+
ignorecase = true,
400+
ignoreCase = ignorecase,
401+
allowRelativePaths = false
398402
} = {}) {
399403
define(this, KEY_IGNORE, true)
400404

401405
this._rules = []
402-
this._ignorecase = ignorecase
406+
this._ignoreCase = ignoreCase
407+
this._allowRelativePaths = allowRelativePaths
403408
this._initCache()
404409
}
405410

@@ -417,7 +422,7 @@ class Ignore {
417422
}
418423

419424
if (checkPattern(pattern)) {
420-
const rule = createRule(pattern, this._ignorecase)
425+
const rule = createRule(pattern, this._ignoreCase)
421426
this._added = true
422427
this._rules.push(rule)
423428
}
@@ -496,7 +501,13 @@ class Ignore {
496501
// Supports nullable path
497502
&& checkPath.convert(originalPath)
498503

499-
checkPath(path, originalPath, throwError)
504+
checkPath(
505+
path,
506+
originalPath,
507+
this._allowRelativePaths
508+
? RETURN_FALSE
509+
: throwError
510+
)
500511

501512
return this._t(path, cache, checkUnignored, slices)
502513
}
@@ -554,10 +565,8 @@ class Ignore {
554565

555566
const factory = options => new Ignore(options)
556567

557-
const returnFalse = () => false
558-
559568
const isPathValid = path =>
560-
checkPath(path && checkPath.convert(path), path, returnFalse)
569+
checkPath(path && checkPath.convert(path), path, RETURN_FALSE)
561570

562571
factory.isPathValid = isPathValid
563572

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ignore",
3-
"version": "5.1.9",
3+
"version": "5.2.0",
44
"description": "Ignore is a manager and filter for .gitignore rules, the one used by eslint, gitbook and many others.",
55
"files": [
66
"legacy.js",

test/others.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,17 @@ IGNORE_TEST_CASES.forEach(([d, patterns, path, [ignored, unignored]]) => {
227227
t.end()
228228
})
229229
})
230+
231+
_test('options.allowRelativePaths', t => {
232+
const ig = ignore({
233+
allowRelativePaths: true
234+
})
235+
236+
ig.add('foo')
237+
238+
t.is(ig.ignores('../foo/bar.js'), true)
239+
240+
t.throws(() => ignore().ignores('../foo/bar.js'))
241+
242+
t.end()
243+
})

0 commit comments

Comments
 (0)