Bug 1917003 - [eslint] Add linter rule to ban await await r=Standard8,frontend-codestyle-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D221187
This commit is contained in:
Julian Descottes 2024-09-16 09:13:30 +00:00
parent 003b2697f4
commit 42cf53ea82
5 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,23 @@
reject-multiple-await
=====================
Disallows using `await await somePromise`. While this is valid syntax, this
usually unintentional and can have a slightly different behavior from using a
single `await`. Using `await await` should ideally be explained with a comment.
Examples of incorrect code for this rule:
-----------------------------------------
.. code-block:: js
await await somePromise;
await await await somePromise;
await (await somePromise);
Examples of correct code for this rule:
---------------------------------------
.. code-block:: js
await somePromise;

View File

@ -176,6 +176,7 @@ module.exports = {
"mozilla/reject-chromeutils-import": "error",
"mozilla/reject-chromeutils-import-params": "error",
"mozilla/reject-importGlobalProperties": ["error", "allownonwebidl"],
"mozilla/reject-multiple-await": "error",
"mozilla/reject-multiple-getters-calls": "error",
"mozilla/reject-scriptableunicodeconverter": "warn",
"mozilla/rejects-requires-await": "error",

View File

@ -76,6 +76,7 @@ module.exports = {
"reject-importGlobalProperties": require("../lib/rules/reject-importGlobalProperties"),
"reject-lazy-imports-into-globals": require("../lib/rules/reject-lazy-imports-into-globals"),
"reject-mixing-eager-and-lazy": require("../lib/rules/reject-mixing-eager-and-lazy"),
"reject-multiple-await": require("../lib/rules/reject-multiple-await.js"),
"reject-multiple-getters-calls": require("../lib/rules/reject-multiple-getters-calls"),
"reject-scriptableunicodeconverter": require("../lib/rules/reject-scriptableunicodeconverter"),
"reject-relative-requires": require("../lib/rules/reject-relative-requires"),

View File

@ -0,0 +1,35 @@
/**
* @fileoverview Reject multiple await operators.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
"use strict";
module.exports = {
meta: {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/rules/reject-multiple-await.html",
},
messages: {
rejectMultipleAwait: "Do not use multiple await operators.",
},
schema: [],
type: "problem",
},
create(context) {
return {
AwaitExpression(node) {
if (
node.parent.type === "AwaitExpression" &&
node.parent.parent.type !== "AwaitExpression"
) {
context.report({ node, messageId: "rejectMultipleAwait" });
}
},
};
},
};

View File

@ -0,0 +1,37 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
var rule = require("../lib/rules/reject-multiple-await");
var RuleTester = require("eslint").RuleTester;
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: "latest" } });
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
function invalidCode(code) {
return { code, errors: [{ messageId: "rejectMultipleAwait" }] };
}
ruleTester.run("reject-multiple-await", rule, {
valid: [
"async () => await new Promise(r => r());",
"async () => await awaitSomething;",
],
invalid: [
invalidCode("async () => await await new Promise(r => r());"),
invalidCode(`async () => {
await
await new Promise(r => r());
}`),
invalidCode("async () => await (await new Promise(r => r()));"),
invalidCode("async () => await await await new Promise(r => r());"),
],
});