question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

outerIIFEBody does not handle all IIFE cases

See original GitHub issue

What version of ESLint are you using? 3.0.1

What parser (default, Babel-ESLint, etc.) are you using? espree

Please show your full configuration:

module.exports = {
    "env": {
        "browser": true,
        "amd": true
    },
    "extends": "eslint:recommended",
    "rules": {
        "indent": [
            "error",
            "tab",
            { "outerIIFEBody": 0 }
        ],
        "linebreak-style": [
            "error",
            "windows"
        ],
        "semi": [
            "error",
            "always"
        ],
        "no-unused-vars": [
            "error",
            { "vars": "local", "args": "after-used" }
        ],
        "no-empty": [
            "error",
            { "allowEmptyCatch": true }
        ]
    },
    "globals": {
        "dojo": false,
        "ActiveXObject": false
    }
};

What did you do? Please include the actual source code causing the issue.

window.ERMS.FloatingDialog = (function(evts) {
"use strict";
var my;
my.foo = function() {
    return evts;
};

return my;
})(window.ERMS.Events);

What did you expect to happen? No errors

What actually happened? Please include the actual, raw output from ESLint.

  2:1  error  Expected indentation of 1 tab character but found 0  indent
  3:1  error  Expected indentation of 1 tab character but found 0  indent
  4:1  error  Expected indentation of 1 tab character but found 0  indent
  8:1  error  Expected indentation of 1 tab character but found 0  indent

I believe this case would be covered if the false negatives mentioned by @wojdyr in https://github.com/eslint/eslint/issues/6585#issuecomment-230183366 were handled.

Pasting those examples here for convenience:

(function () {
var i;  // ok
})();

~function () {
var i;  // not
}();

!(function () {
var i;  // not
})();

!function () {
var i;  // not
}();

;(function () {
var i;  // ok
})();

var MyClass = (function () {
var i;  // not
})();

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
nzakascommented, Jul 7, 2016

@TheCycoONE we never intended to support assignment as in your example, and I’m not entirely sure it makes sense to do so because the IIFE isn’t really “outer” at that point. I can see an argument for supporting unary operators before the IIFE, but I’m not sure about assignment.

1reaction
nzakascommented, Jul 8, 2016

The original purpose for this option was to support the jQuery style guide: https://contribute.jquery.org/style-guide/js/#full-file-closures

And, once I looked back at that, it seems that they do support assignments so we should as well.

I’ll champion this change.

@TheCycoONE are you willing to implement this once accepted?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Essential JavaScript: Mastering Immediately-invoked Function ...
All the above patterns are useful when we are not interested in the return value from the IIFE. But then what if you...
Read more >
enforce consistent indentation (indent-legacy) - ESLint
"outerIIFEBody" (default: 1) enforces indentation level for file-level IIFEs. ... Indent of 2 spaces with SwitchCase set to 0 will not indent case...
Read more >
Why use the `+` (plus) or `!` (logical not) operator in an IIFE?
Function declarations declare a (hoisted) local variable in the scope of the current function and assign the function to that variable. Function ...
Read more >
Immediately Invoked Function Expressions (IIFE) in JavaScript
IIFEs have their own scope i.e. the variables you declare in the Function Expression will not be available outside the function.
Read more >
Immediately Invoked Function Expression - IIFE
As you know that a function in JavaScript creates the local scope. So, you can define variables and function inside a function which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found