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 | },
|
34 |
|
35 | schema: [
|
36 | {
|
37 | enum: ["always", "as-needed", "never"]
|
38 | }
|
39 | ]
|
40 | },
|
41 |
|
42 | create(context) {
|
43 | const never = context.options[0] === "never";
|
44 | const asNeeded = context.options[0] === "as-needed";
|
45 |
|
46 | /**
|
47 | * Determines whether the current FunctionExpression node is a get, set, or
|
48 | * shorthand method in an object literal or a class.
|
49 | * @param {ASTNode} node - A node to check.
|
50 | * @returns {boolean} True if the node is a get, set, or shorthand method.
|
51 | */
|
52 | function isObjectOrClassMethod(node) {
|
53 | const parent = node.parent;
|
54 |
|
55 | return (parent.type === "MethodDefinition" || (
|
56 | parent.type === "Property" && (
|
57 | parent.method ||
|
58 | parent.kind === "get" ||
|
59 | parent.kind === "set"
|
60 | )
|
61 | ));
|
62 | }
|
63 |
|
64 | /**
|
65 | * Determines whether the current FunctionExpression node has a name that would be
|
66 | * inferred from context in a conforming ES6 environment.
|
67 | * @param {ASTNode} node - A node to check.
|
68 | * @returns {boolean} True if the node would have a name assigned automatically.
|
69 | */
|
70 | function hasInferredName(node) {
|
71 | const parent = node.parent;
|
72 |
|
73 | return isObjectOrClassMethod(node) ||
|
74 | (parent.type === "VariableDeclarator" && parent.id.type === "Identifier" && parent.init === node) ||
|
75 | (parent.type === "Property" && parent.value === node) ||
|
76 | (parent.type === "AssignmentExpression" && parent.left.type === "Identifier" && parent.right === node) ||
|
77 | (parent.type === "ExportDefaultDeclaration" && parent.declaration === node) ||
|
78 | (parent.type === "AssignmentPattern" && parent.right === node);
|
79 | }
|
80 |
|
81 | return {
|
82 | "FunctionExpression:exit"(node) {
|
83 |
|
84 | // Skip recursive functions.
|
85 | const nameVar = context.getDeclaredVariables(node)[0];
|
86 |
|
87 | if (isFunctionName(nameVar) && nameVar.references.length > 0) {
|
88 | return;
|
89 | }
|
90 |
|
91 | const hasName = Boolean(node.id && node.id.name);
|
92 | const name = astUtils.getFunctionNameWithKind(node);
|
93 |
|
94 | if (never) {
|
95 | if (hasName) {
|
96 | context.report({
|
97 | node,
|
98 | message: "Unexpected named {{name}}.",
|
99 | data: { name }
|
100 | });
|
101 | }
|
102 | } else {
|
103 | if (!hasName && (asNeeded ? !hasInferredName(node) : !isObjectOrClassMethod(node))) {
|
104 | context.report({
|
105 | node,
|
106 | message: "Unexpected unnamed {{name}}.",
|
107 | data: { name }
|
108 | });
|
109 | }
|
110 | }
|
111 | }
|
112 | };
|
113 | }
|
114 | };
|