зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1625500 - tell people to use promise.finally() instead of passing 2 identical things to .then, r=Standard8
Differential Revision: https://phabricator.services.mozilla.com/D68613
This commit is contained in:
Родитель
f519d74b3b
Коммит
92b577ebfa
|
@ -134,6 +134,7 @@ module.exports = {
|
|||
"mozilla/use-chromeutils-generateqi": "error",
|
||||
"mozilla/use-chromeutils-import": "error",
|
||||
"mozilla/use-default-preference-values": "error",
|
||||
"mozilla/use-finally": "error",
|
||||
"mozilla/use-includes-instead-of-indexOf": "error",
|
||||
"mozilla/use-ownerGlobal": "error",
|
||||
"mozilla/use-returnValue": "error",
|
||||
|
|
|
@ -61,6 +61,7 @@ module.exports = {
|
|||
"use-chromeutils-generateqi": require("../lib/rules/use-chromeutils-generateqi"),
|
||||
"use-chromeutils-import": require("../lib/rules/use-chromeutils-import"),
|
||||
"use-default-preference-values": require("../lib/rules/use-default-preference-values"),
|
||||
"use-finally": require("../lib/rules/use-finally"),
|
||||
"use-ownerGlobal": require("../lib/rules/use-ownerGlobal"),
|
||||
"use-includes-instead-of-indexOf": require("../lib/rules/use-includes-instead-of-indexOf"),
|
||||
"use-returnValue": require("../lib/rules/use-returnValue"),
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* @fileoverview Require .finally() instead of .then(x, x)
|
||||
*
|
||||
* 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";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Rule Definition
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
module.exports = function(context) {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
return {
|
||||
CallExpression(node) {
|
||||
if (
|
||||
node.callee.type == "MemberExpression" &&
|
||||
node.callee.property.name == "then" &&
|
||||
node.arguments.length == 2
|
||||
) {
|
||||
let sourceCode = context.getSourceCode();
|
||||
let firstArg = sourceCode.getText(node.arguments[0]);
|
||||
let secondArg = sourceCode.getText(node.arguments[1]);
|
||||
if (
|
||||
firstArg == secondArg ||
|
||||
firstArg.replace(/\s+/g, "") == secondArg.replace(/\s+/g, "")
|
||||
) {
|
||||
context.report({
|
||||
node,
|
||||
message:
|
||||
"Use .finally instead of passing 2 identical arguments to .then",
|
||||
fix(fixer) {
|
||||
let endNode = node.arguments[1];
|
||||
let comments = sourceCode.getCommentsBefore(endNode);
|
||||
if (comments.length) {
|
||||
endNode = comments[0];
|
||||
}
|
||||
return fixer.replaceTextRange(
|
||||
[node.callee.property.start, endNode.start],
|
||||
"finally("
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
|
@ -0,0 +1,47 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
"use strict";
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
var rule = require("../lib/rules/use-finally");
|
||||
var RuleTester = require("eslint").RuleTester;
|
||||
|
||||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
let gError = [
|
||||
{
|
||||
message: `Use .finally instead of passing 2 identical arguments to .then`,
|
||||
type: "CallExpression",
|
||||
},
|
||||
];
|
||||
|
||||
ruleTester.run("use-finally", rule, {
|
||||
valid: ["foo.finally(a)", "foo.then(a)", "foo.then(() => {})"],
|
||||
invalid: [
|
||||
{
|
||||
code: "foo.then(a, a);",
|
||||
output: "foo.finally(a);",
|
||||
errors: gError,
|
||||
},
|
||||
// ignore whitespace:
|
||||
{
|
||||
code: "foo.then(() => {}, () => {});",
|
||||
output: "foo.finally(() => {});",
|
||||
errors: gError,
|
||||
},
|
||||
// keep comments:
|
||||
{
|
||||
code: "foo.then(a, /* make sure we always do this */ a);",
|
||||
output: "foo.finally(/* make sure we always do this */ a);",
|
||||
errors: gError,
|
||||
},
|
||||
],
|
||||
});
|
Загрузка…
Ссылка в новой задаче