Skip to content

Commit

Permalink
Update: Added auto-fix to multiline-ternary
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sawtaytoes committed Feb 28, 2020
1 parent 4797fb2 commit a00ffc6
Showing 1 changed file with 63 additions and 9 deletions.
72 changes: 63 additions & 9 deletions lib/rules/multiline-ternary.js
Expand Up @@ -24,15 +24,18 @@ module.exports = {

schema: [
{
enum: ["always", "always-multiline", "never"]
}
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: 'code',
},

create(context) {
Expand All @@ -52,10 +55,61 @@ module.exports = {
* @returns {void}
* @private
*/
function reportError(node, parentNode, expected) {
function reportSingleLineError(node, parentNode, expected) {
context.report({
node,
messageId: `${expected ? "expected" : "unexpected"}${node === parentNode.test ? "TestCons" : "ConsAlt"}`,
fix: fixer => ([
!astUtils.isTokenOnSameLine(parentNode.test, parentNode.consequent) &&
fixer.replaceTextRange(
[
parentNode.test.range[1],
parentNode.consequent.range[0],
],
' ? ',
),
!astUtils.isTokenOnSameLine(parentNode.consequent, parentNode.alternate) &&
fixer.replaceTextRange(
[
parentNode.consequent.range[1],
parentNode.alternate.range[0],
],
' : ',
),
]),
});
}

/**
* Tests whether node is preceded by supplied tokens
* @param {ASTNode} node node to check
* @param {ASTNode} parentNode parent of node to report
* @param {boolean} expected whether newline was expected or not
* @returns {void}
* @private
*/
function reportMultilineError(node, parentNode, expected) {
context.report({
node,
messageId: `${expected ? "expected" : "unexpected"}${node === parentNode.test ? "TestCons" : "ConsAlt"}`
messageId: `${expected ? "expected" : "unexpected"}${node === parentNode.test ? "TestCons" : "ConsAlt"}`,
fix: fixer => ([
astUtils.isTokenOnSameLine(parentNode.test, parentNode.consequent) &&
fixer.replaceTextRange(
[
parentNode.test.range[1],
parentNode.consequent.range[0],
],
'\n? ',
),
astUtils.isTokenOnSameLine(parentNode.consequent, parentNode.alternate) &&
fixer.replaceTextRange(
[
parentNode.consequent.range[1],
parentNode.alternate.range[0],
],
'\n: ',
),
]),
});
}

Expand All @@ -70,23 +124,23 @@ module.exports = {

if (!multiline) {
if (!areTestAndConsequentOnSameLine) {
reportError(node.test, node, false);
reportSingleLineError(node.test, node, false);
}

if (!areConsequentAndAlternateOnSameLine) {
reportError(node.consequent, node, false);
reportSingleLineError(node.consequent, node, false);
}
} else {
if (allowSingleLine && node.loc.start.line === node.loc.end.line) {
return;
}

if (areTestAndConsequentOnSameLine) {
reportError(node.test, node, true);
reportMultilineError(node.test, node, true);
}

if (areConsequentAndAlternateOnSameLine) {
reportError(node.consequent, node, true);
reportMultilineError(node.consequent, node, true);
}
}
}
Expand Down

0 comments on commit a00ffc6

Please sign in to comment.