-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[New] sort-prop-types
: support comments on prop types
#1973
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @fileoverview Utility functions for comments handling. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* Backports sourceCode.getCommentsBefore() for ESLint 3 | ||
* | ||
* @param {Object} nodeOrToken Node or token to get comments for. | ||
* @param {Object} sourceCode The SourceCode object. | ||
* @returns {Array} Array of comment tokens. | ||
*/ | ||
function getCommentsBefore(nodeOrToken, sourceCode) { | ||
const token = sourceCode.getFirstToken(nodeOrToken, {includeComments: true}); | ||
let previousComments = []; | ||
|
||
// ESLint >=4.x | ||
if (sourceCode.getCommentsBefore) { | ||
previousComments = sourceCode.getCommentsBefore(token); | ||
// ESLint 3.x | ||
} else { | ||
let currentToken = token; | ||
do { | ||
const previousToken = sourceCode.getTokenBefore(currentToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: this code was extracted from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still failing? It'd be useful to figure out which version of 3.x this api was introduced in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it was originally introduced in 3.0.0. My branch has 182 test failures on that version (not all of them seem to be related to this api or caused by my changes though. Master has 127 failures). Between 3.15.0 and 3.16.0 it drops from 169 to 34 failures, removing a lot of errors related to tokens/comments handling. Although a few of the remaining errors are me using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we backported the eslint API, would that work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way we could runtime-detect these APIs, and provide a reasonable degraded experience for older eslint users? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm skeptical (not that we could detect them, but that there is a reasonable experience beyond just not applying fixes), but let me look into that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah that's what i meant - applying fewer fixes when they weren't good enough. |
||
const potentialComment = sourceCode.getTokenBefore(currentToken, {includeComments: true}); | ||
|
||
if (previousToken !== potentialComment) { | ||
previousComments.push(potentialComment); | ||
currentToken = potentialComment; | ||
} else { | ||
currentToken = null; | ||
} | ||
} while (currentToken); | ||
previousComments = previousComments.reverse(); | ||
} | ||
|
||
return previousComments; | ||
} | ||
|
||
/** | ||
* Backports sourceCode.getCommentsAfter() for ESLint 3 | ||
* | ||
* @param {Object} nodeOrToken Node or token to get comments for. | ||
* @param {Object} sourceCode The SourceCode object. | ||
* @returns {Array} Array of comment tokens. | ||
*/ | ||
function getCommentsAfter(nodeOrToken, sourceCode) { | ||
const token = sourceCode.getLastToken(nodeOrToken, {includeComments: true}); | ||
let nextComments = []; | ||
|
||
// ESLint >=4.x | ||
if (sourceCode.getCommentsAfter) { | ||
nextComments = sourceCode.getCommentsAfter(token); | ||
// ESLint 3.x | ||
} else { | ||
let currentToken = token; | ||
do { | ||
const nextToken = sourceCode.getTokenAfter(currentToken); | ||
const potentialComment = sourceCode.getTokenAfter(currentToken, {includeComments: true}); | ||
|
||
if (nextToken !== potentialComment) { | ||
nextComments.push(potentialComment); | ||
currentToken = potentialComment; | ||
} else { | ||
currentToken = null; | ||
} | ||
} while (currentToken); | ||
} | ||
|
||
return nextComments; | ||
} | ||
|
||
module.exports = { | ||
getCommentsBefore: getCommentsBefore, | ||
getCommentsAfter: getCommentsAfter | ||
}; |
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.
Should this live in a shared utility?
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 don't mind that. Although I imagine the
isSameLine
bit in the output is pretty specific to this use case, I don't think it is generally useful to know whether the comment related to a thing is on the same line as the thing or not 🤔