1 | /**
|
2 | * Requires parentheses around immediately invoked function expressions.
|
3 | *
|
4 | * Type: `Boolean`
|
5 | *
|
6 | * Value: `true`
|
7 | *
|
8 | * JSHint: [`immed`](http://www.jshint.com/docs/options/#immed)
|
9 | *
|
10 | * #### Example
|
11 | *
|
12 | * ```js
|
13 | * "requireParenthesesAroundIIFE": true
|
14 | * ```
|
15 | *
|
16 | * ##### Valid
|
17 | *
|
18 | * ```js
|
19 | * var a = (function(){ return 1; })();
|
20 | * var b = (function(){ return 2; }());
|
21 | * var c = (function(){ return 3; }).call(this, arg1);
|
22 | * var d = (function(){ return 3; }.call(this, arg1));
|
23 | * var e = (function(){ return d; }).apply(this, args);
|
24 | * var f = (function(){ return d; }.apply(this, args));
|
25 | * ```
|
26 | *
|
27 | * ##### Invalid
|
28 | *
|
29 | * ```js
|
30 | * var a = function(){ return 1; }();
|
31 | * var c = function(){ return 3; }.call(this, arg1);
|
32 | * var d = function(){ return d; }.apply(this, args);
|
33 | * ```
|
34 | */
|
35 |
|
36 | var assert = require('assert');
|
37 | var utils = require('../utils');
|
38 |
|
39 | module.exports = function() {};
|
40 |
|
41 | module.exports.prototype = {
|
42 |
|
43 | configure: function(options) {
|
44 | assert(
|
45 | options === true,
|
46 | this.getOptionName() + ' option requires a true value or should be removed'
|
47 | );
|
48 | },
|
49 |
|
50 | getOptionName: function() {
|
51 | return 'requireParenthesesAroundIIFE';
|
52 | },
|
53 |
|
54 | check: function(file, errors) {
|
55 |
|
56 | function isWrapped(node) {
|
57 | var openParensToken = file.getPrevToken(file.getFirstNodeToken(node));
|
58 |
|
59 | var closingParensToken = file.getNextToken(file.getLastNodeToken(node));
|
60 | var closingTokenValue = closingParensToken ? closingParensToken.value : '';
|
61 |
|
62 | return openParensToken.value + closingTokenValue === '()';
|
63 | }
|
64 |
|
65 | file.iterateNodesByType('CallExpression', function(node) {
|
66 | var inner = utils.getFunctionNodeFromIIFE(node);
|
67 |
|
68 | if (inner && !isWrapped(inner) && !isWrapped(node)) {
|
69 | errors.add(
|
70 | 'Wrap immediately invoked function expressions in parentheses',
|
71 | node
|
72 | );
|
73 |
|
74 | }
|
75 | });
|
76 | }
|
77 |
|
78 | };
|