node/tools/node_modules/eslint/lib/rules/no-inner-declarations.js
Michaël Zasso 3dc3063275 tools: move eslint from tools to tools/node_modules
This is required because we need to add the babel-eslint dependency
and it has to be able to resolve "eslint".
babel-eslint is required to support future ES features such as async
iterators and import.meta.

Refs: https://github.com/nodejs/node/pull/17755
PR-URL: https://github.com/nodejs/node/pull/17820
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2018-01-11 09:48:05 +01:00

90 lines
2.5 KiB
JavaScript

/**
* @fileoverview Rule to enforce declarations in program or function body root.
* @author Brandon Mills
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow variable or `function` declarations in nested blocks",
category: "Possible Errors",
recommended: true
},
schema: [
{
enum: ["functions", "both"]
}
]
},
create(context) {
/**
* Find the nearest Program or Function ancestor node.
* @returns {Object} Ancestor's type and distance from node.
*/
function nearestBody() {
const ancestors = context.getAncestors();
let ancestor = ancestors.pop(),
generation = 1;
while (ancestor && ["Program", "FunctionDeclaration",
"FunctionExpression", "ArrowFunctionExpression"
].indexOf(ancestor.type) < 0) {
generation += 1;
ancestor = ancestors.pop();
}
return {
// Type of containing ancestor
type: ancestor.type,
// Separation between ancestor and node
distance: generation
};
}
/**
* Ensure that a given node is at a program or function body's root.
* @param {ASTNode} node Declaration node to check.
* @returns {void}
*/
function check(node) {
const body = nearestBody(),
valid = ((body.type === "Program" && body.distance === 1) ||
body.distance === 2);
if (!valid) {
context.report({
node,
message: "Move {{type}} declaration to {{body}} root.",
data: {
type: (node.type === "FunctionDeclaration" ? "function" : "variable"),
body: (body.type === "Program" ? "program" : "function body")
}
});
}
}
return {
FunctionDeclaration: check,
VariableDeclaration(node) {
if (context.options[0] === "both" && node.kind === "var") {
check(node);
}
}
};
}
};