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.

Generate ast-building code from given source

See original GitHub issue

When I generated code in plugins I often have to look what the various elements are called like and what arguments they expect. Also the “reverse” chaining is not intuitive at times.

I’d be cool if I could input the source I want:

this.emit.bind(this, 'myArgument')

and get the code needed to generate it:

import * as t from "babel-types";

t.callExpression(
    t.memberExpression(
        t.memberExpression(
            t.Identifier("this"),
            t.Identifier("emit")
        ),
        t.Identifier("bind")
    ),
    [
        t.thisExpression(),
        t.StringLiteral("myArgument")
    ]
)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
DanielSWolfcommented, May 29, 2017

@RReverser Thanks for pointing me at babel-template (again). I must confess that when I first followed the link in @hzoo’s comment, I completely misunderstood what that library does.

I’m going to replace my “long nested expression” with a short and readable babel-template expression and not look back! 😄

There are so many great features to Babel if one knows where to look!

1reaction
hzoocommented, Sep 20, 2016

you can use babel-template: https://github.com/babel/babel/tree/master/packages/babel-template, also included in the plugin with export default function ({types: t, template}) {

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building AST nodes from source code - DEV Community ‍ ‍
In this article, we are going to take a look at a tool called AST Builder which will significantly improve the developer experience...
Read more >
How to create AST with ANTLR4? - Stack Overflow
I used parts of code found in the TestRig.java file. Let's suppose again that we have a string of C++ source code from...
Read more >
CS75: Parser
Your final version of the parser will create an AST of the program as it parses. The AST will then be used by...
Read more >
A deep dive into the Elixir AST: Building a static code analyzer
Analyzing the AST to build a static code analyzer. ... At a high level, when you run it, it scans you project, parses...
Read more >
aggalex/Variables: A simple example of parsing and AST ... - GitHub
Variables. A simple example of parsing and AST building with Rust and LALRPOP. What can it do? It can successfuly parse and execute...
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