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

Fix: implicit-arrow-linebreak adds extra characters (fixes #11268) #11407

Merged
merged 12 commits into from Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 8 additions & 14 deletions lib/rules/implicit-arrow-linebreak.js
Expand Up @@ -57,8 +57,7 @@ module.exports = {
* @param {Integer} column The column number of the first token
* @returns {string} A string of comment text joined by line breaks
*/
function formatComments(comments, column) {
const whiteSpaces = " ".repeat(column);
function formatComments(comments) {

return `${comments.map(comment => {

Expand All @@ -67,7 +66,7 @@ module.exports = {
}

return `/*${comment.value}*/`;
}).join(`\n${whiteSpaces}`)}\n${whiteSpaces}`;
}).join("\n")}\n`;
}

/**
Expand Down Expand Up @@ -109,18 +108,13 @@ module.exports = {
let followingBody = arrowBody;
let currentArrow = arrow;

while (currentArrow) {
while (currentArrow && followingBody.value !== "{") {
This conversation was marked as resolved.
Show resolved Hide resolved
if (!isParenthesised(sourceCode, followingBody)) {
parenthesesFixes.push(
fixer.insertTextAfter(currentArrow, " (")
);

const paramsToken = sourceCode.getTokenBefore(currentArrow, token =>
isOpeningParenToken(token) || token.type === "Identifier");

const whiteSpaces = " ".repeat(paramsToken.loc.start.column);

closingParentheses = `\n${whiteSpaces})${closingParentheses}`;
closingParentheses = `\n)${closingParentheses}`;
}

currentArrow = sourceCode.getTokenAfter(currentArrow, isArrowToken);
Expand Down Expand Up @@ -161,7 +155,9 @@ module.exports = {
) {

// If any arrow functions follow, return the necessary parens fixes.
if (sourceCode.getTokenAfter(arrowToken, isArrowToken) && arrowBody.parent.parent.type !== "VariableDeclarator") {
if (sourceCode.getTokenAfter(arrowToken, isArrowToken) &&
This conversation was marked as resolved.
Show resolved Hide resolved
arrowBody.parent.parent.type !== "VariableDeclarator"
) {
return addParentheses(fixer, arrowToken, arrowBody);
}

Expand All @@ -173,11 +169,9 @@ module.exports = {

const firstToken = findFirstToken(node);

const commentText = formatComments(comments, firstToken.loc.start.column);

const commentBeforeExpression = fixer.insertTextBeforeRange(
firstToken.range,
commentText
formatComments(comments)
);

return [placeBesides, commentBeforeExpression];
Expand Down