Skip to content

Commit f148c7b

Browse files
cb1kenobisindresorhus
authored andcommitted
Add support for relative parent paths (#78)
1 parent 2e3dd05 commit f148c7b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

Diff for: index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ module.exports = function (pattern, options) {
1313
}
1414

1515
return streamfilter(function (file, enc, cb) {
16-
var match = typeof pattern === 'function' ? pattern(file) :
17-
multimatch(path.relative(file.cwd, file.path), pattern, options).length > 0;
16+
var match;
17+
if (typeof pattern === 'function') {
18+
match = pattern(file);
19+
} else {
20+
var relPath = path.relative(file.cwd, file.path);
21+
// if the path leaves the current working directory, then we need to
22+
// resolve the absolute path so that the path can be properly matched
23+
// by minimatch (via multimatch)
24+
if (relPath.indexOf('../') === 0) {
25+
relPath = path.resolve(relPath);
26+
}
27+
match = multimatch(relPath, pattern, options).length > 0;
28+
}
1829

1930
cb(!match);
2031
}, {

Diff for: test.js

+22
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ describe('filter()', function () {
160160

161161
stream.end();
162162
});
163+
164+
it('should filter relative paths that leave current directory tree', function (cb) {
165+
var stream = filter('**/test/**/*.js');
166+
var buffer = [];
167+
var gfile = path.join('..', '..', 'test', 'included.js');
168+
169+
stream.on('data', function (file) {
170+
buffer.push(file);
171+
});
172+
173+
stream.on('end', function () {
174+
assert.equal(buffer.length, 1);
175+
assert.equal(buffer[0].relative, gfile);
176+
cb();
177+
});
178+
179+
stream.write(new gutil.File({
180+
path: gfile
181+
}));
182+
183+
stream.end();
184+
});
163185
});
164186

165187
describe('filter.restore', function () {

0 commit comments

Comments
 (0)