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

Adding a method to a class via replace filter? #108

Open
kristianmandrup opened this issue Dec 7, 2016 · 0 comments
Open

Adding a method to a class via replace filter? #108

kristianmandrup opened this issue Dec 7, 2016 · 0 comments

Comments

@kristianmandrup
Copy link

kristianmandrup commented Dec 7, 2016

How do I use replace filters to add a method to a class named this.className
The class body is an array, where elements/nodes typically are MethodDefinition.
Do I use push or after or am I going in the wrong direction? Would be wonderful if there were examples in the docs beyond simple numbers in an Array but something more "real life"
Cheers!

"body": [
        {
            "type": "ClassDeclaration",
            "id": {
                "type": "Identifier",
                "name": "Hello"
            },
            "superClass": null,
            "body": {
                "type": "ClassBody",
                "body": [
                    {
                        "type": "MethodDefinition",
                        "key": {
                            "type": "Identifier",
                            "name": "x"
                        },
                        "computed": false,
                        "value": {
                            "type": "FunctionExpression",
                            "id": null,
                            "params": [],
                            "body": {
                                "type": "BlockStatement",
                                "body": []
                            },
                            "generator": false,
                            "expression": false
                        },
                        "kind": "method",
                        "static": false
                    }
                ]
            }
        }
    ],

The "problem" is that .body includes the scope brackets { } when inserted.
I always seem to get this error when I try...

    operator: undefined
    expected: undefined
    actual: undefined
    at: visitPre (node_modules/grasp-squery/lib/common.js:37:46)

Looking at append doesn't give me much of a clue :(

    if replacement?
      try
        replaced = replace replacement, clean-input, sliced-results, query-engine

...

orig-results = [node]

| 'append' =>
          for arg in args then results.push type: 'Raw', raw: "#arg"

my attempts:

class Hello {{.body hello() { } }}

var find = `class-dec[id=#Hello] body[type=#ClassBody]! `

var replace = `class Hello {{.body | append hello() {
} }}`

or do I append a String?

var replace = `class Hello {{.body | append "hello() {
}" }}`

grasp.replace('squery', find, replace)
this.code = replacer(this.code)

Can't make it work...

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

1 participant