gkz / grasp

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

How do I partially match import-dec source? #134

Closed zArubaru closed 4 years ago

zArubaru commented 4 years ago

For example, if my file contains this line: import { logger } from 'common-components/util/logger';

I can match it with: grasp 'import-dec[source="common-components/util/logger"]

I would like to be able to do something like this: grasp 'import-dec[source=/^common/] grasp 'import-dec[source^="common"]

I tried a few different approaches, but couldn't get them to work.

gkz commented 4 years ago

Look under "Attributes" on http://www.graspjs.com/docs/squery/

For primitive attributes, you can use either of those, or the < less than, > greater than, <= less than or equal to, >= greater to or equal to, or ~= regex test operators. For example, [value<2] matches the literals 1 and 0. [value~=/^hi/] matches the literal "hi there".

zArubaru commented 4 years ago

Thank you, I re-read that part a few times, but for some reason it didn't register that I literally need to use value.

Thus, for future reference, here is the solution: import-dec[source.value~=/^common/]

And maybe the following will also help someone, I was trying to select an imported specifier: import-dec[source.value~=/^common/] > import-specifier[local=#logger]