[estree] attach comments after directives at the end of file#14920
Merged
nicolo-ribaudo merged 3 commits intobabel:mainfrom Sep 13, 2022
Merged
[estree] attach comments after directives at the end of file#14920nicolo-ribaudo merged 3 commits intobabel:mainfrom
nicolo-ribaudo merged 3 commits intobabel:mainfrom
Conversation
…mment, then EOF If a source file contains 1) a directive, followed by 2) a comment, and nothing else, then `@babel/parser` attaches the comment to the `Directive` node as a trailing comment. The ESTree plugin handles `Directive` nodes by creating an `ExpressionStatement` node, then transferring information from the `Directive` node to the `ExpressionStatement` node. Previously, at the time it transfers information, comments hadn't yet been attached to the `Directive` node. And even if they had been, the ESTree plugin didn't do anything with comments attached to the `Directive` node. As a result, the source file's comment wasn't attached to any node in the resulting AST. With this change, the ESTree plugin generates an AST in which the comment is attached to the `ExpressionStatement` node as a trailing comment.
Collaborator
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/52918/ |
JLHwung
reviewed
Sep 12, 2022
Contributor
There was a problem hiding this comment.
Can you add a new test case with estree plugin enabled?
// 1;
"use strict";
// 2;
"use strict";The comment 1 should be the leading comment of the first ExpressionStatement, and 2 should be the trailing comment of the first ExpressionStatement and the leading comment of the second ExpressionStatement.
JLHwung
reviewed
Sep 12, 2022
dca2259 to
cac1252
Compare
Contributor
Author
|
The |
Member
|
Yes, the CI failure has nothing to do with this PR. |
liuxingbaoyu
approved these changes
Sep 13, 2022
liuxingbaoyu
approved these changes
Sep 13, 2022
nicolo-ribaudo
approved these changes
Sep 13, 2022
JLHwung
approved these changes
Sep 13, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If a source file contains 1) a directive, followed by 2) a comment, and nothing else, then
@babel/parserattaches the comment to theDirectivenode as a trailing comment.The ESTree plugin handles
Directivenodes by creating anExpressionStatementnode, then transferring information from theDirectivenode to theExpressionStatementnode. Previously, at the time it transfers information, comments hadn't yet been attached to theDirectivenode. And even if they had been, the ESTree plugin didn't do anything with comments attached to theDirectivenode. As a result, the source file's comment wasn't attached to any node in the resulting AST.With this change, the ESTree plugin generates an AST in which the comment is attached to the
ExpressionStatementnode as a trailing comment.Fixes #14919.