From 371b9ea923bdf607558fb010347c8aed171073c1 Mon Sep 17 00:00:00 2001 From: gfyoung Date: Wed, 3 Nov 2021 10:02:57 +0000 Subject: [PATCH] Restore ability to accept relative paths Blocking the acceptance of relative paths by throwing errors is a relatively harsh change that has made it difficult for downstream libraries (e.g., eslint) to upgrade. Given that relative paths have undefined treatment in ".gitignore" and the usage of this library beyond ".gitignore" behavior, it seems fair to give downstream users the chance to customize the handling of these cases for their own cases. xref: https://github.com/kaelzhang/node-ignore/issues/20 --- index.d.ts | 1 + index.js | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 66657af..45d5c60 100644 --- a/index.d.ts +++ b/index.d.ts @@ -49,6 +49,7 @@ export interface Ignore { interface Options { ignorecase?: boolean + ignorerelative?: boolean } /** diff --git a/index.js b/index.js index 0ff33ea..f6bedf9 100644 --- a/index.js +++ b/index.js @@ -362,7 +362,7 @@ const throwError = (message, Ctor) => { throw new Ctor(message) } -const checkPath = (path, originalPath, doThrow) => { +const checkPath = (path, originalPath, doThrow, ignoreRelative = false) => { if (!isString(path)) { return doThrow( `path must be a string, but got \`${originalPath}\``, @@ -376,7 +376,7 @@ const checkPath = (path, originalPath, doThrow) => { } // Check if it is a relative path - if (checkPath.isNotRelative(path)) { + if (checkPath.isNotRelative(path) && !ignoreRelative) { const r = '`path.relative()`d' return doThrow( `path should be a ${r} string, but got "${originalPath}"`, @@ -394,12 +394,14 @@ checkPath.convert = p => p class Ignore { constructor ({ - ignorecase = true + ignorecase = true, + ignorerelative = false, } = {}) { define(this, KEY_IGNORE, true) this._rules = [] this._ignorecase = ignorecase + this._ignorerelative = ignorerelative this._initCache() } @@ -496,7 +498,7 @@ class Ignore { // Supports nullable path && checkPath.convert(originalPath) - checkPath(path, originalPath, throwError) + checkPath(path, originalPath, throwError, this._ignorerelative) return this._t(path, cache, checkUnignored, slices) } @@ -559,7 +561,7 @@ const returnFalse = () => false const isPathValid = path => checkPath(path && checkPath.convert(path), path, returnFalse) -factory.isPathValid = isPathValid +factory.isPathValid = path => isPathValid(path) // Fixes typescript factory.default = factory