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.

Avoid `parenthesis` and `cast` nodes in ast

See original GitHub issue

Example how this do babylon https://astexplorer.net/#/gist/56cf77073a7a8dcc4a785a423c26a1c7/209de623aac9c972645542acfcefdcf67a7bceb1

Why? Example from our code (print return node):

 case "return": {
      const parts = [];

      parts.push("return");

      if (node.expr) {
        const printedExpr = path.call(print, "expr");

        if (node.expr.kind === "bin") {
          parts.push(
            group(
              concat([
                ifBreak(" (", " "),
                indent(concat([softline, printedExpr])),
                softline,
                ifBreak(")")
              ])
            )
          );
        } else {
          parts.push(" ", printedExpr);
        }
      }

      if (hasDanglingComments(node)) {
        parts.push(
          " ",
          comments.printDanglingComments(path, options, /* sameIndent */ true)
        );
      }

      return concat(parts);
}

Look on node.expr.kind === "bin", in some cases people can have parenthesis and our check does not works correctly and you will get bad output (ugly). Adding check on parenthesis and parenthesis.inner can solve this problem, but it is require a lot of code and new checks (in some places it is really a lot of code). Better use logic as in prettier for js. They have ast without parenthesis and print nodes as is and then add parens basic on own logic for parenthesis (https://github.com/prettier/prettier/blob/master/src/language-js/needs-parens.js#L43).

Same for cast node.

All this would allow us to work in a beautiful form with the ast.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
ichiriaccommented, Aug 17, 2018

I understand better the problem now. I have to handle another problem in order to be sure what to do, actually the cast precedence #172 is incorect (not handled at all) and I need to address this problem first.

In both cases, you’re right, you only need the cast information as an attribute, so what you can do, is to use the parser with this configuration :

      parser: {
          read_expr_cast: function(cast) {
            const rawCast = this.text();
            const expr = this.next().read_expr();
            expr.cast = cast;
            expr.rawCast = rawCast;
            return expr;
          }
          // + other parser options
        }

This way you can rewrite any parsing function, so I’ve made a function specialy for cast nodes and added it to snapshots in order to avoid breaking changes on future releases.

With this kind of atomic function you can still diverge from the parser implementation and continue to integrate bugfixes on each release, the catch is to be sure that these functions are atomic enough and don’t introduce parsing logic.

1reaction
alexander-akaitcommented, Sep 27, 2018

@ichiriac can we do same for silent node?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should I keep parenthesis information while source modeling?
Adding a parenthesis flag to expression AST nodes seems to be the better option to me, it can be ignored by parts that...
Read more >
AST how to deal with empty nodes - Stack Overflow
In my calculator I need to handle parentheses and they create empty nodes in my AST (and that makes my parse tree not...
Read more >
rust-analyzer/syntax.md at master - GitHub
It's always possible to go from an ast node to an untyped SyntaxNode . It's possible to go in the opposite direction with...
Read more >
ast — Abstract Syntax Trees — Python 3.11.1 documentation
simple is a boolean integer set to True for a Name node in target that do not appear in between parenthesis and are...
Read more >
Abstract syntax tree - Wikipedia
In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of text...
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