global-require.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @fileoverview Rule for disallowing require() outside of the top-level module context
  3. * @author Jamund Ferguson
  4. */
  5. "use strict";
  6. const ACCEPTABLE_PARENTS = [
  7. "AssignmentExpression",
  8. "VariableDeclarator",
  9. "MemberExpression",
  10. "ExpressionStatement",
  11. "CallExpression",
  12. "ConditionalExpression",
  13. "Program",
  14. "VariableDeclaration",
  15. "ChainExpression"
  16. ];
  17. /**
  18. * Finds the eslint-scope reference in the given scope.
  19. * @param {Object} scope The scope to search.
  20. * @param {ASTNode} node The identifier node.
  21. * @returns {Reference|null} Returns the found reference or null if none were found.
  22. */
  23. function findReference(scope, node) {
  24. const references = scope.references.filter(reference => reference.identifier.range[0] === node.range[0] &&
  25. reference.identifier.range[1] === node.range[1]);
  26. /* istanbul ignore else: correctly returns null */
  27. if (references.length === 1) {
  28. return references[0];
  29. }
  30. return null;
  31. }
  32. /**
  33. * Checks if the given identifier node is shadowed in the given scope.
  34. * @param {Object} scope The current scope.
  35. * @param {ASTNode} node The identifier node to check.
  36. * @returns {boolean} Whether or not the name is shadowed.
  37. */
  38. function isShadowed(scope, node) {
  39. const reference = findReference(scope, node);
  40. return reference && reference.resolved && reference.resolved.defs.length > 0;
  41. }
  42. module.exports = {
  43. meta: {
  44. deprecated: true,
  45. replacedBy: [],
  46. type: "suggestion",
  47. docs: {
  48. description: "require `require()` calls to be placed at top-level module scope",
  49. category: "Node.js and CommonJS",
  50. recommended: false,
  51. url: "https://eslint.org/docs/rules/global-require"
  52. },
  53. schema: [],
  54. messages: {
  55. unexpected: "Unexpected require()."
  56. }
  57. },
  58. create(context) {
  59. return {
  60. CallExpression(node) {
  61. const currentScope = context.getScope();
  62. if (node.callee.name === "require" && !isShadowed(currentScope, node.callee)) {
  63. const isGoodRequire = context.getAncestors().every(parent => ACCEPTABLE_PARENTS.indexOf(parent.type) > -1);
  64. if (!isGoodRequire) {
  65. context.report({ node, messageId: "unexpected" });
  66. }
  67. }
  68. }
  69. };
  70. }
  71. };