1 | /**
|
2 | * Requires function declarations by disallowing assignment of functions
|
3 | * expressions to variables. Function expressions are allowed in all other
|
4 | * contexts, including when passed as function arguments or immediately invoked.
|
5 | *
|
6 | * Assignment of function expressions to object members is also permitted, since
|
7 | * these can't be declared.
|
8 | *
|
9 | * Type: `Boolean`
|
10 | *
|
11 | * Value: `true`
|
12 | *
|
13 | * #### Example
|
14 | *
|
15 | * ```js
|
16 | * "requireFunctionDeclarations": true
|
17 | * ```
|
18 | *
|
19 | * ##### Valid
|
20 | *
|
21 | * ```js
|
22 | * function declared() {
|
23 | *
|
24 | * };
|
25 | *
|
26 | * (function iife() {
|
27 | * void 0;
|
28 | * })();
|
29 | *
|
30 | * var obj = {
|
31 | * a: function () {}
|
32 | * };
|
33 | *
|
34 | * obj.b = function () { };
|
35 | *
|
36 | * $('#foo').click(function bar() {
|
37 | *
|
38 | * };)
|
39 | * ```
|
40 | *
|
41 | * ##### Invalid
|
42 | *
|
43 | * ```js
|
44 | * var expressed = function() {
|
45 | *
|
46 | * };
|
47 | *
|
48 | * var expressed = function deeply() {
|
49 | *
|
50 | * };
|
51 | * ```
|
52 | */
|
53 |
|
54 | var assert = require('assert');
|
55 |
|
56 | module.exports = function() {};
|
57 |
|
58 | module.exports.prototype = {
|
59 | configure: function(options) {
|
60 | assert(
|
61 | options === true,
|
62 | this.getOptionName() + ' option requires a true value or should be removed'
|
63 | );
|
64 | },
|
65 |
|
66 | getOptionName: function() {
|
67 | return 'requireFunctionDeclarations';
|
68 | },
|
69 |
|
70 | check: function(file, errors) {
|
71 | file.iterateNodesByType(
|
72 | 'VariableDeclarator',
|
73 | function(node) {
|
74 | if (node.init && node.init.type === 'FunctionExpression') {
|
75 | errors.add('Use a function declaration instead', node);
|
76 | }
|
77 | }
|
78 | );
|
79 |
|
80 | file.iterateNodesByType(
|
81 | 'AssignmentExpression',
|
82 | function(node) {
|
83 | if (node.left.type !== 'MemberExpression' &&
|
84 | node.right.type === 'FunctionExpression') {
|
85 | errors.add('Use a function declaration instead', node);
|
86 | }
|
87 | }
|
88 | );
|
89 | }
|
90 | };
|