Skip to content
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

Merged
merged 2 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions lib/rules/no-mocha-arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @author Paul Melnikow
*/

const last = require('ramda/src/last');
const astUtils = require('../util/ast');

module.exports = {
Expand All @@ -16,21 +15,35 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();

function extractSourceTextByRange(start, end) {
return sourceCode.text.slice(start, end).trim();
}

// eslint-disable-next-line max-statements
Copy link
Contributor Author

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.

Copy link
Owner

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 😉.

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]);
let functionKeyword = 'function';
const arrow = sourceCode.getTokenBefore(fn.body);
const beforeArrowToken = sourceCode.getTokenBefore(arrow);
let firstToken = sourceCode.getFirstToken(fn);

let functionKeyword = 'function';
let params = extractSourceTextByRange(firstToken.range[0], beforeArrowToken.range[1]);
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]) })`;
const beforeArrowComment = extractSourceTextByRange(beforeArrowToken.range[1], arrow.range[0]);
const afterArrowComment = extractSourceTextByRange(arrow.range[1], fn.body.range[0]);
let paramsFullText;
if (firstToken.type !== 'Punctuator') {
paramsFullText = `(${params}${beforeArrowComment})${afterArrowComment}`;
} else {
paramsFullText = `${params}${beforeArrowComment}${afterArrowComment}`;
}

return `${functionKeyword}${paramsFullText} `;
Expand Down
9 changes: 7 additions & 2 deletions test/rules/no-mocha-arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], {
{
code: 'it(async () => { assert(something, false) })',
errors,
output: 'it(async function () { assert(something, false) })'
output: 'it(async function() { assert(something, false) })'
},
{
code: 'it(async () => assert(something, false))',
errors,
output: 'it(async function () { return assert(something, false); })'
output: 'it(async function() { return assert(something, false); })'
},
{
code: 'it(async done => assert(something, false))',
Expand All @@ -95,6 +95,11 @@ ruleTester.run('no-mocha-arrows', rules['no-mocha-arrows'], {
code: 'it(async() => assert(something, false))',
errors,
output: 'it(async function() { return assert(something, false); })'
},
{
code: 'it(/*one*/async/*two*/(done)/*three*/=>/*four*/assert(something, false))',
errors,
output: 'it(/*one*/async function/*two*/(done)/*three*//*four*/ { return assert(something, false); })'
}
]

Expand Down