-
Notifications
You must be signed in to change notification settings - Fork 1.9k
JS: add JavaScript printAst query #4434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
e1766f2 to
157665b
Compare
|
Thanks for taking this on, Erik. It's looking great so far. I played around with it in the extension's AST Viewer and have some comments. To get the AST viewer working in vscode, make sure you add I'm pretty sure you can see this with any javascript/typescript file, but I used https://lgtm.com/projects/g/sindresorhus/got/ (you can add this through the vscode command
|
|
So far, I've only looked at javascript. Even though the file above uses a |
|
Looking at the
|
|
Looking at the
|
|
HTML now.
I haven't tried a very complex html file. Nor have I looked at JSX. I'll look at that later. |
|
Looking at JSX https://lgtm.com/projects/g/nukeop/react-ui-cards?mode=list
|
|
Typescript, Got project from above. Looking at
|
There isn't a location specific for the parameters, so I had re-used the function location.
I've changed the default sorting order to be location based.
Fixed 👍
I've replaced all the printed whitespace with just a single space.
👍
We don't have a location for the entire key-value pair in the database.
There is no QL class corresponding to a JSON object key, so I would prefer not creating a node in the print-ast. |
|
Thanks for addressing my comments. I hope to get a chance to look today.
That's surprising. It seems like it would be something useful to select. But, I think it's best to follow what the actual AST is doing. |
|
Thanks. Looks a lot better. Though, now I'm finding a bunch of errors being thrown (likely invalid locations, or locations being included when they shouldn't be). I get errors when I click on JSX The location is malformed since there is no path into the zip file. |
None of those have a result for |
|
I'm happy with this implementation, but someone on the JavaScript team should approve. |
asgerf
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just noticed one little thing
| exists(ClassDefinition c, ConstructorDeclaration constructor | | ||
| constructor = c.getConstructor() and | ||
| constructor.isSynthetic() and | ||
| el = constructor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be a good idea to render the implicit constructor anyway? It's part of the AST.
I recommend trying out the printAst feature (see next section) before reviewing the code.
This PR adds a
printAst.qlquery that can be used for viewing the AST for JavaScript/TypeScript, HTML, JSON, and YAML.Recommended usage instructions:
"codeQL.experimentalAstViewer": trueto your settings in VSCode(Ctrl + Shift + P:
Open Settings (JSON)).View ASTbutton in the bottom.Function/InvokeExprto see what the aggregate nodes look like.The PR was created iteratively by looking at the output from some representative file, improving the PrintAST feature, and repeating.
After a number of iterations I then went through the changed files and added more cases to
getAPrimaryQlClass.I've attempted to implement the
getAPrimaryQlClasspredicate such that it is useful without being overly specific.For example: the expressions
++i,i--, etc. are all identified asUpdateExpr, instead of the more specificPreIncExpr,PostDecExpr, etc.).For most of the nodes in the JavaScript AST the default
ASTNode::getChildrelation can be used to build a useful child-parent relation in the AST-viewer.However for
ASTNodes likeFunction/InvokeExprhaving all the arguments/parameters/type-arguments as direct children look well in the AST viewer, and special aggregate nodes are used to make that look better (try out the PrintAST feature to see the end result).The other languages (HTML/JSON/YAML) generally have simpler implementations, but still use aggregate nodes where they make sense.