|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
-
-
- "use strict";
-
-
-
-
-
- const astUtils = require("../util/ast-utils");
-
-
- function isFunctionName(variable) {
- return variable && variable.defs[0].type === "FunctionName";
- }
-
-
-
-
-
- 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) {
-
-
-
- function getConfigForNode(node) {
- if (
- node.generator &&
- context.options.length > 1 &&
- context.options[1].generators
- ) {
- return context.options[1].generators;
- }
-
- return context.options[0] || "always";
- }
-
-
-
- function isObjectOrClassMethod(node) {
- const parent = node.parent;
-
- return (parent.type === "MethodDefinition" || (
- parent.type === "Property" && (
- parent.method ||
- parent.kind === "get" ||
- parent.kind === "set"
- )
- ));
- }
-
-
-
- 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) ||
- (parent.type === "ExportDefaultDeclaration" && parent.declaration === node) ||
- (parent.type === "AssignmentPattern" && parent.right === node);
- }
-
-
-
- function reportUnexpectedUnnamedFunction(node) {
- context.report({
- node,
- messageId: "unnamed",
- data: { name: astUtils.getFunctionNameWithKind(node) }
- });
- }
-
-
-
- function reportUnexpectedNamedFunction(node) {
- context.report({
- node,
- messageId: "named",
- data: { name: astUtils.getFunctionNameWithKind(node) }
- });
- }
-
- return {
- "FunctionExpression:exit"(node) {
-
-
- const nameVar = context.getDeclaredVariables(node)[0];
-
- if (isFunctionName(nameVar) && nameVar.references.length > 0) {
- return;
- }
-
- const hasName = Boolean(node.id && node.id.name);
- const config = getConfigForNode(node);
-
- if (config === "never") {
- if (hasName) {
- reportUnexpectedNamedFunction(node);
- }
- } else if (config === "as-needed") {
- if (!hasName && !hasInferredName(node)) {
- reportUnexpectedUnnamedFunction(node);
- }
- } else {
- if (!hasName && !isObjectOrClassMethod(node)) {
- reportUnexpectedUnnamedFunction(node);
- }
- }
- }
- };
- }
- };
|