Add internal ESLint rule await-cli-tests (#12013)

Co-authored-by: Sosuke Suzuki <aosukeke@gmail.com>
This commit is contained in:
fisker CheungandSosuke Suzuki authored and GitHub committed 2022-01-03 22:16:25 +08:00
1 parent 8c846bc86c
commit 5f453482bc
4 files changed
+105

No files matched your search

@@ -0,0 +1,72 @@
"use strict";
const selector = [
"CallExpression",
'[callee.type="Identifier"]',
'[callee.name="runPrettier"]',
].join("");
const MESSAGE_ID_CALL = "await-cli-tests/call";
const MESSAGE_ID_GETTER = "await-cli-tests/getter";
module.exports = {
meta: {
type: "suggestion",
docs: {
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/await-cli-tests.js",
},
messages: {
[MESSAGE_ID_CALL]:
"'runPrettier()' should be awaited or calling `.test()`.",
[MESSAGE_ID_GETTER]: "'runPrettier().{{property}}' should be awaited.",
},
},
create(context) {
return {
[selector](callExpression) {
const { parent } = callExpression;
if (
parent.type === "AwaitExpression" &&
parent.argument === callExpression
) {
return;
}
if (
parent.type === "MemberExpression" &&
parent.object === callExpression &&
parent.property.type === "Identifier" &&
parent.property.name === "test" &&
parent.parent.type === "CallExpression" &&
parent.parent.callee === parent
) {
return;
}
if (
parent.type === "MemberExpression" &&
parent.object === callExpression &&
parent.property.type === "Identifier" &&
["status", "write", "stdout", "stderr"].includes(parent.property.name)
) {
const memberExpression = callExpression.parent;
if (
memberExpression.parent.type === "AwaitExpression" &&
memberExpression.parent.argument === memberExpression
) {
return;
}
context.report({
node: memberExpression.property,
messageId: MESSAGE_ID_GETTER,
data: { property: memberExpression.property.name },
});
return;
}
context.report({ node: callExpression, messageId: MESSAGE_ID_CALL });
},
};
},
};
@@ -2,6 +2,7 @@
module.exports = {
rules: {
"await-cli-tests": require("./await-cli-tests.js"),
"better-parent-property-check-in-needs-parens": require("./better-parent-property-check-in-needs-parens.js"),
"consistent-negative-index-access": require("./consistent-negative-index-access.js"),
"directly-loc-start-end": require("./directly-loc-start-end.js"),
@@ -14,6 +14,32 @@ const test = (ruleId, tests) => {
);
};
test("await-cli-tests", {
valid: [
"async () => await runPrettier()",
"runPrettier().test()",
"notRunPrettier()",
"async () => await runPrettier().stderr",
outdent`
async () => {
const originalStdout = await runPrettier("plugins/options", ["--help"]).stdout;
}
`,
],
invalid: [
{
code: "runPrettier()",
errors: [
{ message: "'runPrettier()' should be awaited or calling `.test()`." },
],
},
{
code: "runPrettier().stderr",
errors: [{ message: "'runPrettier().stderr' should be awaited." }],
},
],
});
test("better-parent-property-check-in-needs-parens", {
valid: ["function needsParens() {return parent.test === node;}"],
invalid: [