|
1 | 1 | /** |
2 | | - * @fileoverview Disallow assignment to `require` or `window.require` |
| 2 | + * @fileoverview Rule to disallow assignment to `require` or `window.require` |
3 | 3 | * @author Casey Visco |
4 | 4 | */ |
5 | 5 |
|
6 | 6 | "use strict"; |
7 | 7 |
|
8 | | -const ast = require("../utils/ast"); |
| 8 | +const isIdentifier = require("../utils/ast").isIdentifier; |
| 9 | +const isMemberExpr = require("../utils/ast").isMemberExpr; |
| 10 | + |
| 11 | +// ----------------------------------------------------------------------------- |
| 12 | +// Helpers |
| 13 | +// ----------------------------------------------------------------------------- |
| 14 | + |
| 15 | +/** |
| 16 | + * Determine if supplied `node` represents a `require` identifier |
| 17 | + * @private |
| 18 | + * @param {ASTNode} node - node to test |
| 19 | + * @returns {Boolean} true if `require` identifier |
| 20 | + */ |
| 21 | +function isRequireIdentifier(node) { |
| 22 | + return isIdentifier(node) && node.name === "require"; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Determine if supplied `node` represents a `window` identifier |
| 27 | + * @private |
| 28 | + * @param {ASTNode} node - node to test |
| 29 | + * @returns {Boolean} true if `window` identifier |
| 30 | + */ |
| 31 | +function isWindowIdentifier(node) { |
| 32 | + return isIdentifier(node) && node.name === "window"; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Determine if supplied `node` represents a `window.require` |
| 37 | + * MemberExpression. |
| 38 | + * @private |
| 39 | + * @param {ASTNode} node - node to test |
| 40 | + * @returns {Boolean} true if represents `window.require` expression |
| 41 | + */ |
| 42 | +function isWindowRequireExpr(node) { |
| 43 | + return isMemberExpr(node) && |
| 44 | + isWindowIdentifier(node.object) && |
| 45 | + isRequireIdentifier(node.property); |
| 46 | +} |
| 47 | + |
| 48 | +// ----------------------------------------------------------------------------- |
| 49 | +// Rule Definition |
| 50 | +// ----------------------------------------------------------------------------- |
| 51 | + |
| 52 | +const ERROR_MSG = "Invalid assignment to `require`."; |
9 | 53 |
|
10 | 54 | module.exports = { |
11 | 55 | meta: { |
12 | | - docs: {}, |
| 56 | + docs: { |
| 57 | + description: "Disallow assignment to `require` or `window.require`", |
| 58 | + category: "Stylistic Choices", |
| 59 | + recommended: false |
| 60 | + }, |
13 | 61 | schema: [] |
14 | 62 | }, |
15 | 63 |
|
16 | 64 | create: function (context) { |
17 | | - const MESSAGE = "Invalid assignment to `require`."; |
18 | | - |
19 | | - /** |
20 | | - * Determine if supplied `node` represents a `require` identifier |
21 | | - * @private |
22 | | - * @param {ASTNode} node - node to test |
23 | | - * @returns {Boolean} true if `require` identifier |
24 | | - */ |
25 | | - function isRequireIdentifier(node) { |
26 | | - return ast.isIdentifier(node) && node.name === "require"; |
27 | | - } |
28 | | - |
29 | | - /** |
30 | | - * Determine if supplied `node` represents a `window` identifier |
31 | | - * @private |
32 | | - * @param {ASTNode} node - node to test |
33 | | - * @returns {Boolean} true if `window` identifier |
34 | | - */ |
35 | | - function isWindowIdentifier(node) { |
36 | | - return ast.isIdentifier(node) && node.name === "window"; |
37 | | - } |
38 | | - |
39 | | - /** |
40 | | - * Determine if supplied `node` represents a `window.require` |
41 | | - * MemberExpression. |
42 | | - * @private |
43 | | - * @param {ASTNode} node - node to test |
44 | | - * @returns {Boolean} true if represents `window.require` expression |
45 | | - */ |
46 | | - function isWindowRequireExpr(node) { |
47 | | - return ast.isMemberExpr(node) && |
48 | | - isWindowIdentifier(node.object) && |
49 | | - isRequireIdentifier(node.property); |
50 | | - } |
51 | | - |
52 | 65 | return { |
53 | 66 | "AssignmentExpression": function (node) { |
54 | 67 | if (isRequireIdentifier(node.left) || isWindowRequireExpr(node.left)) { |
55 | | - context.report(node, MESSAGE); |
| 68 | + context.report(node, ERROR_MSG); |
56 | 69 | } |
57 | 70 | }, |
58 | 71 |
|
59 | 72 | "VariableDeclarator": function (node) { |
60 | 73 | if (isRequireIdentifier(node.id)) { |
61 | | - context.report(node, MESSAGE); |
| 74 | + context.report(node, ERROR_MSG); |
62 | 75 | } |
63 | 76 | } |
64 | 77 | }; |
|
0 commit comments