no-inner-declarations.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(["Program", "StaticBlock", "ExportNamedDeclaration", "ExportDefaultDeclaration"]);
  14. const validBlockStatementParent = new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]);
  15. /**
  16. * Finds the nearest enclosing context where this rule allows declarations and returns its description.
  17. * @param {ASTNode} node Node to search from.
  18. * @returns {string} Description. One of "program", "function body", "class static block body".
  19. */
  20. function getAllowedBodyDescription(node) {
  21. let { parent } = node;
  22. while (parent) {
  23. if (parent.type === "StaticBlock") {
  24. return "class static block body";
  25. }
  26. if (astUtils.isFunction(parent)) {
  27. return "function body";
  28. }
  29. ({ parent } = parent);
  30. }
  31. return "program";
  32. }
  33. /** @type {import('../shared/types').Rule} */
  34. module.exports = {
  35. meta: {
  36. type: "problem",
  37. docs: {
  38. description: "Disallow variable or `function` declarations in nested blocks",
  39. recommended: true,
  40. url: "https://eslint.org/docs/rules/no-inner-declarations"
  41. },
  42. schema: [
  43. {
  44. enum: ["functions", "both"]
  45. }
  46. ],
  47. messages: {
  48. moveDeclToRoot: "Move {{type}} declaration to {{body}} root."
  49. }
  50. },
  51. create(context) {
  52. /**
  53. * Ensure that a given node is at a program or function body's root.
  54. * @param {ASTNode} node Declaration node to check.
  55. * @returns {void}
  56. */
  57. function check(node) {
  58. const parent = node.parent;
  59. if (
  60. parent.type === "BlockStatement" && validBlockStatementParent.has(parent.parent.type)
  61. ) {
  62. return;
  63. }
  64. if (validParent.has(parent.type)) {
  65. return;
  66. }
  67. context.report({
  68. node,
  69. messageId: "moveDeclToRoot",
  70. data: {
  71. type: (node.type === "FunctionDeclaration" ? "function" : "variable"),
  72. body: getAllowedBodyDescription(node)
  73. }
  74. });
  75. }
  76. return {
  77. FunctionDeclaration: check,
  78. VariableDeclaration(node) {
  79. if (context.options[0] === "both" && node.kind === "var") {
  80. check(node);
  81. }
  82. }
  83. };
  84. }
  85. };