Skip to content

Commit

Permalink
Update: Refactor no-dynamic require rule
Browse files Browse the repository at this point in the history
* Add meta docs
* Move `isStaticRequire` helper into rule
  • Loading branch information
Casey Visco committed Aug 19, 2016
1 parent 9c401ba commit 7426782
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
40 changes: 34 additions & 6 deletions lib/rules/no-dynamic-require.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
/**
* @fileoverview Disallow use of dynamically generated paths in a `require` call
* @fileoverview Rule to disallow dynamically generated paths in `require` call
* @author Casey Visco
*/

"use strict";

const util = require("../util");
const ast = require("../utils/ast");

const isRequireCall = util.isRequireCall;
const isStringLiteral = ast.isStringLiteral;
const isStringLiteralArray = ast.isStringLiteralArray;

// -----------------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------------

/**
* Determine if supplied `node` represents either a String literal or an Array
* of String literals, indicating a static list of dependencies.
* @private
* @param {ASTNode} node - node to test
* @returns {Boolean} true if node represents static dependencies
*/
function isStatic(node) {
return isStringLiteral(node) || isStringLiteralArray(node);
}

// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------

const ERROR_MSG = "Dynamic `require` calls are not allowed.";

module.exports = {
meta: {
docs: {},
docs: {
description: "Disallow use of dynamically generated paths in a require call",
category: "Stylistic Choices",
recommended: false
},
schema: []
},

create: function (context) {
const MESSAGE = "Dynamic `require` calls are not allowed.";

return {
"CallExpression": function (node) {
if (util.isRequireCall(node) && !util.isStaticRequire(node)) {
context.report(node, MESSAGE);
if (isRequireCall(node) && !isStatic(node.arguments[0])) {
context.report(node, ERROR_MSG);
}
}
};
Expand Down
56 changes: 15 additions & 41 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const isStringLiteral = ast.isStringLiteral;
const isArrayExpr = ast.isArrayExpr;
const isObjectExpr = ast.isObjectExpr;
const isFunctionExpr = ast.isFunctionExpr;
const isStringLiteralArray = ast.isStringLiteralArray;

const COMMON_JS_PARAMS = ["require", "exports", "module"];

Expand Down Expand Up @@ -217,26 +216,6 @@ function isRequireCall(node) {
isRequireIdentifier(node.callee);
}

/**
* Determine if supplied `node` represents a `require` call with statically
* defined dependencies. That is, only string literals.
*
* The single-argument form of `require` is used inside Simplified CommonJS
* Wrapper definitions. The multiple-argument form of `require` can be used
* anywhere, and will contain at least a dependency array and a callback
* function, but may also contain an additional "errback" function as well.
* Here, we only need to check the first argument to determine if the dependency
* list is static.
*
* @public
* @param {ASTNode} node - CallExpression node to test
* @returns {Boolean} true if represents static `require` call
*/
function isStaticRequire(node) {
const args = node.arguments;
return isStringLiteral(args[0]) || isArrayExpr(args[0]) && isStringLiteralArray(args[0]);
}

/**
* Determine if supplied `node` represents a valid `require` format.
*
Expand Down Expand Up @@ -351,26 +330,21 @@ function getAmdCallback(node) {
//------------------------------------------------------------------------------

module.exports = {

// `define` related predicates
isDefineCall: isDefineCall,
isAmdDefine: isAmdDefine,
isObjectDefine: isObjectDefine,
isFunctionDefine: isFunctionDefine,
isCommonJsWrapper: isCommonJsWrapper,
isNamedDefine: isNamedDefine,
isInsideModuleDef: isInsideModuleDef,
isValidDefine: isValidDefine,

// `require` related predicates
isRequireIdentifier: isRequireIdentifier,
isRequireCall: isRequireCall,
isStaticRequire: isStaticRequire,
isValidRequire: isValidRequire,
isAmdRequire: isAmdRequire,
isDefineCall,
isAmdDefine,
isObjectDefine,
isFunctionDefine,
isCommonJsWrapper,
isNamedDefine,
isInsideModuleDef,
isValidDefine,
isRequireIdentifier,
isRequireCall,
isValidRequire,
isAmdRequire,

// general utilities
getDependencyNodes: getDependencyNodes,
getDependencyStringNodes: getDependencyStringNodes,
getAmdCallback: getAmdCallback
getDependencyNodes,
getDependencyStringNodes,
getAmdCallback
};

0 comments on commit 7426782

Please sign in to comment.