RFC: indentIfBreak (#10221)
This commit is contained in:
1 parent
575be32c05
commit
5a33fec904
14 files changed
+221
-31
No files matched your search
@@ -6,10 +6,11 @@ module.exports = {
|
||||
"directly-loc-start-end": require("./directly-loc-start-end"),
|
||||
"jsx-identifier-case": require("./jsx-identifier-case"),
|
||||
"no-doc-builder-concat": require("./no-doc-builder-concat"),
|
||||
"no-empty-flat-contents-for-if-break": require("./no-empty-flat-contents-for-if-break"),
|
||||
"no-node-comments": require("./no-node-comments"),
|
||||
"prefer-fast-path-each": require("./prefer-fast-path-each"),
|
||||
"prefer-indent-if-break": require("./prefer-indent-if-break"),
|
||||
"prefer-is-non-empty-array": require("./prefer-is-non-empty-array"),
|
||||
"require-json-extensions": require("./require-json-extensions"),
|
||||
"no-empty-flat-contents-for-if-break": require("./no-empty-flat-contents-for-if-break"),
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,67 @@
|
||||
"use strict";
|
||||
|
||||
const selector = [
|
||||
"CallExpression",
|
||||
"[optional=false]",
|
||||
'[callee.type="Identifier"]',
|
||||
'[callee.name="ifBreak"]',
|
||||
"[arguments.length=3]",
|
||||
'[arguments.0.type="CallExpression"]',
|
||||
"[arguments.0.optional=false]",
|
||||
'[arguments.0.callee.type="Identifier"]',
|
||||
'[arguments.0.callee.name="indent"]',
|
||||
"[arguments.0.arguments.length=1]",
|
||||
'[arguments.0.arguments.0.type!="SpreadElement"]',
|
||||
'[arguments.1.type!="SpreadElement"]',
|
||||
'[arguments.2.type!="SpreadElement"]',
|
||||
].join("");
|
||||
|
||||
const messageId = "prefer-indent-if-break";
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
type: "suggestion",
|
||||
docs: {
|
||||
url:
|
||||
"https://github.com/prettier/prettier/blob/main/scripts/eslint-plugin-prettier-internal-rules/prefer-indent-if-break.js",
|
||||
},
|
||||
messages: {
|
||||
[messageId]: "Prefer `indentIfBreak(…)` over `ifBreak(indent(…), …)`.",
|
||||
},
|
||||
fixable: "code",
|
||||
},
|
||||
create(context) {
|
||||
const sourceCode = context.getSourceCode();
|
||||
|
||||
return {
|
||||
[selector](node) {
|
||||
const indentDoc = node.arguments[0].arguments[0];
|
||||
const doc = node.arguments[1];
|
||||
|
||||
// Use text to compare same `doc`
|
||||
if (sourceCode.getText(indentDoc) !== sourceCode.getText(doc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
context.report({
|
||||
node,
|
||||
messageId,
|
||||
*fix(fixer) {
|
||||
yield fixer.replaceText(node.callee, "indentIfBreak");
|
||||
const openingParenthesisToken = sourceCode.getTokenAfter(
|
||||
node.callee
|
||||
);
|
||||
const commaToken = sourceCode.getTokenBefore(
|
||||
doc,
|
||||
({ type, value }) => type === "Punctuator" && value === ","
|
||||
);
|
||||
yield fixer.replaceTextRange(
|
||||
[openingParenthesisToken.range[1], commaToken.range[1]],
|
||||
""
|
||||
);
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -155,6 +155,35 @@ test("prefer-fast-path-each", {
|
||||
],
|
||||
});
|
||||
|
||||
test("prefer-indent-if-break", {
|
||||
valid: [
|
||||
"ifBreak(indent(doc))",
|
||||
"notIfBreak(indent(doc), doc, options)",
|
||||
"ifBreak(indent(doc), doc, )",
|
||||
"ifBreak(...a, ...b, ...c)",
|
||||
"ifBreak(notIndent(doc), doc, options)",
|
||||
"ifBreak(indent(doc), notSameDoc, options)",
|
||||
"ifBreak(indent(...a), a, options)",
|
||||
"ifBreak(indent(a, b), a, options)",
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
code: "ifBreak(indent(doc), doc, options)",
|
||||
output: "indentIfBreak( doc, options)",
|
||||
errors: [
|
||||
{
|
||||
message: "Prefer `indentIfBreak(…)` over `ifBreak(indent(…), …)`.",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
code: "ifBreak((indent(doc)), (doc), options)",
|
||||
output: "indentIfBreak( (doc), options)",
|
||||
errors: 1,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
test("prefer-is-non-empty-array", {
|
||||
valid: [
|
||||
// `isNonEmptyArray` self is ignored
|
||||
|
||||
Reference in new issue
Block a user