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: correct regexp slash escapes #149

Open
wants to merge 4 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
14 changes: 12 additions & 2 deletions grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
}
});
}

// https://github.com/estools/esquery/issues/68
// Inside all /regexp/ literals, we replace escaped-backslashes with the \x2F equivalent.
input = input.replaceAll(/\/((?:[^\/\\]|\\.)*?)\//g, (match) => {
return match.replaceAll("\\/", "\\\\x2F");
});
}

start
Expand Down Expand Up @@ -97,8 +103,12 @@ attr
path = i:identifierName { return { type: 'literal', value: i }; }
type = "type(" _ t:[^ )]+ _ ")" { return { type: 'type', value: t.join('') }; }
flags = [imsu]+
regex = "/" d:[^/]+ "/" flgs:flags? { return {
type: 'regexp', value: new RegExp(d.join(''), flgs ? flgs.join('') : '') };
regex = "/" d:[^/]+ "/" flgs:flags? {
// https://github.com/estools/esquery/issues/68
const text = d.join('').replaceAll("\\\\x2F", "\\/");
return {
type: 'regexp', value: new RegExp(text, flgs ? flgs.join('') : '')
};
}

field = "." i:identifierName is:("." identifierName)* {
Expand Down
14 changes: 12 additions & 2 deletions parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/fixtures/literalSlash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as esprima from 'esprima';

const parsed = esprima.parse(`
var s1 = "foo/bar";
var s2 = "foo//bar";

var b1 = "foo\\/bar";
var b2 = "foo\\/\\/bar";
`);

export default parsed;


43 changes: 43 additions & 0 deletions tests/queryAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import esquery from '../esquery.js';
import literal from './fixtures/literal.js';
import conditional from './fixtures/conditional.js';
import forLoop from './fixtures/forLoop.js';
import literalSlash from './fixtures/literalSlash.js';
import simpleFunction from './fixtures/simpleFunction.js';
import simpleProgram from './fixtures/simpleProgram.js';

Expand Down Expand Up @@ -160,6 +161,48 @@ describe('Attribute query', function () {
]);
});

it('single backslash-escaped slash in a literal', function () {
const matches = esquery(literalSlash, '[value="foo\\/bar"]');
assert.includeMembers(matches, [
literalSlash.body[2].declarations[0].init
]);
});

it('single backslash-escaped slash in a regexp', function () {
const matches = esquery(literalSlash, '[value=/foo\\/bar/]');
assert.includeMembers(matches, [
literalSlash.body[0].declarations[0].init
]);
});

it('single encoded slash in a regexp', function () {
const matches = esquery(literalSlash, '[value=/foo\\x2Fbar/]');
assert.includeMembers(matches, [
literalSlash.body[0].declarations[0].init
]);
});

it('double backslash-escaped slash in a literal', function () {
const matches = esquery(literalSlash, '[value="foo\\/\\/bar"]');
assert.includeMembers(matches, [
literalSlash.body[3].declarations[0].init
]);
});

it('double backslash-escaped slash in a regexp', function () {
const matches = esquery(literalSlash, '[value=/foo\\/\\/bar/]');
assert.includeMembers(matches, [
literalSlash.body[1].declarations[0].init
]);
});

it('double backslash-escaped slash in a regexp', function () {
const matches = esquery(literalSlash, '[value=/foo\\x2F\\x2Fbar/]');
assert.includeMembers(matches, [
literalSlash.body[1].declarations[0].init
]);
});

it('multiple regexp flags (i and u)', function () {
const matches = esquery(simpleProgram, '[name=/\\u{61}|[SDFY]/iu]');
assert.includeMembers(matches, [
Expand Down