Skip to content

Commit 7426782

Browse files
author
Casey Visco
committed
Update: Refactor no-dynamic require rule
* Add meta docs * Move `isStaticRequire` helper into rule
1 parent 9c401ba commit 7426782

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

lib/rules/no-dynamic-require.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
11
/**
2-
* @fileoverview Disallow use of dynamically generated paths in a `require` call
2+
* @fileoverview Rule to disallow dynamically generated paths in `require` call
33
* @author Casey Visco
44
*/
55

66
"use strict";
77

88
const util = require("../util");
9+
const ast = require("../utils/ast");
10+
11+
const isRequireCall = util.isRequireCall;
12+
const isStringLiteral = ast.isStringLiteral;
13+
const isStringLiteralArray = ast.isStringLiteralArray;
14+
15+
// -----------------------------------------------------------------------------
16+
// Helpers
17+
// -----------------------------------------------------------------------------
18+
19+
/**
20+
* Determine if supplied `node` represents either a String literal or an Array
21+
* of String literals, indicating a static list of dependencies.
22+
* @private
23+
* @param {ASTNode} node - node to test
24+
* @returns {Boolean} true if node represents static dependencies
25+
*/
26+
function isStatic(node) {
27+
return isStringLiteral(node) || isStringLiteralArray(node);
28+
}
29+
30+
// -----------------------------------------------------------------------------
31+
// Rule Definition
32+
// -----------------------------------------------------------------------------
33+
34+
const ERROR_MSG = "Dynamic `require` calls are not allowed.";
935

1036
module.exports = {
1137
meta: {
12-
docs: {},
38+
docs: {
39+
description: "Disallow use of dynamically generated paths in a require call",
40+
category: "Stylistic Choices",
41+
recommended: false
42+
},
1343
schema: []
1444
},
1545

1646
create: function (context) {
17-
const MESSAGE = "Dynamic `require` calls are not allowed.";
18-
1947
return {
2048
"CallExpression": function (node) {
21-
if (util.isRequireCall(node) && !util.isStaticRequire(node)) {
22-
context.report(node, MESSAGE);
49+
if (isRequireCall(node) && !isStatic(node.arguments[0])) {
50+
context.report(node, ERROR_MSG);
2351
}
2452
}
2553
};

lib/util.js

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const isStringLiteral = ast.isStringLiteral;
1313
const isArrayExpr = ast.isArrayExpr;
1414
const isObjectExpr = ast.isObjectExpr;
1515
const isFunctionExpr = ast.isFunctionExpr;
16-
const isStringLiteralArray = ast.isStringLiteralArray;
1716

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

@@ -217,26 +216,6 @@ function isRequireCall(node) {
217216
isRequireIdentifier(node.callee);
218217
}
219218

220-
/**
221-
* Determine if supplied `node` represents a `require` call with statically
222-
* defined dependencies. That is, only string literals.
223-
*
224-
* The single-argument form of `require` is used inside Simplified CommonJS
225-
* Wrapper definitions. The multiple-argument form of `require` can be used
226-
* anywhere, and will contain at least a dependency array and a callback
227-
* function, but may also contain an additional "errback" function as well.
228-
* Here, we only need to check the first argument to determine if the dependency
229-
* list is static.
230-
*
231-
* @public
232-
* @param {ASTNode} node - CallExpression node to test
233-
* @returns {Boolean} true if represents static `require` call
234-
*/
235-
function isStaticRequire(node) {
236-
const args = node.arguments;
237-
return isStringLiteral(args[0]) || isArrayExpr(args[0]) && isStringLiteralArray(args[0]);
238-
}
239-
240219
/**
241220
* Determine if supplied `node` represents a valid `require` format.
242221
*
@@ -351,26 +330,21 @@ function getAmdCallback(node) {
351330
//------------------------------------------------------------------------------
352331

353332
module.exports = {
354-
355-
// `define` related predicates
356-
isDefineCall: isDefineCall,
357-
isAmdDefine: isAmdDefine,
358-
isObjectDefine: isObjectDefine,
359-
isFunctionDefine: isFunctionDefine,
360-
isCommonJsWrapper: isCommonJsWrapper,
361-
isNamedDefine: isNamedDefine,
362-
isInsideModuleDef: isInsideModuleDef,
363-
isValidDefine: isValidDefine,
364-
365-
// `require` related predicates
366-
isRequireIdentifier: isRequireIdentifier,
367-
isRequireCall: isRequireCall,
368-
isStaticRequire: isStaticRequire,
369-
isValidRequire: isValidRequire,
370-
isAmdRequire: isAmdRequire,
333+
isDefineCall,
334+
isAmdDefine,
335+
isObjectDefine,
336+
isFunctionDefine,
337+
isCommonJsWrapper,
338+
isNamedDefine,
339+
isInsideModuleDef,
340+
isValidDefine,
341+
isRequireIdentifier,
342+
isRequireCall,
343+
isValidRequire,
344+
isAmdRequire,
371345

372346
// general utilities
373-
getDependencyNodes: getDependencyNodes,
374-
getDependencyStringNodes: getDependencyStringNodes,
375-
getAmdCallback: getAmdCallback
347+
getDependencyNodes,
348+
getDependencyStringNodes,
349+
getAmdCallback
376350
};

0 commit comments

Comments
 (0)