|
| 1 | +<!--- |
| 2 | +{ |
| 3 | + "titles": [ |
| 4 | + "immed" |
| 5 | + ], |
| 6 | + "slugs": [ |
| 7 | + "option-jshint-immed" |
| 8 | + ], |
| 9 | + "linters": [ |
| 10 | + "jshint" |
| 11 | + ], |
| 12 | + "author": "jallardice", |
| 13 | + "subject": "option" |
| 14 | +} |
| 15 | +--> |
| 16 | + |
| 17 | +### What does this option do? |
| 18 | + |
| 19 | +The JSHint `immed` option is used to enforce the wrapping of immediately invoked |
| 20 | +function expressions in a pair of parentheses. This prevents the less common use |
| 21 | +of other operators to force a function declaration to be parsed as a function |
| 22 | +expression and ensures functions that would already be parsed as an expression |
| 23 | +to be wrapped in parentheses anyway to make the intentions clearer to the |
| 24 | +reader. |
| 25 | + |
| 26 | +In the following example the function is parsed as an expression as it's part of |
| 27 | +a variable statement. It is not necessary to wrap the function in parentheses to |
| 28 | +be able to invoke it immediately: |
| 29 | + |
| 30 | +<!--- |
| 31 | +{ |
| 32 | + "linter": "jshint" |
| 33 | +} |
| 34 | +--> |
| 35 | +```javascript |
| 36 | +/*jshint immed: true */ |
| 37 | +var MyModule = function () { |
| 38 | + return { |
| 39 | + someData: 1 |
| 40 | + }; |
| 41 | +}(); |
| 42 | +``` |
| 43 | + |
| 44 | +Be aware that this option does not enforce the position of the wrapping |
| 45 | +parentheses. Some coding conventions specify that the closing wrapping |
| 46 | +parenthesis should appear before the invoking pair and others say it should come |
| 47 | +after. |
| 48 | + |
| 49 | +### When should I use this option? |
| 50 | + |
| 51 | +The use of the `immed` JSHint option will cause a "[Wrap an immediate function |
| 52 | +invocation in parentheses][parens]" error any time it encounters an immediately |
| 53 | +invoked function expression that is not wrapped in parentheses. It's common |
| 54 | +convention to place an IIFE within parentheses because it makes your intentions |
| 55 | +immediately clear to readers of your code. If you glance at the example above |
| 56 | +you would most likely assume that `x` refers to the function itself rather than |
| 57 | +the return value of it. By wrapping the function in parentheses you're less |
| 58 | +likely to make that incorrect assumption: |
| 59 | + |
| 60 | +<!--- |
| 61 | +{ |
| 62 | + "linter": "jshint" |
| 63 | +} |
| 64 | +--> |
| 65 | +```javascript |
| 66 | +/*jshint immed: true */ |
| 67 | +var MyModule = (function () { |
| 68 | + return { |
| 69 | + someData: 1 |
| 70 | + }; |
| 71 | +}()); |
| 72 | +``` |
| 73 | + |
| 74 | +Note that this is an *enforcing* option which means JSHint does not apply it by |
| 75 | +default. If you do not explicitly set this option to `true` JSHint will allow |
| 76 | +IIFEs without wrapping parentheses. |
| 77 | + |
| 78 | +#### Recommendation |
| 79 | + |
| 80 | +Set this option to `true` (enforces the wrapping of IIFEs). |
| 81 | + |
| 82 | +[jscs]: https://github.com/mdevils/node-jscs |
| 83 | +[parens]: /wrap-an-immediate-function-invocation-in-parentheses |
0 commit comments