Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: IIFE and arrow functions in no-invalid-this (fixes #9126) #9258

Merged
merged 1 commit into from Sep 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/ast-utils.js
Expand Up @@ -626,6 +626,9 @@ module.exports = {
// // setup...
// return function foo() { ... };
// })();
// obj.foo = (() =>
// function foo() { ... }
// )();
case "ReturnStatement": {
const func = getUpperFunction(parent);

Expand All @@ -635,6 +638,12 @@ module.exports = {
node = func.parent;
break;
}
case "ArrowFunctionExpression":
if (node !== parent.body || !isCallee(parent)) {
return true;
}
node = parent.parent;
break;

// e.g.
// var obj = { foo() { ... } };
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/no-invalid-this.js
Expand Up @@ -308,6 +308,26 @@ const patterns = [
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
},
{
code: "obj.foo = (() => function() { console.log(this); z(x => console.log(x, this)); })();",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
invalid: []
},
{
code: "obj.foo = (function() { return () => { console.log(this); z(x => console.log(x, this)); }; })();",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
errors
},
{
code: "obj.foo = (() => () => { console.log(this); z(x => console.log(x, this)); })();",
parserOptions: { ecmaVersion: 6 },
valid: [NORMAL],
invalid: [USE_STRICT, IMPLIED_STRICT, MODULES],
errors
},

// Class Instance Methods.
{
Expand Down