UNPKG

2 kBJavaScriptView Raw
1/**
2 * @fileoverview Reject using element.parentNode.removeChild(element) when
3 * element.remove() can be used instead.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10"use strict";
11
12// -----------------------------------------------------------------------------
13// Rule Definition
14// -----------------------------------------------------------------------------
15
16var helpers = require("../helpers");
17
18module.exports = function(context) {
19
20 // ---------------------------------------------------------------------------
21 // Public
22 // --------------------------------------------------------------------------
23
24 return {
25 "CallExpression": function(node) {
26 let callee = node.callee;
27 if (callee.type !== "MemberExpression" ||
28 callee.property.type !== "Identifier" ||
29 callee.property.name != "removeChild" ||
30 node.arguments.length != 1) {
31 return;
32 }
33
34 if (callee.object.type == "MemberExpression" &&
35 callee.object.property.type == "Identifier" &&
36 callee.object.property.name == "parentNode" &&
37 helpers.getASTSource(callee.object.object, context) ==
38 helpers.getASTSource(node.arguments[0])) {
39 context.report(node, "use element.remove() instead of " +
40 "element.parentNode.removeChild(element)");
41 }
42
43 if (node.arguments[0].type == "MemberExpression" &&
44 node.arguments[0].property.type == "Identifier" &&
45 node.arguments[0].property.name == "firstChild" &&
46 helpers.getASTSource(callee.object, context) ==
47 helpers.getASTSource(node.arguments[0].object)) {
48 context.report(node, "use element.firstChild.remove() instead of " +
49 "element.removeChild(element.firstChild)");
50 }
51 }
52 };
53};