-
Notifications
You must be signed in to change notification settings - Fork 61
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
Support comments in arrow functions when fixing #253
Changes from 1 commit
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 |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
* @author Paul Melnikow | ||
*/ | ||
|
||
const last = require('ramda/src/last'); | ||
const astUtils = require('../util/ast'); | ||
|
||
module.exports = { | ||
|
@@ -16,21 +15,31 @@ module.exports = { | |
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
// eslint-disable-next-line max-statements | ||
function formatFunctionHead(fn) { | ||
const paramsLeftParen = sourceCode.getFirstToken(fn); | ||
const paramsRightParen = sourceCode.getTokenBefore(sourceCode.getTokenBefore(fn.body)); | ||
let paramsFullText = sourceCode.text.slice(paramsLeftParen.range[0], paramsRightParen.range[1]); | ||
const arrow = sourceCode.getTokenBefore(fn.body); | ||
let firstToken = sourceCode.getFirstToken(fn); | ||
const beforeArrowToken = sourceCode.getTokenBefore(arrow); | ||
let paramsFullText; | ||
let params = sourceCode.text.slice(firstToken.range[0], beforeArrowToken.range[1]).trim(); | ||
let functionKeyword = 'function'; | ||
const beforeArrowComment = sourceCode.text.slice(beforeArrowToken.range[1], arrow.range[0]).trim(); | ||
const afterArrowComment = sourceCode.text.slice(arrow.range[1], fn.body.range[0]).trim(); | ||
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. For better readability I would suggest to extract the repeated pattern of 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 sliceAndTrim( sourceCode.text, beforeArrowToken.range[1], arrow.range[0] ); much more concise or legible? 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 think |
||
|
||
if (fn.async) { | ||
// When 'async' specified, take care about the keyword. | ||
// When 'async' specified strip the token from the params text | ||
// and prepend it to the function keyword | ||
params = params.slice(firstToken.range[1] - firstToken.range[0]).trim(); | ||
functionKeyword = 'async function'; | ||
// Strip 'async (...)' to ' (...)' | ||
paramsFullText = paramsFullText.slice(5); | ||
|
||
// Advance firstToken pointer | ||
firstToken = sourceCode.getTokenAfter(firstToken); | ||
} | ||
|
||
if (fn.params.length > 0) { | ||
paramsFullText = `(${ sourceCode.text.slice(fn.params[0].range[0], last(fn.params).range[1]) })`; | ||
if (firstToken.type !== 'Punctuator') { | ||
paramsFullText = `(${params}${beforeArrowComment})${afterArrowComment}`; | ||
} else { | ||
paramsFullText = `${params}${beforeArrowComment}${afterArrowComment}`; | ||
} | ||
|
||
return `${functionKeyword}${paramsFullText} `; | ||
|
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.
Not sure how you feel about this rule, but I didn't see an obvious way to break up this method without passing multiple return values.
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.
It’s not a hard requirement. I don’t have a better approach in mind for now. As long as we have a good test coverage we can always refactor the code later 😉.