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!: Treats escaped enclosures as normal chars #56

Open
wants to merge 1 commit into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 27 additions & 3 deletions index.js
Expand Up @@ -53,12 +53,36 @@ function isEnclosure(str) {
return false;
}

var foundIndex = str.indexOf(enclosureStart);
if (foundIndex < 0) {
var enclosureIndex = str.length - 1;
var foundIndex = findLastNonBsCharFrom(str, enclosureIndex - 1);
if ((enclosureIndex - foundIndex) % 2 === 0) {
// Last enclosure is escaped.
return false;
}

return str.slice(foundIndex + 1, -1).includes(slash);
while (foundIndex >= 0) {
enclosureIndex = str.lastIndexOf(enclosureStart, foundIndex);
if (enclosureIndex < 0) {
return false;
}

foundIndex = findLastNonBsCharFrom(str, enclosureIndex - 1);
if ((enclosureIndex - foundIndex) % 2 === 0) {
// Enclosure is escaped.
return false;
}
}

return str.slice(enclosureIndex + 1, -1).includes(slash);
}

function findLastNonBsCharFrom(str, startIndex) {
for (var i = startIndex; i >= 0; i--) {
if (str[i] !== '\\') {
return i;
}
}
return -1;
}

function isGlobby(str) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"eslint": "^7.0.0",
"eslint-config-gulp": "^5.0.0",
"eslint-plugin-node": "^11.1.0",
"expect": "^26.0.1",
"mocha": "^7.1.2",
"nyc": "^15.0.1"
Expand Down
Empty file removed test/.gitkeep
Empty file.
34 changes: 31 additions & 3 deletions test/index.test.js
Expand Up @@ -112,12 +112,12 @@ describe('glob-parent', function () {
expect(gp('/{,/,bar/baz,qux}/')).toEqual('/');
expect(gp('/\\{,/,bar/baz,qux}/')).toEqual('/{,/,bar/baz,qux}');
expect(gp('{,/,bar/baz,qux}')).toEqual('.');
expect(gp('\\{,/,bar/baz,qux\\}')).toEqual('{,/,bar/baz,qux}');
expect(gp('\\{,/,bar/baz,qux\\}')).toEqual('{,/,bar');
expect(gp('\\{,/,bar/baz,qux}/')).toEqual('{,/,bar/baz,qux}');
expect(gp('path/foo[a\\/]/')).toEqual('path');
expect(gp('path/foo\\[a\\/]/')).toEqual('path/foo[a\\/]');
expect(gp('foo[a\\/]')).toEqual('.');
expect(gp('foo\\[a\\/]')).toEqual('foo[a\\/]');
expect(gp('foo\\[a\\/]')).toEqual('foo[a\\');
expect(gp('path/(foo/bar|baz)')).toEqual('path');
expect(gp('path/(foo/bar|baz)/')).toEqual('path');
expect(gp('path/\\(foo/bar|baz)/')).toEqual('path/(foo/bar|baz)');
Expand All @@ -136,11 +136,39 @@ describe('glob-parent', function () {
expect(gp('{../,./,{bar,/baz\\},qux\\}')).toEqual('.');
expect(gp('path/{,/,bar/{baz,qux\\}}/')).toEqual('path');
expect(gp('path/{,/,bar/{baz,qux}\\}/')).toEqual('path');
// expect(gp('path/\\{../,./,{bar,/baz},qux}/')).toEqual('path');
expect(gp('path/\\{../,./,{bar,/baz},qux}')).toEqual('path/{../,.');
expect(gp('path/\\{../,./,{bar,/baz},qux}/')).toEqual('path/{../,.');

done();
});

it('should treat escaped brackets as normal chars', function (done) {
if (isWin32) {
expect(gp('aaa\\}')).toEqual('aaa');
expect(gp('\\{aaa/bbb\\}')).toEqual('{aaa');
expect(gp('\\{\\{aaa/bbb\\}')).toEqual('{{aaa');
expect(gp('{\\{aaa/bbb}')).toEqual('.');
expect(gp('ccc/{\\{aaa/bbb}')).toEqual('ccc');
expect(gp('ccc\\/\\{\\{aaa/bbb}')).toEqual('ccc\\/{{aaa');

var opts = { flipBackslashes: true };
expect(gp('aaa\\}', opts)).toEqual('aaa');
expect(gp('\\{aaa/bbb\\}', opts)).toEqual('{aaa');
expect(gp('\\{\\{aaa/bbb\\}', opts)).toEqual('{{aaa');
expect(gp('{\\{aaa/bbb}', opts)).toEqual('.');
expect(gp('ccc/{\\{aaa/bbb}', opts)).toEqual('ccc');
expect(gp('ccc\\/\\{\\{aaa/bbb}', opts)).toEqual('ccc\\/{{aaa');
} else {
expect(gp('aaa\\}')).toEqual('.');
expect(gp('\\{aaa/bbb\\}')).toEqual('{aaa');
expect(gp('\\{\\{aaa/bbb\\}')).toEqual('{{aaa');
expect(gp('{\\{aaa/bbb}')).toEqual('.');
expect(gp('ccc/{\\{aaa/bbb}')).toEqual('ccc');
expect(gp('ccc\\/\\{\\{aaa/bbb}')).toEqual('ccc\\/{{aaa');
}
done();
});

it('should return parent dirname from non-glob paths', function (done) {
expect(gp('path')).toEqual('.');
expect(gp('path/foo')).toEqual('path');
Expand Down