UNPKG

2.32 kBJavaScriptView Raw
1/**
2 * @fileoverview Reject use of Cu.import and XPCOMUtils.defineLazyModuleGetter
3 * in favor of ChromeUtils.
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
16function isIdentifier(node, id) {
17 return node && node.type === "Identifier" && node.name === id;
18}
19
20function isMemberExpression(node, object, member) {
21 return (node.type === "MemberExpression" &&
22 isIdentifier(node.object, object) &&
23 isIdentifier(node.property, member));
24}
25
26module.exports = {
27 meta: {
28 schema: [
29 {
30 "type": "object",
31 "properties": {
32 "allowCu": {
33 "type": "boolean"
34 }
35 },
36 "additionalProperties": false
37 }
38 ],
39 fixable: "code"
40 },
41
42 create(context) {
43 return {
44 "CallExpression": function(node) {
45 if (node.callee.type !== "MemberExpression") {
46 return;
47 }
48
49 let {allowCu} = context.options[0] || {};
50 let {callee} = node;
51
52 // Is the expression starting with `Cu` or `Components.utils`?
53 if (((!allowCu && isIdentifier(callee.object, "Cu")) ||
54 isMemberExpression(callee.object, "Components", "utils")) &&
55 isIdentifier(callee.property, "import")) {
56 context.report({
57 node,
58 message: "Please use ChromeUtils.import instead of Cu.import",
59 fix(fixer) {
60 return fixer.replaceText(callee, "ChromeUtils.import");
61 }
62 });
63 }
64
65 if (isMemberExpression(callee, "XPCOMUtils", "defineLazyModuleGetter") &&
66 node.arguments.length < 4) {
67 context.report({
68 node,
69 message: ("Please use ChromeUtils.defineModuleGetter instead of " +
70 "XPCOMUtils.defineLazyModuleGetter"),
71 fix(fixer) {
72 return fixer.replaceText(callee, "ChromeUtils.defineModuleGetter");
73 }
74 });
75 }
76 }
77 };
78 }
79};