Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: implement https://github.com/facebook/react/pull/21104 close #4 #5

Merged
merged 1 commit into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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