-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: `getFirstToken`/`getLastToken` on comment-only node * Remove unnecessary array bound checks * Split `if`-statement for clarity, remove inaccurate unit test * Fix for trailing comments in root node
- Loading branch information
Showing
3 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
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,28 @@ | ||
// Similar to the default parser, but considers leading and trailing comments to be part of the root node. | ||
// Some custom parsers like @typescript-eslint/parser behave in this way. | ||
|
||
const espree = require("espree"); | ||
exports.parse = function(code, options) { | ||
const ast = espree.parse(code, options); | ||
|
||
if (ast.range && ast.comments && ast.comments.length > 0) { | ||
const firstComment = ast.comments[0]; | ||
const lastComment = ast.comments[ast.comments.length - 1]; | ||
|
||
if (ast.range[0] > firstComment.range[0]) { | ||
ast.range[0] = firstComment.range[0]; | ||
ast.start = firstComment.start; | ||
if (ast.loc) { | ||
ast.loc.start = firstComment.loc.start; | ||
} | ||
} | ||
if (ast.range[1] < lastComment.range[1]) { | ||
ast.range[1] = lastComment.range[1]; | ||
ast.end = lastComment.end; | ||
if (ast.loc) { | ||
ast.loc.end = lastComment.loc.end; | ||
} | ||
} | ||
} | ||
return ast; | ||
}; |
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