style: Add new ESLint rule no-empty-string-for-ifbreak (#10172)
* Add new ESLint rule for ifBreak * Fix for third argument * Run fix with new rule * Address review * Fix * Update no-empty-flat-contents-for-if-break.js * Fix CI config Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
1 parent
c3a59148cf
commit
07b30645ec
9 files changed
+94
-7
No files matched your search
@@ -117,6 +117,7 @@ overrides:
|
||||
prettier-internal-rules/no-doc-builder-concat: error
|
||||
prettier-internal-rules/prefer-fast-path-each: error
|
||||
prettier-internal-rules/prefer-is-non-empty-array: error
|
||||
prettier-internal-rules/no-empty-flat-contents-for-if-break: error
|
||||
- files: src/document/*.js
|
||||
rules:
|
||||
prettier-internal-rules/no-doc-builder-concat: off
|
||||
|
||||
@@ -3,11 +3,11 @@ name: Internal_ESLint_Rules_Test
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- "scripts/tools/eslint-plugin-prettier-internal-rules"
|
||||
- "scripts/tools/eslint-plugin-prettier-internal-rules/**"
|
||||
- ".github/workflows/eslint-rules.yml"
|
||||
pull_request:
|
||||
paths:
|
||||
- "scripts/tools/eslint-plugin-prettier-internal-rules"
|
||||
- "scripts/tools/eslint-plugin-prettier-internal-rules/**"
|
||||
- ".github/workflows/eslint-rules.yml"
|
||||
|
||||
jobs:
|
||||
|
||||
@@ -10,5 +10,6 @@ module.exports = {
|
||||
"prefer-fast-path-each": require("./prefer-fast-path-each"),
|
||||
"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"),
|
||||
},
|
||||
};
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
"use strict";
|
||||
|
||||
const selector = [
|
||||
"CallExpression",
|
||||
"[optional=false]",
|
||||
'[callee.type="Identifier"]',
|
||||
'[callee.name="ifBreak"]',
|
||||
"[arguments.length=2]",
|
||||
'[arguments.1.type="Literal"]',
|
||||
'[arguments.1.value=""]',
|
||||
':not([arguments.0.type="SpreadElement"])',
|
||||
].join("");
|
||||
|
||||
const messageId = "no-empty-flat-contents-for-if-break";
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
type: "suggestion",
|
||||
docs: {
|
||||
url:
|
||||
"https://github.com/prettier/prettier/blob/main/scripts/eslint-plugin-prettier-internal-rules/no-empty-flat-contents-for-if-break.js",
|
||||
},
|
||||
messages: {
|
||||
[messageId]:
|
||||
"Please don't pass an empty string to second parameter of ifBreak.",
|
||||
},
|
||||
fixable: "code",
|
||||
},
|
||||
create(context) {
|
||||
return {
|
||||
[selector](node) {
|
||||
const [breakContents] = node.arguments;
|
||||
context.report({
|
||||
node,
|
||||
messageId,
|
||||
fix: (fixer) =>
|
||||
fixer.removeRange([breakContents.range[1], node.range[1] - 1]),
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -204,3 +204,46 @@ test("require-json-extensions", {
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
test("no-empty-flat-contents-for-if-break", {
|
||||
valid: [
|
||||
"ifBreak('foo', 'bar')",
|
||||
"ifBreak(doc1, doc2)",
|
||||
"ifBreak(',')",
|
||||
"ifBreak(doc)",
|
||||
"ifBreak('foo', '', { groupId })",
|
||||
"ifBreak(...foo, { groupId })",
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
code: "ifBreak('foo', '')",
|
||||
output: "ifBreak('foo')",
|
||||
errors: [
|
||||
{
|
||||
message:
|
||||
"Please don't pass an empty string to second parameter of ifBreak.",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
code: "ifBreak('foo' , '' )",
|
||||
output: "ifBreak('foo')",
|
||||
errors: [
|
||||
{
|
||||
message:
|
||||
"Please don't pass an empty string to second parameter of ifBreak.",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
code: "ifBreak(doc, '')",
|
||||
output: "ifBreak(doc)",
|
||||
errors: [
|
||||
{
|
||||
message:
|
||||
"Please don't pass an empty string to second parameter of ifBreak.",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -186,7 +186,7 @@ function genericPrint(path, options, print) {
|
||||
options.originalText[locEnd(node) - 1] !== ";"
|
||||
? ""
|
||||
: options.__isHTMLStyleAttribute && isLastNode(path, node)
|
||||
? ifBreak(";", "")
|
||||
? ifBreak(";")
|
||||
: ";",
|
||||
];
|
||||
}
|
||||
|
||||
@@ -221,10 +221,10 @@ function printTernary(path, options, print) {
|
||||
// parens when using ?: within JSX, because the parens are analogous to
|
||||
// curly braces in an if statement.
|
||||
const wrap = (doc) => [
|
||||
ifBreak("(", ""),
|
||||
ifBreak("("),
|
||||
indent([softline, doc]),
|
||||
softline,
|
||||
ifBreak(")", ""),
|
||||
ifBreak(")"),
|
||||
];
|
||||
|
||||
// The only things we don't wrap are:
|
||||
|
||||
@@ -348,7 +348,7 @@ function printTypescript(path, options, print) {
|
||||
n.optional ? getTypeScriptMappedTypeModifier(n.optional, "?") : "",
|
||||
n.typeAnnotation ? ": " : "",
|
||||
path.call(print, "typeAnnotation"),
|
||||
ifBreak(semi, ""),
|
||||
ifBreak(semi),
|
||||
]),
|
||||
printDanglingComments(path, options, /* sameIndent */ true),
|
||||
options.bracketSpacing ? line : softline,
|
||||
|
||||
@@ -29,7 +29,7 @@ function printFlowMapping(path, print, options) {
|
||||
alignWithSpaces(options.tabWidth, [
|
||||
bracketSpacing,
|
||||
printChildren(path, print, options),
|
||||
options.trailingComma === "none" ? "" : ifBreak(",", ""),
|
||||
options.trailingComma === "none" ? "" : ifBreak(","),
|
||||
hasEndComments(node)
|
||||
? [hardline, join(hardline, path.map(print, "endComments"))]
|
||||
: "",
|
||||
|
||||
Reference in new issue
Block a user