Skip to content

Commit

Permalink
Fix: IIFE and arrow functions in no-invalid-this (fixes #9126) (#9258)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and not-an-aardvark committed Sep 10, 2017
1 parent 7c95d5d commit 7685fed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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

0 comments on commit 7685fed

Please sign in to comment.