UNPKG

2.44 kBJavaScriptView Raw
1/**
2 * @fileoverview Simply marks exported symbols as used. Designed for use in
3 * .jsm files only.
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
12function markArrayElementsAsUsed(context, node, expression) {
13 if (expression.type != "ArrayExpression") {
14 context.report({
15 node,
16 message: "Unexpected assignment of non-Array to EXPORTED_SYMBOLS"
17 });
18 return;
19 }
20
21 for (let element of expression.elements) {
22 context.markVariableAsUsed(element.value);
23 }
24 // Also mark EXPORTED_SYMBOLS as used.
25 context.markVariableAsUsed("EXPORTED_SYMBOLS");
26}
27
28// -----------------------------------------------------------------------------
29// Rule Definition
30// -----------------------------------------------------------------------------
31
32module.exports = function(context) {
33 // Ignore assignments not in the global scope, e.g. where special module
34 // definitions are required due to having different ways of importing files,
35 // e.g. osfile.
36 function isGlobalScope() {
37 return !context.getScope().upper;
38 }
39
40 // ---------------------------------------------------------------------------
41 // Public
42 // ---------------------------------------------------------------------------
43
44 return {
45 AssignmentExpression(node, parents) {
46 if (node.operator === "=" &&
47 node.left.type === "MemberExpression" &&
48 node.left.object.type === "ThisExpression" &&
49 node.left.property.name === "EXPORTED_SYMBOLS" &&
50 isGlobalScope()) {
51 markArrayElementsAsUsed(context, node, node.right);
52 }
53 },
54
55 VariableDeclaration(node, parents) {
56 if (!isGlobalScope()) {
57 return;
58 }
59
60 for (let item of node.declarations) {
61 if (item.id &&
62 item.id.type == "Identifier" &&
63 item.id.name === "EXPORTED_SYMBOLS") {
64 if (node.kind === "let") {
65 // The use of 'let' isn't allowed as the lexical scope may die after
66 // the script executes.
67 context.report({
68 node,
69 message: "EXPORTED_SYMBOLS cannot be declared via `let`. Use `var` or `this.EXPORTED_SYMBOLS =`"
70 });
71 }
72
73 markArrayElementsAsUsed(context, node, item.init);
74 }
75 }
76 }
77 };
78};