Skip to content

Commit e554105

Browse files
committed
feat!: implement facebook/react#21104 close #4
1 parent 2f2a228 commit e554105

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

tests/hooks-support-IIFE.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ? works with IIFE
2+
while (item) {
3+
;((item) => {
4+
useFoo()
5+
})(item)
6+
}

typescript/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# react-refresh-typescript
22

3+
This package currently matches the upstream (babel version) at https://github.com/facebook/react/commit/516b76b9ae177ecc38cc64ba33ddfc7cfe5f5a99 which means it can be used with `[email protected].*`.
4+
35
This package implements the plugin to integrate Fast Refresh into bundlers. Fast Refresh is a feature that lets you edit React components in a running application without losing their state.
46

57
This package is primarily aimed at developers of bundler plugins. If you’re working on one, here is [a rough guide](https://github.com/facebook/react/issues/16604#issuecomment-528663101) for Fast Refresh integration using this package.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var _a;
2+
_a = $RefreshSig$();
3+
// ? works with IIFE
4+
while (item) {
5+
;
6+
(_a((item) => {
7+
_a();
8+
useFoo();
9+
}, "useFoo{}", true))(item);
10+
}

typescript/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-refresh-typescript",
3-
"version": "1.1.1",
3+
"version": "2.0.0",
44
"description": "React Refresh transformer for TypeScript",
55
"scripts": {
66
"test": "jest",
@@ -24,7 +24,8 @@
2424
},
2525
"homepage": "https://github.com/Jack-Works/react-refresh-transformer#readme",
2626
"peerDependencies": {
27-
"typescript": "^4"
27+
"typescript": "^4",
28+
"react-refresh": "0.10.x"
2829
},
2930
"devDependencies": {},
3031
"@pika/pack": {

typescript/src/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ export default function (opts: Options = {}): TransformerFactory<SourceFile> {
305305
node = newFunction
306306
// if it is an inner decl, we can update it safely
307307
if (findAncestor(oldNode.parent, ts.isFunctionLike)) node = wrapped
308+
else if (isIIFEFunction(oldNode)) return wrapped
308309
}
309310
}
310311
return updateStatements(node, addSignatureReport)
@@ -646,6 +647,12 @@ export default function (opts: Options = {}): TransformerFactory<SourceFile> {
646647
if (['createElement', 'jsx', 'jsxs', 'jsxDEV'].includes(f)) return true
647648
return false
648649
}
650+
function isIIFEFunction(f: HandledFunction): boolean {
651+
let node: Node = f
652+
while (ts.isParenthesizedExpression(node.parent)) node = node.parent
653+
if (ts.isCallExpression(node.parent) && node.parent.expression === node) return true
654+
return false
655+
}
649656
}
650657

651658
function startsWithLowerCase(str: string) {

0 commit comments

Comments
 (0)