Skip to content

Commit

Permalink
Update: add autofix to rule multiline-ternary (#13958)
Browse files Browse the repository at this point in the history
* Update: Added auto-fix to multiline-ternary

I noticed unfixed warnings from this ESLint rule and wanted to auto-fix them. While it may seem like doing `'\n? '` is opinionated, I did have changed to make this take a new option and either put the `?` on the previous line or next line. This is actually unnecessary because `operator-linebreak` handles it for you.

* chore: fix linting

* chore: review suggestions

* Chore: review suggestions

refs: https://github.com/eslint/eslint/pull/12982/files#r409035292

* Chore: add a test incluing comments

* Chore: review suggestions

* Chore: fixer.replaceTextRange() => removeRange()

* chore: review suggestions

* chore: fix typo

Co-authored-by: Kevin Ghadyani <3948069+Sawtaytoes@users.noreply.github.com>
  • Loading branch information
aladdin-add and Sawtaytoes committed Dec 31, 2020
1 parent f6e7e32 commit 0649871
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 6 deletions.
69 changes: 63 additions & 6 deletions lib/rules/multiline-ternary.js
Expand Up @@ -27,19 +27,22 @@ module.exports = {
enum: ["always", "always-multiline", "never"]
}
],

messages: {
expectedTestCons: "Expected newline between test and consequent of ternary expression.",
expectedConsAlt: "Expected newline between consequent and alternate of ternary expression.",
unexpectedTestCons: "Unexpected newline between test and consequent of ternary expression.",
unexpectedConsAlt: "Unexpected newline between consequent and alternate of ternary expression."
}
},

fixable: "whitespace"
},

create(context) {
const sourceCode = context.getSourceCode();
const option = context.options[0];
const multiline = option !== "never";
const allowSingleLine = option === "always-multiline";
const sourceCode = context.getSourceCode();

//--------------------------------------------------------------------------
// Public
Expand All @@ -59,6 +62,8 @@ module.exports = {
const areTestAndConsequentOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfTest, firstTokenOfConsequent);
const areConsequentAndAlternateOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfConsequent, firstTokenOfAlternate);

const hasComments = !!sourceCode.getCommentsInside(node).length;

if (!multiline) {
if (!areTestAndConsequentOnSameLine) {
context.report({
Expand All @@ -67,7 +72,24 @@ module.exports = {
start: firstTokenOfTest.loc.start,
end: lastTokenOfTest.loc.end
},
messageId: "unexpectedTestCons"
messageId: "unexpectedTestCons",
fix: fixer => {
if (hasComments) {
return null;
}
const fixers = [];
const areTestAndQuestionOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfTest, questionToken);
const areQuestionAndConsOnSameLine = astUtils.isTokenOnSameLine(questionToken, firstTokenOfConsequent);

if (!areTestAndQuestionOnSameLine) {
fixers.push(fixer.removeRange([lastTokenOfTest.range[1], questionToken.range[0]]));
}
if (!areQuestionAndConsOnSameLine) {
fixers.push(fixer.removeRange([questionToken.range[1], firstTokenOfConsequent.range[0]]));
}

return fixers;
}
});
}

Expand All @@ -78,7 +100,24 @@ module.exports = {
start: firstTokenOfConsequent.loc.start,
end: lastTokenOfConsequent.loc.end
},
messageId: "unexpectedConsAlt"
messageId: "unexpectedConsAlt",
fix: fixer => {
if (hasComments) {
return null;
}
const fixers = [];
const areConsAndColonOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfConsequent, colonToken);
const areColonAndAltOnSameLine = astUtils.isTokenOnSameLine(colonToken, firstTokenOfAlternate);

if (!areConsAndColonOnSameLine) {
fixers.push(fixer.removeRange([lastTokenOfConsequent.range[1], colonToken.range[0]]));
}
if (!areColonAndAltOnSameLine) {
fixers.push(fixer.removeRange([colonToken.range[1], firstTokenOfAlternate.range[0]]));
}

return fixers;
}
});
}
} else {
Expand All @@ -93,7 +132,16 @@ module.exports = {
start: firstTokenOfTest.loc.start,
end: lastTokenOfTest.loc.end
},
messageId: "expectedTestCons"
messageId: "expectedTestCons",
fix: fixer => (hasComments ? null : (
fixer.replaceTextRange(
[
lastTokenOfTest.range[1],
questionToken.range[0]
],
"\n"
)
))
});
}

Expand All @@ -104,7 +152,16 @@ module.exports = {
start: firstTokenOfConsequent.loc.start,
end: lastTokenOfConsequent.loc.end
},
messageId: "expectedConsAlt"
messageId: "expectedConsAlt",
fix: (fixer => (hasComments ? null : (
fixer.replaceTextRange(
[
lastTokenOfConsequent.range[1],
colonToken.range[0]
],
"\n"
)
)))
});
}
}
Expand Down

0 comments on commit 0649871

Please sign in to comment.