Skip to content

Commit

Permalink
Update: Refactor no-assign-require rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Casey Visco committed Aug 14, 2016
1 parent d36a064 commit b2135b6
Showing 1 changed file with 53 additions and 40 deletions.
93 changes: 53 additions & 40 deletions lib/rules/no-assign-require.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
Expand Down

0 comments on commit b2135b6

Please sign in to comment.