From ac49494781d9210e41610b90ce124290a71e40b2 Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 17 Jun 2020 16:51:43 +0800 Subject: [PATCH 1/3] `consistent-function-scoping`: Fix wrong detect of IIFE --- rules/consistent-function-scoping.js | 3 ++- test/consistent-function-scoping.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/rules/consistent-function-scoping.js b/rules/consistent-function-scoping.js index 864a430888..1b050f0139 100644 --- a/rules/consistent-function-scoping.js +++ b/rules/consistent-function-scoping.js @@ -105,7 +105,8 @@ const iifeFunctionTypes = new Set([ const isIife = node => node && iifeFunctionTypes.has(node.type) && node.parent && - node.parent.type === 'CallExpression'; + node.parent.type === 'CallExpression' && + node.parent.callee === node; function checkNode(node, scopeManager) { const scope = scopeManager.acquire(node); diff --git a/test/consistent-function-scoping.js b/test/consistent-function-scoping.js index a72317339e..02690462c1 100644 --- a/test/consistent-function-scoping.js +++ b/test/consistent-function-scoping.js @@ -513,6 +513,18 @@ ruleTester.run('consistent-function-scoping', rule, { })(); `, errors: [createError({name: 'bar'})] + }, + // #770 + { + code: outdent` + process.nextTick(() => { + function returnsZero() { + return true; + } + process.exitCode = returnsZero(); + }); + `, + errors: [createError({name: 'returnsZero'})] } ] }); From 72534c673f2d020f03705ec829d8c222b3d2acb5 Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 17 Jun 2020 16:56:37 +0800 Subject: [PATCH 2/3] Add another case --- test/consistent-function-scoping.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test/consistent-function-scoping.js b/test/consistent-function-scoping.js index 02690462c1..88587693d7 100644 --- a/test/consistent-function-scoping.js +++ b/test/consistent-function-scoping.js @@ -224,7 +224,7 @@ ruleTester.run('consistent-function-scoping', rule, { function foo() {} }, []) `, - // IIEF + // IIFE outdent` (function() { function bar() {} @@ -525,6 +525,22 @@ ruleTester.run('consistent-function-scoping', rule, { }); `, errors: [createError({name: 'returnsZero'})] + }, + { + code: outdent` + process.nextTick(() => { + function returnsZero() { + return true; + } + process.exitCode = returnsZero(); + },(() => { + function returnsZero() { + return true; + } + process.exitCode = returnsZero(); + })()); + `, + errors: [createError({name: 'returnsZero'})] } ] }); From de07847f15bef8c8f788817d2ccb9fc8bf9faea7 Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 17 Jun 2020 17:00:17 +0800 Subject: [PATCH 3/3] More complicated case --- test/consistent-function-scoping.js | 39 +++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/test/consistent-function-scoping.js b/test/consistent-function-scoping.js index 88587693d7..376c2a6659 100644 --- a/test/consistent-function-scoping.js +++ b/test/consistent-function-scoping.js @@ -528,19 +528,36 @@ ruleTester.run('consistent-function-scoping', rule, { }, { code: outdent` - process.nextTick(() => { - function returnsZero() { - return true; - } - process.exitCode = returnsZero(); - },(() => { - function returnsZero() { - return true; + foo( + // This is not IIFE + function() { + function bar() { + } + }, + // This is IIFE + (function() { + function baz() { + } + })(), + ) + `, + errors: [createError({name: 'bar'})] + }, + { + code: outdent` + // This is IIFE + (function() { + function bar() { } - process.exitCode = returnsZero(); - })()); + })( + // This is not IIFE + function() { + function baz() { + } + }, + ) `, - errors: [createError({name: 'returnsZero'})] + errors: [createError({name: 'baz'})] } ] });