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 to match import specifier by id? #106

Closed
kristianmandrup opened this issue Dec 5, 2016 · 4 comments
Closed

How to match import specifier by id? #106

kristianmandrup opened this issue Dec 5, 2016 · 4 comments

Comments

@kristianmandrup
Copy link

kristianmandrup commented Dec 5, 2016

As per estools/esquery#50

How do I match import x in the following!?

Error: Expected operator ), but found: {"type":"operator","value":"="}

let importStmt = `import x from 'x'`

const find = `import-dec[specifiers=(id=#x)]`
const replace = `import y from 'x'`
const replacer = grasp.replace('squery', find, replace)
let result = replacer(importStmt)

Or why not?

import-dec[specifiers[id=#x]] OR import-dec[specifiers=(import-default-specifier[id=#x])]

TypeError: Cannot read property 'nodes' of undefined (last attempt)

From esprima parser

    "body": [
        {
            "type": "ImportDeclaration",
            "specifiers": [
                {
                    "type": "ImportDefaultSpecifier",
                    "local": {
                        "type": "Identifier",
                        "name": "x"
                    }
                }
            ],
            "source": {
                "type": "Literal",
                "value": "./x",
                "raw": "'./x'"
            }
        },
@kristianmandrup
Copy link
Author

Finally getting somewhere, but not quite...

const find = `import-dec > specifiers import-default-specifier[id=#x]`
const replace = `y`

const replacer = grasp.replace('squery', find, replace)

How do I select on the outer import node and not the specifier? Even in this case, the specifier is not replaced by y

@gkz
Copy link
Owner

gkz commented Dec 6, 2016

Can you give a better example of what you want?

ie. this is the code you have, this is what you want matched. It is unclear from your post

Are you wanting to use grasp "import-dec! import-default-specifier[local=#x]"? ie using http://www.graspjs.com/docs/squery/#subject

@kristianmandrup
Copy link
Author

oh, wow! Thanks @gkz exactly what I wanted. I wish you had some more examples on how to use the query/select syntax to such great effect for more complex queries. Cheers!

@kristianmandrup
Copy link
Author

Perfect! It works like a charm! Thanks again :)

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