babel / babylon

PSA: moved into babel/babel as @babel/parser -->

Home Page:https://github.com/babel/babel/tree/master/packages/babel-parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get the "root" path

zoecarver opened this issue · comments

I am wondering if there is any way to get the root path of an expression. For example, I am trying to make a plugin (only to learn more about babel) that uses the | OR operator to create functional pipelines. It would take something like this:

2 | double | square

and turn it into this:

square(double(2))

The way I am going at it is that I add every path.node.right or path.node.left that contains a value or name to an array. Then I create a bunch of nested Expression calls. The only issue is that it outputs something like this:

square(double(2)) | square

because it is not replacing all the paths (or the root path?).

Input Code (case.js)

2 | double | square | half | root | round | add | subtract

Babylon/Babel Script (index.js)

  var t = babel.types;
  var traverse = [];

  function format(pns) {
    if (pns) {
      if (pns.name) {
        return pns.name;
      }
      return pns;
    }
    return null;
  }

  function createNesting(i){
    if (i < traverse.length-1) {
      return t.callExpression(
        t.identifier(traverse[i]),
        [createNesting(i+1)]
      )
    }
    return traverse[i]
  }

  return {
    visitor: {
      BinaryExpression: function(path){
        //console.log(JSON.stringify(path.node, null, 4));
        var pn = path.node;

        if (path.node.operator === '|') {
          traverse.push(format(pn.right))
          if (!pn.left.left) {
            traverse.push(pn.left)

            var other = t.callExpression(
                          t.identifier(traverse[1]),
                          [createNesting(2)]
                        )

            var newPath = t.callExpression(
              t.identifier(traverse[0]),
              [other]
            )

            path.replaceWith(
              newPath
            );
          }
        }
      }
    }
  };

Expected Behavior

output:  subtract(add(round(root(half(square(double(2)))))))

Current Behavior

output:  subtract(add(round(root(half(square(double(2))))))) | square | half | root | round | add | subtract;

Your Environment

software version
Babylon 6.26.0
node v8.4.0
npm 5.4.0
Operating System OSX
commented

Hey @pudility! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

commented

this is more of a support question, and not really related to babylon? I would ask in slack for further help but if you want to traverse up you can use path.find/findParent https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md

Thank you! I asked on slack, but I will also try that.

commented

Oh must of missed it, trying not to look at it all the time 😄 .

You can probably just search up until you find the ExpressionStatement?

Thanks! What exactly would be the syntax for that? I was looking here, could I use findParent?

Edit: Using path.findParent((path) => path.isExpressionStatement()); worked perfectly!