Skip to content

Commit

Permalink
feat!: implement facebook/react#21104 close #4 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works authored Apr 10, 2021
1 parent 2f2a228 commit be48fbe
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions tests/hooks-support-IIFE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ? works with IIFE
while (item) {
;((item) => {
useFoo()
})(item)
}
2 changes: 2 additions & 0 deletions typescript/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# react-refresh-typescript

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].*`.

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.

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.
Expand Down
10 changes: 10 additions & 0 deletions typescript/__snapshots__/hooks-support-IIFE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var _a;
_a = $RefreshSig$();
// ? works with IIFE
while (item) {
;
(_a((item) => {
_a();
useFoo();
}, "useFoo{}", true))(item);
}
5 changes: 3 additions & 2 deletions typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-refresh-typescript",
"version": "1.1.1",
"version": "2.0.0",
"description": "React Refresh transformer for TypeScript",
"scripts": {
"test": "jest",
Expand All @@ -24,7 +24,8 @@
},
"homepage": "https://github.com/Jack-Works/react-refresh-transformer#readme",
"peerDependencies": {
"typescript": "^4"
"typescript": "^4",
"react-refresh": "0.10.x"
},
"devDependencies": {},
"@pika/pack": {
Expand Down
7 changes: 7 additions & 0 deletions typescript/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ export default function (opts: Options = {}): TransformerFactory<SourceFile> {
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)
Expand Down Expand Up @@ -646,6 +647,12 @@ export default function (opts: Options = {}): TransformerFactory<SourceFile> {
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) {
Expand Down

0 comments on commit be48fbe

Please sign in to comment.