github-pages-deploy-action/node_modules/eslint/lib/rules/func-names.js

191 lines
6.2 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Rule to warn when a function expression does not have a name.
* @author Kyle T. Nunery
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
/**
* Checks whether or not a given variable is a function name.
2020-03-31 20:40:00 +08:00
* @param {eslint-scope.Variable} variable A variable to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} `true` if the variable is a function name.
*/
function isFunctionName(variable) {
return variable && variable.defs[0].type === "FunctionName";
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "require or disallow named `function` expressions",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/func-names"
},
schema: {
definitions: {
value: {
enum: [
"always",
"as-needed",
"never"
]
}
},
items: [
{
$ref: "#/definitions/value"
},
{
type: "object",
properties: {
generators: {
$ref: "#/definitions/value"
}
},
additionalProperties: false
}
]
},
messages: {
unnamed: "Unexpected unnamed {{name}}.",
named: "Unexpected named {{name}}."
}
},
create(context) {
2020-03-31 20:40:00 +08:00
const sourceCode = context.getSourceCode();
2020-03-07 11:45:40 +08:00
/**
* Returns the config option for the given node.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to get the config for.
2020-03-07 11:45:40 +08:00
* @returns {string} The config option.
*/
function getConfigForNode(node) {
if (
node.generator &&
context.options.length > 1 &&
context.options[1].generators
) {
return context.options[1].generators;
}
return context.options[0] || "always";
}
/**
* Determines whether the current FunctionExpression node is a get, set, or
* shorthand method in an object literal or a class.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} True if the node is a get, set, or shorthand method.
*/
function isObjectOrClassMethod(node) {
const parent = node.parent;
return (parent.type === "MethodDefinition" || (
parent.type === "Property" && (
parent.method ||
parent.kind === "get" ||
parent.kind === "set"
)
));
}
/**
* Determines whether the current FunctionExpression node has a name that would be
* inferred from context in a conforming ES6 environment.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} True if the node would have a name assigned automatically.
*/
function hasInferredName(node) {
const parent = node.parent;
return isObjectOrClassMethod(node) ||
(parent.type === "VariableDeclarator" && parent.id.type === "Identifier" && parent.init === node) ||
(parent.type === "Property" && parent.value === node) ||
(parent.type === "AssignmentExpression" && parent.left.type === "Identifier" && parent.right === node) ||
2020-05-15 05:33:08 +08:00
(parent.type === "AssignmentPattern" && parent.left.type === "Identifier" && parent.right === node);
2020-03-07 11:45:40 +08:00
}
/**
* Reports that an unnamed function should be named
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node The node to report in the event of an error.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function reportUnexpectedUnnamedFunction(node) {
context.report({
node,
messageId: "unnamed",
2020-03-31 20:40:00 +08:00
loc: astUtils.getFunctionHeadLoc(node, sourceCode),
2020-03-07 11:45:40 +08:00
data: { name: astUtils.getFunctionNameWithKind(node) }
});
}
/**
* Reports that a named function should be unnamed
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node The node to report in the event of an error.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function reportUnexpectedNamedFunction(node) {
context.report({
node,
messageId: "named",
2020-03-31 20:40:00 +08:00
loc: astUtils.getFunctionHeadLoc(node, sourceCode),
2020-03-07 11:45:40 +08:00
data: { name: astUtils.getFunctionNameWithKind(node) }
});
}
2020-05-15 05:33:08 +08:00
/**
* The listener for function nodes.
* @param {ASTNode} node function node
* @returns {void}
*/
function handleFunction(node) {
2020-03-07 11:45:40 +08:00
2020-05-15 05:33:08 +08:00
// Skip recursive functions.
const nameVar = context.getDeclaredVariables(node)[0];
2020-03-07 11:45:40 +08:00
2020-05-15 05:33:08 +08:00
if (isFunctionName(nameVar) && nameVar.references.length > 0) {
return;
}
2020-03-07 11:45:40 +08:00
2020-05-15 05:33:08 +08:00
const hasName = Boolean(node.id && node.id.name);
const config = getConfigForNode(node);
if (config === "never") {
if (hasName && node.type !== "FunctionDeclaration") {
reportUnexpectedNamedFunction(node);
}
} else if (config === "as-needed") {
if (!hasName && !hasInferredName(node)) {
reportUnexpectedUnnamedFunction(node);
}
} else {
if (!hasName && !isObjectOrClassMethod(node)) {
reportUnexpectedUnnamedFunction(node);
2020-03-07 11:45:40 +08:00
}
}
2020-05-15 05:33:08 +08:00
}
return {
"FunctionExpression:exit": handleFunction,
"ExportDefaultDeclaration > FunctionDeclaration": handleFunction
2020-03-07 11:45:40 +08:00
};
}
};