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
53 changes: 47 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,16 @@ module.exports = {
start: firstTokenOfTest.loc.start,
end: lastTokenOfTest.loc.end
},
messageId: "unexpectedTestCons"
messageId: "unexpectedTestCons",
fix: fixer => (hasComments ? null : (
fixer.replaceTextRange(
[
lastTokenOfTest.range[1],
firstTokenOfConsequent.range[0]
],
" ? "
)
))
aladdin-add marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand All @@ -78,7 +92,16 @@ module.exports = {
start: firstTokenOfConsequent.loc.start,
end: lastTokenOfConsequent.loc.end
},
messageId: "unexpectedConsAlt"
messageId: "unexpectedConsAlt",
fix: fixer => (hasComments ? null : (
fixer.replaceTextRange(
[
lastTokenOfConsequent.range[1],
firstTokenOfAlternate.range[0]
],
" : "
)
))
});
}
} else {
Expand All @@ -93,7 +116,16 @@ module.exports = {
start: firstTokenOfTest.loc.start,
end: lastTokenOfTest.loc.end
},
messageId: "expectedTestCons"
messageId: "expectedTestCons",
fix: fixer => (hasComments ? null : (
fixer.replaceTextRange(
[
lastTokenOfTest.range[1],
firstTokenOfConsequent.range[0]
],
"\n? "
)
))
aladdin-add marked this conversation as resolved.
Show resolved Hide resolved
});
}

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