Skip to content

Commit

Permalink
Merge pull request #253 from edg2s/arrow-comments
Browse files Browse the repository at this point in the history
Support comments in arrow functions when fixing
  • Loading branch information
lo1tuma committed May 29, 2020
2 parents d095e93 + 2eefa13 commit bb1bc45
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
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
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

0 comments on commit bb1bc45

Please sign in to comment.