2 * @fileoverview Rule to enforce declarations in program or function body root.
3 * @author Brandon Mills
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const validParent
= new Set(["Program", "ExportNamedDeclaration", "ExportDefaultDeclaration"]);
19 const validBlockStatementParent
= new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]);
26 description
: "disallow variable or `function` declarations in nested blocks",
27 category
: "Possible Errors",
29 url
: "https://eslint.org/docs/rules/no-inner-declarations"
34 enum: ["functions", "both"]
39 moveDeclToRoot
: "Move {{type}} declaration to {{body}} root."
46 * Ensure that a given node is at a program or function body's root.
47 * @param {ASTNode} node Declaration node to check.
50 function check(node
) {
51 const parent
= node
.parent
;
54 parent
.type
=== "BlockStatement" && validBlockStatementParent
.has(parent
.parent
.type
)
59 if (validParent
.has(parent
.type
)) {
63 const upperFunction
= astUtils
.getUpperFunction(parent
);
67 messageId
: "moveDeclToRoot",
69 type
: (node
.type
=== "FunctionDeclaration" ? "function" : "variable"),
70 body
: (upperFunction
=== null ? "program" : "function body")
78 FunctionDeclaration
: check
,
79 VariableDeclaration(node
) {
80 if (context
.options
[0] === "both" && node
.kind
=== "var") {