From d96a7d98c5cfabb71aa69cb2c54431fef1a76307 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 21 Nov 2022 14:23:25 -0700 Subject: [PATCH 1/3] fix: Check negative patterns before trimming --- index.js | 14 +++++++------- test.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index a09536a..8b2d74c 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,13 @@ module.exports = function(glob, options) { rootDir = escape(rootDir); } + // store last character before glob is modified + var suffix = glob.slice(-1); + + // check to see if glob is negated (and not a leading negated-extglob) + var ing = isNegated(glob); + glob = ing.pattern; + // trim starting ./ from glob patterns if (glob.slice(0, 2) === './') { glob = glob.slice(2); @@ -35,13 +42,6 @@ module.exports = function(glob, options) { glob = ''; } - // store last character before glob is modified - var suffix = glob.slice(-1); - - // check to see if glob is negated (and not a leading negated-extglob) - var ing = isNegated(glob); - glob = ing.pattern; - // make glob absolute if (rootDir && glob.charAt(0) === '/') { glob = join(rootDir, glob); diff --git a/test.js b/test.js index d530cbb..124d956 100644 --- a/test.js +++ b/test.js @@ -45,6 +45,21 @@ describe('resolve', function() { assert.equal(actual, '!' + unixify(path.resolve('a/*.js'))); }); + it('should make a negative glob (starting with `./`) absolute', function() { + actual = resolve('!./a/*.js'); + assert.equal(actual, '!' + unixify(path.resolve('a/*.js'))); + }); + + it('should make a negative glob (just `./`) absolute', function() { + actual = resolve('!./'); + assert.equal(actual, '!' + unixify(path.resolve('.')) + '/'); + }); + + it('should make a negative glob (just `.`) absolute', function() { + actual = resolve('!.'); + assert.equal(actual, '!' + unixify(path.resolve('.'))); + }); + it('should make a negative extglob absolute', function() { actual = resolve('!(foo)'); assert.equal(actual, unixify(path.resolve('!(foo)'))); From 575de86309ab8a0845733d5cb63b2a3bf749ff06 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 21 Nov 2022 14:27:31 -0700 Subject: [PATCH 2/3] chore: Revert the mocha change to test on old node --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dc5db47..1218621 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "devDependencies": { "gulp-format-md": "^2.0.0", - "mocha": "^10.1.0" + "mocha": "^3.0.2" }, "keywords": [ "absolute", From c36d5ddfc085749dbb8cd024a23e2da3ed210667 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 21 Nov 2022 15:15:10 -0700 Subject: [PATCH 3/3] fix: Resolve ../ at the beginning of globs --- index.js | 7 +++++++ test.js | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/index.js b/index.js index 8b2d74c..f9f3834 100644 --- a/index.js +++ b/index.js @@ -80,5 +80,12 @@ function join(dir, glob) { glob = glob.slice(1); } if (!glob) return dir; + + // Resolve `../` segements in the glob + while (glob.slice(0, 3) === '../') { + dir = dir.slice(0, dir.lastIndexOf('/')); + glob = glob.slice(3); + } + return dir + '/' + glob; } diff --git a/test.js b/test.js index 124d956..c3c811e 100644 --- a/test.js +++ b/test.js @@ -40,6 +40,18 @@ describe('resolve', function() { assert.equal(actual, unixify(path.resolve(fixture)) + '/'); }); + it('should handle ../ at the beginnnig of a glob', function() { + fixture = '../fixtures/whatsgoingon/*/'; + actual = resolve(fixture, {cwd: __dirname}); + assert.equal(actual, unixify(path.resolve(fixture)) + '/'); + }); + + it('should handle multiple ../ at the beginnnig of a glob', function() { + fixture = '../../fixtures/whatsgoingon/*/'; + actual = resolve(fixture, {cwd: __dirname}); + assert.equal(actual, unixify(path.resolve(fixture)) + '/'); + }); + it('should make a negative glob absolute', function() { actual = resolve('!a/*.js'); assert.equal(actual, '!' + unixify(path.resolve('a/*.js'))); @@ -60,6 +72,16 @@ describe('resolve', function() { assert.equal(actual, '!' + unixify(path.resolve('.'))); }); + it('should make a negative glob (starting with ../) absolute', function() { + actual = resolve('!../fixtures/whatsgoingon/*/'); + assert.equal(actual, '!' + unixify(path.resolve('../fixtures/whatsgoingon/*/')) + '/'); + }); + + it('should make a negative glob (starting with multiple ../) absolute', function() { + actual = resolve('!../../fixtures/whatsgoingon/*/'); + assert.equal(actual, '!' + unixify(path.resolve('../../fixtures/whatsgoingon/*/')) + '/'); + }); + it('should make a negative extglob absolute', function() { actual = resolve('!(foo)'); assert.equal(actual, unixify(path.resolve('!(foo)')));