gkz / grasp

JavaScript structural search, replace, and refactor
http://graspjs.com
MIT License
1.28k stars 33 forks source link

How do I query for shorthand properties #133

Open ainthek opened 5 years ago

ainthek commented 5 years ago

I would expect this to work:

echo "let a=10,o={a}" | grasp 'prop[shorthand=true]'

$ echo "let a=10,o={a}" | acorn { "type": "Program", "start": 0, "end": 15, "body": [ { "type": "VariableDeclaration", "start": 0, "end": 14, "declarations": [ { "type": "VariableDeclarator", "start": 4, "end": 8, "id": { "type": "Identifier", "start": 4, "end": 5, "name": "a" }, "init": { "type": "Literal", "start": 6, "end": 8, "value": 10, "raw": "10" } }, { "type": "VariableDeclarator", "start": 9, "end": 14, "id": { "type": "Identifier", "start": 9, "end": 10, "name": "o" }, "init": { "type": "ObjectExpression", "start": 11, "end": 14, "properties": [ { "type": "Property", "start": 12, "end": 13, "method": false, "shorthand": true, "computed": false, "key": { "type": "Identifier", "start": 12, "end": 13, "name": "a" }, "kind": "init", "value": { "type": "Identifier", "start": 12, "end": 13, "name": "a" } } ] } } ], "kind": "let" } ], "sourceType": "script" }

adros commented 5 years ago

Your (s)query is (wrongly??) interpreted as compound attribute. That's why it does not match. See the docs https://www.graspjs.com/docs/squery/.

If you want to only match primitive attributes, you can add a & before your attribute name, eg. [&value=2].

I am not sure if this is a bug or feature, but if you use this syntax, it works. echo "let a=10,o={a}" | grasp 'prop[&shorthand=true]'