Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Resolve ../ at the beginning of globs #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"gulp-format-md": "^2.0.0",
"mocha": "^10.1.0"
"mocha": "^3.0.2"
},
"keywords": [
"absolute",
Expand Down
37 changes: 37 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,48 @@ 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')));
});

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 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)')));
Expand Down