зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1334831 - add an eslint rule to report usage of .parentNode.removeChild when .remove() could be used instead, r=jaws.
This commit is contained in:
Родитель
f5038afffa
Коммит
77bac28360
|
@ -6,6 +6,7 @@ module.exports = {
|
|||
"mozilla"
|
||||
],
|
||||
"rules": {
|
||||
"mozilla/avoid-removeChild": "error",
|
||||
"mozilla/import-globals": "warn",
|
||||
"mozilla/no-import-into-var-and-global": "error",
|
||||
"mozilla/no-useless-parameters": "error",
|
||||
|
|
|
@ -2,6 +2,11 @@
|
|||
Mozilla ESLint Plugin
|
||||
=====================
|
||||
|
||||
avoid-removeChild
|
||||
-----------------
|
||||
|
||||
Rejects using element.parentNode.removeChild(element) when element.remove()
|
||||
can be used instead.
|
||||
|
||||
balanced-listeners
|
||||
------------------
|
||||
|
|
|
@ -17,6 +17,7 @@ module.exports = {
|
|||
".xml": require("../lib/processors/xbl-bindings"),
|
||||
},
|
||||
rules: {
|
||||
"avoid-removeChild": require("../lib/rules/avoid-removeChild"),
|
||||
"balanced-listeners": require("../lib/rules/balanced-listeners"),
|
||||
"import-browserjs-globals": require("../lib/rules/import-browserjs-globals"),
|
||||
"import-globals": require("../lib/rules/import-globals"),
|
||||
|
@ -34,6 +35,7 @@ module.exports = {
|
|||
"var-only-at-top-level": require("../lib/rules/var-only-at-top-level")
|
||||
},
|
||||
rulesConfig: {
|
||||
"avoid-removeChild": 0,
|
||||
"balanced-listeners": 0,
|
||||
"import-browserjs-globals": 0,
|
||||
"import-globals": 0,
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* @fileoverview Reject using element.parentNode.removeChild(element) when
|
||||
* element.remove() can be used instead.
|
||||
*
|
||||
* 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
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
var helpers = require("../helpers");
|
||||
|
||||
module.exports = function(context) {
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
return {
|
||||
"CallExpression": function(node) {
|
||||
let callee = node.callee;
|
||||
if (callee.type !== "MemberExpression" ||
|
||||
callee.property.type !== "Identifier" ||
|
||||
callee.property.name != "removeChild" ||
|
||||
callee.object.type != "MemberExpression" ||
|
||||
callee.object.property.type != "Identifier" ||
|
||||
callee.object.property.name != "parentNode" ||
|
||||
helpers.getASTSource(callee.object.object) !=
|
||||
helpers.getASTSource(node.arguments[0]) ||
|
||||
node.arguments.length != 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
context.report(node, "use element.remove() instead of " +
|
||||
"element.parentNode.removeChild(element)");
|
||||
}
|
||||
};
|
||||
};
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "eslint-plugin-mozilla",
|
||||
"version": "0.2.15",
|
||||
"version": "0.2.16",
|
||||
"description": "A collection of rules that help enforce JavaScript coding standard in the Mozilla project.",
|
||||
"keywords": [
|
||||
"eslint",
|
||||
|
|
|
@ -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/avoid-removeChild");
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Tests
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
function invalidCode(code) {
|
||||
let message = "use element.remove() instead of " +
|
||||
"element.parentNode.removeChild(element)";
|
||||
return {code: code, errors: [{message: message, type: "CallExpression"}]};
|
||||
}
|
||||
|
||||
exports.runTest = function(ruleTester) {
|
||||
ruleTester.run("no-useless-removeEventListener", rule, {
|
||||
valid: [
|
||||
"elt.remove();",
|
||||
"elt.parentNode.parentNode.removeChild(elt2.parentNode);",
|
||||
"elt.parentNode.removeChild(elt2);",
|
||||
"elt.removeChild(elt2);"
|
||||
],
|
||||
invalid: [
|
||||
invalidCode("elt.parentNode.removeChild(elt);"),
|
||||
invalidCode("elt.parentNode.parentNode.removeChild(elt.parentNode);"),
|
||||
invalidCode("$(e).parentNode.removeChild($(e));"),
|
||||
invalidCode("$('e').parentNode.removeChild($('e'));"),
|
||||
]
|
||||
});
|
||||
};
|
Загрузка…
Ссылка в новой задаче