UNPKG

1.62 kBJavaScriptView Raw
1/**
2 * @fileoverview Reject some uses of require.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9"use strict";
10
11// -----------------------------------------------------------------------------
12// Rule Definition
13// -----------------------------------------------------------------------------
14
15module.exports = function(context) {
16
17 // ---------------------------------------------------------------------------
18 // Public
19 // --------------------------------------------------------------------------
20
21 if (typeof(context.options[0]) !== "string") {
22 throw new Error("reject-some-requires expects a regexp");
23 }
24 const RX = new RegExp(context.options[0]);
25
26 const checkPath = function(node, path) {
27 if (RX.test(path)) {
28 context.report(node, `require(${path}) is not allowed`);
29 }
30 };
31
32 return {
33 "CallExpression": function(node) {
34 if (node.callee.type == "Identifier" &&
35 node.callee.name == "require" &&
36 node.arguments.length == 1 &&
37 node.arguments[0].type == "Literal") {
38 checkPath(node, node.arguments[0].value);
39 } else if (node.callee.type == "MemberExpression" &&
40 node.callee.property.type == "Identifier" &&
41 node.callee.property.name == "lazyRequireGetter" &&
42 node.arguments.length >= 3 &&
43 node.arguments[2].type == "Literal") {
44 checkPath(node, node.arguments[2].value);
45 }
46 }
47 };
48};