Comments (3)
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.
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.
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 theIdentifier
I kinda think it's a can of worms and super use-case-dependent.
from esprima-dotnet.
Related Issues (20)
- Eliminate JavaScript letter casing inconsistencies HOT 12
- Add a way to parse string into Location
- Optional chaining-related parser issues
- Big drop in possible maximum value of MaxAssignmentDepth HOT 4
- Missing ECMAScript 2020+ features HOT 18
- How to get Body.Range from Node? HOT 2
- Issue with spread/rest operator HOT 3
- Add a note README.md stating that the Ast is immutable HOT 2
- Problem with the latest build on Xamarin/iOS HOT 10
- Do we have accessor keyword for properties? HOT 4
- Wrong handling of private Identifierers HOT 3
- Test262 - New testcase -> write back javascript and compare
- RegEx checking via C# Engine does not work the same as in Javascript HOT 1
- Refactor of Class parsing (and other esprima features) HOT 3
- Invalid for loop when writing to javascript HOT 3
- Undefined group issues in regex parser
- Add using keyword as the proposal is in stage 3 HOT 2
- Version 3.0.0-beta-10 suffix problem HOT 10
- Import Assertions (aka Import Atributes) are back at stage 3 so we should add them again HOT 2
- Inconsistencies with ESTree HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esprima-dotnet.