1 | /**
|
2 | * @fileoverview Rule to warn when a function expression does not have a name.
|
3 | * @author Kyle T. Nunery
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //------------------------------------------------------------------------------
|
9 | // Requirements
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | const astUtils = require("../ast-utils");
|
13 |
|
14 | /**
|
15 | * Checks whether or not a given variable is a function name.
|
16 | * @param {eslint-scope.Variable} variable - A variable to check.
|
17 | * @returns {boolean} `true` if the variable is a function name.
|
18 | */
|
19 | function isFunctionName(variable) {
|
20 | return variable && variable.defs[0].type === "FunctionName";
|
21 | }
|
22 |
|
23 | //------------------------------------------------------------------------------
|
24 | // Rule Definition
|
25 | //------------------------------------------------------------------------------
|
26 |
|
27 | module.exports = {
|
28 | meta: {
|
29 | docs: {
|
30 | description: "require or disallow named `function` expressions",
|
31 | category: "Stylistic Issues",
|
32 | recommended: false,
|
33 | url: "https://eslint.org/docs/rules/func-names"
|
34 | },
|
35 |
|
36 | schema: [
|
37 | {
|
38 | enum: ["always", "as-needed", "never"]
|
39 | }
|
40 | ]
|
41 | },
|
42 |
|
43 | create(context) {
|
44 | const never = context.options[0] === "never";
|
45 | const asNeeded = context.options[0] === "as-needed";
|
46 |
|
47 | /**
|
48 | * Determines whether the current FunctionExpression node is a get, set, or
|
49 | * shorthand method in an object literal or a class.
|
50 | * @param {ASTNode} node - A node to check.
|
51 | * @returns {boolean} True if the node is a get, set, or shorthand method.
|
52 | */
|
53 | function isObjectOrClassMethod(node) {
|
54 | const parent = node.parent;
|
55 |
|
56 | return (parent.type === "MethodDefinition" || (
|
57 | parent.type === "Property" && (
|
58 | parent.method ||
|
59 | parent.kind === "get" ||
|
60 | parent.kind === "set"
|
61 | )
|
62 | ));
|
63 | }
|
64 |
|
65 | /**
|
66 | * Determines whether the current FunctionExpression node has a name that would be
|
67 | * inferred from context in a conforming ES6 environment.
|
68 | * @param {ASTNode} node - A node to check.
|
69 | * @returns {boolean} True if the node would have a name assigned automatically.
|
70 | */
|
71 | function hasInferredName(node) {
|
72 | const parent = node.parent;
|
73 |
|
74 | return isObjectOrClassMethod(node) ||
|
75 | (parent.type === "VariableDeclarator" && parent.id.type === "Identifier" && parent.init === node) ||
|
76 | (parent.type === "Property" && parent.value === node) ||
|
77 | (parent.type === "AssignmentExpression" && parent.left.type === "Identifier" && parent.right === node) ||
|
78 | (parent.type === "ExportDefaultDeclaration" && parent.declaration === node) ||
|
79 | (parent.type === "AssignmentPattern" && parent.right === node);
|
80 | }
|
81 |
|
82 | return {
|
83 | "FunctionExpression:exit"(node) {
|
84 |
|
85 | // Skip recursive functions.
|
86 | const nameVar = context.getDeclaredVariables(node)[0];
|
87 |
|
88 | if (isFunctionName(nameVar) && nameVar.references.length > 0) {
|
89 | return;
|
90 | }
|
91 |
|
92 | const hasName = Boolean(node.id && node.id.name);
|
93 | const name = astUtils.getFunctionNameWithKind(node);
|
94 |
|
95 | if (never) {
|
96 | if (hasName) {
|
97 | context.report({
|
98 | node,
|
99 | message: "Unexpected named {{name}}.",
|
100 | data: { name }
|
101 | });
|
102 | }
|
103 | } else {
|
104 | if (!hasName && (asNeeded ? !hasInferredName(node) : !isObjectOrClassMethod(node))) {
|
105 | context.report({
|
106 | node,
|
107 | message: "Unexpected unnamed {{name}}.",
|
108 | data: { name }
|
109 | });
|
110 | }
|
111 | }
|
112 | }
|
113 | };
|
114 | }
|
115 | };
|