From 4389cf310531559b1615c69ac3bb63db9cebba28 Mon Sep 17 00:00:00 2001 From: Aleksey Kolesnikov Date: Sat, 22 May 2021 13:12:27 +0700 Subject: [PATCH] Revert "Revert "feat!: implement https://github.com/facebook/react/pull/21104 close #4 (#5)", close #2" This reverts commit 945849368bdc9e0ad85e71ed3123225d2a91256a. --- tests/hooks-support-IIFE.js | 6 ++++++ typescript/__snapshots__/hooks-support-IIFE.js | 10 ++++++++++ typescript/src/core.ts | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 tests/hooks-support-IIFE.js create mode 100644 typescript/__snapshots__/hooks-support-IIFE.js diff --git a/tests/hooks-support-IIFE.js b/tests/hooks-support-IIFE.js new file mode 100644 index 0000000..52a8df3 --- /dev/null +++ b/tests/hooks-support-IIFE.js @@ -0,0 +1,6 @@ +// ? works with IIFE +while (item) { + ;((item) => { + useFoo() + })(item) +} diff --git a/typescript/__snapshots__/hooks-support-IIFE.js b/typescript/__snapshots__/hooks-support-IIFE.js new file mode 100644 index 0000000..7d1277c --- /dev/null +++ b/typescript/__snapshots__/hooks-support-IIFE.js @@ -0,0 +1,10 @@ +var _a; +_a = $RefreshSig$(); +// ? works with IIFE +while (item) { + ; + (_a((item) => { + _a(); + useFoo(); + }, "useFoo{}", true))(item); +} diff --git a/typescript/src/core.ts b/typescript/src/core.ts index c0c16b9..64d6e5c 100644 --- a/typescript/src/core.ts +++ b/typescript/src/core.ts @@ -305,6 +305,7 @@ export default function (opts: Options = {}): TransformerFactory { node = newFunction // if it is an inner decl, we can update it safely if (findAncestor(oldNode.parent, ts.isFunctionLike)) node = wrapped + else if (isIIFEFunction(oldNode)) return wrapped } } return updateStatements(node, addSignatureReport) @@ -646,6 +647,12 @@ export default function (opts: Options = {}): TransformerFactory { if (['createElement', 'jsx', 'jsxs', 'jsxDEV'].includes(f)) return true return false } + function isIIFEFunction(f: HandledFunction): boolean { + let node: Node = f + while (ts.isParenthesizedExpression(node.parent)) node = node.parent + if (ts.isCallExpression(node.parent) && node.parent.expression === node) return true + return false + } } function startsWithLowerCase(str: string) {