123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /**
- * @fileoverview Reject common XPCOM methods called with useless optional
- * parameters, or non-existent parameters.
- *
- * 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/no-useless-parameters.html",
- },
- fixable: "code",
- type: "suggestion",
- },
- create(context) {
- function getRangeAfterArgToEnd(argNumber, args) {
- let sourceCode = context.getSourceCode();
- return [
- sourceCode.getTokenAfter(args[argNumber]).range[0],
- args[args.length - 1].range[1],
- ];
- }
- return {
- CallExpression(node) {
- let callee = node.callee;
- if (
- callee.type !== "MemberExpression" ||
- callee.property.type !== "Identifier"
- ) {
- return;
- }
- let isFalse = arg => arg.type === "Literal" && arg.value === false;
- let isFalsy = arg => arg.type === "Literal" && !arg.value;
- let isBool = arg =>
- arg.type === "Literal" && (arg.value === false || arg.value === true);
- let name = callee.property.name;
- let args = node.arguments;
- if (
- ["addEventListener", "removeEventListener", "addObserver"].includes(
- name
- ) &&
- args.length === 3 &&
- isFalse(args[2])
- ) {
- context.report({
- node,
- fix: fixer => {
- return fixer.removeRange(getRangeAfterArgToEnd(1, args));
- },
- message: `${name}'s third parameter can be omitted when it's false.`,
- });
- }
- if (name === "clearUserPref" && args.length > 1) {
- context.report({
- node,
- fix: fixer => {
- return fixer.removeRange(getRangeAfterArgToEnd(0, args));
- },
- message: `${name} takes only 1 parameter.`,
- });
- }
- if (name === "removeObserver" && args.length === 3 && isBool(args[2])) {
- context.report({
- node,
- fix: fixer => {
- return fixer.removeRange(getRangeAfterArgToEnd(1, args));
- },
- message: "removeObserver only takes 2 parameters.",
- });
- }
- if (name === "appendElement" && args.length === 2 && isFalse(args[1])) {
- context.report({
- node,
- fix: fixer => {
- return fixer.removeRange(getRangeAfterArgToEnd(0, args));
- },
- message: `${name}'s second parameter can be omitted when it's false.`,
- });
- }
- if (
- name === "notifyObservers" &&
- args.length === 3 &&
- isFalsy(args[2])
- ) {
- context.report({
- node,
- fix: fixer => {
- return fixer.removeRange(getRangeAfterArgToEnd(1, args));
- },
- message: `${name}'s third parameter can be omitted.`,
- });
- }
- if (
- name === "getComputedStyle" &&
- args.length === 2 &&
- isFalsy(args[1])
- ) {
- context.report({
- node,
- fix: fixer => {
- return fixer.removeRange(getRangeAfterArgToEnd(0, args));
- },
- message: "getComputedStyle's second parameter can be omitted.",
- });
- }
- if (
- name === "newURI" &&
- args.length > 1 &&
- isFalsy(args[args.length - 1])
- ) {
- context.report({
- node,
- fix: fixer => {
- if (args.length > 2 && isFalsy(args[args.length - 2])) {
- return fixer.removeRange(getRangeAfterArgToEnd(0, args));
- }
- return fixer.removeRange(
- getRangeAfterArgToEnd(args.length - 2, args)
- );
- },
- message: "newURI's last parameters are optional.",
- });
- }
- },
- };
- },
- };
|