no-inner-declarations.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @fileoverview Rule to enforce declarations in program or function body root.
  3. * @author Brandon Mills
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. const validParent = new Set([
  14. "Program",
  15. "StaticBlock",
  16. "ExportNamedDeclaration",
  17. "ExportDefaultDeclaration",
  18. ]);
  19. const validBlockStatementParent = new Set([
  20. "FunctionDeclaration",
  21. "FunctionExpression",
  22. "ArrowFunctionExpression",
  23. ]);
  24. /**
  25. * Finds the nearest enclosing context where this rule allows declarations and returns its description.
  26. * @param {ASTNode} node Node to search from.
  27. * @returns {string} Description. One of "program", "function body", "class static block body".
  28. */
  29. function getAllowedBodyDescription(node) {
  30. let { parent } = node;
  31. while (parent) {
  32. if (parent.type === "StaticBlock") {
  33. return "class static block body";
  34. }
  35. if (astUtils.isFunction(parent)) {
  36. return "function body";
  37. }
  38. ({ parent } = parent);
  39. }
  40. return "program";
  41. }
  42. /** @type {import('../types').Rule.RuleModule} */
  43. module.exports = {
  44. meta: {
  45. type: "problem",
  46. defaultOptions: ["functions", { blockScopedFunctions: "allow" }],
  47. docs: {
  48. description:
  49. "Disallow variable or `function` declarations in nested blocks",
  50. recommended: false,
  51. url: "https://eslint.org/docs/latest/rules/no-inner-declarations",
  52. },
  53. schema: [
  54. {
  55. enum: ["functions", "both"],
  56. },
  57. {
  58. type: "object",
  59. properties: {
  60. blockScopedFunctions: {
  61. enum: ["allow", "disallow"],
  62. },
  63. },
  64. additionalProperties: false,
  65. },
  66. ],
  67. messages: {
  68. moveDeclToRoot: "Move {{type}} declaration to {{body}} root.",
  69. },
  70. },
  71. create(context) {
  72. const both = context.options[0] === "both";
  73. const { blockScopedFunctions } = context.options[1];
  74. const sourceCode = context.sourceCode;
  75. const ecmaVersion = context.languageOptions.ecmaVersion;
  76. /**
  77. * Ensure that a given node is at a program or function body's root.
  78. * @param {ASTNode} node Declaration node to check.
  79. * @returns {void}
  80. */
  81. function check(node) {
  82. const parent = node.parent;
  83. if (
  84. parent.type === "BlockStatement" &&
  85. validBlockStatementParent.has(parent.parent.type)
  86. ) {
  87. return;
  88. }
  89. if (validParent.has(parent.type)) {
  90. return;
  91. }
  92. context.report({
  93. node,
  94. messageId: "moveDeclToRoot",
  95. data: {
  96. type:
  97. node.type === "FunctionDeclaration"
  98. ? "function"
  99. : "variable",
  100. body: getAllowedBodyDescription(node),
  101. },
  102. });
  103. }
  104. return {
  105. FunctionDeclaration(node) {
  106. const isInStrictCode = sourceCode.getScope(node).upper.isStrict;
  107. if (
  108. blockScopedFunctions === "allow" &&
  109. ecmaVersion >= 2015 &&
  110. isInStrictCode
  111. ) {
  112. return;
  113. }
  114. check(node);
  115. },
  116. VariableDeclaration(node) {
  117. if (both && node.kind === "var") {
  118. check(node);
  119. }
  120. },
  121. };
  122. },
  123. };