How to convert to a `NodePath`?
See original GitHub issueI’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:
- Created 8 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 aNodePath
instead of the raw AST node value.Examples:
.get
takes multiple arguments. To get theNodePath
to the first AST node inarguments
, you can do:See also https://github.com/benjamn/ast-types#nodepath .
j
will always return a collection ofNodePath
s.Oh yeah, react-codemod is fine for React specific codemods.