1 | /**
|
2 | * Requires that a function expression be named.
|
3 | * Named functions provide more information in the error stack trace than anonymous functions.
|
4 | *
|
5 | * This option does not help if you use Arrow functions (ES6) which are always anonymous.
|
6 | *
|
7 | * Type: `Boolean`
|
8 | *
|
9 | * Value: `true`
|
10 | *
|
11 | * #### Example
|
12 | *
|
13 | * ```js
|
14 | * "disallowAnonymousFunctions": true
|
15 | * ```
|
16 | *
|
17 | * ##### Valid
|
18 | *
|
19 | * ```js
|
20 | * var a = function foo(){
|
21 | *
|
22 | * };
|
23 | *
|
24 | * $('#foo').click(function bar(){
|
25 | *
|
26 | * });
|
27 | * ```
|
28 | *
|
29 | * ##### Invalid
|
30 | *
|
31 | * ```js
|
32 | * var a = function(){
|
33 | *
|
34 | * };
|
35 | *
|
36 | * $('#foo').click(function(){
|
37 | *
|
38 | * });
|
39 | * ```
|
40 | */
|
41 |
|
42 | var assert = require('assert');
|
43 |
|
44 | module.exports = function() {};
|
45 |
|
46 | module.exports.prototype = {
|
47 | configure: function(options) {
|
48 | assert(
|
49 | options === true,
|
50 | this.getOptionName() + ' option requires a true value or should be removed'
|
51 | );
|
52 | },
|
53 |
|
54 | getOptionName: function() {
|
55 | return 'disallowAnonymousFunctions';
|
56 | },
|
57 |
|
58 | check: function(file, errors) {
|
59 | file.iterateNodesByType(['FunctionExpression', 'FunctionDeclaration'], function(node) {
|
60 | if (node.id === null) {
|
61 | errors.add('Anonymous functions need to be named', node);
|
62 | }
|
63 | });
|
64 | }
|
65 | };
|