1 | /**
|
2 | * @fileoverview Rule to warn when a function expression does not have a name.
|
3 | * @author Kyle T. Nunery
|
4 | * @copyright 2015 Brandon Mills. All rights reserved.
|
5 | * @copyright 2014 Kyle T. Nunery. All rights reserved.
|
6 | */
|
7 |
|
8 | ;
|
9 |
|
10 | //------------------------------------------------------------------------------
|
11 | // Rule Definition
|
12 | //------------------------------------------------------------------------------
|
13 |
|
14 | module.exports = function(context) {
|
15 |
|
16 | /**
|
17 | * Determines whether the current FunctionExpression node is a get, set, or
|
18 | * shorthand method in an object literal or a class.
|
19 | * @returns {boolean} True if the node is a get, set, or shorthand method.
|
20 | */
|
21 | function isObjectOrClassMethod() {
|
22 | var parent = context.getAncestors().pop();
|
23 |
|
24 | return (parent.type === "MethodDefinition" || (
|
25 | parent.type === "Property" && (
|
26 | parent.method ||
|
27 | parent.kind === "get" ||
|
28 | parent.kind === "set"
|
29 | )
|
30 | ));
|
31 | }
|
32 |
|
33 | return {
|
34 | "FunctionExpression": function(node) {
|
35 |
|
36 | var name = node.id && node.id.name;
|
37 |
|
38 | if (!name && !isObjectOrClassMethod()) {
|
39 | context.report(node, "Missing function expression name.");
|
40 | }
|
41 | }
|
42 | };
|
43 | };
|
44 |
|
45 | module.exports.schema = [];
|