This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 208
Remove comment attachment #736
Merged
kaicataldo
merged 2 commits into
babel:master
from
kaicataldo:remove-comment-attachment
Jan 11, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use strict"; | ||
|
||
module.exports = function(ast) { | ||
ast.type = "Program"; | ||
ast.sourceType = ast.program.sourceType; | ||
ast.directives = ast.program.directives; | ||
ast.body = ast.program.body; | ||
delete ast.program; | ||
|
||
if (ast.comments.length) { | ||
const lastComment = ast.comments[ast.comments.length - 1]; | ||
|
||
if (!ast.tokens.length) { | ||
// if no tokens, the program starts at the end of the last comment | ||
ast.start = lastComment.end; | ||
ast.loc.start.line = lastComment.loc.end.line; | ||
ast.loc.start.column = lastComment.loc.end.column; | ||
} else { | ||
const lastToken = ast.tokens[ast.tokens.length - 1]; | ||
|
||
if (lastComment.end > lastToken.end) { | ||
// If there is a comment after the last token, the program ends at the | ||
// last token and not the comment | ||
ast.range[1] = lastToken.end; | ||
ast.loc.end.line = lastToken.loc.end.line; | ||
ast.loc.end.column = lastToken.loc.end.column; | ||
} | ||
} | ||
} else { | ||
if (!ast.tokens.length) { | ||
ast.loc.start.line = 1; | ||
ast.loc.end.line = 1; | ||
} | ||
} | ||
|
||
if (ast.body && ast.body.length > 0) { | ||
ast.loc.start.line = ast.body[0].loc.start.line; | ||
ast.range[0] = ast.body[0].start; | ||
} | ||
}; |
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
lib/babylon-to-espree/toTokens.js → lib/babylon-to-espree/convertTokens.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
"use strict"; | ||
|
||
const convertTemplateType = require("./convertTemplateType"); | ||
const toToken = require("./toToken"); | ||
const convertToken = require("./convertToken"); | ||
|
||
module.exports = function(tokens, tt, code) { | ||
return convertTemplateType(tokens, tt) | ||
.filter(t => t.type !== "CommentLine" && t.type !== "CommentBlock") | ||
.map(t => toToken(t, tt, code)); | ||
.map(t => convertToken(t, tt, code)); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,11 @@ | ||
"use strict"; | ||
|
||
const attachComments = require("./attachComments"); | ||
const convertTokens = require("./convertTokens"); | ||
const convertComments = require("./convertComments"); | ||
const toTokens = require("./toTokens"); | ||
const toAST = require("./toAST"); | ||
const convertAST = require("./convertAST"); | ||
|
||
module.exports = function(ast, traverse, tt, code) { | ||
// convert tokens | ||
ast.tokens = toTokens(ast.tokens, tt, code); | ||
|
||
// add comments | ||
ast.tokens = convertTokens(ast.tokens, tt, code); | ||
convertComments(ast.comments); | ||
|
||
// transform esprima and acorn divergent nodes | ||
toAST(ast, traverse, code); | ||
|
||
// ast.program.tokens = ast.tokens; | ||
// ast.program.comments = ast.comments; | ||
// ast = ast.program; | ||
|
||
// remove File | ||
ast.type = "Program"; | ||
ast.sourceType = ast.program.sourceType; | ||
ast.directives = ast.program.directives; | ||
ast.body = ast.program.body; | ||
delete ast.program; | ||
|
||
attachComments(ast, ast.comments, ast.tokens); | ||
convertAST(ast, traverse, code); | ||
}; |
This file contains 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
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.
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.
Just for my own understanding, does ESLint just not used these properties?
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.
That's right! We dropped the need for comment attachment to happen at the parser level in v4 (see docs here). We decided to move away from thinking about comments in the context of AST nodes (which had to be reimplemented by every parser and was buggy at best) and instead to just think of them in the context of tokens. ESLint now just takes the tokens and comments arrays provided by the parser and gives rules access to comments through the use of utility methods like
sourceCode#getTokenBefore(nodeOrToken, { includeComments: true })
.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.
I was talking to Kai about being able to turn off even adding the comments in @babel/parser but if the only use case is for babel-eslint we probably don't need to make that option then? Unless there's a case for it being much faster (would need to test)
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.
I could explore that - I haven't looked at how comment attachment works in @babel/parser in a long time now, but if it's not too crazy of a thing to add I'm happy to do it! That said, I don't think it needs to be tied to this release, since it won't result in any user-facing changes.