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

How do I query for shorthand properties #133

Open
ainthek opened this issue Oct 9, 2019 · 1 comment
Open

How do I query for shorthand properties #133

ainthek opened this issue Oct 9, 2019 · 1 comment

Comments

@ainthek
Copy link

ainthek commented Oct 9, 2019

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
Copy link
Contributor

adros commented Nov 6, 2019

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]'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants