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

Update: add autofix to rule multiline-ternary #13958

Merged
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 areTestAndcolonOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfConsequent, colonToken);
aladdin-add marked this conversation as resolved.
Show resolved Hide resolved
const areColonAndAltOnSameLine = astUtils.isTokenOnSameLine(colonToken, firstTokenOfAlternate);

if (!areTestAndcolonOnSameLine) {
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