question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to convert to a `NodePath`?

See original GitHub issue

I’m trying to create a transformer that will go from:

React.createElement(Foo, null);
React.createElement(Foo, { bar: "baz", onClick: this.divClicked });

React.createElement(
    Foo,
    { bar: "baz", onClick: this.divClicked },
    React.createElement("div", { foo: "bar" })
  );

to

<Foo></Foo>;
<Foo bar="baz" onClick={this.divClicked}></Foo>;

<Foo bar="baz" onClick={this.divClicked}>
    <div foo="bar">
    </div>
</Foo>

I’ve gotten the first two examples, but I’m confused on recursively handling the children.

My interactive codemod is here: http://felix-kling.de/esprima_ast_explorer/#/hb6iLO9hTe/4

If I change convertNodeToJSX to:

function convertNodeToJSX(node) { 
    if (!node) {
      return;
    }

    console.log(node);

   // other code

   const children = convertNodeToJSX(args[2]);

then I get printed out

NodePath {value: Object, parentPath: NodePath, name: "expression", __childCache: Object}
NodePath {value: Object, parentPath: NodePath, name: "expression", __childCache: Object}
NodePath {value: Object, parentPath: NodePath, name: "expression", __childCache: Object}
Object {type: "CallExpression", start: 175, end: 217, loc: i, callee: Object…}

So how can I convert the CallExpression to a NodePath? Wrapping the element with j() gives me:

e {_parent: undefined, __paths: Array[1], _types: Array[5]}

Or perhaps as a higher level question, is this even the right approach?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fklingcommented, Oct 28, 2015

Recast (ast-types actually) provides two APIs: One lets you access the underlying “raw” AST node, with .value or .node, as you already know. The other gives you a NodePath instead of the raw AST node value.

Examples:

var args = path.value.arguments; // array of AST nodes
var args = path.get('arguments'); // NodePath representing the array of AST nodes

.get takes multiple arguments. To get the NodePath to the first AST node in arguments, you can do:

var pathToFirstArg = path.get('arguments', 0); // or args.get(0) from above

See also https://github.com/benjamn/ast-types#nodepath .

Wrapping the element with j() gives me …

j will always return a collection of NodePaths.

0reactions
cpojercommented, Oct 29, 2015

Oh yeah, react-codemod is fine for React specific codemods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodePath — Godot Engine (stable) documentation in English
Exporting a NodePath variable will give you a node selection widget in the properties panel of the editor, which can often be useful....
Read more >
How can I get node from nodepath in other script? : r/godot
I used export nodepath in the board manager of the game to specify nodepath in the variable. In the ready function, we changed...
Read more >
How to change nodepath in Node.js? - Stack Overflow
How do I view the current node_path? Is this in a configuration file? The command node NODE_PATH causes error "Cannot find module". node.js....
Read more >
NodePath Implicit Conversion (String to NodePath)
Parameters. from: Type: System.String. [Missing <param name="from"/> documentation for "M:Godot.NodePath.op_Implicit(System.String)~Godot.NodePath"] ...
Read more >
NodePath - Panda3D Manual
Each NodePath therefore uniquely describes one instance of a node. ... Converts the NodePath object into a single stream of data using a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found