]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-inner-declarations.js
2 * @fileoverview Rule to enforce declarations in program or function body root.
3 * @author Brandon Mills
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description
: "disallow variable or `function` declarations in nested blocks",
18 category
: "Possible Errors",
20 url
: "https://eslint.org/docs/rules/no-inner-declarations"
25 enum: ["functions", "both"]
30 moveDeclToRoot
: "Move {{type}} declaration to {{body}} root."
37 * Find the nearest Program or Function ancestor node.
38 * @returns {Object} Ancestor's type and distance from node.
40 function nearestBody() {
41 const ancestors
= context
.getAncestors();
42 let ancestor
= ancestors
.pop(),
45 while (ancestor
&& ["Program", "FunctionDeclaration",
46 "FunctionExpression", "ArrowFunctionExpression"
47 ].indexOf(ancestor
.type
) < 0) {
49 ancestor
= ancestors
.pop();
54 // Type of containing ancestor
57 // Separation between ancestor and node
63 * Ensure that a given node is at a program or function body's root.
64 * @param {ASTNode} node Declaration node to check.
67 function check(node
) {
68 const body
= nearestBody(),
69 valid
= ((body
.type
=== "Program" && body
.distance
=== 1) ||
75 messageId
: "moveDeclToRoot",
77 type
: (node
.type
=== "FunctionDeclaration" ? "function" : "variable"),
78 body
: (body
.type
=== "Program" ? "program" : "function body")
86 FunctionDeclaration
: check
,
87 VariableDeclaration(node
) {
88 if (context
.options
[0] === "both" && node
.kind
=== "var") {