Substitutions not working
Opened this issue · 1 comments
zmorris commented
I think there might be a problem with the current version of JSPath, or maybe I'm doing something wrong?
const JSPath = require('jspath');
console.log(JSPath.apply('.$name', {
'x': 7
}, {
name: 'x'
}));
console.log(JSPath.apply('.x', {
'x': 7
}, {
name: 'x'
}));
Output:
[]
[ 7 ]
Should be:
[ 7 ]
[ 7 ]
Live demo:
zmorris commented
Found part of the issue, the pattern parser is sensitive to whitespace around the ==
comparison:
const JSPath = require('jspath');
// works (spaces around ==)
console.log(JSPath.apply('.{.x == $name}', {
'x': 7
}, {
name: 7
}));
// fails (no spaces around ==)
console.log(JSPath.apply('.{.x==$name}', {
'x': 7
}, {
name: 7
}));
Output:
[ { x: 7 } ]
Error: Unexpected token "name"
at throwError (/home/runner/node_modules/jspath/lib/jspath.js:671:21)
at throwUnexpected (/home/runner/node_modules/jspath/lib/jspath.js:661:9)
at parsePrimaryExpr (/home/runner/node_modules/jspath/lib/jspath.js:364:16)
at parseUnaryExpr (/home/runner/node_modules/jspath/lib/jspath.js:342:16)
at parseMultiplicativeExpr (/home/runner/node_modules/jspath/lib/jspath.js:288:20)
at parseAdditiveExpr (/home/runner/node_modules/jspath/lib/jspath.js:274:20)
at parseRelationalExpr (/home/runner/node_modules/jspath/lib/jspath.js:260:20)
at parseEqualityExpr (/home/runner/node_modules/jspath/lib/jspath.js:241:20)
at parseEqualityExpr (/home/runner/node_modules/jspath/lib/jspath.js:252:31)
at parseLogicalANDExpr (/home/runner/node_modules/jspath/lib/jspath.js:223:20)
Live demo:
https://repl.it/@ZackMorris1/DarkorchidThoroughSquare
So I'm also not sure if substitutions can work for node names, which means that the second example JSPath.apply(".$name", { 'hello world': 7 }, {name: 'hello world'});
in #25 probably can't work either. It would be good to update that issue to mention that, since I kind of went on a wild goose chase to get to this issue.