Giter VIP home page Giter VIP logo

Comments (3)

adams85 avatar adams85 commented on July 21, 2024 1

You're right and this is what I was talking about in my previous comment. To be able to implement this correctly, we'd also need keywords and punctuators included in AST nodes. Which will probably not happen because of Jint. Maybe in a fork if someone desperately need this.

from esprima-dotnet.

adams85 avatar adams85 commented on July 21, 2024

A few thoughts for those who would make an attempt on implementing this: now there's everything in place to port CommentHandler from Esprima JS. But be aware that it'll be capable of an approximation at best. There's no way to precisely represent comments in the current AST model because it's simply not detailed enough: adding LeadingComments/TrailingComments to AST nodes is not enough to describe pieces of syntax like: async /*some comment*/ function f() { ... }. To handle such cases, AST nodes should also include tokens as child nodes. (I suggest checking out how the problem is solved in Roslyn.)

However, it's unlikely that proper comment handling will ever be possible in this lib because its main consumer is Jint, which wouldn't want to pay the price of a more detailed AST model. The best we can have is what CommentHandler does but it will be a half-assed solution. Which, of course, still could be useful in some use cases.

from esprima-dotnet.

CharlieEriksen avatar CharlieEriksen commented on July 21, 2024

I worked on this problem last week and concluded that this is fools' errand most likely, and shouldn't be a first-class citizen in the public API.

Consider a system where you want to tag javascript code, and have these markings show up in the AST. Here's the code with markings:

true; 
/* mark */ 
var /* mark */ x = /* mark */ "test"; /* mark */ 

The solution I came up with was:

public class TestMarkerVisitor : AstVisitor
{
    public static object Comment = "Comment";
    private readonly List<SyntaxComment> _comments;

    private TestMarkerVisitor(IReadOnlyList<SyntaxComment> comments)
    {
        _comments = comments;
    }

    public static void Parse(Node node, IReadOnlyList<SyntaxComment> comments)
    {
        if (comments.Count == 0) return;
        var visitor = new TestMarkerVisitor(comments);
        visitor.Visit(node);
    }

    private Node? previousNode = null;

    public override object? Visit(Node node)
    {
        // Check if we went past the start
        if (previousNode != null)
        {
           var commentsMatching = _comments.Where(currentComment => 
               currentComment.Location.Start.Compare(previousNode.Location.End) >= 0 // Previous node ends before the comment
            && currentComment.Location.End.Compare(node.Location.Start) <= 0        // Next node starts after the end of the comment
               ).ToList();
           if (commentsMatching.Count > 0)
           {
               node.SetAdditionalData(Comment, commentsMatching);
               _comments.RemoveAll(x=> commentsMatching.Contains(x));
           }
           

        }
        previousNode = node;
        return base.Visit(node);
    }


}

But I really don't think this is a great solution either. And esprima doesn't produce the correct result IMO:
Example.

It makes the following mistakes, IMO:

  • mark1 is attached as trailing to the true literal. That makes no sense. In this case, that makes it more of a statement in the tree to me.
  • mark1 is also attached as leading to the VariableDeclaration. This is more correct in my opinion.
  • mark2 is attached as leading to the VariableDeclaration. It should be leading to the Identifier

I kinda think it's a can of worms and super use-case-dependent.

from esprima-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.