From b2135b667a763b7aedc9955d5da208869d48da88 Mon Sep 17 00:00:00 2001 From: Casey Visco Date: Sun, 14 Aug 2016 01:08:18 -0400 Subject: [PATCH] Update: Refactor no-assign-require rule --- lib/rules/no-assign-require.js | 93 +++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/lib/rules/no-assign-require.js b/lib/rules/no-assign-require.js index b19977c..1e5a4cb 100644 --- a/lib/rules/no-assign-require.js +++ b/lib/rules/no-assign-require.js @@ -1,64 +1,77 @@ /** - * @fileoverview Disallow assignment to `require` or `window.require` + * @fileoverview Rule to disallow assignment to `require` or `window.require` * @author Casey Visco */ "use strict"; -const ast = require("../utils/ast"); +const isIdentifier = require("../utils/ast").isIdentifier; +const isMemberExpr = require("../utils/ast").isMemberExpr; + +// ----------------------------------------------------------------------------- +// Helpers +// ----------------------------------------------------------------------------- + +/** + * Determine if supplied `node` represents a `require` identifier + * @private + * @param {ASTNode} node - node to test + * @returns {Boolean} true if `require` identifier + */ +function isRequireIdentifier(node) { + return isIdentifier(node) && node.name === "require"; +} + +/** + * Determine if supplied `node` represents a `window` identifier + * @private + * @param {ASTNode} node - node to test + * @returns {Boolean} true if `window` identifier + */ +function isWindowIdentifier(node) { + return isIdentifier(node) && node.name === "window"; +} + +/** + * Determine if supplied `node` represents a `window.require` + * MemberExpression. + * @private + * @param {ASTNode} node - node to test + * @returns {Boolean} true if represents `window.require` expression + */ +function isWindowRequireExpr(node) { + return isMemberExpr(node) && + isWindowIdentifier(node.object) && + isRequireIdentifier(node.property); +} + +// ----------------------------------------------------------------------------- +// Rule Definition +// ----------------------------------------------------------------------------- + +const ERROR_MSG = "Invalid assignment to `require`."; module.exports = { meta: { - docs: {}, + docs: { + description: "Disallow assignment to `require` or `window.require`", + category: "Stylistic Choices", + recommended: false + }, schema: [] }, create: function (context) { - const MESSAGE = "Invalid assignment to `require`."; - - /** - * Determine if supplied `node` represents a `require` identifier - * @private - * @param {ASTNode} node - node to test - * @returns {Boolean} true if `require` identifier - */ - function isRequireIdentifier(node) { - return ast.isIdentifier(node) && node.name === "require"; - } - - /** - * Determine if supplied `node` represents a `window` identifier - * @private - * @param {ASTNode} node - node to test - * @returns {Boolean} true if `window` identifier - */ - function isWindowIdentifier(node) { - return ast.isIdentifier(node) && node.name === "window"; - } - - /** - * Determine if supplied `node` represents a `window.require` - * MemberExpression. - * @private - * @param {ASTNode} node - node to test - * @returns {Boolean} true if represents `window.require` expression - */ - function isWindowRequireExpr(node) { - return ast.isMemberExpr(node) && - isWindowIdentifier(node.object) && - isRequireIdentifier(node.property); - } - return { "AssignmentExpression": function (node) { if (isRequireIdentifier(node.left) || isWindowRequireExpr(node.left)) { - context.report(node, MESSAGE); + context.report(node, ERROR_MSG); } }, "VariableDeclarator": function (node) { if (isRequireIdentifier(node.id)) { - context.report(node, MESSAGE); + context.report(node, ERROR_MSG); } } };