Compare commits

..

474 Commits

Author SHA1 Message Date
Dan Abramov
8e99618afa Rm bad tests 2020-07-01 15:59:26 +01:00
Dan Abramov
d0e6cbc3b0 Run ReactBrowserEventEmitter test on bundles 2020-07-01 15:52:06 +01:00
Dan Abramov
f918b0eb41 Fix development mode hang when iframe is removed (#19220)
* Fix development mode hang when iframe is removed

* Also fix #16734
2020-07-01 15:33:29 +01:00
Dan Abramov
8bff8987e5 Don't bailout after Suspending in Legacy Mode (#19216)
* Add a failing test for legacy Suspense blocking context updates in memo

* Add more test case coverage for variations of #17356

* Don't bailout after Suspending in Legacy Mode

Co-authored-by: Tharuka Devendra <tsdevendra1@gmail.com>
2020-06-30 22:06:46 +01:00
Shailendra Gupta
f4097c1aef Added warning to <Context.Provider> in case no value prop is provided (#19054)
* added warning to context.provider in case no value prop

* update message

* updated message and pass undefined
2020-06-30 19:50:55 +01:00
Dan Abramov
47ff31a77a Revert "Add regression test for #18497 (#18538)" (#19215)
This reverts commit e9c1445ba0.
2020-06-30 13:38:31 +01:00
Vetrivel Chinnasamy
b621dab5d4 make link https (#19147) 2020-06-30 12:43:52 +01:00
jddxf
fa32cf299f Add regression tests where sync render causes later concurrent render to expire soon (#18608)
* Add a failing test for #17911

* Add more test cases where sync render causes later concurrent render to expire soon
2020-06-30 12:42:59 +01:00
Dan Abramov
1887c5d946 eslint-plugin-react-hooks@4.0.5 2020-06-30 12:16:49 +01:00
Fred Vollmer
b0533fe33c fix(eslint-plugin-react-hooks): Support optional chaining when accessing prototype method inside useCallback and useMemo #19061 (#19062)
* fix(eslint-plugin-react-hooks): Support optional chaining when accessing prototype method #19061

* run prettier

* Add fix for #19043
2020-06-30 12:14:00 +01:00
Sophie Alpert
e9c1445ba0 Add regression test for #18497 (#18538) 2020-06-30 12:09:05 +01:00
Brian Vaughn
9d9cf383c9 Fixed test script handling of unknown/additional args (#19209) 2020-06-29 15:36:45 -04:00
Dominic Gannaway
3c1a7ac87c Move TOP_BEFORE_BLUR to bubble phase (#19197) 2020-06-26 20:35:21 +01:00
Ricky
72bbcad160 Add new test cli (#19184)
* Add new test cli

* Remove --variant accidentally added to test-persist

* s/test/tests

* Updates from review

* Update package.json tests

* Missed a release channel in circle.yaml

* Update config.yml to use just run: with test commands

* Update release-channel options and add build dir checks

* Update test args to use the new release-channel options

* Fix error in circle config.yml

* Fix a wrong condition for the --variant check

* Fix a wrong condition for the --persistent check

* Prettier

* Require build check for devtool tests as well
2020-06-25 20:39:50 -04:00
Christoph Nakazawa
7d0e4150aa Fix react-runtime main field (#19193)
The "main" field in this package points to a non-existent file. This fixes it.
2020-06-25 12:17:42 -04:00
finico
ed94600fc6 Remove unnecessary tag end from CommitRanked view (#19195) 2020-06-25 12:10:47 -04:00
Chen Gang
ffe516f3bf use NoTimestamp instead of -1 (#19182) 2020-06-23 16:28:24 -04:00
Brian Vaughn
1425fcbb86 Re-enabled DebugTracing feature for old reconciler fork (#19161)
* Re-enabled DebugTracing feature for old reconciler fork

It was temporarily removed by @sebmarkbage via PR #18697. Newly re-added tracing is simplified, since the lane(s) data type does not require the (lossy) conversion between priority and expiration time values.

@sebmarkbage mentioned that he removed this because it might get in the way of his planned discrete/sync refactor. I'm not sure if that concern still applies, but just in case- I have only re-added it to the old reconciler fork for now.

* Force Code Sandbox CI to re-run
2020-06-23 12:16:23 -04:00
Brian Vaughn
0836f62a5b Updates the DevTools test script to make it easier to test other URLs. (#19179) 2020-06-23 11:48:35 -04:00
Brian Vaughn
1cfd332fd7 Shutdown DevTools Bridge synchronously when unmounting (#19180) 2020-06-23 11:48:04 -04:00
Jack Works
5b98656909 fix: use define property to overwrite console, close #19099 (#19123)
* fix: use define property to overwrite console, close #19099

* fix: lint error
2020-06-23 10:34:53 -04:00
Andrew Clark
6ba25b96df Bugfix: Legacy Mode + DevTools "force fallback" (#19164)
DevTools has a feature to force a Suspense boundary to show a fallback.
This feature causes us to skip the first render pass (where we render
the primary children) and go straight to rendering the fallback.

There's a Legacy Mode-only codepath that failed to take this scenario
into account, instead assuming that whenever a fallback is being
rendered, it was preceded by an attempt to render the primary children.

SuspenseList can also cause us to skip the first pass, but the relevant
branch is Legacy Mode-only, and SuspenseList is not supported in
Legacy Mode.

Fixes a test that I had temporarily disabled when upstreaming the Lanes
implementation in #19108.
2020-06-19 11:06:42 -07:00
Luna Ruan
d1d9054e09 Revert "Re-enabled DebugTracing feature for old reconciler fork (#19142)" (#19159)
This reverts commit cc7c1aece4.
2020-06-18 18:18:56 -07:00
YeonJuan
090c6ed751 [eslint-plugin-react-hooks]: handling sparse array when no-inline callback (#19145) 2020-06-17 12:29:02 -04:00
Brian Vaughn
cc7c1aece4 Re-enabled DebugTracing feature for old reconciler fork (#19142)
It was temporarily removed by @sebmarkbage via PR #18697. Newly re-added tracing is simplified, since the lane(s) data type does not require the (lossy) conversion between priority and expiration time values.

@sebmarkbage mentioned that he removed this because it might get in the way of his planned discrete/sync refactor. I'm not sure if that concern still applies, but just in case- I have only re-added it to the old reconciler fork for now.
2020-06-16 15:16:15 -04:00
阿林
d4fc2c1457 Fix spelling (#19084) 2020-06-15 20:56:56 -04:00
Ricky
30b47103d4 Fix spelling errors and typos (#19138) 2020-06-15 19:59:44 -04:00
Rick Hanlon
655affa302 Clarifications
Co-authored-by: shengxinjing <316783812@qq.com>
2020-06-12 21:09:29 -04:00
Andrew Clark
103ed08c46 Remove shouldDeprioritizeSubtree from host config (#19124)
No longer being used.
2020-06-12 12:57:20 -07:00
Andrew Clark
8f05f2bd6d Land Lanes implementation in old fork (#19108)
* Add autofix to cross-fork lint rule

* replace-fork: Replaces old fork contents with new

For each file in the new fork, copies the contents into the
corresponding file of the old fork, replacing what was already there.

In contrast to merge-fork, which performs a three-way merge.

* Replace old fork contents with new fork

First I ran  `yarn replace-fork`.

Then I ran `yarn lint` with autofix enabled. There's currently no way to
do that from the command line (we should fix that), so I had to edit the
lint script file.

* Manual fix-ups

Removes dead branches, removes prefixes from internal fields.  Stuff
like that.

* Fix DevTools tests

DevTools tests only run against the old fork, which is why I didn't
catch these earlier.

There is one test that is still failing. I'm fairly certain it's related
to the layout of the Suspense fiber: we no longer conditionally wrap the
primary children. They are always wrapped in an extra fiber.

Since this has been running in www for weeks without major issues, I'll
defer fixing the remaining test to a follow up.
2020-06-11 20:05:15 -07:00
Sebastian Markbåge
7f28234f84 Enable component stacks everywhere except RN (#19120)
This would still affect test renderer and isomorphic in RN.
2020-06-11 19:13:13 -07:00
Dominic Gannaway
e3ccdf1544 Remove synamic modern event system flag for FB (#19059)
* Remove synamic modern event system flag for FB
2020-06-11 19:49:45 +01:00
Andrew Clark
4c7036e807 Bugfix: Infinite loop in beforeblur event (#19053)
* Failing test: Infinite loop in beforeblur event

If the focused node is hidden by a Suspense boundary, we fire the
beforeblur event. Our check for whether a tree is being hidden isn't
specific enough. It should only fire when the tree is initially hidden,
but it's being fired for updates, too.

* Only fire beforeblur on visible -> hidden

Should only beforeblur fire if the node was previously visible. Not
during updates to an already hidden tree.

To optimize this, we should use a dedicated effect tag and mark it in
the render phase. I've left this for a follow-up, though. Maybe can
revisit after the planned refactor of the commit phase.

* Move logic to commit phase

isFiberSuspenseAndTimedOut is used elsewhere, so I inlined the commit
logic into the commit phase itself.
2020-06-01 09:01:21 -07:00
Sebastian Markbåge
1d85bb3ce1 Build FB specific Isomorphic Bundles (#19049)
We need this so we can version them separately and use different
feature flags than we use for OSS RN.

I put them in a separate facebook-react-native folder which won't go
into the RN GH repo. I plan on moving the renderers there too but not yet.
2020-05-29 15:32:38 -07:00
Brian Vaughn
2efe63d99c DevTools: Add break-on-warn feature (#19048)
This commit adds a new tab to the Settings modal: Debugging

This new tab has the append component stacks feature and a new one: break on warn

This new feature adds a debugger statement into the console override
2020-05-29 14:34:43 -07:00
Sebastian Markbåge
89edb0eae3 Enable component stack locations in www (#19047) 2020-05-29 13:26:38 -07:00
Sebastian Markbåge
cb14168175 Remove unnecessary throw catch (#19044)
This was originally added so you could use "break on caught exceptions"
but that feature is pretty useless these days since it's used for feature
detection and Suspense.

The better pattern is to use the stack trace, jump to source and set a
break point here.

Since DevTools injects its own console.error, we could inject a "debugger"
statement in there. Conditionally. E.g. React DevTools could have a flag
to toggle "break on warnings".
2020-05-29 11:25:48 -07:00
Sebastian Markbåge
c03b8661a9 Upgrade Closure Compiler (#19041)
The Symbol bug has now been fixed so we can remove the hack that renames
the global variable.
2020-05-29 09:17:14 -07:00
Sebastian Markbåge
6d375f3078 Revert autofix lint (#19040)
I accidentally committed this since I had it on locally so I didn't have
to manually convert things to const.

However, this causes things to always pass lint since CI also runs this.
2020-05-28 20:32:34 -07:00
Sebastian Markbåge
8f511754db Prettier wants me to change this (#19039) 2020-05-28 20:13:56 -07:00
Sebastian Markbåge
2e7cc949ae Remove www builds of fetch (#19038)
I don't think we'll ever use this just because we have such a unique set up
for network delivery so we'll use something custom for this case.

Also, we don't need a profiling build for this since it doesn't have an
entry point.
2020-05-28 16:12:37 -07:00
Sebastian Markbåge
76f157e3dd Add simple Node build (#19022)
The webpack plugin doesn't really need a separate prod and dev build.
It also needs to be ES2015 otherwise we can't extend native classes.
2020-05-28 15:56:34 -07:00
Sebastian Markbåge
60afa3c117 Lint bundles using the bundle config instead of scanning for files (#19025)
* Lint bundles using the bundle config instead of scanning for files

This ensures that we look for all the files that we expect to see there.
If something doesn't get built we wouldn't detect it.

However, this doesn't find files that aren't part of our builds such as
indirection files in the root. This will need to change with ESM anyway
since indirection files doesn't work. Everything should be built anyway.

This ensures that we can use the bundles.js config to determine special
cases instead of relying on file system conventions.

* Run lint with flag
2020-05-28 15:04:25 -07:00
Sebastian Markbåge
518ce9c25f Add Lazy Elements Behind a Flag (#19033)
We really needed this for Flight before as well but we got away with it
because Blocks were lazy but with the removal of Blocks, we'll need this
to ensure that we can lazily stream in part of the content.

Luckily LazyComponent isn't really just a Component. It's just a generic
type that can resolve into anything kind of like a Promise.

So we can use that to resolve elements just like we can components.

This allows keys and props to become lazy as well.

To accomplish this, we suspend during reconciliation. This causes us to
not be able to render siblings because we don't know if the keys will
reconcile. For initial render we could probably special case this and
just render a lazy component fiber.

Throwing in reconciliation didn't work correctly with direct nested
siblings of a Suspense boundary before but it does now so it depends
on new reconciler.
2020-05-28 14:16:35 -07:00
Sebastian Markbåge
4985bb0a80 Rename 17 to 18 in warnings (#19031)
We're not really supposed to refer to future versions by numbers.

These will all slip so these numbers don't make sense anymore.
2020-05-28 10:25:39 -07:00
Brian Vaughn
86b4070ddb Cleaned up passive effects experimental flags (#19021) 2020-05-28 08:32:37 -07:00
Sebastian Markbåge
607148673b Remove ReactComponentTreeHook from internals (#19032)
We don't really support mixing minor versions anymore anyway. But seems
safe to remove in 17.
2020-05-27 20:41:13 -07:00
Sebastian Markbåge
43e59f29d6 Delete Entries without Build Output from package.json and the build directory (#19029)
* Gate test

* Delete entrypoints without Build Outputs from package.json and build output

If an entry point exists in bundles.js but doesn't have any bundleTypes,
I delete that entry point file from the build directory. I also remove it
from the files field in package.json if it exists.

This allows us to remove bundles from being built in the stable release
channel.
2020-05-27 19:43:08 -07:00
Sebastian Markbåge
e668f1b54f Remove unstable-fire from package.json (#19026)
This hasn't existed for a while now.
2020-05-27 17:07:25 -07:00
Sebastian Markbåge
0219925e02 Remove regenerator from noop (#19024)
This isn't used and it wouldn't work anyway.
2020-05-27 16:14:46 -07:00
Sebastian Markbåge
c66ac10f4d Lint classic www build (#19023) 2020-05-27 14:13:54 -07:00
Dan Abramov
b41beb1a35 Revert "Fix mistyped script arbitrary code execution vulnerability (#18660)" (#19018)
This reverts commit e5cc1462b3.
2020-05-27 17:37:27 +01:00
Sebastian Markbåge
55cb0b7eeb Only prepare extra stack frames if needed (#19014)
We currently prepare an extra stack frame before they're needed.
Particularly for propTypes. This causes problems as they can have
side-effects with the new component stacks and it's slow.

This moves it to be lazy.
2020-05-27 08:43:50 -07:00
Aaron Pettengill
9752d31f12 Document additionalHooks option (#19005)
* Document `additionalHooks` option

* Update README.md

Co-authored-by: Aaron Pettengill <aaron.pettengill@echoman.com>
Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2020-05-27 15:27:43 +01:00
Dan Abramov
b7ff888190 eslint-plugin-react-hooks@4.0.4 2020-05-27 14:38:27 +01:00
Andrew Clark
18de3b6e7c Bug: Spawning hydration in response to Idle update (#19011)
* Bug: Spawning hydration in response to Idle update

Adds a test that fails in the new fork.

* Fix typos related to Idle priority

These are just silly mistakes that weren't caught by any of our tests.

There's a lot of duplication in the Lanes module right now. It's also
not super stable as we continue to refine our heuristics. Hopefully the
final state is simpler and less prone to these types of mistakes.
2020-05-26 18:53:50 -07:00
Andrew Clark
9273e6585f Disable DevTools build job to unblock master (#19012) 2020-05-26 18:52:58 -07:00
Toru Kobayashi
fb735423bb Fix rollup validate script (#18900)
* Revert "Revert "deps: update ESLint version to v7 (#18897)" (#18899)"

This reverts commit 84fd4b853f.

* fix rollup validate script
2020-05-27 01:19:32 +01:00
Julien Deniau
8abc202c7f [eslint-plugin-react-hooks] Prefer recommended eslint configuration (#18951)
* [eslint-plugin-react-hooks] Prefer recommended eslint configuration

* Update README.md

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2020-05-27 01:08:41 +01:00
Yann アウネ Eves
4821d609e9 fix(eslint-plugin-react-hooks): Added matching for nullish coalescing and optional chaining of dependencies, relates to #18985 (#19008) 2020-05-27 01:07:10 +01:00
Brian Vaughn
a012858ba9 Move isCustomComponent() function call outside of loop (#19007) 2020-05-26 13:41:27 -07:00
Dan Abramov
67e130fc68 eslint-plugin-react-hooks@4.0.3 2020-05-26 16:11:08 +01:00
Dan Abramov
bb2239dc90 Revert "Feature: Add support to exhaustive-deps rule for any hook ending with Effect (#18580)" (#19004)
This reverts commit 5ac9ca72df.
2020-05-26 16:08:50 +01:00
Andrew Clark
03e6b8ba2f Make LegacyHidden match semantics of old fork (#18998)
Facebook currently relies on being able to hydrate hidden HTML. So
skipping those trees is a regression.

We don't have a proper solution for this in the new API yet. So I'm
reverting it to match the old behavior.

Now the server renderer will treat LegacyHidden the same as a fragment,
with no other special behavior. We can only get away with this because
we assume that every instance of LegacyHidden is accompanied by a host
component wrapper. In the hidden mode, the host component is given a
`hidden` attribute, which ensures that the initial HTML is not visible.
To support the use of LegacyHidden as a true fragment, without an extra
DOM node, we will have to hide the initial HTML in some other way.
2020-05-25 18:16:53 -07:00
Dan Abramov
3ca1904b37 react-refresh@0.8.3 2020-05-23 14:26:26 +01:00
yjimk
0aa4cc544c Resolve an edge case where ref.node can be falsy (#18984)
Co-authored-by: Jimmy Cann <jimmy.cann@ironstar.io>
2020-05-23 14:25:24 +01:00
Brian Vaughn
8f4dc3e5d0 Warn if MutableSource snapshot is a function (#18933)
* Warn if MutableSource snapshot is a function

useMutableSource does not properly support snapshots that are functions. In part this is because of how it is implemented internally (the function gets mistaken for a state updater function). To fix this we could just wrap another function around the returned snapshot, but this pattern seems problematic to begin with- because the function that gets returned might itself close over mutable values, which would defeat the purpose of using the hook in the first place.

This PR proposes adding a new DEV warning if the snapshot returned is a function. It does not change the behavior (meaning that a function could still work in some cases- but at least the current behavior prevents passing around a closure that may later become stale unless you're really intentional about it e.g. () => () => {...}).

* Replaced .warn with .error
2020-05-21 16:14:29 -07:00
Brian Vaughn
142d4f1c00 useMutableSource hydration support (#18771)
* useMutableSource hydration support

* Remove unnecessary ReactMutableSource fork

* Replaced root.registerMutableSourceForHydration() with mutableSources option

* Response to PR feedback:

1. Moved mutableSources root option to hydrationOptions object
2. Only initialize root mutableSourceEagerHydrationData if supportsHydration config is true
3. Lazily initialize mutableSourceEagerHydrationData on root object
2020-05-21 16:00:46 -07:00
Brian Vaughn
aefb97e6bb DevTools: Add root and renderer version to inspected props panel (#18963)
* DevTools: Add root and renderer version to inspected props panel
* Removed redundant .length check
2020-05-21 14:40:49 -07:00
Andrew Clark
74394aa8cb Add unstable_isNewReconciler to dispatcher (#18975)
This is a bit gross but I need to be able to access it without importing
the renderer.

There might be a better way but I need this to unblock internal bugfix.
2020-05-21 11:56:00 -07:00
Brian Vaughn
099f73710e DevTools: Improve error boundary (#18956)
1. Add support for dark mode
2. Add retry option for case where Profiler data is corrupted
2020-05-21 11:21:22 -07:00
Dominic Gannaway
730ae7afa2 Clear fiber.sibling field when clearing nextEffect (#18970)
* Clear fiber.sibling field when clearing nextEffect
2020-05-21 18:53:56 +01:00
Brian Vaughn
c93a6cb4d5 DevTools: Fix highlight updates Canvas side problem (#18973) 2020-05-21 10:04:37 -07:00
Andrew Clark
f9bf828701 Add `unstable_isNewReconciler export to FB builds (#18974)
Should check this at runtime instead of the GK
2020-05-21 10:03:59 -07:00
Dominic Gannaway
4a3f779d67 Remove event pooling in the modern system (#18969) 2020-05-21 13:54:05 +01:00
Brian Vaughn
22f7663f14 Profiler: Don't count timed out (hidden) subtrees in base duration (#18966) 2020-05-20 18:36:57 -07:00
Andrew Clark
64f50c667a Remove disableHiddenPropDeprioritization flag (#18964)
This is rolled out to 100% public, so we can remove it.
2020-05-20 15:29:34 -07:00
Sebastian Markbåge
a30a1c6ef3 Transfer actualDuration only once for SuspenseList (#18959) 2020-05-20 15:12:38 -07:00
Andrew Clark
8b3b5c3524 Bugfix: Missing mode check in resetChildLanes (#18961)
Deferring a hidden tree is only supported in Concurrent Mode.

The missing check leads to an infinite loop when an update is scheduled
inside a hidden tree, because the pending work never gets reset.

This "accidentally" worked in the old reconciler because the heurstic
we used to detect offscreen trees was if `childExpirationTime`
was `Never`.

In the new reconciler, we check the tag instead. Which means we also
need to check the mode, like we do in the begin phase.

We should move this check out of the hot path. It shouldn't have been
in the hot path of the old reconciler, either.

Probably by moving `resetChildLanes` into the switch statement
in ReactFiberCompleteWork.
2020-05-19 23:10:49 -07:00
Andrew Clark
95ea8ed47c LegacyHidden: mode that defers without hiding (#18958)
Need this to unblock www. Not sure yet how we'll support this properly
long term.

While adding this, I noticed that the normal "hidden" mode of
LegacyHidden doesn't work properly because it doesn't toggle the
visibility of newly inserted nodes. This is fine for now since we only
use it via a userspace abstraction that wraps the children in an
additional node. But implementing this correctly is required for us
to start using it like a fragment, without the wrapper node.
2020-05-19 15:58:02 -07:00
Brian Vaughn
5aa967b69b DevTools editable props tweaks (#18954)
1. Made non-editable prop text higher contrast (easier to read)
2. Also makes it stand out as different from dimmer placeholder text for "new prop"
2020-05-19 09:42:00 -07:00
Toru Kobayashi
dbf2bba632 remove an unused ISSUE_TEMPLATE.md (#18953) 2020-05-19 08:44:01 -07:00
Brian Vaughn
23309eb386 DevTools 4.6.0 -> 4.7.0 2020-05-18 14:16:04 -07:00
Nick Reiley
d897c35ecf [DevTools] Add Component Highlighting to Profiler (#18745)
Co-authored-by: Moji Izadmehr <m.eezadmehr@gmail.com>
Co-authored-by: Brian Vaughn <bvaughn@fb.com>
2020-05-18 11:13:16 -07:00
Nick Reiley
081b565cd7 [DevTools] enable Electron interactions on Linux & auto copy script tags (#18772)
Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-05-18 10:27:40 -07:00
Sebastian Silbermann
c390ab3643 Add test for displayName on React.memo components (#18925)
* Add test for displayName on React.memo components

* Added extra memo displayName test

Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-05-15 15:56:50 -07:00
Gabriele Prestifilippo
6ed5c2243f Add MIT license to use-subscription package (#18927)
This package is missing the license attribute (or a license file).

Being a sub-package of React, it should get the same license, however, none was specified.
A scan with `license_checker` would recognize this as `UNKNOWN`.
2020-05-15 15:36:48 -07:00
Brian Vaughn
8dba6691d0 Updated DevTools CHANGELOG for upcoming changes 2020-05-15 10:50:39 -07:00
Nick Reiley
dd2e36df33 Profiler: Skip reading element for imported data (#18913)
* skip reading element for imported data

* rename nodes & enable store lookup for components tab

* replace names

* Added some more test coverage; reverted rename

Co-authored-by: Brian Vaughn <bvaughn@fb.com>
2020-05-15 10:38:01 -07:00
Nick Reiley
7c080902ef [DevTools] Improve HOC search UX (#18802)
Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-05-15 10:37:34 -07:00
Ricky Vetter
121af3143c Update inaccurate mapChildren comment (#18931)
The function you provide will only be passed a child and an index. It will not be passed a key. This is confirmed in the source, the Flow types, and the jsdoc comments.
2020-05-15 18:05:08 +01:00
Sophie Alpert
21dc41c320 Simplify logic for mutable workInProgressSources (#18920)
isPrimaryRenderer is always constant for a given React build, so these two arrays do nothing.
2020-05-14 14:30:44 -07:00
Andrew Clark
0fb747f368 Add LegacyHidden to server renderer (#18919)
* Add LegacyHidden to server renderer

When the tree is hidden, the server renderer renders nothing. The
contents will be completely client rendered.

When the tree is visible it acts like a fragment.

The future streaming server renderer may want to pre-render these trees
and send them down in chunks, as with Suspense boundaries.

* Force client render, even at Offscreen pri
2020-05-14 11:28:11 -07:00
Andrew Clark
b4a1a4980c Disable <div hidden /> API in old fork, too (#18917)
The motivation for doing this is to make it impossible for additional
uses of pre-rendering to sneak into www without going through the
LegacyHidden abstraction. Since this feature was already disabled in
the new fork, this brings the two closer to parity.

The LegacyHidden abstraction itself still needs to opt into
pre-rendering somehow, so rather than totally disabling the feature, I
updated the `hidden` prop check to be obnoxiously specific. Before, you
could set it to any truthy value; now, you must set it to the string
"unstable-do-not-use-legacy-hidden".

The node will still be hidden in the DOM, since any truthy value will
cause the browser to apply a style of `display: none`.

I will have to update the LegacyHidden component in www to use the
obnoxious string prop. This doesn't block merge, though, since the
behavior is gated by a dynamic flag. I will update the component before
I enable the flag.
2020-05-13 20:01:10 -07:00
Brian Vaughn
9e5b2c94e6 Add expando prop to disabledLog function (#18914)
This will enable it to be identified by Facebook infra even if the function name is mangled during DevTools build process.
2020-05-13 17:03:50 -07:00
Andrew Clark
fdb641629e Fix useMutableSource tearing bug (#18912)
* Failing useMutableSource test

If a source is mutated after initial read but before subscription is set
up, it should still entangle all pending mutations even if snapshot of
new subscription happens to match.

Test case illustrates how not doing this can lead to tearing.

* Fix useMutableSource tearing bug

Fix is to move the entanglement call outside of the block that checks
if the snapshot has changed.
2020-05-13 14:28:17 -07:00
Andrew Clark
33589f7423 useMutableSource: "Entangle" instead of expiring (#18889)
* useMutableSource: "Entangle" instead of expiring

A lane is said to be entangled with another when it's not allowed to
render in a batch that does not also include the other lane.

This commit implements entanglement for `useMutableSource`. If a source
is mutated in between when it's read in the render phase, but before
it's subscribed to in the commit phase, we must account for whether the
same source has pending mutations elsewhere. The old subscriptions must
not be allowed to re-render without also including the new subscription
(and vice versa), to prevent tearing.

In the old reconciler, we did this by synchronously flushing all the
pending subscription updates. This works, but isn't ideal. The new
reconciler can entangle the updates without de-opting to sync.

In the future, we plan to use this same mechanism for other features,
like skipping over intermediate useTransition states.

* Use clz instead of ctrz to pick an arbitrary lane

Should be slightly faster since most engines have built-in support.
2020-05-13 11:33:32 -07:00
Dan Abramov
43063fd844 eslint-plugin-react-hooks@4.0.2 2020-05-13 16:46:26 +01:00
Dan Abramov
f6ff4c43dd Update changelog 2020-05-13 16:45:50 +01:00
Boris Sergeev
487c693846 [eslint-plugin-react-hooks] useWithoutEffectSuffix fix (#18902) (#18907)
* [eslint-plugin-react-hooks] reproduce bug with a test and fix it (#18902)

Since we only reserve `-Effect` suffix, react-hooks/exhaustive-deps is
expected to succeed without warning on a custom hook which contains -Effect- in
the middle of it's name (but does NOT contain it as a suffix).

* [eslint-plugin-react-hooks] reproduced bug with a test and fix it

Since we only reserve `-Effect` suffix, react-hooks/exhaustive-deps is expected
to succeed without warning on a render helper which contains -use- in the middle
of it's name (but does NOT contain it as a prefix, since that would violate hook
naming convetion).

Co-authored-by: Boris Sergeyev <boris.sergeyev@quolab.com>
2020-05-13 16:44:05 +01:00
Dominic Gannaway
6514e4a179 React Flare: fix PressLegacy preventDefault issue (#18904) 2020-05-13 13:59:32 +01:00
Nick Reiley
a3fccd2567 Fix Profiler root change error (#18880) 2020-05-12 15:47:23 -07:00
Dominic Gannaway
61f2a560e0 Add experimental ReactDOM.createEventHandle (#18756) 2020-05-12 20:24:25 +01:00
Dan Abramov
84fd4b853f Revert "deps: update ESLint version to v7 (#18897)" (#18899)
This reverts commit 039ad34a05.
2020-05-12 20:06:14 +01:00
Dan Abramov
14e554b310 Add missing changelog item 2020-05-12 19:18:26 +01:00
Dominic Gannaway
80c4dea0d1 Modern Event System: Add scaffolding for createEventHandle (#18898) 2020-05-12 19:01:12 +01:00
Toru Kobayashi
039ad34a05 deps: update ESLint version to v7 (#18897) 2020-05-12 18:41:37 +01:00
Dan Abramov
9f396bdd5d eslint-plugin-react-hooks@4.0.1 2020-05-12 17:22:00 +01:00
Richard Maisano
c512aa0081 [Blocks] Scaffolding react-fetch + first pass at node implementation (#18863)
* First pass at scaffolding out the Node implementation of react-data.

While incomplete, this patch contains some changes to the react-data
package in order to start adding support for Node.

The first part of this change accounts for splitting react-data/fetch
into two discrete entries, adding (and defaulting to) the Node
implementation.

The second part is sketching out a rough approximation of `fetch` for
Node. This implementation is not complete by any means, but provides a
starting point.

* Remove NodeFetch module and put it directly into ReactDataFetchNode.

* Replaced react-data with react-fetch.

This patch shuffles around some of the scaffolding that was in
react-data in favor of react-fetch. It also removes the additional
"fetch" package in favor of something flatter.

* Tweak package organization

* Simplify and add a test

Co-authored-by: Dan Abramov <dan.abramov@me.com>
2020-05-12 17:21:45 +01:00
Dan Abramov
e936034eec Add item to ESLint Hooks plugin changelog 2020-05-12 17:02:37 +01:00
Michaël De Boey
c3ff21e01b feat(eslint-plugin-react-hooks): Support ESLint 7.x (#18878) 2020-05-12 17:01:28 +01:00
Andrew Clark
8b9c4d1688 Expose LegacyHidden type and disable <div hidden /> API in new fork (#18891)
* Expose LegacyHidden type

I will use this internally at Facebook to migrate away from
<div hidden />. The end goal is to migrate to the Offscreen type, but
that has different semantics. This is an incremental step.

* Disable <div hidden /> API in new fork

Migrates to the unstable_LegacyHidden type instead. The old fork does
not support the new component type, so I updated the tests to use an
indirection that picks the correct API. I will remove this once the
LegacyHidden (and/or Offscreen) type has landed in both implementations.

* Add gated warning for `<div hidden />` API

Only exists so we can detect callers in www and migrate them to the new
API. Should not visible to anyone outside React Core team.
2020-05-11 20:02:08 -07:00
Dominic Gannaway
ef0bf8e31c Revert "Hard code enableComponentStacks in www (#18869)" (#18890)
This reverts commit fd696df472.
2020-05-11 23:29:24 +01:00
Dominic Gannaway
e16703e6c7 Modern Event System: revise ancestor logic (#18886) 2020-05-11 22:09:52 +01:00
Karl Horky
2b9d7cf65f Devtools: Show inspectedElement key in right pane (#18737)
* Start MVP for showing inspected element key

* Add key in other places

* Add key from backend

* Remove unnecessary hydrateHelper call

* Hide copy button when no label

* Move above props

* Revert changes to InspectedElementTree.js

* Move key to left of component name

* Updated CSS

Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-05-11 13:17:13 -07:00
Rohith Srivathsav
ddcc69c83b Added clear message for functional component starting with lowercase (#18881) 2020-05-10 01:52:11 +01:00
Sebastian Markbåge
539527b642 Don't cut off effects at end of list if hydrating (#18872) 2020-05-08 21:26:51 -07:00
Sebastian Markbåge
fd696df472 Hard code enableComponentStacks in www (#18869)
This is now fully rolled out.
2020-05-08 18:14:54 -07:00
Andrew Clark
6edaf6f764 Detect and prevent render starvation, per lane (#18864)
* Detect and prevent render starvation, per lane

If an update is CPU-bound for longer than expected according to its
priority, we assume it's being starved by other work on the main thread.

To detect this, we keep track of the elapsed time using a fixed-size
array where each slot corresponds to a lane. What we actually store is
the event time when the lane first became CPU-bound.

Then, when receiving a new update or yielding to the main thread, we
check how long each lane has been pending. If the time exceeds a
threshold constant corresponding to its priority, we mark it as expired
to force it to synchronously finish.

We don't want to mistake time elapsed while an update is IO-bound
(waiting for data to resolve) for time when it is CPU-bound. So when a
lane suspends, we clear its associated event time from the array. When
it receives a signal to try again, either a ping or an update, we assign
a new event time to restart the clock.

* Store as expiration time, not start time

I originally stored the start time because I thought I could use this
in the future to also measure Suspense timeouts. (Event times are
currently stored on each update object for this purpose.) But that
won't work because in the case of expiration times, we reset the clock
whenever the update becomes IO-bound. So to replace the per-update
field, I'm going to have to track those on the room separately from
expiration times.
2020-05-08 12:47:51 -07:00
Nick Reiley
6207743168 [DevTools] Allow to continue dragging when leaving profiler picker (#18852)
Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-05-08 11:19:57 -07:00
Luna Ruan
df14b5bcc1 add new IDs for each each server renderer instance and prefixes to distinguish between each server render (#18576)
There is a worry that `useOpaqueIdentifier` might run out of unique IDs if running for long enough. This PR moves the unique ID counter so it's generated per server renderer object instead. For people who render different subtrees, this PR adds a prefix option to `renderToString`, `renderToStaticMarkup`, `renderToNodeStream`, and `renderToStaticNodeStream` so identifiers can be differentiated for each individual subtree.
2020-05-07 20:46:27 -07:00
Brian Vaughn
69e732ac9d Disable Profiler commit filtering (#18862)
* Disable Profiler commit filtering

We used to filter "empty" DevTools commits, but it was error prone (see #18798). A commit may appear to be empty (no actual durations) because of component filters, but filtering these empty commits causes interaction commit indices to be off by N. This not only corrupts the resulting data, but also potentially causes runtime errors.

For that matter, hiding "empty" commits might cause confusion too. A commit *did happen* even if none of the components the Profiler is showing were involved.

* Restart flaky CI
2020-05-07 17:07:20 -07:00
Brian Vaughn
fb3f0acad9 Disable Webpack setImmediate polyfill for DevTools (#18860)
* Upgrade Webpack deps to latet

* Disable Webpack setImmediate polyfill
2020-05-07 13:13:47 -07:00
Sebastian Markbåge
edf6eac8a1 Don't cut off the tail of a SuspenseList if hydrating (#18854) 2020-05-06 23:51:46 -07:00
Sebastian Markbåge
55f5cdee01 Disable setState before mount in legacy mode (#18851)
We kind of "support" this pattern in legacy mode. It's only deprecated in
Concurrent Mode.
2020-05-06 20:30:59 -07:00
Andrew Clark
47ebc90b08 Put render phase update change behind a flag (#18850)
In the new reconciler, I made a change to how render phase updates
work. (By render phase updates, I mean when a component updates
another component during its render phase. Or when a class component
updates itself during the render phase. It does not include when
a hook updates its own component during the render phase. Those have
their own semantics. So really I mean anything triggers the "`setState`
in render" warning.)

The old behavior is to give the update the same "thread" (expiration
time) as whatever is currently rendering. So if you call `setState` on a
component that happens later in the same render, it will flush during
that render. Ideally, we want to remove the special case and treat them
as if they came from an interleaved event.

Regardless, this pattern is not officially supported. This behavior is
only a fallback. The flag only exists until we can roll out the
`setState` warnning, since existing code might accidentally rely on the
current behavior.
2020-05-06 19:19:14 -07:00
Dominic Gannaway
6778c53c16 Modern Event System: fix bug in EnterLeave (#18849) 2020-05-07 00:13:13 +01:00
Brian Vaughn
7eaa1d7e33 Updated DevTools unstable_ API references (#18847) 2020-05-06 11:14:30 -07:00
Andrew Clark
a71aa803a1 [Release] Follow redirect when downloading tarball (#18845)
Adds -L option to `curl` command.

See: https://curl.haxx.se/docs/manpage.html#-L
2020-05-06 10:28:49 -07:00
Sebastian Markbåge
96f3b7d1c3 Warn if calling setState outside of render but before commit (#18838)
* Don't attempt to render the children of a dehydrated Suspense boundary

The DehydratedFragment tag doesn't exist so doing so throws.

This can happen if we schedule childExpirationTime on the boundary and
bail out.

* Warn if scheduling work on a component before it is committed
2020-05-06 09:45:35 -07:00
Dan Abramov
33c3af284c [Blocks Fixture] Remove remaining Blocks (#18840) 2020-05-06 15:53:51 +01:00
Dan Abramov
595d27bd73 [Blocks Fixture] Drop the Blocks (#18837)
* [Blocks Fixture] Add a tiny router

* Add a way to load nested entrypoint

* Only expose URL params to nested routers

* Add keys to route definitions

* [Blocks Fixture] Drop the Blocks
2020-05-06 05:17:44 +01:00
Dominic Gannaway
823dc581fe Modern Event System: fix EnterLeave plugin logic (#18830) 2020-05-05 22:10:07 +01:00
Fernando Lores
40e6029462 wrap SuspenseInstanceRetry callback so scheduler waits for it (#18805) 2020-05-05 12:47:49 -07:00
Brian Vaughn
3cde22a84e Experimental test selector API (#18607)
Adds several new experimental APIs to aid with automated testing.

Each of the methods below accepts an array of "selectors" that identifies a path (or paths) through a React tree. There are four basic selector types:

* Component: Matches Fibers with the specified React component type
* Role: Matches Host Instances matching the (explicit or implicit) accessibility role.
* Test name: Matches Host Instances with a data-testname attribute.
* Text: Matches Host Instances that directly contain the specified text.
* There is also a special lookahead selector type that enables further matching within a path (without actually including the path in the result). This selector type was inspired by the :has() CSS pseudo-class. It enables e.g. matching a <section> that contained a specific header text, then finding a like button within that <section>.

API
* findAllNodes(): Finds all Host Instances (e.g. HTMLElement) within a host subtree that match the specified selector criteria.
* getFindAllNodesFailureDescription(): Returns an error string describing the matched and unmatched portions of the selector query.
* findBoundingRects(): For all React components within a host subtree that match the specified selector criteria, return a set of bounding boxes that covers the bounds of the nearest (shallowed) Host Instances within those trees.
* observeVisibleRects(): For all React components within a host subtree that match the specified selector criteria, observe if it’s bounding rect is visible in the viewport and is not occluded.
* focusWithin(): For all React components within a host subtree that match the specified selector criteria, set focus within the first focusable Host Instance (as if you started before this component in the tree and moved focus forwards one step).
2020-05-05 10:37:46 -07:00
Dan Abramov
c13a59c7d6 Fix Blocks fixture 2020-05-05 18:14:06 +01:00
Kevin Lewis
7992ca10df eslint-plugin-react-hooks: allow OptionalMemberExpression in deps (#18819) (#18820)
* eslint-plugin-react-hooks: allow OptionalMemberExpression in deps (#18819)

* add test case for #18819

* fix test

* run prettier
2020-05-05 13:53:42 +01:00
Dominic Gannaway
e028ce2ab7 Modern Event System: ensure target ancestors are only host nodes (#18827) 2020-05-05 13:10:05 +01:00
Dan Abramov
4d124a4f67 [Blocks Fixture] Misc updates (#18811)
* [Blocks Fixture] Update navigation buttons immediately

* Add more profile links

* Minor refactor

* Add subroutes to Profile
2020-05-05 11:53:02 +01:00
Andrew Clark
fe7163e73d Add unstable prefix to experimental APIs (#18825)
We've been shipping unprefixed experimental APIs (like `createRoot` and
`useTransition`) to the Experimental release channel, with the rationale
that because these APIs do not appear in any stable release, we're free
to change or remove them later without breaking any downstream projects.

What we didn't consider is that downstream projects might be tempted to
use feature detection:

```js
const useTransition = React.useTransition || fallbackUseTransition;
```

This pattern assumes that the version of `useTransition` that exists in
the Experimental channel today has the same API contract as the final
`useTransition` API that we'll eventually ship to stable.

To discourage feature detection, I've added an `unstable_` prefix to
all of our unstable APIs.

The Facebook builds still have the unprefixed APIs, though. We will
continue to support those; if we make any breaking changes, we'll
migrate the internal callers like we usually do. To make testing easier,
I added the `unstable_`-prefixed APIs to the www builds, too. That way
our tests can always use the prefixed ones without gating on the
release channel.
2020-05-04 22:25:41 -07:00
Sebastian Markbåge
cd4a96035f Remove old CM exports (#18710) 2020-05-04 21:39:57 -07:00
Dominic Gannaway
9751935abf Modern Event System: improve dispatching queue (#18799) 2020-05-04 16:55:45 +01:00
Dan Abramov
3e13d70984 [RN] Remove debugging invariant (#18813) 2020-05-04 15:48:35 +01:00
Dan Abramov
f6fcae58e9 [Blocks] Add Shell to Fixture (#18803)
* [Blocks] Add Feed page to fixture

* Add minimal routing

* Always show post with comments
2020-05-04 01:59:55 +01:00
Dan Abramov
d830cd9984 [Blocks] Fix stale data on updates (#18810)
* [Blocks] Failing test for nested load

* Simplify the test

* Add a similar test that fails in PROD

* Copy .type when cloning work in progress
2020-05-04 01:43:51 +01:00
Sebastian Markbåge
64d4b84204 Rename Flight to Transport (#18808)
* Rename Flight to Transport

Flight is still the codename for the implementation details (like Fiber).

However, now the public package is react-transport-... which is only
intended to be used directly by integrators.

* Rename names
2020-05-03 11:33:48 -07:00
Andrew Clark
93e078ddf2 Initial Lanes implementation (#18796)
See PR #18796 for more information.

All of the changes I've made in this commit are behind the
`enableNewReconciler` flag. Merging this to master will not affect the
open source builds or the build that we ship to Facebook.

The only build that is affected is the `ReactDOMForked` build, which is
deployed to Facebook **behind an experimental flag (currently disabled
for all users)**. We will use this flag to gradually roll out the new
reconciler, and quickly roll it back if we find any problems.

Because we have those protections in place, what I'm aiming for with
this initial PR is the **smallest possible atomic change that lands
cleanly and doesn't rely on too many hacks**. The goal has not been to
get every single test or feature passing, and it definitely is not to
implement all the features that we intend to build on top of the new
model. When possible, I have chosen to preserve existing semantics and
defer changes to follow-up steps. (Listed in the section below.)

(I did not end up having to disable any tests, although if I had, that
should not have necessarily been a merge blocker.)

For example, even though one of the primary goals of this project is to
improve our model for parallel Suspense transitions, in this initial
implementation, I have chosen to keep the same core heuristics for
sequencing and flushing that existed in the ExpirationTimes model: low
priority updates cannot finish without also finishing high priority
ones.

Despite all these precautions, **because the scope of this refactor is
inherently large, I do expect we will find regressions.** The flip side
is that I also expect the new model to improve the stability of the
codebase and make it easier to fix bugs when they arise.
2020-05-02 17:09:31 -07:00
Moti Zilberman
3c7d52c3d6 Give unresolved lazy() a name in component stack (#16104)
* Give unresolved lazy() a name in component stack

* Normalize stack in tests

Co-authored-by: Sebastian Markbage <sema@fb.com>
2020-05-01 16:41:39 -07:00
Benedikt Franke
333deb707d Remove hint to post feedback about exhaustive-deps rule (#18712)
The thread is closed and locked.
2020-05-01 19:13:59 +01:00
Dominic Gannaway
aa88589d0b Refine experimental Scopes API (#18778)
* Refine experimental Scopes API
2020-05-01 19:01:43 +01:00
Dan Abramov
d804f99b9e eslint-plugin-react-hooks@4.0.0 2020-05-01 17:13:56 +01:00
Dan Abramov
4e93b9364c Add changelog for eslint-plugin-react-hooks (#18801) 2020-05-01 17:12:32 +01:00
Aaron Pettengill
5ac9ca72df Feature: Add support to exhaustive-deps rule for any hook ending with Effect (#18580)
* Add test cases for support exhaustive deps ending in Effect

* Apply the exhaustive deps lint rule to any hook ending with Effect

* Add another test for supporting linting useXEffect hooks

Co-authored-by: Aaron Pettengill <aaron.pettengill@echoman.com>
2020-05-01 16:57:25 +01:00
Dan Abramov
eab94403d7 Build Flight runtime for WWW (revert part of earlier change) 2020-05-01 16:44:25 +01:00
Sebastian Markbåge
17dcc29cd1 Don't "schedule" discrete work if we're scheduling sync work (#18797) 2020-05-01 08:43:15 -07:00
Dan Abramov
9491f39472 react-refresh@0.8.2 2020-05-01 16:18:13 +01:00
Dan Abramov
43a1c72751 Don't build react-data for WWW 2020-05-01 16:13:11 +01:00
Dan Abramov
01914a7673 Update bundle configs to not build some packages (#18800)
* Stop building old ReactCache for FB

* Update other bundle configs
2020-05-01 16:07:36 +01:00
Sophie Alpert
21670cf4be [react-refresh/babel] Always insert registrations after var (#18794)
Previously, we transformed

```
let Foo = styled.div``;
```

to

```
let Foo = _c1 = styled.div``;
```

and then babel-plugin-styled-components would infer `_c1` as the display name. Widen the existing case that applies to function expressions to apply to any type of variable declaration.
2020-05-01 16:01:11 +01:00
Andrew Clark
914b57be27 Move legacy hidden API to new internal Fiber type (#18782)
* Unhide Suspense trees without entanglement

When a Suspense boundary is in its fallback state, you cannot switch
back to the main content without also finishing any updates inside the
tree that might have been skipped. That would be a form of tearing.

Before we fixed this in #18411, the way this bug manifested was that a
boundary was suspended by an update that originated from a child
component (as opposed to props from a parent). While the fallback was
showing, it received another update, this time at high priority. React
would render the high priority update without also including the
original update. That would cause the fallback to switch back to the
main content, since the update that caused the tree to suspend was no
longer part of the render. But then, React would immediately try to
render the original update, which would again suspend and show the
fallback, leading to a momentary flicker in the UI.

The approach added in #18411 is, when receiving a high priority update
to a Suspense tree that's in its fallback state is to bail out, keep
showing the fallback and finish the update in the rest of the tree.
After that commits, render again at the original priority. Because low
priority expiration times are inclusive of higher priority expiration
times, this ensures that all the updates are committed together.

The new approach in this commit is to turn `renderExpirationTime` into a
context-like value that lives on the stack. Then, when unhiding the
Suspense boundary, we can push a new `renderExpirationTime` that is
inclusive of both the high pri update and the original update that
suspended. Then the boundary can be unblocked in a single render pass.

An advantage of the old approach is that by deferring the work of
unhiding, there's less work to do in the high priority update.

The key advantage of the new approach is that it solves the consistency
problem without having to entangle the entire root.

* Create internal LegacyHidden type

This only exists so we can clean up the internal implementation of
`<div hidden={isHidden} />`, which is not a stable feature. The goal
is to move everything to the new Offscreen type instead. However,
Offscreen has different semantics, so before we can remove the legacy
API, we have to migrate our internal usage at Facebook. So we'll need
to maintain both temporarily.

In this initial commit, I've only added the type. It's not used
anywhere. The next step is to use it to implement `hidden`.

* Use LegacyHidden to implement old hidden API

If a host component receives a `hidden` prop, we wrap its children in
an Offscreen fiber. This is similar to what we do for Suspense children.

The LegacyHidden type happens to share the same implementation as the
new Offscreen type, for now, but using separate types allows us to fork
the behavior later when we implement our planned changes to the
Offscreen API.

There are two subtle semantic changes here. One is that the children of
the host component will have their visibility toggled using the same
mechanism we use for Offscreen and Suspense: find the nearest host node
children and give them a style of `display: none`. We didn't used to do
this in the old API, because the `hidden` DOM attribute on the parent
already hides them. So with this change, we're actually "overhiding" the
children. I considered addressing this, but I figure I'll leave it as-is
in case we want to expose the LegacyHidden component type temporarily
to ease migration of Facebook's internal callers to the Offscreen type.

The other subtle semantic change is that, because of the extra fiber
that wraps around the children, this pattern will cause the children
to lose state:

```js
return isHidden ? <div hidden={true} /> : <div />;
```

The reason is that I didn't want to wrap every single host component
in an extra fiber. So I only wrap them if a `hidden` prop exists. In
the above example, that means the children are conditionally wrapped
in an extra fiber, so they don't line up during reconciliation, so
they get remounted every time `isHidden` changes.

The fix is to rewrite to:

```js
return <div hidden={isHidden} />;
```

I don't anticipate this will be a problem at Facebook, especially since
we're only supposed to use `hidden` via a userspace wrapper component.
(And since the bad pattern isn't very React-y, anyway.)

Again, the eventual goal is to delete this completely and replace it
with Offscreen.
2020-04-30 19:37:27 -07:00
Brian Vaughn
ac533fde3d Prevent stale legacy root from clearing a container (DRAFT) (#18792)
* Don't clear a container because of a stale legacy root

* Added test repro for FB error
2020-04-30 15:56:21 -07:00
Dan Abramov
5b89d353e2 Update react-refresh README 2020-04-30 15:43:21 +01:00
Dan Abramov
3a9c373521 [Blocks] Add preload to fetch (#18785) 2020-04-30 02:23:18 +01:00
Dan Abramov
dd7f0deb94 [Blocks] Use native fetch (#18784)
* [Blocks] Use native fetch

* Use the prototype

* Support arrayBuffer() and blob()

* ctor

* Simplify

* Use an expando

* Keep a map of formats

* Unused

* Remove unnecessary second property read

* Keep it simple

* Store the original thenable
2020-04-30 01:24:04 +01:00
Alex Taylor
e71f5df9c3 Consistent useCallback implementation in ReactDOMServer (#18783) 2020-04-29 16:22:10 -07:00
Dan Abramov
515326753b [Blocks] Initial implementation of cache and data/fetch (#18774)
* Rename ReactCache -> ReactCacheOld

We still use it in some tests so I'm going to leave it for now. I'll start making the new one in parallel in the react package.

* Add react/unstable-cache entry point

* Add react-data entry point

* Initial implementation of cache and data/fetch

* Address review
2020-04-29 19:14:15 +01:00
Dan Abramov
53d68b33ff [Blocks] Add Client Fixture (#18773)
* [Blocks] Add Client Fixture

* Add more TODOs
2020-04-29 02:23:09 +01:00
Dominic Gannaway
88d0be6da5 Refactor ElementListenerMap (#18766) 2020-04-28 21:53:51 +01:00
Brian Vaughn
ea2af878cc Root API should clear non-empty roots before mounting (#18730)
* Root API should clear non-empty roots before mounting

Legacy render-into-subtree API removes children from a container before rendering into it. The root API did not do this previously, but just left the children around in the document.

This commit adds a new FiberRoot flag to clear a container's contents before mounting. This is done during the commit phase, to avoid multiple, observable mutations.
2020-04-28 13:07:42 -07:00
Nick Reiley
d2ef120089 Allow Node 14.x (#18755) 2020-04-28 15:57:45 +01:00
Sebastian Markbåge
53ce0c3452 Allow flushSync to noop in life cycles but with a warning (#18759) 2020-04-27 20:07:39 -07:00
Andrew Clark
f342a2399f SuspenseList: Reschedule at same priority (#18738)
SuspenseList progressively renders items even if the list is CPU bound,
i.e. it isn't waiting for missing data. It does this by showing a
fallback for the remaining items, committing the items in that have
already finished, then starting a new render to continue working on
the rest.

When it schedules that subsequent render, it uses a slightly lower
priority than the current render: `renderExpirationTime - 1`.

This commit changes it to reschedule at `renderExpirationTime` instead.

I don't know what the original motivation was for bumping the expiration
time slightly lower. The comment says that the priorities of the two
renders are the same (which makes sense to me) so I imagine it was
motivated by some implementation detail. I don't think it's necessary
anymore, though perhaps it was when it was originally written. If it is
still necessary, we should write a test case that illustrates why.
2020-04-25 20:27:35 -07:00
Andrew Clark
cb70753993 Move hide/unhide logic to Offscreen component
The Offscreen component is not a public type, yet, but once it is, it
will share the same hide/unhide logic as Suspense children.
2020-04-25 00:10:09 -07:00
Andrew Clark
cb48f974c9 Wrap primary tree in Offscreen fiber type
Still just acts like a fragment. Next step is to move the hide/unhide
logic to the Offscreen implementation.
2020-04-25 00:10:09 -07:00
Luna Ruan
ed01fdacce Add Offscreen component type
Doesn't do anything special in this initial commit. Just acts like a
fragment.
2020-04-25 00:10:09 -07:00
Andrew Clark
1df756ba2c Always wrap Suspense children in fragment (#18711)
Changes the internal fiber structure of the Suspense component. When a
Suspense boundary can't finish rendering and switches to a fallback, we
wrap the "primary" tree in a Fragment fiber and hide all its DOM nodes.
Then we mount the fallback tree into a separate Fragment fiber. Both
trees will render into the same parent DOM node (since React fragments
aren't part of the host tree), but the wrappers ensure that the children
in each tree are reconciled separately.

The old implementation would try to be clever and only add the fragment
wrapper when the fallback was in place, to save memory. This "worked"
but was prone to regressions, since this is the only such place in the
codebase where we wrap existing nodes in a new node. (In other words,
it's a form of reparenting, which we don't implement elsewhere).

Since the original implementation, we've also added lots of additional
requirements to the Suspense component that have led to an explosion in
complexity, like limited support in Legacy Mode (with very different
semantics) and progressive hydration.

We're planning to add even more features to the Suspense boundary, so
we're going to sacrifice a bit more memory for a simpler implementation
that is less prone to regressions.

This ended up removing a lot of weird hacks and edge cases, but there
are still plenty left over. Most of the remaining complexity is related
to Legacy mode. That's the next thing we should aim to drop support for.

Because this is a risky change, I've only changed this in the new
reconciler. It blocks some other features, but as of now we're not
planning to implement those in the old reconciler. If that changes, this
should cherry-pick to the other implementation without much effort.
2020-04-25 00:09:19 -07:00
Andrew Clark
db6513914f Make ExpirationTime an opaque type (#18732)
* Add LanePriority type

React's internal scheduler has more priority levels than the external
Scheduler package. Let's use React as the source of truth for tracking
the priority of updates so we have more control. We'll still fall back
to Scheduler in the default case. In the future, we should consider
removing `runWithPriority` from Scheduler and replacing the valid use
cases with React-specific APIs.

This commit adds a new type, called a LanePriority to disambiguate from
the Scheduler one.

("Lane" refers to another type that I'm planning. It roughly translates
to "thread." Each lane will have a priority associated with it.)

I'm not actually using the lane anywhere, yet. Only setting stuff up.

* Remove expiration times train model

In the old reconciler, expiration times are computed by applying an
offset to the current system time. This has the effect of increasing
the priority of updates as time progresses. Because we also use
expiration times as a kind of "thread" identifier, it turns out this
is quite limiting because we can only flush work sequentially along
the timeline.

The new model will use a bitmask to represent parallel threads that
can be worked on in any combination and in any order.

In this commit, expiration times and the linear timeline are still in
place, but they are no longer based on a timestamp. Effectively, they
are constants based on their priority level.

* Stop using ExpirationTime to represent timestamps

Follow up to the previous commit. This converts the remaining places
where we were using the ExpirationTime type to represent a timestamp,
like Suspense timeouts.

* Fork Dependencies and PendingInteractionMap types

These contain expiration times

* Make ExpirationTime an opaque type

ExpirationTime is currently just an alias for the `number` type, for a
few reasons. One is that it predates Flow's opaque type feature. Another
is that making it opaque means we have to move all our comparisons and
number math to the ExpirationTime module, and use utility functions
everywhere else.

However, this is actually what we want in the new system, because the
Lanes type that will replace ExpirationTime is a bitmask with a
particular layout, and performing operations on it will involve more
than just number comparisions and artihmetic. I don't want this logic to
spread ad hoc around the whole codebase.

The utility functions get inlined by Closure so it doesn't matter
performance-wise.

I automated most of the changes with JSCodeshift, with only a few manual
tweaks to stuff like imports. My goal was to port the logic exactly to
prevent subtle mistakes, without trying to simplify anything in the
process. I'll likely need to audit many of these sites again when I
replace them with the new type, though, especially the ones
in ReactFiberRoot.

I added the codemods I used to the `scripts` directory. I won't merge
these to master. I'll remove them in a subsequent commit. I'm only
committing them here so they show up in the PR for future reference.

I had a lot of trouble getting Flow to pass. Somehow it was not
inferring the correct type of the constants exported from the
ExpirationTime module, despite being annotated correctly.

I tried converting them them to constructor functions — `NoWork`
becomes `NoWork()` — and that made it work. I used that to unblock me,
and fixed all the other type errors. Once there were no more type
errors, I tried converting the constructors back to constants. Started
getting errors again.

Then I added a type constraint everywhere a constant was referenced.
That fixed it. I also figured out that you only have to add a constraint
when the constant is passed to another function, even if the function is
annotated. So this indicates to me that it's probably a Flow bug. I'll
file an issue with Flow.

* Delete temporary codemods used in previous commit

I only added these to the previous commit so that I can easily run it
again when rebasing. When the stack is squashed, it will be as if they
never existed.
2020-04-24 23:26:04 -07:00
Chang Yan
4b02b66111 [ESLint] Extend isHook to recognize those under PascalCase's namespace (#18722)
* Extend Namespace to PascalCase

* Add valid case for jest.useFakeTimer

* format

* format :(

* fix nits
2020-04-24 02:50:52 +01:00
Dominic Gannaway
30cee2f4c7 Modern Event System: register onMouseEnter for portals (#18720) 2020-04-23 20:57:52 +01:00
Dominic Gannaway
4e3545fd6f ReactDOMEventListener: clean up module (#18713) 2020-04-23 20:19:28 +01:00
Sebastian Markbåge
b58dec979a Remove unnecessary stack (#18707) 2020-04-23 09:44:37 -07:00
Sebastian Markbåge
a2fb84bebd Disable prepareStackTrace while we're generating stacks (#18708)
This could be used to do custom formatting of the stack trace in a way
that isn't compatible with how we use it. So we disable it while we use
it.

In theory we could call this ourselves with the result of our stack.
It would be a lot of extra production code though. My personal opinion
is that this should always be done server side instead of on the client.

We could expose a custom parser that converts it and passes it through
prepareStackTrace as structured data. That way it's external and doesn't
have to be built-in to React.
2020-04-23 09:16:58 -07:00
Brian Vaughn
b9fc3d8d59 DevTools temporarily disable prepareStackTrace before creating custom component stacks (#18717) 2020-04-23 09:02:42 -07:00
Sebastian Markbåge
b0cb137bcb Don't dedupe using the stack (#18693)
We currently use the stack to dedupe warnings in a couple of places.
This is a very heavy weight way of computing that a warning doesn't need
to be fired.

This uses parent component name as a heuristic for deduping. It's not
perfect but as soon as you fix one you'll uncover the next. It might be a
little annoying but having many logs is also annoying.

We now have no special cases for stacks. The only thing that uses stacks in
dev is the console.error and dev tools. This means that we could
externalize this completely to an console.error patching module and drop
it from being built-in to react.

The only prod/dev behavior is the one we pass to error boundaries or the
error we throw if you don't have an error boundary.
2020-04-22 19:02:11 -07:00
Sebastian Markbåge
571f5ad2d6 Temporarily Remove DebugTracing from the New Reconciler (#18697)
* Remove priority field from tracing

* Remove DebugTracing mode from new reconciler (temporarily)

* Run DebugTracing tests in the *other* variant so it's no on for new reconciler
2020-04-21 20:14:40 -07:00
Sebastian Markbåge
2325375f4f Nit: add suffix _TIMEOUT consistently in scheduler (#18696) 2020-04-21 19:44:42 -07:00
Nick Reiley
e5cc1462b3 Fix mistyped script arbitrary code execution vulnerability (#18660)
* add test to trustedTypes-test.internal.js

* fix mistyped script arbitrary code execution

* Removed redundant .toLowerCase() call

Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-04-21 12:10:25 -07:00
Brian Vaughn
9025949d84 Pin RNW to canary (#18692)
This unbreaks DevTools shell with the latest ReactDOM in master.
2020-04-21 11:46:29 -07:00
Brian Vaughn
36cab2720a DevTools: Improved "native" component stacks (#18656)
* DevTools console override handles new component stack format

DevTools does not attempt to mimic the default browser console format for its component stacks but it does properly detect the new format for Chrome, Firefox, and Safari.
2020-04-21 11:46:11 -07:00
Sebastian Markbåge
940f48b999 Avoid passing custom stacks to console.error (#18685)
* Detect double stacks in the new format in tests

* Remove unnecessary uses of getStackByFiberInDevAndProd

These all execute in the right execution context already.

* Set the debug fiber around the cases that don't have an execution context

* Remove stack detection in our console log overrides

We never pass custom stacks as part of the args anymore.

* Bonus: Don't append getStackAddendum to invariants

We print component stacks for every error anyway so this is just duplicate
information.
2020-04-21 09:22:46 -07:00
Dominic Gannaway
ff431b7fc4 Remove ReactDOM.useEvent and associated types+tests (#18689) 2020-04-21 16:40:44 +01:00
Dominic Gannaway
7b3934b34f Revert "Pressable click fix (#18625)" (#18688)
This reverts commit 5f7b175b35.
2020-04-21 11:08:30 +01:00
Dominic Gannaway
80d39d8b57 Remove null sibling (#18687) 2020-04-21 10:43:50 +01:00
Sebastian Markbåge
0960b7ba58 Upgrade fbjs-scripts (#18684)
* Upgrade fbjs-scripts

This script takes into account the NODE_ENV as part of jest cache keys.
This avoids flaky tests since we depend on different transforms in prod
and dev.

* Upgrade Fresh test to Babel 7 transform
2020-04-20 18:20:03 -07:00
Naman Goel
5f7b175b35 Pressable click fix (#18625)
* Update press-legacy to use native click events

* update tests for pressable change

* fix formatting issue

* Address comments. Bring back some tests, remove others. Cleanup

* Fix flow and lint errors

* formatting fix missed by yarn lint
2020-04-20 23:13:17 +01:00
Dominic Gannaway
4f59a149b8 Modern event system: fix selectionchange bug (#18680) 2020-04-20 19:54:12 +01:00
Dominic Gannaway
a152827ef6 Refine the heuristics around beforeblur/afterblur (#18668)
* Refine the heuristics around beforeblur/afterblur
2020-04-20 19:32:22 +01:00
Sebastian Silbermann
707478e68a chore: Build react-dom/server in codesandbox CI (#18679) 2020-04-20 18:48:07 +01:00
Sebastian Silbermann
ffb6c6c07b fix: skip dangerouslySetInnerHtml hydration warning if it's undefined (#18676)
* test: Add failing case for dangerouslySetInnerHtml=undefined

* fix: skip dangerouslySetInnerHtml warning if it's undefined

* test: add similar test that should trigger the warning

* chore: Remove redundant nullish check

* Poke yarn_test_www_variant which timed out

* test: Add smaller test for innerHTML=string to innerHTML=undefined
2020-04-20 18:46:55 +01:00
Dominic Gannaway
1078029af6 Revert "Revert focus event PRs (#18655)" (#18671)
This reverts commit 58c895e59c.
2020-04-20 16:00:33 +01:00
Nicolas Gallagher
96203240d3 ReactDOM: remove unstable-native-dependencies bundle (#18483)
This is only used by react-native-web and will be replaced by a user-space
implementation. See: https://github.com/necolas/react-native-web/issues/1568
2020-04-18 15:40:13 -07:00
Brian Vaughn
263bc5d54b Fix incorrect unmounted state update warning (#18617)
* Fix incorrect unmounted state update warning

We detach fibers (which nulls the  field) when we commit a deletion, so any state updates scheduled between that point and when we eventually flush passive effect destroys won't have a way to check if there is a pending passive unmount effect scheduled for its alternate unless we also explicitly track this for both the current and the alternate.

This commit adds a new DEV-only effect type, `PendingPassiveUnmountDev`, to handle this case.
2020-04-18 12:18:19 -07:00
Andrew Clark
52a0d6b5ab Make Flow work with your editor (#18664)
We typecheck the reconciler against each one of our host configs.
`yarn flow dom` checks it against the DOM renderer, `yarn flow native`
checks it against the native renderer, and so on.

To do this, we generate separate flowconfig files.

Currently, there is no root-level host config, so running Flow
directly via `flow` CLI doesn't work. You have to use the `yarn flow`
command and pick a specific renderer.

A drawback of this design, though, is that our Flow setup doesn't work
with other tooling. Namely, editor integrations.

I think the intent of this was maybe so you don't run Flow against a
renderer than you intended, see it pass, and wrongly think you fixed
all the errors. However, since they all run in CI, I don't think this
is a big deal. In practice, I nearly always run Flow against the same
renderer (DOM), and I'm guessing that's the most common workflow for
others, too.

So what I've done in this commit is modify the `yarn flow` command to
copy the generated `.flowconfig` file into the root directory. The
editor integration will pick this up and show Flow information for
whatever was the last renderer you checked.

Everything else about the setup is the same, and all the renderers will
continue to be checked by CI.
2020-04-18 10:24:46 -07:00
Andrew Clark
cfefc81ab2 Fix for #18657 (#18663)
* Failing test for #18657

* Remove incorrect priority check

I think this was just poor factoring on my part in #18411. Honestly it
doesn't make much sense to me, but my best guess is that I must have
thought that when `baseTime > currentChildExpirationTime`, the function
would fall through to the
`currentChildExpirationTime < renderExpirationTime` branch below.

Really I think just made an oopsie.

Regardless, this logic is galaxy brainéd. A goal of the Lanes refactor
I'm working on is to make these types of checks -- is there remaining
work in this tree? -- a lot easier to think about. Hopefully.
2020-04-17 16:32:55 -07:00
Dominic Gannaway
e7163a9c2f Revise isFiberInsideHiddenOrRemovedTree (#18662) 2020-04-17 23:58:40 +01:00
Andrew Clark
a4b1e65afc Remove redundant expiration time comparisons (#18620)
I'm going through all the expiration times comparisons as part of my
refactor and I noticed this one has a redundancy.
2020-04-17 11:48:05 -07:00
Dominic Gannaway
58c895e59c Revert focus event PRs (#18655)
* Revert "Further cleanup to before/after blur (#18649)"

This reverts commit e2ccbf0358.

* Revert "Unify Flare FocusWithin responder with useFocusWithin (#18636)"

This reverts commit f24a9e7098.
2020-04-17 16:27:19 +01:00
Dominic Gannaway
e2ccbf0358 Further cleanup to before/after blur (#18649) 2020-04-17 11:52:29 +01:00
Brian Vaughn
c5c25d35a3 Fixed DevTools extension build:dev command (#18648) 2020-04-16 17:39:16 -07:00
dependabot[bot]
f8b084276d Bump https-proxy-agent from 2.2.1 to 2.2.4 in /scripts/release (#18642)
Bumps [https-proxy-agent](https://github.com/TooTallNate/node-https-proxy-agent) from 2.2.1 to 2.2.4.
- [Release notes](https://github.com/TooTallNate/node-https-proxy-agent/releases)
- [Commits](https://github.com/TooTallNate/node-https-proxy-agent/compare/2.2.1...2.2.4)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-04-16 16:06:13 -07:00
dependabot[bot]
0f9b30f7b5 Bump https-proxy-agent from 2.2.2 to 2.2.4 (#18643)
Bumps [https-proxy-agent](https://github.com/TooTallNate/node-https-proxy-agent) from 2.2.2 to 2.2.4.
- [Release notes](https://github.com/TooTallNate/node-https-proxy-agent/releases)
- [Commits](https://github.com/TooTallNate/node-https-proxy-agent/compare/2.2.2...2.2.4)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-04-16 16:06:06 -07:00
Dominic Gannaway
32bb44c80a Clean up modern plugins to remove dead code (#18639) 2020-04-16 20:10:23 +01:00
Dan Abramov
0301f3e24f Statically disable factory components for WWW (#18641) 2020-04-16 19:42:30 +01:00
Dominic Gannaway
f24a9e7098 Unify Flare FocusWithin responder with useFocusWithin (#18636) 2020-04-16 19:35:41 +01:00
Dominic Gannaway
b1a083183c Revert legacy plugin modules (#18638) 2020-04-16 18:46:24 +01:00
Dominic Gannaway
31977d1bb3 Create copy of Legacy event plugins for Modern system (#18637) 2020-04-16 16:09:05 +01:00
Andrew Clark
c12c2c4c8c Run error replay test again new reconciler (#18628)
This is one of our few tests of an internal function. Need to import
the module that corresponds to the reconciler that's being tested.
2020-04-16 08:08:22 -07:00
Boris Serdiuk
7b4403cecd Fix requiring timers module with non standard require (#18632)
fixes #18589
2020-04-16 12:36:09 +01:00
Thomas Brown
5f6b75dd26 ADD: disableRemotePlayback attribute for HTML5 videos (#18619)
* Add support for disableremoteplayback

* Order attributes alphabetically

Co-authored-by: Tom Brown <Thomas.Brown-CIC-UK@ibm.com>
2020-04-16 10:23:48 +01:00
Brian Vaughn
71964c0346 Fix CI (#18626) 2020-04-15 22:27:24 -07:00
Andrew Clark
50b9f154c1 Fix wrong command in config.yml (#18624) 2020-04-15 21:44:38 -07:00
Brian Vaughn
22dc2e42bd Add experimental DebugTracing logger for internal use (#18531) 2020-04-15 19:10:15 -07:00
Andrew Clark
dbd2626855 Update nvmrc to the same one used in CI (#18623) 2020-04-15 16:03:02 -07:00
Andrew Clark
b680174841 Delete mutableSourceFirstPendingUpdateTime (#18613) 2020-04-15 07:47:37 -07:00
Andrew Clark
cbafbf4f32 Fix typo in TestFlags
Copypasta when setting up `old` and `new` aliases. `old` should mean
that `enableNewReconciler` is off.
2020-04-14 23:32:53 -07:00
Dominic Gannaway
d53988a9d1 ReactDOM.useEvent: add useEvent interaction hook (#18604) 2020-04-14 22:06:46 +01:00
Dominic Gannaway
bf55ea7434 Move beforeblur phase to prepareForCommit (#18609) 2020-04-14 21:30:54 +01:00
Andrew Clark
843b50cbe1 Remove .internal from more test suites (#18597) 2020-04-14 07:43:00 -07:00
BetaSu
1d7bd52688 remove unnecessary variable comparation (#18598)
Since it will continue when the props are equal in line 767, there is no need to compare whether they are not equal.
2020-04-14 14:53:47 +01:00
Andrew Clark
b928fc030a Delete flushSuspenseFallbacksInTests flag (#18596)
* Move renderer `act` to work loop

* Delete `flushSuspenseFallbacksInTests`

This was meant to be a temporary hack to unblock the `act` work, but it
quickly spread throughout our tests.

What it's meant to do is force fallbacks to flush inside `act` even in
Concurrent Mode. It does this by wrapping the `setTimeout` call in a
check to see if it's in an `act` context. If so, it skips the delay and
immediately commits the fallback.

Really this is only meant for our internal React tests that need to
incrementally render. Nobody outside our team (and Relay) needs to do
that, yet. Even if/when we do support that, it may or may not be with
the same `flushAndYield` pattern we use internally.

However, even for our internal purposes, the behavior isn't right
because a really common reason we flush work incrementally is to make
assertions on the "suspended" state, before the fallback has committed.
There's no way to do that from inside `act` with the behavior of this
flag, because it causes the fallback to immediately commit. This has led
us to *not* use `act` in a lot of our tests, or to write code that
doesn't match what would actually happen in a real environment.

What we really want is for the fallbacks to be flushed at the *end` of
the `act` scope. Not within it.

This only affects the noop and test renderer versions of `act`, which
are implemented inside the reconciler. Whereas `ReactTestUtils.act` is
implemented in "userspace" for backwards compatibility. This is fine
because we didn't have any DOM Suspense tests that relied on this flag;
they all use test renderer or noop.

In the future, we'll probably want to move always use the reconciler
implementation of `act`. It will not affect the prod bundle, because we
currently only plan to support `act` in dev. Though we still haven't
completely figured that out. However, regardless of whether we support a
production `act` for users, we'll still need to write internal React
tests in production mode. For that use case, we'll likely add our own
internal version of `act` that assumes a mock Scheduler and might rely
on hacks that don't 100% align up with the public one.
2020-04-13 20:02:18 -07:00
Andrew Clark
f3f3d77c20 Fix www tests not running in prod (#18593)
I made a mistake when setting these up a while ago. Setting the NODE_ENV
in the CircleCI config doesn't work because it's also set in the node
script command.

The number of test commands is getting out of control. Might need to fix
it at some point. Not today for me.
2020-04-13 15:53:13 -07:00
Andrew Clark
bec7599067 Migrate conditional tests to gate pragma (#18585)
* Migrate conditional tests to gate pragma

I searched through the codebase for this pattern:

```js
describe('test suite', () => {
  if (!__EXPERIMENTAL__) { // or some other condition
    test("empty test so Jest doesn't complain", () => {});
    return;
  }

  // Unless we're in experimental mode, none of the tests in this block
  // will run.
})
```

and converted them to the `@gate` pragma instead.

The reason this pattern isn't preferred is because you end up disabling
more tests than you need to.

* Add flag for www release channels

Using a heuristic where I check a flag that is known to only be enabled
in www. I left a TODO to instead set the release channel explicitly in
each test config.
2020-04-13 14:45:52 -07:00
Chris Dobson
6c43a62c0f DevTools: Switch between "Rendered At" renders using keyboard arrow keys (#18586)
* Add keyboard navigation to fibre info sidebar

Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-04-13 13:00:13 -07:00
Andrew Clark
0c3c27a718 Fix "missing flag" error for non-boolean types (#18592)
Not all test flags are booleans, e.g. the build type
2020-04-13 12:57:31 -07:00
Andrew Clark
65237a237e Codemod it.experimental to gate pragma (#18582)
* Codemod it.experimental to gate pragma

Find-and-replace followed by Prettier

* Delete it.experimental

Removes the API from our test setup script
2020-04-13 10:28:59 -07:00
Andrew Clark
42d7c2e8f7 Add pragma for feature testing: @gate (#18581)
* Add pragma for feature testing: @gate

The `@gate` pragma declares under which conditions a test is expected to
pass.

If the gate condition passes, then the test runs normally (same as if
there were no pragma).

If the conditional fails, then the test runs and is *expected to fail*.

An alternative to `it.experimental` and similar proposals.

Examples
--------

Basic:

```js
// @gate enableBlocksAPI
test('passes only if Blocks API is available', () => {/*...*/})
```

Negation:

```js
// @gate !disableLegacyContext
test('depends on a deprecated feature', () => {/*...*/})
```

Multiple flags:

```js
// @gate enableNewReconciler
// @gate experimental
test('needs both useEvent and Blocks', () => {/*...*/})
```

Logical operators (yes, I'm sorry):

```js
// @gate experimental && (enableNewReconciler || disableSchedulerTimeoutBasedOnReactExpirationTime)
test('concurrent mode, doesn\'t work in old fork unless Scheduler timeout flag is disabled', () => {/*...*/})
```

Strings, and comparion operators

No use case yet but I figure eventually we'd use this to gate on
different release channels:

```js
// @gate channel ===  "experimental" || channel === "modern"
test('works in OSS experimental or www modern', () => {/*...*/})
```

How does it work?

I'm guessing those last two examples might be controversial. Supporting
those cases did require implementing a mini-parser.

The output of the transform is very straightforward, though.

Input:
```js
// @gate a && (b || c)
test('some test', () => {/*...*/})
```

Output:

```js
_test_gate(ctx => ctx.a && (ctx.b || ctx.c, 'some test'), () => {/*...*/});
```

It also works  with `it`, `it.only`, and `fit`. It leaves `it.skip` and
`xit` alone because those tests are disabled anyway.

`_test_gate` is a global method that I set up in our Jest config. It
works about the same as the existing `it.experimental` helper.

The context (`ctx`) argument is whatever we want it to be. I set it up
so that it throws if you try to access a flag that doesn't exist. I also
added some shortcuts for common gating conditions, like `old`
and `new`:

```js
// @gate experimental
test('experimental feature', () => {/*...*/})

// @gate new
test('only passes in new reconciler', () => {/*...*/})
```

Why implement this as a pragma instead of a runtime API?

- Doesn't require monkey patching built-in Jest methods. Instead it
  compiles to a runtime function that composes Jest's API.
- Will be easy to upgrade if Jest ever overhauls their API or we switch
  to a different testing framework (unlikely but who knows).
- It feels lightweight so hopefully people won't feel gross using it.
  For example, adding or removing a gate pragma will never affect the
  indentation of the test, unlike if you wrapped the test in a
  conditional block.

* Compatibility with console error/warning tracking

We patch console.error and console.warning to track unexpected calls
in our tests. If there's an unexpected call, we usually throw inside
an `afterEach` hook. However, that's too late for tests that we
expect to fail, because our `_test_gate` runtime can't capture the
error. So I also check for unexpected calls inside `_test_gate`.

* Move test flags to dedicated file

Added some instructions for how the flags are set up and how to
use them.

* Add dynamic version of gate API

Receives same flags as the pragma.

If we ever decide to revert the pragma, we can codemod them to use
this instead.
2020-04-13 10:14:34 -07:00
Sebastian Markbåge
72d00ab623 Fix Component Stacks for IE and Native Classes in Safari (#18575)
* Add more edge cases to fixture

Also adjust some expectations. I think the column should ideally be 1 but varies.
The Example row is one line off because it throws on the hook but should ideally be the component.
Similarly class components with constructors may have the line in the constructor.

* Account for the construct call taking a stack frame

We do this by first searching for the first different frame, then find
the same frames and then find the first different frame again.

* Throw controls

Otherwise they don't get a stack frame associated with them in IE.

* Protect against generating stacks failing

Errors while generating stacks will bubble to the root. Since this technique
is a bit sketchy, we should probably protect against it.

* Don't construct the thing that throws

Instead, we pass the prototype as the "this". It's new every time anyway.
2020-04-10 19:39:02 -07:00
Sebastian Markbåge
98d410f500 Build Component Stacks from Native Stack Frames (#18561)
* Implement component stack extraction hack

* Normalize errors in tests

This drops the requirement to include owner to pass the test.

* Special case tests

* Add destructuring to force toObject which throws before the side-effects

This ensures that we don't double call yieldValue or advanceTime in tests.

Ideally we could use empty destructuring but ES lint doesn't like it.

* Cache the result in DEV

In DEV it's somewhat likely that we'll see many logs that add component
stacks. This could be slow so we cache the results of previous components.

* Fixture

* Add Reflect to lint

* Log if out of range.

* Fix special case when the function call throws in V8

In V8 we need to ignore the first line. Normally we would never get there
because the stacks would differ before that, but the stacks are the same if
we end up throwing at the same place as the control.
2020-04-10 13:32:12 -07:00
Brian Vaughn
8e13f099ab Overhauled release scripts and docs (#18569)
* Overhauled release scripts and docs
2020-04-10 12:28:40 -07:00
Andrew Clark
af1b039bdd ESLint rule to forbid cross fork imports (#18568)
Modules that belong to one fork should not import modules that belong to
the other fork.

Helps make sure you correctly update imports when syncing changes across
implementations.

Also could help protect against code size regressions that might happen
if one of the forks accidentally depends on two copies of the same
module.
2020-04-09 18:11:34 -07:00
Sebastian Markbåge
50bdd75a60 Bubble errors if processing the error itself errors (#18567)
If we've tried completing an incomplete boundary once and failed, we don't
need to try again.
2020-04-09 17:40:11 -07:00
Sebastian Markbåge
348ed0e93f Don't return from perform/completeUnitOfWork (#18566)
* Remove unnecessary workInProgress line

* Mutate workInProgress instead of returning

We were ambivalent about this before.

* Make handleError a void method too
2020-04-09 17:12:04 -07:00
Andrew Clark
b04c7fa28c Decouple expiration times and transition timeouts (#17920)
We currently use the expiration time to represent the timeout of a
transition. Since we intend to stop treating work priority as a
timeline, we can no longer use this trick.

In this commit, I've changed it to store the event time on the update
object instead. Long term, we will store event time on the root as a map
of transition -> event time. I'm only storing it on the update object
as a temporary workaround to unblock the rest of the changes.
2020-04-09 15:09:36 -07:00
Andrew Clark
dc630d3374 Fork ReactFiberExpirationTime
I had thought I wouldn't fork this one because the new "lanes" module
will be pretty different, but I need it to make some
incremental changes.
2020-04-09 15:09:34 -07:00
Dan Abramov
e5d06e34b6 Revert "Clear more Fiber fields in detachFiber (#18556)" (#18562)
This reverts commit d48dbb8249.
2020-04-09 20:45:02 +01:00
Sebastian Markbåge
cbab25bb51 Exclude forwardRef and memo from stack frames (#18559)
We can't patch the row. We could give these their own "built-in" stack
frame since they're conceptually HoCs. However, from a debugging
perspective this is not very useful meta data and quite noisy. So I'm
just going to exclude them.
2020-04-09 11:42:22 -07:00
Andrew Clark
26fc16484e Script for syncing changes between forks (#18550)
Adds command `yarn merge-fork`.

```sh
yarn merge-fork --base-dir=packages/react-reconciler/src ReactFiberWorkLoop
```

This will take all the changes in `ReactFiberWorkLoop.new.js` and apply
them to `ReactFiberWorkLoop.old.js`.

You can merge multiple modules at a time:

```sh
yarn merge-fork \
  --base-dir=packages/react-reconciler/src \
  ReactFiberWorkLoop \
  ReactFiberBeginWork \
  ReactFiberCompleteWork \
  ReactFiberCommitWork
```

You can provide explicit "old" and "new" file names. This only works
for one module at a time:

```sh
yarn merge-fork \
  --base-dir=packages/react-reconciler/src \
  --old=ReactFiberExpirationTime.js \
  --new=ReactFiberLane.js
```

The default is to merge changes from the new module to the old one. To
merge changes in the opposite direction, use `--reverse`.

```sh
yarn merge-fork \
  --reverse \
  --base-dir=packages/react-reconciler/src \
  ReactFiberWorkLoop
```

By default, the changes are compared to HEAD. You can use `--base-ref`
to compare to any rev. For example, while working on a PR, you might
make multiple commits to the new fork before you're ready to backport
them to the old one. In that case, you want to compare to the merge
base of your PR branch:

```sh
yarn merge-fork \
  --base-ref=$(git merge-base HEAD origin/master)
  --base-dir=packages/react-reconciler/src \
  ReactFiberWorkLoop
```
2020-04-09 11:37:13 -07:00
Sebastian Markbåge
11ac10b44e Port error boundaries test to yieldValue (#18558) 2020-04-09 11:13:01 -07:00
Dominic Gannaway
d48dbb8249 Clear more Fiber fields in detachFiber (#18556) 2020-04-09 18:31:34 +01:00
Dominic Gannaway
0566b706ee Fix fiber memory leak with runAllPassiveEffectDestroysBeforeCreates (#18554) 2020-04-09 15:35:07 +01:00
Dominic Gannaway
ca1a703d21 Make enableLegacyFBSupport flag dynamic for www (#18551) 2020-04-09 15:31:36 +01:00
Dominic Gannaway
8cbce05be1 Move plugins into their own directory (#18553) 2020-04-09 14:18:33 +01:00
Dan Abramov
c74f0b0646 Update stale.yml 2020-04-09 13:36:11 +01:00
Andrew Clark
4c6470cb3b Point ReactDOMForked to the new implementation
Updates Rollup, Jest, and Flow configuration to point to the new
entry points.
2020-04-09 00:03:35 -07:00
Andrew Clark
17f582e045 Add forked copies of reconciler modules
All changes in this commit were generated by the following commands.

Copy each module that ends with `.old` to a new file that ends
with `.new`:

```sh
for f in packages/react-reconciler/src/*.old.js; do cp "$f" "$(echo "$f" | sed s/\.old/\.new/)"; done
```

Then transform the internal imports:

```sh
grep -rl --include="*.new.js" '.old' packages/react-reconciler/src/| xargs sed -i '' "s/\.old\'/\.new\'/g"
```
2020-04-09 00:03:35 -07:00
Andrew Clark
376d5c1b5a Split cross-package types from implementation
Some of our internal reconciler types have leaked into other packages.
Usually, these types are treated as opaque; we don't read and write
to its fields. This is good.

However, the type is often passed back to a reconciler method. For
example, React DOM creates a FiberRoot with `createContainer`, then
passes that root to `updateContainer`. It doesn't do anything with the
root except pass it through, but because `updateContainer` expects a
full FiberRoot, React DOM is still coupled to all its fields.

I don't know if there's an idiomatic way to handle this in Flow. Opaque
types are simlar, but those only work within a single file. AFAIK,
there's no way to use a package as the boundary for opaqueness.

The immediate problem this presents is that the reconciler refactor will
involve changes to our internal data structures. I don't want to have to
fork every single package that happens to pass through a Fiber or
FiberRoot, or access any one of its fields. So my current plan is to
share the same Flow type across both forks. The shared type will be a
superset of each implementation's type, e.g. Fiber will have both an
`expirationTime` field and a `lanes` field. The implementations will
diverge, but not the types.

To do this, I lifted the type definitions into a separate module.
2020-04-08 23:49:23 -07:00
Andrew Clark
d686f3f16a Add .old prefix to reconciler modules 2020-04-08 23:49:19 -07:00
Sebastian Markbåge
147bdef11b Port more tests to the Scheduler.unstable_yieldValue pattern and drop internal.js (#18549)
* Drop the .internal.js suffix on some files that don't need it anymore

* Port some ops patterns to scheduler yield

* Fix triangle test to avoid side-effects in constructor

* Move replaying of setState updaters until after the effect

Otherwise any warnings get silenced if they're deduped.

* Drop .internal.js in more files

* Don't check propTypes on a simple memo component unless it's lazy

Comparing the elementType doesn't work for this because it will never be
the same for a simple element.

This caused us to double validate these. This was covered up because in
internal tests this was deduped since they shared the prop types cache
but since we now inline it, it doesn't get deduped.
2020-04-08 20:54:54 -07:00
Sebastian Markbåge
b014e2d5e3 Don't use closures in DevTools injection (#18278)
* Don't use closures in DevTools injection

Nested closures are tricky. They're not super efficient and when they share
scope between multiple closures they're hard for a compiler to optimize.
It's also unclear how many versions will be created.

By hoisting things out an just make it simple calls the compiler can do
a much better job.

* Store injected hook to work around fast refresh
2020-04-08 17:57:17 -07:00
Sebastian Markbåge
5474a83e25 Disable console.logs in the second render pass of DEV mode double render (#18547)
* Disable console log during the second rerender

* Use the disabled log to avoid double yielding values in scheduler mock

* Reenable debugRenderPhaseSideEffectsForStrictMode in tests that can
2020-04-08 16:43:51 -07:00
Dan Abramov
b225d4f261 Revert "Revert "Refactor commitPlacement to recursively insert nodes (#17996)" (#18517)" (#18540)
This reverts commit e69ca310ea.
2020-04-08 11:22:02 +01:00
jddxf
241103a6fb Fix bailout broken in lazy components due to default props resolving (#18539)
* Add failing tests for lazy components

* Fix bailout broken in lazy components due to default props resolving

We should never compare unresolved props with resolved props. Since comparing
resolved props by reference doesn't make sense, we use unresolved props in that
case. Otherwise, resolved props are used.

* Avoid reassigning props warning when we bailout
2020-04-08 10:58:57 +01:00
Andrew Clark
2dddd1e00c Bugfix: Render phase update causes remaining updates in same component to be dropped (#18537)
* Bugfix: Render phase update leads to dropped work

Render phase updates should not affect the `fiber.expirationTime` field.
We don't have to set anything on the fiber because we're going to
process the render phase update immediately.

We also shouldn't reset the `expirationTime` field in between render
passes because it represents the remaining work left in the update
queues. During the re-render, the updates that were skipped in the
original pass are not processed again.

I think my original motivation for using this field for render phase
updates was so I didn't have to add another module level variable.

* Add repro case for #18486

Co-authored-by: Dan Abramov <dan.abramov@me.com>
2020-04-07 19:17:01 -07:00
Andrew Clark
2def7b3caf More robust fix for #18515 (#18535)
* Add another test for #18515 using pings

Adds a regression test for the same underlying bug as #18515 but using
pings.

Test already passes, but I confirmed it fails if you revert the fix
in #18515.

* Set nextPendingLevel after commit, too
2020-04-07 18:52:31 -07:00
Dominic Gannaway
948fad357b Improve detachFiber (#18536) 2020-04-08 01:45:23 +01:00
Carl Vitullo
03de849af0 Make uncontrolled -> controlled warning clearer (#17070)
* Make uncontrolled -> controlled warning clearer

* Update phrasing, mirror for opposite direction

* Remove unused substitution

* Update warning tests

* Literally got these backwards, womp womp

* Rerere-fix tests
2020-04-07 23:19:56 +01:00
jddxf
ddc4b65cfe Clear finished discrete updates during commit phase (#18515)
* Reproduce a bug where `flushDiscreteUpdates` causes fallback never to be committed

* Ping suspended level when canceling its timer

Make sure the suspended level is marked as pinged so that we return back
to it later, in case the render we're about to start gets aborted.
Generally we only reach this path via a ping, but we shouldn't assume
that will always be the case.

* Clear finished discrete updates during commit phase

If a root is finished at a priority lower than that of the latest pending discrete
updates on it, these updates must have been finished so we can clear them now.
Otherwise, a later call of `flushDiscreteUpdates` would start a new empty render
pass which may cause a scheduled timeout to be cancelled.

* Add TODO

Happened to find this while writing a test. A JSX element comparison
failed because one of them elements had a functional component as an
owner, which should ever happen.

I'll add a regression test later.

Co-authored-by: Andrew Clark <git@andrewclark.io>
2020-04-07 13:34:41 -07:00
Dominic Gannaway
d53a4dbbc2 Export unstable_useEvent for www FB ReactDOM builds (#18532) 2020-04-07 21:32:38 +01:00
Brian Vaughn
8edcd03b64 DevTools Profiler: Fix "cannot read property 'memoizedState' of null" (#18522) 2020-04-07 10:30:25 -07:00
Brian Vaughn
e8ac48f90b Fix whitespace problem wiht DevTools Components search (#18527) 2020-04-07 10:28:21 -07:00
Brian Vaughn
dc49ea108c Filter certain DOM attributes (e.g. src) if value is empty string (#18513)
* Filter certain DOM attributes (e.g. src, href) if their values are empty strings

This prevents e.g. <img src=""> from making an unnecessar HTTP request for certain browsers.

* Expanded warning recommendation

* Improved error message

* Further refined error message
2020-04-07 09:52:36 -07:00
Dan Abramov
bce982b799 Change stalebot messages (#18524) 2020-04-07 17:30:45 +01:00
Brian Vaughn
32621a354f Improved Profiler tooltip confusing/misleading wording (#18523) 2020-04-07 09:26:50 -07:00
Dan Abramov
e69ca310ea Revert "Refactor commitPlacement to recursively insert nodes (#17996)" (#18517)
This reverts commit df5faddcc2.
2020-04-07 13:41:51 +01:00
Luna Ruan
3278d24218 Add useOpaqueIdentifier Hook (#17322)
* Add useOpaqueIdentifier Hook

We currently use unique IDs in a lot of places. Examples are:
  * `<label for="ID">`
  * `aria-labelledby`

This can cause some issues:
  1. If we server side render and then hydrate, this could cause an
     hydration ID mismatch
  2. If we server side render one part of the page and client side
     render another part of the page, the ID for one part could be
     different than the ID for another part even though they are
     supposed to be the same
  3. If we conditionally render something with an ID ,  this might also
     cause an ID mismatch because the ID will be different on other
     parts of the page

This PR creates a new hook `useUniqueId` that generates a different
unique ID based on whether the hook was called on the server or client.
If the hook is called during hydration, it generates an opaque object
that will rerender the hook so that the IDs match.

Co-authored-by: Andrew Clark <git@andrewclark.io>
2020-04-06 17:17:27 -07:00
Sebastian Markbåge
4169420198 Refactor Component Stack Traces (#18495)
* Add feature flag

* Split stack from current fiber

You can get stack from any fiber, not just current.

* Refactor description of component frames

These should use fiber tags for switching. This also puts the relevant code
behind DEV flags.

* We no longer expose StrictMode in component stacks

They're not super useful and will go away later anyway.

* Update tests

Context is no longer part of SSR stacks. This was already the case on the
client.

forwardRef no longer is wrapped on the stack. It's still in getComponentName
but it's probably just noise in stacks. Eventually we'll remove the wrapper
so it'll go away anyway. If we use native stack frames they won't have this
extra wrapper.

It also doesn't pick up displayName from the outer wrapper. We could maybe
transfer it but this will also be fixed by removing the wrapper.

* Forward displayName onto the inner function for forwardRef and memo in DEV

This allows them to show up in stack traces.

I'm not doing this for lazy because lazy is supposed to be called on the
consuming side so you shouldn't assign it a name on that end. Especially
not one that mutates the inner.

* Use multiple instances of the fake component

We mutate the inner component for its name so we need multiple copies.
2020-04-06 15:43:39 -07:00
Dominic Gannaway
a3875663f6 React Event System: cleanup plugins + break out update batching logic (#18503) 2020-04-06 22:01:20 +01:00
dependabot[bot]
717a33abb9 Bump minimist from 1.2.0 to 1.2.3 in /scripts/bench (#18500)
Bumps [minimist](https://github.com/substack/minimist) from 1.2.0 to 1.2.3.
- [Release notes](https://github.com/substack/minimist/releases)
- [Commits](https://github.com/substack/minimist/compare/1.2.0...1.2.3)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-04-06 19:29:11 +01:00
dependabot[bot]
8c974a70c9 Bump minimist from 1.2.0 to 1.2.3 (#18501)
Bumps [minimist](https://github.com/substack/minimist) from 1.2.0 to 1.2.3.
- [Release notes](https://github.com/substack/minimist/releases)
- [Commits](https://github.com/substack/minimist/compare/1.2.0...1.2.3)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-04-06 19:28:56 +01:00
Brian Vaughn
3498f13669 Profiler tooltip shows self duration (#18510) 2020-04-06 09:47:15 -07:00
Faelivrinx
c781156163 Fix performance issue in react-devtools when highlight enabled (#18498)
* Fix performance issue in react-devtools when highlight enabled

* getting minimum expiration time (fix)
2020-04-06 16:17:45 +01:00
Sophie Alpert
fe2cb52554 [eslint] Consider useRef() as ... as constant (#18496)
Sometimes you need to use casts, eg: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/28884#issuecomment-471341041. This change ignores them and allows you to still omit the ref object from the deps list.

Test Plan: unit tests
2020-04-05 14:13:34 -07:00
Sebastian Markbåge
1fd45437d7 Don't use checkPropTypes for internals (#18488)
We use console.error for internal warnings.
2020-04-04 15:10:46 -07:00
Zen
2ff27ec112 [eslint] strip tailing property in assignments (#16784)
* [eslint] strip tailing property in assignments

* inline `stripTailingPropInAssignment`
2020-04-04 14:26:39 +01:00
Nick Reiley
f625fce857 Add KeyboardEvent.code to synthetic event (#18287)
* Add KeyboardEvent.code to synthetic event

* remove null to 0 transformation

* make onKeyPress work
2020-04-04 14:24:46 +01:00
Sebastian Markbåge
5022fdfd5f Refactor Error Dialog Logging (#18487)
* Remove unnecessary CapturedError fields.

componentName is not necessary and is misleading when the error is caused
elsewhere in the stack. The stack is sufficient.

The many error boundary fields are unnecessary because they can be inferred
by the boundary itself.

* Don't attempt to build a stack twice

If it was possible, it would've been done in createCapturedValue.

* Push the work needed by the works into the forks

This avoids needing this in the npm published case.
2020-04-03 19:01:40 -07:00
Sebastian Markbåge
e2dd30898e [Flight] Lazily parse models and allow any value to suspend (#18476)
* Lazily initialize models as they're read intead of eagerly when received

This ensures that we don't spend CPU cycles processing models that we're
not going to end up rendering.

This model will also allow us to suspend during this initialization if
data is not yet available to satisfy the model.

* Refactoring carefully to ensure bundles still compile to something optimal

* Remove generic from Response

The root model needs to be cast at one point or another same as othe
chunks. So we can parameterize the read instead of the whole Response.

* Read roots from the 0 key of the map

The special case to read the root isn't worth the field and code.

* Store response on each Chunk

Instead of storing it on the data tuple which is kind of dynamic, we store
it on each Chunk. This uses more memory. Especially compared to just making
initializeBlock a closure, but overall is simpler.

* Rename private fields to underscores

Response objects are exposed.

* Encode server components as delayed references

This allows us to stream in server components one after another over the
wire. It also allows parallelizing their fetches and resuming only the
server component instead of the whole parent block.

This doesn't yet allow us to suspend deeper while waiting on this content
because we don't have "lazy elements".
2020-04-03 14:58:02 -07:00
Sebastian Markbåge
59fd09cb67 [Flight] Add webpack plugin build (#18485)
* Eject CRA from Flight

We need to eject because we're going to add a custom Webpack Plugin.

We can undo this once the plugin has upstreamed into CRA.

* Add Webpack plugin build

I call this entry point "webpack-plugin" instead of "plugin" even though
this is a webpack specific package. That's because there will also be a
Node.js plugin to do the server transform.

* Add Flight Webpack plugin to fixture

* Rm UMD builds

* Transform classes

* Rename webpack-plugin to plugin

This avoids the double webpack name. We're going to reuse this for both
server and client.
2020-04-03 14:04:56 -07:00
Luna Ruan
a876808f0a remove jsx plugin from react (#18484)
Now that the React JSX Babel Plugin is in Babel, remove this code from react
2020-04-03 13:00:50 -07:00
Hristo Kanchev
7785a5263e [DevTools] - Highlight rendered by elements on hover. (#18479)
* [DevTools] - Highlight rendered by elements on hover.

* Fixed formatting issue.

* DevTools - Extracted highlight logic to custom hook. Added highlight support for rendered by elements.

* Removed unnecessary padding style

* Removed unnecessary wrapper function.

Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-04-03 10:49:49 -07:00
Brian Vaughn
f312a3fc36 useMutableSource: bugfix for new getSnapshot with mutation (#18297) 2020-04-03 10:20:17 -07:00
Dan Abramov
a8f2165e83 Update to Jest 25 (#18480)
* Revert "Revert "Upgrade to jest 25 (#17896)" (#18376)"

This reverts commit fc7835c657.

* Other fixes

* Fix a broken test
2020-04-03 16:37:36 +01:00
Nick Reiley
c083a643b1 Add <progress> to DOM fixtures (#18293)
* Add <progress> to DOM fixtures

* Remove uncontrolled
2020-04-03 14:39:59 +01:00
Sebastian Markbåge
3c16baf848 Remove /dist/ UMD builds (#18473)
* Remove /dist/ UMD builds

We publish UMDs to npm (and we're considering stopping even that).

This means we'll stop publishing to http://react.zpao.com/builds/master/latest/

* Update fixture paths
2020-04-02 17:52:32 -07:00
Dominic Gannaway
91c9d69f9a Modern Event System: Remove TestUtils.SimulateNative support (#18471) 2020-04-03 00:12:47 +01:00
Dan Abramov
e55855e7ae Deprecate TestUtils.SimulateNative (#13407) 2020-04-02 23:48:03 +01:00
Brian Vaughn
4123d729e5 Revert "Revert: Enable new passive effect behavior for FB builds (#18467)" (#18468)
This reverts commit 3966081cf2.
2020-04-02 15:24:36 -07:00
Dominic Gannaway
8815e4cc72 Cleanup getListener and EventSystemFlags (#18469) 2020-04-02 23:17:10 +01:00
Dominic Gannaway
5e464546af ReactDOM.useEvent: fix scope propagation issue (#18464) 2020-04-02 18:26:56 +01:00
Dominic Gannaway
d5e4b3ae1d Modern Event System: refine flags and handling of enableLegacyFBSupport (#18466) 2020-04-02 18:26:34 +01:00
Dominic Gannaway
3966081cf2 Revert: Enable new passive effect behavior for FB builds (#18467) 2020-04-02 16:25:35 +01:00
Dan Abramov
7dfdff42af Run more flags in VARIANT tests (#18461)
* Run more flags in VARIANT tests

* Revert enabling modern system

* Fix
2020-04-02 16:19:09 +01:00
Dan Abramov
31734540dc Remove a flag for style collision warning (#18462) 2020-04-02 11:55:17 +01:00
Dominic Gannaway
663c13d71d Refactor Enter/Leave listener accumulation (#18405)
* Refactor Enter/Leave listener accumulation
2020-04-02 11:07:20 +01:00
Dan Abramov
c80cd8ee27 Revert "Fix email cursor jump (#18379)" (#18459)
* Revert "Fix email cursor jump (#18379)"

This reverts commit 9b88b78b3d.

* Leave fixtures be
2020-04-01 22:44:59 +01:00
Andrew Clark
58afba0663 Fix: Don't read primaryChild.childExpirationTime (#18457)
This is a variant of the fix in 5a0f1d. We can't rely on the primary
fiber's `childExpirationTime` field to be correct.

In this case, we can read from the Suspense boundary fiber instead.
This will include updates that exist in the fallback fiber, but that's
not a big deal; the important thing is that we don't drop updates.
2020-04-01 14:36:42 -07:00
Brian Vaughn
2ea7c60794 Fixed race condition in release script (#18456) 2020-04-01 14:17:55 -07:00
Dan Abramov
99d779f629 Don't close "Needs Investigation" issues (#18458) 2020-04-01 22:08:09 +01:00
Gabriel McAdams
3f46844899 [eslint-plugin-react-hooks] Added meta property (including docs) (#16607)
* [eslint-plugin-react-hooks] Added meta property (including docs)

* [eslint-plugin-react-hooks] Fixed typo (`RuleOfHooks` -> `RulesOfHooks`)

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2020-04-01 21:13:10 +01:00
Dan Abramov
2bf60d9f9c Fix ESLint rule crash (#18455) 2020-04-01 20:54:42 +01:00
Sebastian Markbåge
3e94bce765 Enable prefer-const lint rules (#18451)
* Enable prefer-const rule

Stylistically I don't like this but Closure Compiler takes advantage of
this information.

* Auto-fix lints

* Manually fix the remaining callsites
2020-04-01 12:35:52 -07:00
Sebastian Markbåge
e6ea3d3873 Use Closure Compiler to compile to ES5 instead of Babel (#18449)
* Upgrade Closure

There are newer versions but they don't yet have corresponding releases
of google-closure-compiler-osx.

* Configure build

* Refactor ReactSymbols a bit

Provides a little better output.
2020-04-01 12:08:37 -07:00
Utkarsh Kukreti
5200547100 Do not warn when a controlled input has onInput handler. (#18189)
`onInput` behaves the same as `onChange` for controlled inputs as far as I
know, so React should not print the following warning when `onInput` is
present.

> Failed prop type: You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.
2020-04-01 19:58:07 +01:00
Sebastian Silbermann
7516bdfce3 feat(createContext): Include displayName in warning (#18386) 2020-04-01 19:42:38 +01:00
Nick Reiley
9b88b78b3d Fix email cursor jump (#18379)
* add email input fixture to show cursor jump

* fix cursor jump in email input

Co-authored-by: Peter Potapov <dr.potapoff-peter@yandex.ru>

* add regression tests to ensure attributes are working

Co-authored-by: Peter Potapov <dr.potapoff-peter@yandex.ru>
2020-04-01 19:30:26 +01:00
Brian Vaughn
5ee0efe832 Remove state update warning for passive effect cleanup functions (#18453) 2020-04-01 10:49:24 -07:00
Dan Abramov
d8d2b6e89c Disable module components dynamically for WWW (#18446)
* Make disableModulePatternComponents dynamic for WWW

* Run both flags and tests and respect the flag in SSR
2020-04-01 18:31:59 +01:00
Sebastian Markbåge
e2c6702fca Remove ConcurrentMode and AsyncMode symbols (#18450)
This API was never released.
2020-04-01 10:18:52 -07:00
Dominic Gannaway
d6d5f5aabe Move accumulateEventTargetListeners to its own module/function (#18407) 2020-04-01 14:19:21 +01:00
Dominic Gannaway
dc3c6c9565 ReactDOM.useEvent: revert and add guard for null stateNode (#18441) 2020-04-01 12:45:26 +01:00
Sebastian Markbåge
0f334553c9 Reset stateNode in resetWorkInProgress (#18448)
* test(SuspenseList): Add failing test for class component

* Reset stateNode when resetWorkInProgress

This is supposed to put the Fiber into the same state as if it was just
created by child fiber reconciliation. For newly created fibers, that means
that stateNode is null.

Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
2020-03-31 21:14:59 -07:00
Brian Vaughn
153b5c305d Cleanup previous shims directories before re-copying (#18447) 2020-03-31 12:22:02 -07:00
Brian Vaughn
4de3a60325 Remove disableMapsAsChildren flag (#18445)
Change warning to say the case is unsupported (not "will be deprecated")
2020-03-31 11:00:51 -07:00
Brian Vaughn
f4cc970276 Enable new passive effect behavior for FB builds (#18444)
* Enable new passive effect behavior for FB builds

Previously this behavior was controlled by GKs. This PR updates the flags to be enabled statically. It also enables the flags in the test builds.
2020-03-31 10:05:15 -07:00
Dan Abramov
9065e02d66 Fix a warning typo (#18443) 2020-03-31 15:55:30 +01:00
Dan Abramov
1960131f11 Add opt-in support for dangerous autofix (#18437) 2020-03-31 11:43:01 +01:00
Andrew Clark
90e90ac8e0 Revert useEvent PRs (#18438)
* Revert "ReactDOM.useEvent: enable on internal www and add inspection test (#18395)"

This reverts commit e0ab1a429d.

* Revert "ReactDOM.useEvent: Add support for experimental scopes API (#18375)"

This reverts commit a16b349745.

* ReactDOM.useEvent: Add support for experimental scopes API
2020-03-30 19:16:28 -07:00
Dan Abramov
da54641a10 [ESLint] Check deps when callback body is outside the Hook call, too (#18435)
* Refactor: visit CallExpression

Instead of visiting the functions and looking up to see if they're in a Hook call, visit Hook calls and look down to see if there's a callback inside. I will need this refactor so I can visit functions declared outside the call.

* Check deps when callback body is outside the Hook call

* Handle the unknown case
2020-03-31 02:09:32 +01:00
Dan Abramov
bf30e370a5 Remove User Timings (#18417) 2020-03-31 00:29:53 +01:00
Ricky
dd7e5e4f5a Add getInspectorDataForViewAtPoint (take two) (#18388)
* Add getInspectorDataForViewAtPoint (take two)

* Updates from review

* Add DEV to dev-only variable

* Missed this rename
2020-03-30 15:42:41 -04:00
Andrew Clark
d7382b6c43 Bugfix: Do not unhide a suspended tree without finishing the suspended update (#18411)
* Bugfix: Suspended update must finish to unhide

When we commit a fallback, we cannot unhide the content without including
the level that originally suspended. That's because the work at level
outside the boundary (i.e. everything that wasn't hidden during that
render) already committed.

* Test unblocking with a high-pri update
2020-03-30 11:25:04 -07:00
Dan Abramov
1f8c40451a Make interaction tracing on by default in all WWW builds (#18419) 2020-03-30 16:07:58 +01:00
Sebastian Silbermann
ba31ad40a9 feat(StrictMode): Double-invoke render for every component (#18430)
* feat(StrictMode): Double-invoke render for every component

* fix: Mark ReactTestRendererAsync as internal
2020-03-29 23:13:46 +01:00
Sebastian Markbåge
689d27586e Reset lastEffect when resuming SuspenseList (#18412)
We store an effect pointer so we can backtrack in the effect list in some
cases. This is a stateful variable. If we interrupt a render we need to
reset it.

This field was added after the optimization was added and I didn't remember
to reset it here.

Otherwise we end up not resetting the firstEffect so it points to a stale
list. As a result children don't end up inserted like we think they were.
Then we try to remove them it errors.

It would be nicer to just get rid of the effect list and use the tree for
effects instead. Maybe we still need something for deletions tho.
2020-03-29 10:13:17 -07:00
Rodrigo Graça
1af2a10891 https link to editorconfig.org (#18421) 2020-03-29 15:18:52 +01:00
zefeng
d7918f4a9b chore: npm link more directly (#18428) 2020-03-29 15:18:15 +01:00
Brian Vaughn
be4c8b19c1 Don't show destroy function state update warning when updating ancestors (#18409)
React can't directly detect a memory leak, but there are some clues that warn about one. One of these clues is when an unmounted React component tries to update its state. For example, if a component forgets to remove an event listener when unmounting, that listener may be called later and try to update state, at which point React would warn about the potential leak.

Warning signals like this are more useful if they're strong. For this reason, it's good to always avoid updating state from inside of an effect's cleanup function. Even when you know there is no potential leak, React has no way to know and so it will warn anyway.

In most cases we suggest moving state updates to the useEffect() body instead (to avoid triggering the warning). This works so long as the component is updating its own state (or the state of a descendant). However this will not work when a component updates its parent state in a cleanup function. If such a component is unmounted but its parent remains mounted, the state will be incorrect. For this reason, we now avoid showing the warning if a component is updating an ancestor.
2020-03-28 10:18:14 -07:00
Andrew Clark
35a2f74974 Delete leftover assignment from #18384
This assignment should have been deleted in #18384. It was deleted in
the other branches, but I missed this one. About to open a PR that
includes a test that covers this branch.
2020-03-27 15:17:30 -07:00
Andrew Clark
9d67847f7b [Bugfix] Dropped updates inside a suspended tree (#18384)
* Minor test refactor: `resolveText`

Adds a `resolveText` method as an alternative to using timers. Also
removes dependency on react-cache (for just this one test file; can do
the others later).

Timer option is still there if you provide a `ms` prop.

* Bugfix: Dropped updates in suspended tree

When there are multiple updates at different priority levels inside
a suspended subtree, all but the highest priority one is dropped after
the highest one suspends.

We do have tests that cover this for updates that originate outside of
the Suspense boundary, but not for updates that originate inside.

I'm surprised it's taken us this long to find this issue, but it makes
sense in that transition updates usually originate outside the boundary
or "seam" of the part of the UI that is transitioning.

* Bugfix: Suspense fragment skipped by setState

Fixes a bug where updates inside a suspended tree are dropped because
the fragment fiber we insert to wrap the hidden children is not part of
the return path, so it doesn't get marked during setState.

As a workaround, I recompute `childExpirationTime` right before deciding
to bail out by bubbling it up from the next level of children.

This is something we should consider addressing when we refactor the
Fiber data structure.

* Add back `lastPendingTime` field

This reverts commit 9a541139dfe36e8b9b02b1c6585889e2abf97389.

I want to use this so we can check if there might be any lower priority
updates in a suspended tree.

We can remove it again during the expiration times refactor.

* Use `lastPendingTime` instead of Idle

We don't currently have an mechanism to check if there are lower
priority updates in a subtree, but we can check if there are any in the
whole root. This still isn't perfect but it's better than using Idle,
which frequently leads to redundant re-renders.

When we refactor `expirationTime` to be a bitmask, this will no longer
be necessary because we'll know exactly which "task bits" remain.

* Add a test for updating the fallback
2020-03-26 11:31:40 -07:00
Dan Abramov
5bd1bc29b3 eslint-plugin-react-hooks@3.0.0 2020-03-26 17:59:25 +00:00
Dominic Gannaway
e0ab1a429d ReactDOM.useEvent: enable on internal www and add inspection test (#18395) 2020-03-26 17:13:05 +00:00
Brian Vaughn
6cceaeb67a DevTools v4.5.0 -> 4.6.0 2020-03-26 10:00:37 -07:00
Brian Vaughn
d92631eab1 Remove "es6-symbol" package from DevTools (#18397) 2020-03-26 09:31:36 -07:00
Dominic Gannaway
a16b349745 ReactDOM.useEvent: Add support for experimental scopes API (#18375)
* ReactDOM.useEvent: Add support for experimental scopes API
2020-03-26 13:29:54 +00:00
Sebastian Markbåge
dbb060d561 Pass BundlerConfig through to Relay Integration (#18393)
I wasn't sure we needed this but looks like it'll come in handy.
2020-03-25 20:25:54 -07:00
Sebastian Markbåge
ce6fe50b01 Add server-runtime to create Server Blocks (#18392)
This is equivalent to the jsx-runtime in that this is what the compiled
output on the server is supposed to target.

It's really just the same code for all the different Flights, but they
have different types in their arguments so each one gets their own entry
point. We might use this to add runtime warnings per entry point.

Unlike the client-side React.block call this doesn't provide the factory
function that curries the load function. The compiler is expected to wrap
this call in the currying factory.
2020-03-25 19:03:31 -07:00
Sebastian Markbåge
64ed221c3d Formalize the Wakeable and Thenable types (#18391)
* Formalize the Wakeable and Thenable types

We use two subsets of Promises throughout React APIs. This introduces
the smallest subset - Wakeable. It's the thing that you can throw to
suspend. It's something that can ping.

I also use a shared type for Thenable in the cases where we expect a value
so we can be a bit more rigid with our us of them.

* Make Chunks into Wakeables instead of using native Promises

This value is just going from here to React so we can keep it a lighter
abstraction throughout.

* Renamed thenable to wakeable in variable names
2020-03-25 16:49:37 -07:00
Sebastian Markbåge
a6924d77b1 Change .model getter to .readRoot method (#18382)
Originally the idea was to hide all suspending behind getters or proxies.
However, this has some issues with perf on hot code like React elements.

It also makes it too easy to accidentally access it the first time in an
effect or callback where things aren't allowed to suspend. Making it
an explicit method call avoids this issue.

All other suspending has moved to explicit lazy blocks (and soon elements).
The only thing remaining is the root. We could require the root to be an
element or block but that creates an unfortunate indirection unnecessarily.

Instead, I expose a readRoot method on the response. Typically we try to
avoid virtual dispatch but in this case, it's meant that you build
abstractions on top of a Flight response so passing it a round is useful.
2020-03-25 11:47:55 -07:00
Brian Vaughn
bd5781962a Inlined DevTools event emitter impl (#18378)
DevTools previously used the NPM events package for dispatching events. This package has an unfortunate flaw though- if a listener throws during event dispatch, no subsequent listeners are called. I've replaced that event dispatcher with my own implementation that ensures all listeners are called before it re-throws an error.

This commit replaces that event emitter with a custom implementation that calls all listeners before re-throwing an error.
2020-03-25 10:26:40 -07:00
Dan Abramov
6498f62edc Fix a mistake in ReactChildren refactor (#18380)
* Regression test for map() returning an array

* Add forgotten argument

This fixes the bug.

* Remove unused arg and retval

These aren't directly observable. The arg wasn't used, it's accidental and I forgot to remove. The retval was triggering a codepath that was unnecessary (pushing to array) so I removed that too.

* Flowify ReactChildren

* Tighten up types

* Rename getComponentKey to getElementKey
2020-03-25 09:20:46 +00:00
Dan Abramov
2ba43edc26 Rename internal fields (#18377) 2020-03-24 18:19:20 +00:00
Sebastian Markbåge
a317bd033f Flip the arguments of Blocks and make the query optional (#18374)
* Flip the arguments of Blocks and make the query optional

* Rename Query to Load
2020-03-24 10:58:23 -07:00
Andrew Clark
fc7835c657 Revert "Upgrade to jest 25 (#17896)" (#18376)
This reverts commit cf0081263c.

The changes to the test code relate to changes in JSDOM that come with Jest 25:

* Several JSDOM workarounds are no longer needed.
* Several tests made assertions to match incorrect JSDOM behavior (e.g. setAttribute calls) that JSDOM has now patched to match browsers.
  * https://codesandbox.io/s/resets-value-of-datetime-input-to-fix-bugs-in-ios-safari-1ppwh
* JSDOM no longer triggers default actions when dispatching click events.
  * https://codesandbox.io/s/beautiful-cdn-ugn8f
* JSDOM fixed (jsdom/jsdom#2700) a bug so that calling focus() on an already focused element does not dispatch a FocusEvent.
* JSDOM now supports passive events.
* JSDOM has improved support for custom CSS properties.
  * But requires jsdom/cssstyle#112 to land to support webkit prefixed properties.
2020-03-24 10:51:48 -07:00
Dominic Gannaway
0140118e8e ReactDOM.useEvent: add support for beforeblur/afterblur (#18370)
* ReactDOM.useEvent: add support for beforeblur/afterblur
2020-03-24 16:14:55 +00:00
Sebastian Markbåge
a56309fb88 [Flight] Integrate Blocks into Flight (#18371)
* Resolve Server-side Blocks instead of Components

React elements should no longer be used to extract arbitrary data but only
for prerendering trees.

Blocks are used to create asynchronous behavior.

* Resolve Blocks in the Client

* Tests

* Bug fix relay JSON traversal

It's supposed to pass the original object and not the new one.

* Lint

* Move Noop Module Test Helpers to top level entry points

This module has shared state. It needs to be external from builds.

This lets us test the built versions of the Noop renderer.
2020-03-23 17:53:45 -07:00
Dan Abramov
fc96a52be3 Refactor React.Children to reduce indirection (#18332)
* Don't pool traversal context

* Remove traverseAllChildrenImpl indirection

All usages are internal so we can simply use the inner function directly.

* Implement forEach through map

* Remove second usage of traverseAllChildren

This isn't useful by itself but makes the layering easier to follow. traverseAllChildren is only used at the lowest layer now.

* Reimplement count() and toArray() in terms of map()

* Inline the only use of mapSingleChildIntoContext

* Move forEach down in the file

* Use the language

Get rid of the traversal context. Use closures.

* Make mapIntoArray take an already escaped prefix

* Move count state out of mapIntoArray

* Inline traverseAllChildren into mapIntoArray

* Inline handleChild into mapIntoArray
2020-03-23 22:56:00 +00:00
Sebastian Markbåge
fd61f7ea53 Refactor Lazy Components to use teh Suspense (and wrap Blocks in Lazy) (#18362)
* Refactor Lazy Components

* Switch Blocks to using a Lazy component wrapper

Then resolve to a true Block inside.

* Test component names of lazy Blocks
2020-03-22 21:53:05 -07:00
Mohammad Aziz
31a9e391f7 Remove unnecessary lines for hasBadMapPolyfill issue for rollup (#16231) 2020-03-22 14:52:59 +00:00
Sebastian Markbåge
c0cd1be908 [Flight] Move bundler configs to use Suspense instead of returning thenable (#18367)
* Move bundler configs to use suspense instead of returning thenable

* Fix some Flow types
2020-03-21 22:28:36 -07:00
Sebastian Markbåge
4b7f8496a8 Rename CI jobs after the command line equivalent (#18364)
This makes it easier to know what to write on your command line to replicate
a failure locally.
2020-03-21 16:02:01 -07:00
Sebastian Markbåge
b779dd51e8 Stop syncing ReactTypes to RN (#18366)
This is a really old one and all callers have since been codemodded away
anyway because of problems.

This file is not really as rigorously maintained as the official Flow types
but has a few more specifics. However, the inconsistency causes problems
when you try to pass files typed using the built-in Flow typing for React
and mix it with these.

We just happen to get away with it because we compile out the types. If we
didn't we would hit those problems by even using these in our renderers.
2020-03-21 15:22:40 -07:00
Sebastian Markbåge
c5d2fc7127 Move some files out of /shared and rename to upper case (#18363)
* Rename lower case isomorphic default exports modules to upper case named exports

We're somewhat inconsistent here between e.g. ReactLazy and memo.

Let's pick one.

This also moves the responder, fundamental, scope creators from shared
since they're isomorphic and same as the other creators.

* Move some files that are specific to the react-reconciler from shared

Individual renderers are allowed to deep require into the reconciler.

* Move files specific to react-dom from shared

react-interactions is right now dom specific (it wasn't before) so we can
type check it together with other dom stuff. Avoids the need for
a shared ReactDOMTypes to be checked by RN for example.

* Move ReactWorkTags to the reconciler

* Move createPortal to export from reconciler

Otherwise Noop can't access it since it's not allowed deep requires.
2020-03-21 15:22:01 -07:00
Dominic Gannaway
a600408b28 ReactDOM.useEvent: add EventTarget support (#18355)
* ReactDOM.useEvent: add support for all EventTarget types
2020-03-20 18:31:47 +00:00
Dominic Gannaway
dbd85a08d9 ReactDOM.useEvent: support custom types (#18351)
* ReactDOM.useEvent: support custom types
2020-03-20 14:45:33 +00:00
Luna Ruan
7c1478680f fix string ref cannot be auto converted warning for React.jsxDEV (#18354)
The string ref cannot be auto converted warning was using the wrong _self. This diff fixes this so it is now using the correct __self
2020-03-19 23:47:53 -07:00
Andrew Clark
f198872716 Empty commit to fix master.
I accidentally did that thing again where I updated a PR branch to
be the same as the tip of master, which confused GitHub and caused it
to run PR checks against master.
2020-03-19 17:53:09 -07:00
Sebastian Markbåge
fc91508c1f Follow ups to bundler configs (#18352)
Follow ups from https://github.com/facebook/react/pull/18334

I also introduced the concept of a module reference on the client too.
We don't need this for webpack so that gets compiled out but we need it
for www. Similarly I also need a difference between preload and load.
2020-03-19 17:49:40 -07:00
Andrew Clark
31e6756b8c Remove error code step from publish script (#18350)
Error codes don't need to be pulled from CI anymore because the ones
in source are already expected to match the build output.

I noticed this when running the 16.13.1 release. Patch releases are cut
with the commit used to build the previous release as a base. So the
publish script accidentally reverted the changes that had landed to
the error codes file since then.
2020-03-19 14:45:07 -07:00
Dominic Gannaway
9e7f334c71 ModernEventSystem: fix event replaying (#18346)
* ModernEventSystem: fix event replaying
2020-03-19 20:44:38 +00:00
Dominic Gannaway
ffefb4e77f ModernEventSystem: refine Flow types (#18349) 2020-03-19 20:25:52 +00:00
Andrew Clark
6818a2aa01 Revert accidental changes to package.json (#18348)
The publish script was written before we switched to running patch
releases out-of-band, so when updating the local package.json version
numbers, it accidentally reverted other changes that have landed to
master since 16.13 was released.
2020-03-19 13:16:37 -07:00
Andrew Clark
ad445b127e Update package.jsons for 16.13.1 patch relase 2020-03-19 12:58:31 -07:00
Andrew Clark
8b48d52fb4 Changelog for 16.13.1 2020-03-19 12:36:34 -07:00
Dominic Gannaway
8311cb5d24 Modern Event System: refactor legacy FB support logic (#18336) 2020-03-19 13:22:36 +00:00
Dan Abramov
1cf4a170ac Stop exposing ReactDOMComponentTree from FB builds (#18338) 2020-03-18 20:35:58 +00:00
Ian Obermiller
d3368beeec [eslint-plugin-react-hooks] Disallow hooks in class components (#18341)
Per discussion at Facebook, we think hooks have reached a tipping point where it is more valuable to lint against potential hooks in classes than to worry about false positives.

Test plan:
```
# run from repo root
yarn test --watch RuleOfHooks
```
2020-03-18 19:55:13 +00:00
Sebastian Markbåge
8206b4b864 Wire up bundler configs (#18334)
This allows different flight server and clients to have different configs
depending on bundler to serialize and resolve modules.
2020-03-18 12:18:34 -07:00
Kerollos Magdy
6b7281ec14 [DevTools] Add shortcut keys for tab switching (#18248)
* [DevTools] Add shortcut keys for tab switching
* Use LocalStorage to remember most recently selected tab

Resolves #18227 and #18226

Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-03-18 11:05:41 -07:00
ItsByteMe
5152c4a9fd Fixed inconcistency with surrounding punctuation (#18339)
There was an inconsistency present on line 99 regarding the punctuation of the comment, all other comments found end in a period and this line had it's period omitted.
2020-03-18 17:10:32 +00:00
Sebastian Markbåge
94505b9613 Don't use EventListener Fork in Modern WWW Builds (#18333)
* Move unsubscribe fork to EventListener

That way we can statically compile out more of these indirections.

* Don't use the EventListener fork for Modern WWW builds
2020-03-18 13:13:50 +00:00
Luna Ruan
aae83a4b9a Fix UMD Builds (ReactSharedInternals)
ReactCurrentBatchConfig.suspense does not exist in ReactSharedInternals.umd. This PR adds it.
2020-03-17 17:56:14 -07:00
Dominic Gannaway
7d466bcc25 React Event System: Refactor ElementListenerMap for upgrading (#18308) 2020-03-18 00:15:50 +00:00
Dan Abramov
fe1f79b95b Don't fire the render phase update warning for class lifecycles (#18330)
* Change the warning to not say "function body"

This warning is more generic and may happen with class components too.

* Dedupe by the rendering component

* Don't warn outside of render
2020-03-18 00:07:14 +00:00
Sebastian Silbermann
22cab1cbd6 test(getComponentName): Increase test coverage (#18149)
Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-03-17 14:47:24 -07:00
Sebastian Silbermann
756e1ea5d4 fix(react-devtools-shared): useDebugValue with complex types (#18070)
Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
2020-03-17 13:57:01 -07:00
Luna Ruan
90f8fe6f55 add jsx-runtime and jsx-dev-runtime (#18299)
This PR adds the jsx-runtime and jsx-dev-runtime modules for the JSX Babel Plugin. WWW still relies on jsx/jsxs/jsxDEV from the "react" module, so once we refactor the code to point to the runtime modules we will remove jsx/jsxs/jsxDEV from the "react" module.
2020-03-17 13:22:19 -07:00
Dan Abramov
26666427d6 Don't support older isomorphic React with newer renderers (#18329)
* Don't support older isomorphic React with newer renderers

* Remove the lazy hack
2020-03-17 19:30:10 +00:00
Dominic Gannaway
3a0076e0e8 useEvent: Small tweaks and changes (#18328) 2020-03-17 15:32:26 +00:00
Minh Nguyen
9240918536 Bump react-shallow-renderer to 16.13.1 (#18187) 2020-03-17 00:35:28 +00:00
Dominic Gannaway
c804f9aebb ReactDOM.useEvent: wire to event system to the hook (#18304) 2020-03-16 21:46:17 +00:00
Brian Vaughn
297f7588c4 Moved passive effects flag to be beside execution context (#18322) 2020-03-16 10:27:42 -07:00
Jovi De Croock
95df39b6b1 improve error message for cross-functional component updates (#18316)
* improve error message for cross-functional component updates

* correctly use %s by quoting it

* use workInProgress and lint

* add test assertion

* fix test

* Improve the error message

Co-authored-by: Dan Abramov <dan.abramov@me.com>
2020-03-16 17:05:30 +00:00
Brian Vaughn
c0ed0a2468 Replace passive effect context with boolean (#18309) 2020-03-16 09:56:43 -07:00
Brian Vaughn
730389b9d3 Warn against state updates from useEffect destroy functions (#18307)
Don't warn about unmounted state updates from within passive destroy function

* Fixed test conditional. (It broke after recent variant refactor.)
* Changed warning wording for setState from within useEffect destroy callback
2020-03-13 15:33:50 -07:00
Dominic Gannaway
0705b7282f Refine event system types + pass through priority (#18305) 2020-03-13 17:57:17 +00:00
Dominic Gannaway
45d26f6edb Remove un-used function arg (#18303) 2020-03-13 14:16:12 +00:00
Dan Abramov
73ff8b9094 Run ReactElementJSX-test against bundles (#18301)
* Run ReactElementJSX-test against bundles

* Try the new thing
2020-03-13 12:07:22 +00:00
Andrew Clark
8c015e0e14 Revert "Move MS Windows build to CircleCI (#17984)" (#18302)
This reverts commit 885ed46909.
2020-03-12 20:57:13 -07:00
wittgenst
885ed46909 Move MS Windows build to CircleCI (#17984)
* Move MS Windows build to CircleCI

* Update to latest NodeJS

Co-authored-by: Chris Lüer <lueer@fb.com>
2020-03-12 17:15:30 -07:00
Dominic Gannaway
2a7cd58956 ReactDOM.useEvent: Add DOM host event listener logic (#18292) 2020-03-12 22:41:33 +00:00
Dominic Gannaway
d3ec42020d Address feedback for accumulateTwoPhaseListeners (#18289) 2020-03-12 21:48:29 +00:00
Andrew Clark
cd48a06547 Set up infra for react-reconciler fork (#18285)
* ReactFiberReconciler -> ReactFiberReconciler.old

* Set up infra for react-reconciler fork

We're planning to land some significant refactors of the reconciler.
We want to be able to gradually roll out the new implementation side-by-
side with the existing one. So we'll create a short lived fork of the
react-reconciler package. Once the new implementation has stabilized,
we'll delete the old implementation and promote the new one.

This means, for as long as the fork exists, we'll need to maintain two
separate implementations. This sounds painful, but since the forks will
still be largely the same, most changes will not require two separate
implementations. In practice, you'll implement the change in the old
fork and then copy paste it to the new one.

This commit only sets up the build and testing infrastructure. It does
not actually fork any modules. I'll do that in subsequent PRs.

The forked version of the reconciler will be used to build a special
version of React DOM. I've called this build ReactDOMForked. It's only
built for www; there's no open source version.

The new reconciler is disabled by default. It's enabled in the
`yarn test-www-variant` command. The reconciler fork isn't really
related to the "variant" feature of the www builds, but I'm piggy
backing on that concept to avoid having to add yet another
testing dimension.
2020-03-12 11:38:32 -07:00
Andrew Clark
615df434bb Remove manual feature flag overrides (#18291)
Tests now run against both versions of these flags, using the
test-www-variant command. So we can remove the manual overrides.
2020-03-12 10:17:02 -07:00
Dominic Gannaway
99d271228d ReactDOM.useEvent: more scaffolding changes (#18282) 2020-03-12 09:12:06 +00:00
Brian Vaughn
97a8c72bf8 Wrap Components and Profiler tabs with box-size style too (#18286)
For the browser extension, these views get rendered into portals and so they don't inherit the box-sizing style from the .DevTools wrapper element. This causes views like the Profiler commit selector to subtly break.
2020-03-11 19:12:57 -07:00
Andrew Clark
5374919033 Empty commit to fix CI on master
Weird GitHub bug caused CI jobs to run against master as if it was
a PR. Now it looks like master is failing, even though it isn't.

Bloop.
2020-03-11 18:29:46 -07:00
Dominic Gannaway
8b155d2613 Flow type ReactDOMComponentTree (#18280) 2020-03-11 19:38:23 +00:00
Brian Vaughn
322cdcd3ab useMutableSource hook (#18000)
useMutableSource hook

useMutableSource() enables React components to safely and efficiently read from a mutable external source in Concurrent Mode. The API will detect mutations that occur during a render to avoid tearing and it will automatically schedule updates when the source is mutated.

RFC: reactjs/rfcs#147
2020-03-11 12:34:39 -07:00
Dominic Gannaway
30a998debf ModernEventSystem: refactor accumulateTwoPhaseListeners (#18274) 2020-03-11 18:55:01 +00:00
Andrew Clark
8fe066fdac Bugfix: "Captured" updates on legacy queue (#18265)
* Bugfix: "Captured" updates on legacy queue

This fixes a bug with error boundaries. Error boundaries have a notion
of "captured" updates that represent errors that are thrown in its
subtree during the render phase. These updates are meant to be dropped
if the render is aborted.

The bug happens when there's a concurrent update (an update from an
interleaved event) in between when the error is thrown and when the
error boundary does its second pass. The concurrent update is
transferred from the pending queue onto the base queue. Usually, at this
point the base queue is the same as the current queue. So when we
append the pending updates to the work-in-progress queue, it also
appends to the current queue.

However, in the case of an error boundary's second pass, the base queue
has already forked from the current queue; it includes both the
"captured" updates and any concurrent updates. In that case, what we
need to do is append separately to both queues. Which we weren't doing.

That isn't the full story, though. You would expect that this mistake
would manifest as dropping the interleaved updates. But instead what
was happening is that the "captured" updates, the ones that are meant
to be dropped if the render is aborted, were being added to the
current queue.

The reason is that the `baseQueue` structure is a circular linked list.
The motivation for this was to save memory; instead of separate `first`
and `last` pointers, you only need to point to `last`.

But this approach does not work with structural sharing. So what was
happening is that the captured updates were accidentally being added
to the current queue because of the circular link.

To fix this, I changed the `baseQueue` from a circular linked list to a
singly-linked list so that we can take advantage of structural sharing.

The "pending" queue, however, remains a circular list because it doesn't
need to be persistent.

This bug also affects the root fiber, which uses the same update queue
implementation and also acts like an error boundary.

It does not affect the hook update queue because they do not have any
notion of "captured" updates. So I've left it alone for now. However,
when we implement resuming, we will have to account for the same issue.

* Ensure base queue is a clone

When an error boundary captures an error, we append the error update
to the work-in-progress queue only so that if the render is aborted,
the error update is dropped.

Before appending to the queue, we need to make sure the queue is a
work-in-progress copy. Usually we clone the queue during
`processUpdateQueue`; however, if the base queue has lower priority
than the current render, we may have bailed out on the boundary fiber
without ever entering `processUpdateQueue`. So we need to lazily clone
the queue.

* Add warning to protect against refactor hazard

The hook queue does not have resuming or "captured" updates, but if
we ever add them in the future, we'll need to make sure we check if the
queue is forked before transfering the pending updates to them.
2020-03-11 11:53:45 -07:00
Sebastian Markbåge
b5c6dd2de5 Don't use Spread in DevTools Injection (#18277) 2020-03-11 10:58:25 -07:00
Sebastian Markbage
a463fef31b Revert "[React Native] Add getInspectorDataForViewAtPoint (#18233)"
This reverts commit bf351089a0.
2020-03-11 10:05:26 -07:00
Sebastian Markbåge
dc7eedae3c Encode server rendered host components as array tuples (#18273)
This replaces the HTML renderer with instead resolving host elements into
arrays tagged with the react.element symbol. These turn into proper
React Elements on the client.

The symbol is encoded as the magical value "$". This has security implications
so this special value needs to remain escaped for other strings.

We could just encode the element as {$$typeof: "$", key: key props: props}
but that's a lot more bytes. So instead I encode it as:
["$", key, props] and then convert it back.

It would be nicer if React's reconciler could just accept these tuples.
2020-03-11 09:48:02 -07:00
Ricky
bf351089a0 [React Native] Add getInspectorDataForViewAtPoint (#18233) 2020-03-11 16:12:41 +00:00
Sebastian Markbåge
99d7371863 [Flight] Split Streaming from Relay Implemenation (#18260)
* Add ReactFlightServerConfig intermediate

This just forwards to the stream version of Flight which is itself forked
between Node and W3C streams.

The dom-relay goes directly to the Relay config though which allows it to
avoid the stream part of Flight.

* Separate streaming protocol into the Stream config

* Split streaming parts into the ReactFlightServerConfigStream

This decouples it so that the Relay implementation doesn't have to encode
the JSON to strings. Instead it can be fed the values as JSON objects and
do its own encoding.

* Split FlightClient into a basic part and a stream part

Same split as the server.

* Expose lower level async hooks to Relay

This requires an external helper file that we'll wire up internally.
2020-03-10 14:55:04 -07:00
Dominic Gannaway
160505b0ca ReactDOM.useEvent: Add more scaffolding for useEvent hook (#18271) 2020-03-10 20:31:12 +00:00
Dominic Gannaway
a3bf668812 Flare: Fix listener upgrade bug (#18270) 2020-03-10 17:59:03 +00:00
Brian Vaughn
526c12f49e Enable enableProfilerCommitHooks flag for FB (#18230) 2020-03-10 10:40:45 -07:00
Kerollos Magdy
9e5626cdde Fix yarn warning on running 'yarn build-for-devtools' (#18232)
fixes #18231
2020-03-10 15:37:02 +00:00
Dominic Gannaway
29534252ad ReactDOM.useEvent add flag and entry point (#18267) 2020-03-10 12:18:49 +00:00
Dominic Gannaway
704c8b0118 Fix Flow type for AnyNativeEvent (#18266) 2020-03-10 10:46:12 +00:00
Sebastian Markbåge
bdc5cc4635 Add Relay Flight Build (#18242)
* Rename to clarify that it's client-only

* Rename FizzStreamer to FizzServer for consistency

* Rename react-flight to react-client/flight

For consistency with react-server. Currently this just includes flight
but it could be expanded to include the whole reconciler.

* Add Relay Flight Build

* Rename ReactServerHostConfig to ReactServerStreamConfig

This will be the config specifically for streaming purposes.
There will be other configs for other purposes.
2020-03-07 11:23:30 -08:00
Sebastian Markbåge
7a1691cdff Refactor Host Config Infra (getting rid of .inline*.js) (#18240)
* Require deep for reconcilers

* Delete inline* files

* Delete react-reconciler/persistent

This no longer makes any sense because it react-reconciler takes
supportsMutation or supportsPersistence as options. It's no longer based
on feature flags.

* Fix jest mocking

* Fix Flow strategy

We now explicitly list which paths we want to be checked by a renderer.
For every other renderer config we ignore those paths.

Nothing is "any" typed. So if some transitive dependency isn't reachable
it won't be accidentally "any" that leaks.
2020-03-06 16:20:42 -08:00
Sebastian Markbåge
238b57f0f7 [Blocks] Make it possible to have lazy initialized and lazy loaded Blocks (#18220)
* Lazify Blocks

Blocks now initialize lazily.

* Initialize Blocks eagerly in ChildFiber

This is for the case when it's a new Block that hasn't yet initialized.
We need to first initialize it to see what "render function" it resolves
to so that we can use that in our comparison.

* Remove extra import type line
2020-03-06 15:14:46 -08:00
Andrew Clark
235a6c4af6 Bugfix: Dropped effects in Legacy Mode Suspense (#18238)
* Failing: Dropped effects in Legacy Mode Suspense

* Transfer mounted effects on suspend in legacy mode

In legacy mode, a component that suspends bails out and commit in
its previous state. If the component previously had mounted effects,
we must transfer those to the work-in-progress so they don't
get dropped.
2020-03-06 11:10:01 -08:00
Andrew Clark
5fbb165602 Hard-code disableModulePatternComponents (#18239)
Hard-coding this until tests are fixed, to unblock master.
2020-03-06 11:09:36 -08:00
Brian Vaughn
cccba39f5b Fixed broken anchor tag for patch release 2020-03-06 11:05:01 -08:00
Dan Abramov
562cf013db Add a flag to disable module pattern components (#18133) 2020-03-06 18:46:32 +00:00
Andrew Clark
115cd12d9b Add test run that uses www feature flags (#18234)
In CI, we run our test suite against multiple build configurations. For
example, we run our tests in both dev and prod, and in both the
experimental and stable release channels. This is to prevent accidental
deviations in behavior between the different builds. If there's an
intentional deviation in behavior, the test author must account
for them.

However, we currently don't run tests against the www builds. That's
a problem, because it's common for features to land in www before they
land anywhere else, including the experimental release channel.
Typically we do this so we can gradually roll out the feature behind
a flag before deciding to enable it.

The way we test those features today is by mutating the
`shared/ReactFeatureFlags` module. There are a few downsides to this
approach, though. The flag is only overridden for the specific tests or
test suites where you apply the override. But usually what you want is
to run *all* tests with the flag enabled, to protect against unexpected
regressions.

Also, mutating the feature flags module only works when running the
tests against source, not against the final build artifacts, because the
ReactFeatureFlags module is inlined by the build script.

Instead, we should run the test suite against the www configuration,
just like we do for prod, experimental, and so on. I've added a new
command, `yarn test-www`. It automatically runs in CI.

Some of the www feature flags are dynamic; that is, they depend on
a runtime condition (i.e. a GK). These flags are imported from an
external module that lives in www. Those flags will be enabled for some
clients and disabled for others, so we should run the tests against
*both* modes.

So I've added a new global `__VARIANT__`, and a new test command `yarn
test-www-variant`. `__VARIANT__` is set to false by default; when
running `test-www-variant`, it's set to true.

If we were going for *really* comprehensive coverage, we would run the
tests against every possible configuration of feature flags: 2 ^
numberOfFlags total combinations. That's not practical, though, so
instead we only run against two combinations: once with `__VARIANT__`
set to `true`, and once with it set to `false`. We generally assume that
flags can be toggled independently, so in practice this should
be enough.

You can also refer to `__VARIANT__` in tests to detect which mode you're
running in. Or, you can import `shared/ReactFeatureFlags` and read the
specific flag you can about. However, we should stop mutating that
module going forward. Treat it as read-only.

In this commit, I have only setup the www tests to run against source.
I'll leave running against build for a follow up.

Many of our tests currently assume they run only in the default
configuration, and break when certain flags are toggled. Rather than fix
these all up front, I've hard-coded the relevant flags to the default
values. We can incrementally migrate those tests later.
2020-03-06 09:29:05 -08:00
Christoph Nakazawa
4027f2a3b8 Break up require/import statements in strings (#18222) 2020-03-05 22:27:55 +00:00
Brian Vaughn
024a764310 Implemented Profiler onCommit() and onPostCommit() hooks (#17910)
* Implemented Profiler onCommit() and onPostCommit() hooks
* Added enableProfilerCommitHooks feature flag for commit hooks
* Moved onCommit and onPassiveCommit behind separate feature flag
2020-03-05 11:02:00 -08:00
Brian Vaughn
d35f8a5818 feat: honor displayName of context types (#18224)
* Revert "Revert "feat: honor displayName of context types (#18035)" (#18223)"

This reverts commit 3ee812e6b6.

* Add warning of displayName is set on the consumer

* dedupe warning
2020-03-05 10:13:52 -08:00
Dominic Gannaway
3ee812e6b6 Revert "feat: honor displayName of context types (#18035)" (#18223)
This reverts commit 45c172d948.
2020-03-05 15:58:04 +00:00
Dominic Gannaway
6a0efddd89 Modern Event System: export internal FB flag for testing (#18221) 2020-03-05 14:05:00 +00:00
Sunil Pai
21b713b9fd Updated React Native home URL (#18218) 2020-03-05 13:15:46 +00:00
Dominic Gannaway
60b11f6c1b Modern Event System: Support nested portal/root boundaries (#18201) 2020-03-05 09:00:13 +00:00
Sebastian Markbåge
fa03206ee4 Remove _ctor field from Lazy components (#18217)
* This type is all wrong and nothing cares because it's all any

* Refine Flow types of Lazy Components

We can type each condition.

* Remove _ctor field from Lazy components

This field is not needed because it's only used before we've initialized,
and we don't have anything else to store before we've initialized.

* Check for _ctor in case it's an older isomorphic that created it

We try not to break across minors but it's no guarantee.

* Move types and constants from shared to isomorphic

The "react" package owns the data structure of the Lazy component. It
creates it and decides how any downstream renderer may use it.

* Move constants to shared

Apparently we can't depend on react/src/ because the whole package is
considered "external" as far as rollup is concerned.
2020-03-04 20:52:48 -08:00
Dominic Gannaway
2fe0fbb05e Use accumulateTwoPhaseDispatchesSingle directly (#18203) 2020-03-05 00:04:27 +00:00
Dominic Gannaway
503fd82b42 Modern Event System: Add support for internal FB Primer (#18210) 2020-03-04 23:41:59 +00:00
Brian Vaughn
45c172d948 feat: honor displayName of context types (#18035)
* test: Add test for current behavior of displayName
* feat: consider displayName of context types
2020-03-04 14:54:16 -08:00
Dominic Gannaway
83c3ed290c Fix instanceContainsElem bug from typo (#18213) 2020-03-04 19:53:16 +00:00
Vishal Jagtap
7e94d89f3e Updated React Native Website URL (#18207) 2020-03-04 12:10:48 +00:00
Brian Vaughn
355970aa4b DevTools 4.4.0 -> 4.5.0 2020-03-03 15:04:07 -08:00
Andrew Clark
ec652f4daf Bugfix: Expired partial tree infinite loops (#17949)
* Bugfix: Expiring a partially completed tree (#17926)

* Failing test: Expiring a partially completed tree

We should not throw out a partially completed tree if it expires in the
middle of rendering. We should finish the rest of the tree without
yielding, then finish any remaining expired levels in a single batch.

* Check if there's a partial tree before restarting

If a partial render expires, we should stay in the concurrent path
(performConcurrentWorkOnRoot); we'll stop yielding, but the rest of the
behavior remains the same.

We will only revert to the sync path (performSyncWorkOnRoot) when
starting on a new level.

This approach prevents partially completed concurrent work from
being discarded.

* New test: retry after error during expired render

* Regression: Expired partial tree infinite loops

Adds regression tests that reproduce a scenario where a partially
completed tree expired then fell into an infinite loop.

The code change that exposed this bug made the assumption that if you
call Scheduler's `shouldYield` from inside an expired task, Scheduler
will always return `false`. But in fact, Scheduler sometimes returns
`true` in that scenario, which is a bug.

The reason it worked before is that once a task timed out, React would
always switch to a synchronous work loop without checking `shouldYield`.

My rationale for relying on `shouldYield` was to unify the code paths
between a partially concurrent render (i.e. expires midway through) and
a fully concurrent render, as opposed to a render that was synchronous
the whole time. However, this bug indicates that we need a stronger
guarantee within React for when tasks expire, given that the failure
case is so catastrophic. Instead of relying on the result of a dynamic
method call, we should use control flow to guarantee that the work is
synchronously executed.

(We should also fix the Scheduler bug so that `shouldYield` always
returns false inside an expired task, but I'll address that separately.)

* Always switch to sync work loop when task expires

Refactors the `didTimeout` check so that it always switches to the
synchronous work loop, like it did before the regression.

This breaks the error handling behavior that I added in 5f7361f (an
error during a partially concurrent render should retry once,
synchronously). I'll fix this next. I need to change that behavior,
anyway, to support retries that occur as a result of `flushSync`.

* Retry once after error even for sync renders

Except in legacy mode.

This is to support the `useOpaqueReference` hook, which uses an error
to trigger a retry at lower priority.

* Move partial tree check to performSyncWorkOnRoot

* Factor out render phase

Splits the work loop and its surrounding enter/exit code into their own
functions. Now we can do perform multiple render phase passes within a
single call to performConcurrentWorkOnRoot or performSyncWorkOnRoot.
This lets us get rid of the `didError` field.
2020-03-03 13:42:18 -08:00
Brian Vaughn
d2158d6ccb Fix flow types (#18204)
* Added missing @flow pragma to React.js

* Fixed useContext() return type definition

* Fixed previously masked Flow errors in DevTools and react-interactions packages

* Added displayName to internal Context Flow type

* Removed Flow generic annotations for createResponder

This seems to cause a parsing error. (Not sure why.) The API is deprecated anyway so I'm being lazy for now and just adding a .
2020-03-03 12:46:24 -08:00
Dominic Gannaway
8e6a08ea4f Modern Event System: add plugin handling and forked paths (#18195) 2020-03-03 14:37:06 +00:00
Luna Ruan
7e83af17ce Put React.jsx and React.jsxDEV behind experimental build (#18023)
This PR puts the React.jsx and React.jsxDEV (enableJSXTransformAPI feature flag) in the experimental build so that we can use it to test React Native.
2020-03-02 18:08:40 -08:00
Dominic Gannaway
8cb2fb21eb Refine isFiberSuspenseAndTimedOut (#18184) 2020-03-02 14:15:42 +00:00
Dominic Gannaway
dbc7b9f50c Fix bug with PressLegacy blur (#18194) 2020-03-02 13:42:46 +00:00
Dominic Gannaway
62861bbcc7 More event system cleanup and scaffolding (#18179) 2020-03-02 10:59:07 +00:00
Sebastian Markbåge
8ccfce460f Only use Rollup's CommonJS plugin for "react-art" (#18186)
* Only use Rollup's CommonJS plugin for "react-art"

We still need it for the "art" UMD builds but nothing else should have
CommonJS dependencies anymore.

* react-debug-tools and jest-react should leave object-assign as an external dep

This avoids it being compiled into the output.
2020-02-28 16:46:16 -08:00
Minh Nguyen
c26506a7d2 Update react-shallow-renderer from 16.12.0 to 16.13.0 (#18185) 2020-02-28 16:35:52 -08:00
Eli White
26aa1987ce [Native] Enable and remove targetAsInstance feature flag. (#18182) 2020-02-28 13:45:42 -08:00
Sebastian Markbåge
4469700bb6 Change ReactVersion from CJS to ES module (#18181) 2020-02-28 13:09:02 -08:00
Sebastian Markbåge
58eedbb024 Check in a forked version of object-assign only for UMD builds (#18180)
* Check in a forked version of object-assign

This one uses ES modules so that we can inline it into UMD builds.

We could wait for object-assign to make an ESM export but we're going to
remove this dependency and assume global polyfills in the next version
anyway. However, we'd have to figure out how to keep the copyright header
and it'll get counted in terms of byte size (even if other tooling removes
it).

A lot of headache when we have our own implementation anyway. So I'll just
use that.

Ours is not resilient to checking certain browser bugs but those browsers
are mostly unused anyway. (Even FB breaks on them presumably.)

We also don't need to be resilient to Symbols since the way React uses it
we shouldn't need to copy symbols

* Don't transpile Object.assign to object-assign in object-assign

The polyfill needs to be able to feature detect Object.assign.
2020-02-28 11:14:09 -08:00
Henry Q. Dineen
053347e6bc react-test-renderer: improve findByType() error message (#17439)
* improve findByType error message

* fix flow typing

* Adding a test for the "Unknown" branch when `getComponentName()` returns a falsy value. The error message in this case not the most descriptive but seems consistent with the `getComponentName(type) || 'Unknown'` pattern seen in multiple places in this code base.
2020-02-28 17:55:33 +00:00
Dan Abramov
4ee592e95a Add an early invariant to debug a mystery crash (#18159) 2020-02-28 11:56:36 +00:00
Sophie Alpert
7ea4e4111f Fix typo in warning text (#18103)
Mentioned in #18090.
2020-02-28 11:53:20 +00:00
Simen Bekkhus
79a25125b1 feat: add recommended config eslint rule (#14762)
* feat: add recommended config eslint rule

* add exhaustive-deps to recommended as well
2020-02-28 02:01:17 +00:00
Joshua Gross
ae60caacfd [Fabric] Fix targetAsInstance dispatchEvent "cannot read property of null" (#18156)
* Fix Fabric targetAsInstance dispatchEvent: targetFiber stateNode is null in some cases

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2020-02-27 17:23:25 -08:00
Dan Abramov
d72700ff5a Remove runtime dependency on prop-types (#18127)
* Remove runtime dep on prop-types

* Fix test
2020-02-28 01:21:54 +00:00
Sebastian Markbåge
549e418830 Move remaining things to named exports (#18165)
* Move remaining things to named exports

The interesting case here is the noop renderers. The wrappers around the
reconciler now changed to use a local export that gets mutated.

ReactNoop and ReactNoopPersistent now have to destructure the object to
list out the names it's going to export. We should probably refactor
ReactNoop away from createReactNoop. Especially since it's also not Flow
typed.

* Switch interactions to star exports

This will have esModule compatibility flag on them. They should ideally
export default instead.
2020-02-27 17:18:55 -08:00
Sebastian Markbåge
739f20beda Remove Node shallow builds (#18157)
This is just a forwarding module. We can hardcode it.
2020-02-27 14:11:40 -08:00
Sebastian Markbåge
3e809bf5d4 Convert React Native builds to named exports (#18136)
These don't need any forks because we always export the same things atm.
2020-02-27 11:33:44 -08:00
Dan Abramov
869dbda722 Don't build shallow renderer for FB (#18153) 2020-02-27 18:17:58 +00:00
Minh Nguyen
293878e079 Replace ReactShallowRenderer with a dependency (#18144)
Closes #17321.
2020-02-27 18:10:25 +00:00
Dan Abramov
abcca45951 Run test-prod job for experimental builds (#18152) 2020-02-27 16:21:36 +00:00
Dan Abramov
1ad2179002 Bust Circle caches 2020-02-27 15:33:10 +00:00
Dan Abramov
b4e3148918 Remove unused flag (#18132) 2020-02-27 12:58:15 +00:00
Dan Abramov
849e8328b5 Remove unnecessary warnings (#18135) 2020-02-27 02:14:30 +00:00
Sebastian Markbåge
f9c0a45441 Convert the rest of react-dom and react-test-renderer to Named Exports (#18145)
Nothing interesting here except that ReactShallowRenderer currently exports
a class with a static method instead of an object.

I think the public API is probably just meant to be createRenderer but
currently the whole class is exposed. So this means that we have to keep
it as default export. We could potentially also expose a named export for
createRenderer but that's going to cause compatibility issues.

So I'm just going to make that export default.

Unfortunately Rollup and Babel (which powers Jest) disagree on how to
import this. So to make it work I had to move the jest tests to imports.

This doesn't work with module resetting. Some tests weren't doing that
anyway and the rest is just testing ReactShallowRenderer so meh.
2020-02-26 18:04:32 -08:00
Brian Vaughn
2738b6d022 Updated CHANGELOG to remove passive effects change from 16.13 release. (It wasn't part of that release.) 2020-02-26 13:06:02 -08:00
Dan Abramov
69c769ae04 Fix changelog link 2020-02-26 21:04:36 +00:00
Dan
efaffc4797 Prettier 2020-02-26 21:02:20 +00:00
941 changed files with 98293 additions and 32912 deletions

View File

@@ -11,9 +11,9 @@ aliases:
restore_cache:
name: Restore node_modules cache
keys:
- v1-node-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
- v1-node-{{ arch }}-{{ .Branch }}-
- v1-node-{{ arch }}-
- v2-node-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
- v2-node-{{ arch }}-{{ .Branch }}-
- v2-node-{{ arch }}-
- &run_yarn
run:
name: Install Packages
@@ -31,7 +31,6 @@ aliases:
- *restore_yarn_cache
- *run_yarn
- run: node ./scripts/rollup/consolidateBundleSizes.js
- run: ./scripts/circleci/upload_build.sh
- run: ./scripts/circleci/pack_and_store_artifact.sh
- store_artifacts:
path: ./node_modules.tgz
@@ -62,11 +61,11 @@ jobs:
- *run_yarn
- save_cache:
name: Save node_modules cache
key: v1-node-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
key: v2-node-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn
lint:
yarn_lint:
docker: *docker
environment: *environment
@@ -80,7 +79,7 @@ jobs:
- run: ./scripts/circleci/check_modules.sh
- run: ./scripts/circleci/test_print_warnings.sh
flow:
yarn_flow:
docker: *docker
environment: *environment
@@ -90,7 +89,7 @@ jobs:
- *run_yarn
- run: node ./scripts/tasks/flow-ci
test_source:
RELEASE_CHANNEL_stable_yarn_test:
docker: *docker
environment: *environment
@@ -98,24 +97,90 @@ jobs:
- checkout
- *restore_yarn_cache
- *run_yarn
- run:
environment:
RELEASE_CHANNEL: stable
command: yarn test --maxWorkers=2
- run: yarn test --release-channel=stable --ci
test_source_experimental:
yarn_test:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run:
environment:
RELEASE_CHANNEL: experimental
command: yarn test --maxWorkers=2
- run: yarn test --ci
test_source_persistent:
RELEASE_CHANNEL_stable_yarn_test_www:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=www-classic --ci
RELEASE_CHANNEL_stable_yarn_test_www_variant:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=www-classic --variant --ci
RELEASE_CHANNEL_stable_yarn_test_prod_www:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=www-classic --prod --ci
RELEASE_CHANNEL_stable_yarn_test_prod_www_variant:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=www-classic --prod --variant --ci
yarn_test_www:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=www-modern --ci
yarn_test_www_variant:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=www-modern --variant --ci
yarn_test_prod_www:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=www-modern --prod --ci
yarn_test_prod_www_variant:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=www-modern --prod --variant --ci
RELEASE_CHANNEL_stable_yarn_test_persistent:
docker: *docker
environment: *environment
@@ -123,12 +188,9 @@ jobs:
- checkout
- *restore_yarn_cache
- *run_yarn
- run:
environment:
RELEASE_CHANNEL: stable
command: yarn test-persistent --maxWorkers=2
- run: yarn test --release-channel=stable --persistent --ci
test_source_prod:
RELEASE_CHANNEL_stable_yarn_test_prod:
docker: *docker
environment: *environment
@@ -136,12 +198,18 @@ jobs:
- checkout
- *restore_yarn_cache
- *run_yarn
- run:
environment:
RELEASE_CHANNEL: stable
command: yarn test-prod --maxWorkers=2
- run: yarn test --release-channel=stable --prod --ci
build:
yarn_test_prod:
docker: *docker
environment: *environment
steps:
- checkout
- *restore_yarn_cache
- *run_yarn
- run: yarn test --release-channel=experimental --prod --ci
RELEASE_CHANNEL_stable_yarn_build:
docker: *docker
environment: *environment
parallelism: 20
@@ -162,12 +230,13 @@ jobs:
paths:
- RELEASE_CHANNEL
- facebook-www
- facebook-react-native
- node_modules
- react-native
- dist
- sizes/*.json
build_experimental:
yarn_build:
docker: *docker
environment: *environment
parallelism: 20
@@ -188,6 +257,7 @@ jobs:
paths:
- RELEASE_CHANNEL
- facebook-www
- facebook-react-native
- node_modules
- react-native
- dist
@@ -214,7 +284,7 @@ jobs:
process_artifacts: *process_artifacts
process_artifacts_experimental: *process_artifacts
sizebot:
sizebot_stable:
docker: *docker
environment: *environment
steps:
@@ -246,7 +316,7 @@ jobs:
RELEASE_CHANNEL: experimental
command: node ./scripts/tasks/danger
lint_build:
yarn_lint_build:
docker: *docker
environment: *environment
steps:
@@ -257,7 +327,7 @@ jobs:
- run: yarn lint-build
- run: scripts/circleci/check_minified_errors.sh
test_build:
RELEASE_CHANNEL_stable_yarn_lint_build:
docker: *docker
environment: *environment
steps:
@@ -268,9 +338,10 @@ jobs:
- run:
environment:
RELEASE_CHANNEL: stable
command: yarn test-build --maxWorkers=2
command: yarn lint-build
- run: scripts/circleci/check_minified_errors.sh
test_build_experimental:
RELEASE_CHANNEL_stable_yarn_test_build:
docker: *docker
environment: *environment
steps:
@@ -278,12 +349,9 @@ jobs:
- attach_workspace: *attach_workspace
- *restore_yarn_cache
- *run_yarn
- run:
environment:
RELEASE_CHANNEL: experimental
command: yarn test-build --maxWorkers=2
- run: yarn test --release-channel=stable --build --ci
test_devtools:
yarn_test_build:
docker: *docker
environment: *environment
steps:
@@ -291,12 +359,19 @@ jobs:
- attach_workspace: *attach_workspace
- *restore_yarn_cache
- *run_yarn
- run:
environment:
RELEASE_CHANNEL: experimental
command: yarn test-build-devtools --maxWorkers=2
- run: yarn test --release-channel=experimental --build --ci
test_dom_fixtures:
yarn_test_build_devtools:
docker: *docker
environment: *environment
steps:
- checkout
- attach_workspace: *attach_workspace
- *restore_yarn_cache
- *run_yarn
- run: yarn test --project=devtools --build --ci
RELEASE_CHANNEL_stable_yarn_test_dom_fixtures:
docker: *docker
environment: *environment
steps:
@@ -323,10 +398,10 @@ jobs:
- run:
name: Run fuzz tests
command: |
FUZZ_TEST_SEED=$RANDOM yarn test fuzz --maxWorkers=2
FUZZ_TEST_SEED=$RANDOM yarn test-prod fuzz --maxWorkers=2
FUZZ_TEST_SEED=$RANDOM yarn test fuzz --ci
FUZZ_TEST_SEED=$RANDOM yarn test --prod fuzz --ci
test_build_prod:
RELEASE_CHANNEL_stable_yarn_test_build_prod:
docker: *docker
environment: *environment
steps:
@@ -334,12 +409,9 @@ jobs:
- attach_workspace: *attach_workspace
- *restore_yarn_cache
- *run_yarn
- run:
environment:
RELEASE_CHANNEL: stable
command: yarn test-build-prod --maxWorkers=2
- run: yarn test --release-channel=stable --build --prod --ci
test_build_prod_experimental:
yarn_test_build_prod:
docker: *docker
environment: *environment
steps:
@@ -347,83 +419,108 @@ jobs:
- attach_workspace: *attach_workspace
- *restore_yarn_cache
- *run_yarn
- run:
environment:
RELEASE_CHANNEL: experimental
command: yarn test-build-prod --maxWorkers=2
- run: yarn test --release-channel=experimental --build --prod --ci
workflows:
version: 2
stable:
jobs:
- setup
- lint:
- yarn_lint:
requires:
- setup
- flow:
- yarn_flow:
requires:
- setup
- test_source:
- RELEASE_CHANNEL_stable_yarn_test:
requires:
- setup
- test_source_prod:
- RELEASE_CHANNEL_stable_yarn_test_prod:
requires:
- setup
- test_source_persistent:
- RELEASE_CHANNEL_stable_yarn_test_persistent:
requires:
- setup
- build:
- RELEASE_CHANNEL_stable_yarn_test_www:
requires:
- setup
- RELEASE_CHANNEL_stable_yarn_test_www_variant:
requires:
- setup
- RELEASE_CHANNEL_stable_yarn_test_prod_www:
requires:
- setup
- RELEASE_CHANNEL_stable_yarn_test_prod_www_variant:
requires:
- setup
- RELEASE_CHANNEL_stable_yarn_build:
requires:
- setup
- process_artifacts:
requires:
- build
- sizebot:
- RELEASE_CHANNEL_stable_yarn_build
- sizebot_stable:
requires:
- build
- lint_build:
- RELEASE_CHANNEL_stable_yarn_build
- RELEASE_CHANNEL_stable_yarn_lint_build:
requires:
- build
- test_build:
- RELEASE_CHANNEL_stable_yarn_build
- RELEASE_CHANNEL_stable_yarn_test_build:
requires:
- build
- test_build_prod:
- RELEASE_CHANNEL_stable_yarn_build
- RELEASE_CHANNEL_stable_yarn_test_build_prod:
requires:
- build
- test_dom_fixtures:
- RELEASE_CHANNEL_stable_yarn_build
- RELEASE_CHANNEL_stable_yarn_test_dom_fixtures:
requires:
- build
- RELEASE_CHANNEL_stable_yarn_build
experimental:
jobs:
- setup
- test_source_experimental:
- yarn_test:
requires:
- setup
- build_experimental:
- yarn_test_prod:
requires:
- setup
- yarn_test_www:
requires:
- setup
- yarn_test_www_variant:
requires:
- setup
- yarn_test_prod_www:
requires:
- setup
- yarn_test_prod_www_variant:
requires:
- setup
- yarn_build:
requires:
- setup
- process_artifacts_experimental:
requires:
- build_experimental
- yarn_build
- sizebot_experimental:
requires:
- build_experimental
- test_build_experimental:
- yarn_build
- yarn_test_build:
requires:
- build_experimental
- test_build_prod_experimental:
- yarn_build
- yarn_test_build_prod:
requires:
- build_experimental
- lint_build:
- yarn_build
- yarn_lint_build:
requires:
- build_experimental
- test_devtools:
- yarn_build
- yarn_test_build_devtools:
requires:
- build_experimental
- build_devtools_and_process_artifacts:
requires:
- build_experimental
- yarn_build
# FIXME: Temporarily disabled to unblock master.
# - build_devtools_and_process_artifacts:
# requires:
# - yarn_build
fuzz_tests:
triggers:

View File

@@ -1,6 +1,6 @@
{
"packages": ["packages/react", "packages/react-dom", "packages/scheduler"],
"buildCommand": "build --type=NODE react/index,react-dom/index,scheduler/index,scheduler/tracing",
"buildCommand": "build --type=NODE react/index,react-dom/index,react-dom/server,scheduler/index,scheduler/tracing",
"publishDirectory": {
"react": "build/node_modules/react",
"react-dom": "build/node_modules/react-dom",

View File

@@ -1,4 +1,4 @@
# http://editorconfig.org
# https://editorconfig.org
root = true
[*]

View File

@@ -11,10 +11,7 @@ const OFF = 0;
const ERROR = 2;
module.exports = {
extends: [
'fbjs',
'prettier'
],
extends: ['fbjs', 'prettier'],
// Stop ESLint from looking for a configuration file in parent folders
root: true,
@@ -101,6 +98,7 @@ module.exports = {
'react-internal/invariant-args': ERROR,
'react-internal/warning-args': ERROR,
'react-internal/no-production-logging': ERROR,
'react-internal/no-cross-fork-imports': ERROR,
},
overrides: [
@@ -129,6 +127,7 @@ module.exports = {
},
rules: {
'no-var': ERROR,
'prefer-const': ERROR,
strict: OFF,
},
},
@@ -147,7 +146,7 @@ module.exports = {
'scripts/**/*.js',
'packages/*/npm/**/*.js',
'packages/dom-event-testing-library/**/*.js',
'packages/react-devtools*/**/*.js'
'packages/react-devtools*/**/*.js',
],
rules: {
'react-internal/no-production-logging': OFF,
@@ -160,6 +159,13 @@ module.exports = {
nativeFabricUIManager: true,
},
},
{
files: ['packages/react-transport-dom-webpack/**/*.js'],
globals: {
__webpack_chunk_load__: true,
__webpack_require__: true,
},
},
],
globals: {
@@ -171,6 +177,8 @@ module.exports = {
__PROFILE__: true,
__UMD__: true,
__EXPERIMENTAL__: true,
__VARIANT__: true,
gate: true,
trustedTypes: true,
},
};

View File

@@ -1,4 +0,0 @@
👉 Please follow one of these issue templates:
- https://github.com/facebook/react/issues/new/choose
Note: to keep the backlog clean and actionable, issues may be immediately closed if they do not follow one of the above issue templates.

View File

@@ -12,7 +12,7 @@
6. If you need a debugger, run `yarn debug-test --watch TestName`, open `chrome://inspect`, and press "Inspect".
7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files.
9. Run the [Flow](https://flowtype.org/) typechecks (`yarn flow`).
9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
10. If you haven't already, complete the CLA.
Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html

11
.github/stale.yml vendored
View File

@@ -10,22 +10,25 @@ exemptLabels:
- "Resolution: Backlog"
- "Type: Bug"
- "Type: Discussion"
- "Type: Needs Investigation"
- "Type: Regression"
# Label to use when marking an issue as stale
staleLabel: "Resolution: Stale"
issues:
# Comment to post when marking an issue as stale.
markComment: >
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contribution.
This issue has been automatically marked as stale.
**If this issue is still affecting you, please leave any comment** (for example, "bump"), and we'll keep it open.
We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
# Comment to post when closing a stale issue.
closeComment: >
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!
pulls:
# Comment to post when marking a pull request as stale.
markComment: >
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contribution.
This pull request has been automatically marked as stale.
**If this pull request is still relevant, please leave any comment** (for example, "bump"), and we'll keep it open.
We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.
# Comment to post when closing a stale pull request.
closeComment: >
Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

2
.gitignore vendored
View File

@@ -1,6 +1,7 @@
.DS_STORE
node_modules
scripts/flow/*/.flowconfig
.flowconfig
*~
*.pyc
.grunt
@@ -31,5 +32,6 @@ packages/react-devtools-extensions/firefox/build
packages/react-devtools-extensions/firefox/*.xpi
packages/react-devtools-extensions/firefox/*.pem
packages/react-devtools-extensions/shared/build
packages/react-devtools-extensions/.tempUserDataDir
packages/react-devtools-inline/dist
packages/react-devtools-shell/dist

2
.nvmrc
View File

@@ -1 +1 @@
v10.16.3
v12.16.2

View File

@@ -1,6 +1,13 @@
## 16.13.1 (March 19, 2020)
### React DOM
* Fix bug in legacy mode Suspense where effect clean-up functions are not fired. This only affects users who use Suspense for data fetching in legacy mode, which is not technically supported. ([@acdlite](https://github.com/acdlite) in [#18238](https://github.com/facebook/react/pull/18238))
* Revert warning for cross-component updates that happen inside class render lifecycles (`componentWillReceiveProps`, `shouldComponentUpdate`, and so on). ([@gaearon](https://github.com/gaearon) in [#18330](https://github.com/facebook/react/pull/18330))
## 16.13.0 (February 26, 2020)
### React
### React
* Warn when a string ref is used in a manner that's not amenable to a future codemod ([@lunaruan](https://github.com/lunaruan) in [#17864](https://github.com/facebook/react/pull/17864))
* Deprecate `React.createFactory()` ([@trueadm](https://github.com/trueadm) in [#17878](https://github.com/facebook/react/pull/17878))
@@ -8,9 +15,8 @@
### React DOM
* Warn when changes in `style` may cause an unexpected collision ([@sophiebits](https://github.com/sophiebits) in [#14181](https://github.com/facebook/react/pull/14181), [#18002](https://github.com/facebook/react/pull/18002))
* Warn when a function component is updated during another component's render phase ([@acdlite]((https://github.com/acdlite)) in [#17099](https://github.com/facebook/react/pull/17099))
* Warn when a function component is updated during another component's render phase ([@acdlite](https://github.com/acdlite) in [#17099](https://github.com/facebook/react/pull/17099))
* Deprecate `unstable_createPortal` ([@trueadm](https://github.com/trueadm) in [#17880](https://github.com/facebook/react/pull/17880))
* Flush all passive effect (`useEffect`) destroy functions before calling subsequent create functions ([@bvaughn](https://github.com/bvaughn) in [#17925](https://github.com/facebook/react/pull/17925), [#17947](https://github.com/facebook/react/pull/17947))
* Fix `onMouseEnter` being fired on disabled buttons ([@AlfredoGJ](https://github.com/AlfredoGJ) in [#17675](https://github.com/facebook/react/pull/17675))
* Call `shouldComponentUpdate` twice when developing in `StrictMode` ([@bvaughn](https://github.com/bvaughn) in [#17942](https://github.com/facebook/react/pull/17942))
* Add `version` property to ReactDOM ([@ealush](https://github.com/ealush) in [#15780](https://github.com/facebook/react/pull/15780))
@@ -25,7 +31,7 @@
* Adjust `SuspenseList` CPU bound heuristic ([@sebmarkbage](https://github.com/sebmarkbage) in [#17455](https://github.com/facebook/react/pull/17455))
* Add missing event plugin priorities ([@trueadm](https://github.com/trueadm) in [#17914](https://github.com/facebook/react/pull/17914))
* Fix `isPending` only being true when transitioning from inside an input event ([@acdlite](https://github.com/acdlite) in [#17382](https://github.com/facebook/react/pull/17382))
* Fix `React.memo` components dropping updates when interrupted by a higher priority update ([@acdlite]((https://github.com/acdlite)) in [#18091](https://github.com/facebook/react/pull/18091))
* Fix `React.memo` components dropping updates when interrupted by a higher priority update ([@acdlite]((https://github.com/acdlite)) in [#18091](https://github.com/facebook/react/pull/18091))
* Don't warn when suspending at the wrong priority ([@gaearon](https://github.com/gaearon) in [#17971](https://github.com/facebook/react/pull/17971))
* Fix a bug with rebasing updates ([@acdlite](https://github.com/acdlite) and [@sebmarkbage](https://github.com/sebmarkbage) in [#17560](https://github.com/facebook/react/pull/17560), [#17510](https://github.com/facebook/react/pull/17510), [#17483](https://github.com/facebook/react/pull/17483), [#17480](https://github.com/facebook/react/pull/17480))
@@ -776,7 +782,7 @@ Starting with 16.1.0, we will no longer be publishing new releases on Bower. You
* Fix bug in QtWebKit when wrapping synthetic events in proxies. ([@walrusfruitcake](https://github.com/walrusfruitcake) in [#10115](https://github.com/facebook/react/pull/10011))
* Prevent event handlers from receiving extra argument in development. ([@aweary](https://github.com/aweary) in [#10115](https://github.com/facebook/react/pull/8363))
* Fix cases where `onChange` would not fire with `defaultChecked` on radio inputs. ([@jquense](https://github.com/jquense) in [#10156](https://github.com/facebook/react/pull/10156))
* Add support for `controlList` attribute to DOM property whitelist ([@nhunzaker](https://github.com/nhunzaker) in [#9940](https://github.com/facebook/react/pull/9940))
* Add support for `controlList` attribute to allowed DOM properties ([@nhunzaker](https://github.com/nhunzaker) in [#9940](https://github.com/facebook/react/pull/9940))
* Fix a bug where creating an element with a ref in a constructor did not throw an error in development. ([@iansu](https://github.com/iansu) in [#10025](https://github.com/facebook/react/pull/10025))
## 15.6.1 (June 14, 2017)

View File

@@ -4,7 +4,7 @@ React is a JavaScript library for building user interfaces.
* **Declarative:** React makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes. Declarative views make your code more predictable, simpler to understand, and easier to debug.
* **Component-Based:** Build encapsulated components that manage their own state, then compose them to make complex UIs. Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.
* **Learn Once, Write Anywhere:** We don't make assumptions about the rest of your technology stack, so you can develop new features in React without rewriting existing code. React can also render on the server using Node and power mobile apps using [React Native](https://facebook.github.io/react-native/).
* **Learn Once, Write Anywhere:** We don't make assumptions about the rest of your technology stack, so you can develop new features in React without rewriting existing code. React can also render on the server using Node and power mobile apps using [React Native](https://reactnative.dev/).
[Learn how to use React in your own project](https://reactjs.org/docs/getting-started.html).
@@ -16,7 +16,7 @@ React has been designed for gradual adoption from the start, and **you can use a
* [Add React to a Website](https://reactjs.org/docs/add-react-to-a-website.html) as a `<script>` tag in one minute.
* [Create a New React App](https://reactjs.org/docs/create-a-new-react-app.html) if you're looking for a powerful JavaScript toolchain.
You can use React as a `<script>` tag from a [CDN](https://reactjs.org/docs/cdn-links.html), or as a `react` package on [npm](https://www.npmjs.com/).
You can use React as a `<script>` tag from a [CDN](https://reactjs.org/docs/cdn-links.html), or as a `react` package on [npm](https://www.npmjs.com/package/react).
## Documentation

View File

@@ -255,7 +255,7 @@ const attributes = [
{
name: 'cite',
tagName: 'blockquote',
overrideStringValue: 'http://reactjs.com/',
overrideStringValue: 'https://reactjs.com/',
},
{name: 'class', read: getAttribute('class')},
{name: 'classID', tagName: 'object', read: getAttribute('classid')},
@@ -453,6 +453,11 @@ const attributes = [
tagName: 'video',
read: getProperty('disablepictureinpicture'),
},
{
name: 'disableRemotePlayback',
tagName: 'video',
read: getProperty('disableremoteplayback'),
},
{
name: 'display',
tagName: 'svg',

1
fixtures/blocks/.env Normal file
View File

@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true

77
fixtures/blocks/db.json Normal file
View File

@@ -0,0 +1,77 @@
{
"posts": [
{
"id": 1,
"userId": 2,
"title": "Welcome",
"body": "Hello, world!"
},
{
"id": 2,
"userId": 3,
"title": "A Guide to useEffect",
"body": "Let me tell you everything about useEffect"
},
{
"id": 3,
"userId": 1,
"title": "Here and There",
"body": "Browsers are smart"
}
],
"comments": [
{
"id": 1,
"body": "Hey there",
"postId": 1,
"userId": 1
},
{
"id": 2,
"body": "Welcome to the chat",
"postId": 1,
"userId": 2
},
{
"id": 3,
"body": "What editor/font are you using?",
"postId": 2,
"userId": 2
},
{
"id": 4,
"body": "It's always been hard",
"postId": 3,
"userId": 1
},
{
"id": 5,
"body": "It's still easy",
"postId": 3,
"userId": 2
}
],
"users": [{
"id": 1,
"name": "Sebastian",
"bioId": 10
}, {
"id": 2,
"name": "Sophie",
"bioId": 20
}, {
"id": 3,
"name": "Dan",
"bioId": 30
}],
"bios": [{
"id": 10,
"text": "I like European movies"
}, {
"id": 20,
"text": "I like math puzzles"
}, {
"id": 30,
"text": "I like reading twitter"
}]
}

14
fixtures/blocks/delay.js Normal file
View File

@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
module.exports = (req, res, next) => {
if (req.query.delay) {
setTimeout(next, Number(req.query.delay));
} else {
next();
}
};

View File

@@ -0,0 +1,37 @@
{
"name": "blocks",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:3001/",
"dependencies": {
"concurrently": "^5.2.0",
"json-server": "^0.16.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"prestart": "cp -r ../../build/node_modules/* ./node_modules/",
"prebuild": "cp -r ../../build/node_modules/* ./node_modules/",
"start": "concurrently \"npm run start:client\" \"npm run start:api\"",
"start:api": "json-server --watch db.json --port 3001 --delay 1000 --middlewares delay.js",
"start:client": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Blocks Fixture</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>

View File

@@ -0,0 +1,105 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, {
useReducer,
useEffect,
unstable_useTransition as useTransition,
useCallback,
useMemo,
Suspense,
} from 'react';
import {createCache, CacheProvider} from 'react/unstable-cache';
import {RouterProvider} from './client/RouterContext';
// TODO: can't really import a server component on the client.
import App from './server/App';
const initialUrl = window.location.pathname;
const initialState = {
// TODO: use this for invalidation.
cache: createCache(),
url: initialUrl,
pendingUrl: initialUrl,
root: <App route={initialUrl} />,
};
function reducer(state, action) {
switch (action.type) {
case 'startNavigation':
return {
...state,
pendingUrl: action.url,
};
case 'completeNavigation':
// TODO: cancel previous fetch?
return {
...state,
url: action.url,
pendingUrl: action.url,
root: action.root,
};
default:
throw new Error();
}
}
function Router() {
const [state, dispatch] = useReducer(reducer, initialState);
const [startTransition, isPending] = useTransition({
timeoutMs: 1500,
});
useEffect(() => {
document.body.style.cursor = isPending ? 'wait' : '';
}, [isPending]);
const navigate = useCallback(
url => {
startTransition(() => {
// TODO: Here, There, and Everywhere.
// TODO: Instant Transitions, somehow.
dispatch({
type: 'completeNavigation',
root: <App route={url} />,
url,
});
});
dispatch({
type: 'startNavigation',
url,
});
},
[startTransition]
);
useEffect(() => {
const listener = () => {
navigate(window.location.pathname);
};
window.addEventListener('popstate', listener);
return () => window.removeEventListener('popstate', listener);
}, [navigate]);
const routeContext = useMemo(
() => ({
pendingUrl: state.pendingUrl,
url: state.url,
navigate,
}),
[state.url, state.pendingUrl, navigate]
);
return (
<Suspense fallback={<h2>Loading...</h2>}>
<CacheProvider value={state.cache}>
<RouterProvider value={routeContext}>{state.root}</RouterProvider>
</CacheProvider>
</Suspense>
);
}
export default Router;

View File

@@ -0,0 +1,25 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import {useRouter} from './RouterContext';
export default function Link({to, children, ...rest}) {
const {navigate} = useRouter();
return (
<a
href={to}
onClick={e => {
e.preventDefault();
window.history.pushState(null, null, to);
navigate(to);
}}
{...rest}>
{children}
</a>
);
}

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {TabBar, TabLink} from '../client/TabNav';
export default function ProfileNav({userId}) {
// TODO: Don't hardcode ID.
return (
<TabBar>
<TabLink to={`/profile/${userId}`}>Timeline</TabLink>
<TabLink to={`/profile/${userId}/bio`}>Bio</TabLink>
</TabBar>
);
}

View File

@@ -0,0 +1,9 @@
import {createContext, useContext} from 'react';
const RouterContext = createContext(null);
export const RouterProvider = RouterContext.Provider;
export function useRouter() {
return useContext(RouterContext);
}

View File

@@ -0,0 +1,31 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import {TabBar, TabLink} from './TabNav';
// TODO: Error Boundaries.
function MainTabNav() {
return (
<TabBar>
<TabLink to="/">Home</TabLink>
<TabLink to="/profile/3" partial={true}>
Profile
</TabLink>
</TabBar>
);
}
export default function Shell({children}) {
return (
<>
<MainTabNav />
{children}
</>
);
}

View File

@@ -0,0 +1,50 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import Link from './Link';
import {useRouter} from './RouterContext';
export function TabBar({children}) {
return (
<div
style={{
border: '1px solid #aaa',
padding: 20,
marginBottom: 20,
width: 500,
}}>
{children}
</div>
);
}
export function TabLink({to, partial, children}) {
const {pendingUrl: activeUrl} = useRouter();
const active = partial ? activeUrl.startsWith(to) : activeUrl === to;
if (active) {
return (
<b
style={{
display: 'inline-block',
marginRight: 20,
}}>
{children}
</b>
);
}
return (
<Link
style={{
display: 'inline-block',
marginRight: 20,
}}
to={to}>
{children}
</Link>
);
}

View File

@@ -0,0 +1,8 @@
body {
font-family: Helvetica;
padding-left: 10px;
}
* {
box-sizing: border-box;
}

View File

@@ -0,0 +1,13 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import {unstable_createRoot as createRoot} from 'react-dom';
import './index.css';
import Router from './Router';
createRoot(document.getElementById('root')).render(<Router />);

View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {matchRoute} from './ServerRouter';
import FeedPage from './FeedPage';
import ProfilePage from './ProfilePage';
// TODO: Replace with asset reference.
import Shell from '../client/Shell';
// TODO: Router component?
const AppRoutes = {
'/': props => <FeedPage {...props} key="home" />,
'/profile/:userId/*': props => (
<ProfilePage {...props} key={`profile-${props.userId}`} />
),
};
export default function App(props) {
const match = matchRoute(props, AppRoutes);
return <Shell>{match}</Shell>;
}

View File

@@ -0,0 +1,31 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {fetch} from 'react-fetch';
// TODO: Replace with asset reference.
import Link from '../client/Link';
export default function Comments({postId}) {
const comments = fetch(`/comments?postId=${postId}&_expand=user`).json();
return (
<>
<h5>Comments</h5>
<ul>
{comments.slice(0, 5).map(comment => (
<li key={comment.id}>
{comment.body}
{' • '}
<Link to={`/profile/${comment.user.id}`}>{comment.user.name}</Link>
</li>
))}
</ul>
</>
);
}

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {fetch} from 'react-fetch';
import PostList from './PostList';
export default function Feed() {
const posts = fetch('/posts?_expand=user').json();
return (
<>
<h2>Feed</h2>
<PostList posts={posts} />
</>
);
}

View File

@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {Suspense} from 'react';
import PostGlimmer from './PostGlimmer';
import Feed from './Feed';
export default function FeedPage() {
return (
<Suspense fallback={<PostGlimmer />}>
<Feed />
</Suspense>
);
}

View File

@@ -0,0 +1,39 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {Suspense} from 'react';
import Comments from './Comments';
// TODO: Replace with asset reference.
import Link from '../client/Link';
export default function Post({post}) {
return (
<div
style={{
border: '1px solid #aaa',
borderRadius: 10,
marginBottom: 20,
padding: 20,
maxWidth: 500,
}}>
<h4 style={{marginTop: 0}}>
{post.title}
{' by '}
<Link to={`/profile/${post.user.id}`}>{post.user.name}</Link>
</h4>
<p>{post.body}</p>
<Suspense
fallback={<h5>Loading comments...</h5>}
unstable_avoidThisFallback={true}>
<Comments postId={post.id} />
</Suspense>
</div>
);
}

View File

@@ -0,0 +1,56 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
export default function PostGlimmer() {
return (
<div
style={{
border: '1px solid #aaa',
borderRadius: 10,
marginBottom: 20,
padding: 20,
maxWidth: 500,
height: 180,
}}>
<div
style={{
marginBottom: 20,
width: '50%',
height: 20,
background: '#ddd',
}}
/>
<div
style={{
marginBottom: 20,
width: '60%',
height: 20,
background: '#eee',
}}
/>
<div
style={{
marginBottom: 20,
width: '50%',
height: 20,
background: '#eee',
}}
/>
<div
style={{
marginBottom: 20,
width: '60%',
height: 20,
background: '#eee',
}}
/>
</div>
);
}

View File

@@ -0,0 +1,28 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {Suspense, unstable_SuspenseList as SuspenseList} from 'react';
import {preload} from 'react-fetch';
import PostGlimmer from './PostGlimmer';
import Post from './Post';
export default function PostList({posts}) {
return (
<SuspenseList revealOrder="forwards" tail="collapsed">
{posts.map(post => {
preload(`/comments?postId=${post.id}&_expand=user`);
return (
<Suspense key={post.id} fallback={<PostGlimmer />}>
<Post post={post} />
</Suspense>
);
})}
</SuspenseList>
);
}

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {fetch} from 'react-fetch';
export default function ProfileBio({userId}) {
const user = fetch(`/users/${userId}`).json();
const bio = fetch(`/bios/${user.bioId}`).json().text;
return (
<>
<h3>{user.name}'s Bio</h3>
<p>{bio}</p>
</>
);
}

View File

@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import {Suspense} from 'react';
import {fetch} from 'react-fetch';
import {matchRoute} from './ServerRouter';
import ProfileTimeline from './ProfileTimeline';
import ProfileBio from './ProfileBio';
// TODO: Replace with asset reference.
import ProfileNav from '../client/ProfileNav';
// TODO: Router component?
const ProfileRoutes = {
'/': props => <ProfileTimeline {...props} key="timeline" />,
'/bio': props => <ProfileBio {...props} key="bio" />,
};
export default function ProfilePage(props) {
const user = fetch(`/users/${props.userId}`).json();
const match = matchRoute(props, ProfileRoutes);
return (
<>
<h2>{user.name}</h2>
<ProfileNav userId={user.id} />
<Suspense fallback={<h3>Loading...</h3>}>{match}</Suspense>
</>
);
}

View File

@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
import * as React from 'react';
import {fetch} from 'react-fetch';
import PostList from './PostList';
export default function ProfileTimeline({userId}) {
const posts = fetch(`/posts?userId=${userId}&_expand=user`).json();
return <PostList posts={posts} />;
}

View File

@@ -0,0 +1,54 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable import/first */
function tryMatch(props, def) {
const defSegments = def.split('/').filter(Boolean);
const routeSegments = props.route.split('/').filter(Boolean);
let innerProps = {...props};
while (routeSegments.length > 0) {
if (defSegments.length === 0) {
return null;
}
const urlSegment = routeSegments.shift();
const defSegment = defSegments.shift();
if (urlSegment === defSegment) {
continue;
}
if (defSegment[0] === ':') {
innerProps[defSegment.slice(1)] = urlSegment;
continue;
}
if (defSegment === '*') {
innerProps.route = '/' + urlSegment + routeSegments.join('/');
return innerProps;
}
return null;
}
if (defSegments.length === 0) {
return innerProps;
}
if (defSegments.length === 1 && defSegments[0] === '*') {
innerProps.route = '/';
return innerProps;
}
return null;
}
export function matchRoute(props, defs) {
for (let def in defs) {
if (!defs.hasOwnProperty(def)) {
continue;
}
const innerProps = tryMatch(props, def);
if (innerProps) {
const match = defs[def](innerProps);
return match;
}
}
throw Error('Not found.');
}

11163
fixtures/blocks/yarn.lock Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -118,7 +118,9 @@ it('warns when using the wrong act version - test + dom: updates', () => {
TestRenderer.act(() => {
setCtr(1);
});
}).toWarnDev(["It looks like you're using the wrong act()"]);
}).toWarnDev(["It looks like you're using the wrong act()"], {
withoutStack: true,
});
});
it('warns when using the wrong act version - dom + test: .create()', () => {
@@ -154,7 +156,9 @@ it('warns when using the wrong act version - dom + test: updates', () => {
TestUtils.act(() => {
setCtr(1);
});
}).toWarnDev(["It looks like you're using the wrong act()"]);
}).toWarnDev(["It looks like you're using the wrong act()"], {
withoutStack: true,
});
});
it('does not warn when nesting react-act inside react-dom', () => {
@@ -179,7 +183,7 @@ it("doesn't warn if you use nested acts from different renderers", () => {
if (__EXPERIMENTAL__) {
it('warns when using createRoot() + .render', () => {
const root = ReactDOM.createRoot(document.createElement('div'));
const root = ReactDOM.unstable_createRoot(document.createElement('div'));
expect(() => {
TestRenderer.act(() => {
root.render(<App />);

View File

@@ -71,8 +71,10 @@ class Header extends React.Component {
<option value="/text-inputs">Text Inputs</option>
<option value="/number-inputs">Number Input</option>
<option value="/password-inputs">Password Input</option>
<option value="/email-inputs">Email Input</option>
<option value="/selects">Selects</option>
<option value="/textareas">Textareas</option>
<option value="/progress">Progress</option>
<option value="/input-change-events">
Input change events
</option>

View File

@@ -0,0 +1,39 @@
import Fixture from '../../Fixture';
const React = window.React;
class EmailDisabledAttributesTestCase extends React.Component {
state = {value: 'a@fb.com'};
onChange = event => {
this.setState({value: event.target.value});
};
render() {
return (
<Fixture>
<div>{this.props.children}</div>
<div className="control-box">
<fieldset>
<legend>Controlled</legend>
<input
type="email"
value={this.state.value}
onChange={this.onChange}
/>
<span className="hint">
{' '}
Value: {JSON.stringify(this.state.value)}
</span>
</fieldset>
<fieldset>
<legend>Uncontrolled</legend>
<input type="email" defaultValue="" />
</fieldset>
</div>
</Fixture>
);
}
}
export default EmailDisabledAttributesTestCase;

View File

@@ -0,0 +1,48 @@
import Fixture from '../../Fixture';
const React = window.React;
class EmailAttributesTestCase extends React.Component {
state = {value: 'a@fb.com'};
onChange = event => {
this.setState({value: event.target.value});
};
render() {
return (
<Fixture>
<div>{this.props.children}</div>
<div className="control-box">
<fieldset>
<legend>Controlled</legend>
<input
type="email"
pattern=".+@fb.com"
maxlength={17}
multiple={true}
value={this.state.value}
onChange={this.onChange}
/>
<span className="hint">
{' '}
Value: {JSON.stringify(this.state.value)}
</span>
</fieldset>
<fieldset>
<legend>Uncontrolled</legend>
<input
type="email"
defaultValue=""
pattern=".+@fb.com"
maxlength={17}
multiple={true}
/>
</fieldset>
</div>
</Fixture>
);
}
}
export default EmailAttributesTestCase;

View File

@@ -0,0 +1,39 @@
import Fixture from '../../Fixture';
const React = window.React;
class JumpingCursorTestCase extends React.Component {
state = {value: ''};
onChange = event => {
this.setState({value: event.target.value});
};
render() {
return (
<Fixture>
<div>{this.props.children}</div>
<div className="control-box">
<fieldset>
<legend>Controlled</legend>
<input
type="email"
value={this.state.value}
onChange={this.onChange}
/>
<span className="hint">
{' '}
Value: {JSON.stringify(this.state.value)}
</span>
</fieldset>
<fieldset>
<legend>Uncontrolled</legend>
<input type="email" defaultValue="" />
</fieldset>
</div>
</Fixture>
);
}
}
export default JumpingCursorTestCase;

View File

@@ -0,0 +1,68 @@
import FixtureSet from '../../FixtureSet';
import TestCase from '../../TestCase';
import JumpingCursorTestCase from './JumpingCursorTestCase';
import EmailEnabledAttributesTestCase from './EmailEnabledAttributesTestCase';
import EmailDisabledAttributesTestCase from './EmailDisabledAttributesTestCase';
const React = window.React;
function EmailInputs() {
return (
<FixtureSet title="Email inputs">
<TestCase
title="Spaces in email inputs"
description={`
Some browsers are trying to remove spaces from email inputs and after
doing this place cursor to the beginning.
`}
affectedBrowsers="Chrome">
<TestCase.Steps>
<li>Type space and character</li>
<li>Type character, space, character, delete last character</li>
</TestCase.Steps>
<TestCase.ExpectedResult>Cursor not moving.</TestCase.ExpectedResult>
<JumpingCursorTestCase />
</TestCase>
<TestCase
title="Attributes enabled"
description={`
Test enabled pattern, maxlength, multiple attributes.
`}>
<TestCase.Steps>
<li>Type after existing text ',b@tt.com'</li>
<li>Try to type spaces after typed text</li>
</TestCase.Steps>
<TestCase.ExpectedResult>
Spaces not added. When cursor hovered over input, popup "Please match
the requested format." is showed.
</TestCase.ExpectedResult>
<EmailEnabledAttributesTestCase />
</TestCase>
<TestCase
title="Attributes disabled"
description={`
Test disabled maxlength, multiple attributes.
`}>
<TestCase.Steps>
<li>Type after existing text ',b@tt.com'</li>
<li>Try to type spaces after typed text</li>
</TestCase.Steps>
<TestCase.ExpectedResult>
Spaces are added freely. When cursor hovered over input, popup "A part
following '@' should not contain the symbol ','." is showed.
</TestCase.ExpectedResult>
<EmailDisabledAttributesTestCase />
</TestCase>
</FixtureSet>
);
}
export default EmailInputs;

View File

@@ -105,7 +105,7 @@ class Hydration extends React.Component {
Version:
<VersionPicker
id="hydration_version"
name="hyration_version"
name="hydration_version"
version={version}
onChange={this.setVersion}
/>

View File

@@ -0,0 +1,87 @@
import Fixture from '../../Fixture';
import FixtureSet from '../../FixtureSet';
import TestCase from '../../TestCase';
const React = window.React;
class ProgressFixture extends React.Component {
state = {value: 0, max: 1, enabled: false, backwards: false};
startTest = () => {
this.setState({enabled: true}, () => {
this.progressIntervalId = setInterval(() => {
if (this.state.backwards) {
if (this.state.value > 0) {
this.setState({value: this.state.value - this.state.max / 100});
} else {
if (this.state.max === 10000) {
this.resetTest();
} else {
this.setState({max: this.state.max * 100, backwards: false});
}
}
} else {
if (this.state.value < this.state.max) {
this.setState({value: this.state.value + this.state.max / 100});
} else {
this.setState({backwards: true});
}
}
}, 10);
});
};
resetTest = () => {
clearInterval(this.progressIntervalId);
this.setState({value: 0, max: 1, enabled: false, backwards: false});
};
render() {
return (
<FixtureSet title="Progress">
<TestCase title="Fill and reset progress bar">
<TestCase.Steps>
<li>Press enable button</li>
</TestCase.Steps>
<TestCase.ExpectedResult>
When enabled, bar value should increase from 0% to 100% and
backwards during three step loop: 0-1, 0-100, 0-10000. Reset button
stops loop, sets value to 0 and max to 1.
</TestCase.ExpectedResult>
<Fixture>
<div className="control-box">
<fieldset>
<legend>Controlled</legend>
<progress
value={this.state.value}
max={this.state.max}></progress>
<button
onClick={
this.state.enabled ? this.resetTest : this.startTest
}>
{this.state.enabled ? 'Reset' : 'Enable'}
</button>
<br />
<span className="hint">
{' '}
max: {JSON.stringify(this.state.max)}
</span>
<span className="hint">
{' '}
value:{' '}
{JSON.stringify(
Math.round((this.state.value + Number.EPSILON) * 100) / 100
)}
</span>
</fieldset>
</div>
</Fixture>
</TestCase>
</FixtureSet>
);
}
}
export default ProgressFixture;

View File

@@ -16,8 +16,8 @@
If you checked out the source from GitHub make sure to run <code>npm run build</code>.
</p>
</div>
<script src="../../build/dist/react.development.js"></script>
<script src="../../build/dist/react-dom-unstable-fizz.browser.development.js"></script>
<script src="../../build/node_modules/react/umd/react.development.js"></script>
<script src="../../build/node_modules/react-dom/umd/react-dom-unstable-fizz.browser.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.js"></script>
<script type="text/babel">
let stream = ReactDOMFizzServer.renderToReadableStream(<body>Success</body>);

View File

@@ -16,11 +16,11 @@
If you checked out the source from GitHub make sure to run <code>npm run build</code>.
</p>
</div>
<script src="../../build/dist/react.development.js"></script>
<script src="../../build/dist/react-dom.development.js"></script>
<script src="../../build/dist/react-dom-server.browser.development.js"></script>
<script src="../../build/dist/react-flight-dom-webpack-server.browser.development.js"></script>
<script src="../../build/dist/react-flight-dom-webpack.development.js"></script>
<script src="../../build/node_modules/react/umd/react.development.js"></script>
<script src="../../build/node_modules/react-dom/umd/react-dom.development.js"></script>
<script src="../../build/node_modules/react-dom/umd/react-dom-server.browser.development.js"></script>
<script src="../../build/node_modules/react-transport-dom-webpack/umd/react-transport-dom-webpack-server.browser.development.js"></script>
<script src="../../build/node_modules/react-transport-dom-webpack/umd/react-transport-dom-webpack.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.js"></script>
<script type="text/babel">
let Suspense = React.Suspense;
@@ -57,12 +57,10 @@
let model = {
title: <Title />,
content: {
__html: <HTML />,
}
content: <HTML />,
};
let stream = ReactFlightDOMServer.renderToReadableStream(model);
let stream = ReactTransportDOMServer.renderToReadableStream(model);
let response = new Response(stream, {
headers: {'Content-Type': 'text/html'},
});
@@ -72,25 +70,25 @@
let blob = await responseToDisplay.blob();
let url = URL.createObjectURL(blob);
let data = ReactFlightDOMClient.readFromFetch(
let data = ReactTransportDOMClient.createFromFetch(
fetch(url)
);
// The client also supports XHR streaming.
// var xhr = new XMLHttpRequest();
// xhr.open('GET', url);
// let data = ReactFlightDOMClient.readFromXHR(xhr);
// let data = ReactTransportDOMClient.createFromXHR(xhr);
// xhr.send();
renderResult(data);
}
function Shell({ data }) {
let model = data.model;
let model = data.readRoot();
return <div>
<Suspense fallback="...">
<h1>{model.title}</h1>
</Suspense>
<div dangerouslySetInnerHTML={model.content} />
{model.content}
</div>;
}

View File

@@ -0,0 +1,93 @@
'use strict';
const fs = require('fs');
const path = require('path');
const paths = require('./paths');
// Make sure that including paths.js after env.js will read .env variables.
delete require.cache[require.resolve('./paths')];
const NODE_ENV = process.env.NODE_ENV;
if (!NODE_ENV) {
throw new Error(
'The NODE_ENV environment variable is required but was not specified.'
);
}
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
const dotenvFiles = [
`${paths.dotenv}.${NODE_ENV}.local`,
`${paths.dotenv}.${NODE_ENV}`,
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
NODE_ENV !== 'test' && `${paths.dotenv}.local`,
paths.dotenv,
].filter(Boolean);
// Load environment variables from .env* files. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set. Variable expansion is supported in .env files.
// https://github.com/motdotla/dotenv
// https://github.com/motdotla/dotenv-expand
dotenvFiles.forEach(dotenvFile => {
if (fs.existsSync(dotenvFile)) {
require('dotenv-expand')(
require('dotenv').config({
path: dotenvFile,
})
);
}
});
// We support resolving modules according to `NODE_PATH`.
// This lets you use absolute paths in imports inside large monorepos:
// https://github.com/facebook/create-react-app/issues/253.
// It works similar to `NODE_PATH` in Node itself:
// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
// https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
// We also resolve them to make sure all tools using them work consistently.
const appDirectory = fs.realpathSync(process.cwd());
process.env.NODE_PATH = (process.env.NODE_PATH || '')
.split(path.delimiter)
.filter(folder => folder && !path.isAbsolute(folder))
.map(folder => path.resolve(appDirectory, folder))
.join(path.delimiter);
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
// injected into the application via DefinePlugin in Webpack configuration.
const REACT_APP = /^REACT_APP_/i;
function getClientEnvironment(publicUrl) {
const raw = Object.keys(process.env)
.filter(key => REACT_APP.test(key))
.reduce(
(env, key) => {
env[key] = process.env[key];
return env;
},
{
// Useful for determining whether were running in production mode.
// Most importantly, it switches React into the correct mode.
NODE_ENV: process.env.NODE_ENV || 'development',
// Useful for resolving the correct path to static assets in `public`.
// For example, <img src={process.env.PUBLIC_URL + '/img/logo.png'} />.
// This should only be used as an escape hatch. Normally you would put
// images into the `src` and `import` them in code to get their paths.
PUBLIC_URL: publicUrl,
}
);
// Stringify all values so we can feed into Webpack DefinePlugin
const stringified = {
'process.env': Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key]);
return env;
}, {}),
};
return {raw, stringified};
}
module.exports = getClientEnvironment;

View File

@@ -0,0 +1,14 @@
'use strict';
// This is a custom Jest transformer turning style imports into empty objects.
// http://facebook.github.io/jest/docs/en/webpack.html
module.exports = {
process() {
return 'module.exports = {};';
},
getCacheKey() {
// The output is always the same.
return 'cssTransform';
},
};

View File

@@ -0,0 +1,40 @@
'use strict';
const path = require('path');
const camelcase = require('camelcase');
// This is a custom Jest transformer turning file imports into filenames.
// http://facebook.github.io/jest/docs/en/webpack.html
module.exports = {
process(src, filename) {
const assetFilename = JSON.stringify(path.basename(filename));
if (filename.match(/\.svg$/)) {
// Based on how SVGR generates a component name:
// https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6
const pascalCaseFilename = camelcase(path.parse(filename).name, {
pascalCase: true,
});
const componentName = `Svg${pascalCaseFilename}`;
return `const React = require('react');
module.exports = {
__esModule: true,
default: ${assetFilename},
ReactComponent: React.forwardRef(function ${componentName}(props, ref) {
return {
$$typeof: Symbol.for('react.element'),
type: 'svg',
ref: ref,
key: null,
props: Object.assign({}, props, {
children: ${assetFilename}
})
};
}),
};`;
}
return `module.exports = ${assetFilename};`;
},
};

View File

@@ -0,0 +1,141 @@
'use strict';
const fs = require('fs');
const path = require('path');
const paths = require('./paths');
const chalk = require('react-dev-utils/chalk');
const resolve = require('resolve');
/**
* Get additional module paths based on the baseUrl of a compilerOptions object.
*
* @param {Object} options
*/
function getAdditionalModulePaths(options = {}) {
const baseUrl = options.baseUrl;
// We need to explicitly check for null and undefined (and not a falsy value) because
// TypeScript treats an empty string as `.`.
if (baseUrl == null) {
// If there's no baseUrl set we respect NODE_PATH
// Note that NODE_PATH is deprecated and will be removed
// in the next major release of create-react-app.
const nodePath = process.env.NODE_PATH || '';
return nodePath.split(path.delimiter).filter(Boolean);
}
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
// We don't need to do anything if `baseUrl` is set to `node_modules`. This is
// the default behavior.
if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
return null;
}
// Allow the user set the `baseUrl` to `appSrc`.
if (path.relative(paths.appSrc, baseUrlResolved) === '') {
return [paths.appSrc];
}
// If the path is equal to the root directory we ignore it here.
// We don't want to allow importing from the root directly as source files are
// not transpiled outside of `src`. We do allow importing them with the
// absolute path (e.g. `src/Components/Button.js`) but we set that up with
// an alias.
if (path.relative(paths.appPath, baseUrlResolved) === '') {
return null;
}
// Otherwise, throw an error.
throw new Error(
chalk.red.bold(
"Your project's `baseUrl` can only be set to `src` or `node_modules`." +
' Create React App does not support other values at this time.'
)
);
}
/**
* Get webpack aliases based on the baseUrl of a compilerOptions object.
*
* @param {*} options
*/
function getWebpackAliases(options = {}) {
const baseUrl = options.baseUrl;
if (!baseUrl) {
return {};
}
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
if (path.relative(paths.appPath, baseUrlResolved) === '') {
return {
src: paths.appSrc,
};
}
}
/**
* Get jest aliases based on the baseUrl of a compilerOptions object.
*
* @param {*} options
*/
function getJestAliases(options = {}) {
const baseUrl = options.baseUrl;
if (!baseUrl) {
return {};
}
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
if (path.relative(paths.appPath, baseUrlResolved) === '') {
return {
'src/(.*)$': '<rootDir>/src/$1',
};
}
}
function getModules() {
// Check if TypeScript is setup
const hasTsConfig = fs.existsSync(paths.appTsConfig);
const hasJsConfig = fs.existsSync(paths.appJsConfig);
if (hasTsConfig && hasJsConfig) {
throw new Error(
'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
);
}
let config;
// If there's a tsconfig.json we assume it's a
// TypeScript project and set up the config
// based on tsconfig.json
if (hasTsConfig) {
const ts = require(resolve.sync('typescript', {
basedir: paths.appNodeModules,
}));
config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
// Otherwise we'll check if there is jsconfig.json
// for non TS projects.
} else if (hasJsConfig) {
config = require(paths.appJsConfig);
}
config = config || {};
const options = config.compilerOptions || {};
const additionalModulePaths = getAdditionalModulePaths(options);
return {
additionalModulePaths: additionalModulePaths,
webpackAliases: getWebpackAliases(options),
jestAliases: getJestAliases(options),
hasTsConfig,
};
}
module.exports = getModules();

View File

@@ -0,0 +1,88 @@
'use strict';
const path = require('path');
const fs = require('fs');
const url = require('url');
// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebook/create-react-app/issues/637
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
const envPublicUrl = process.env.PUBLIC_URL;
function ensureSlash(inputPath, needsSlash) {
const hasSlash = inputPath.endsWith('/');
if (hasSlash && !needsSlash) {
return inputPath.substr(0, inputPath.length - 1);
} else if (!hasSlash && needsSlash) {
return `${inputPath}/`;
} else {
return inputPath;
}
}
const getPublicUrl = appPackageJson =>
envPublicUrl || require(appPackageJson).homepage;
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// Webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
function getServedPath(appPackageJson) {
const publicUrl = getPublicUrl(appPackageJson);
const servedUrl =
envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
return ensureSlash(servedUrl, true);
}
const moduleFileExtensions = [
'web.mjs',
'mjs',
'web.js',
'js',
'web.ts',
'ts',
'web.tsx',
'tsx',
'json',
'web.jsx',
'jsx',
];
// Resolve file paths in the same order as webpack
const resolveModule = (resolveFn, filePath) => {
const extension = moduleFileExtensions.find(extension =>
fs.existsSync(resolveFn(`${filePath}.${extension}`))
);
if (extension) {
return resolveFn(`${filePath}.${extension}`);
}
return resolveFn(`${filePath}.js`);
};
// config after eject: we're in ./config/
module.exports = {
dotenv: resolveApp('.env'),
appPath: resolveApp('.'),
appBuild: resolveApp('build'),
appPublic: resolveApp('public'),
appHtml: resolveApp('public/index.html'),
appIndexJs: resolveModule(resolveApp, 'src/index'),
appPackageJson: resolveApp('package.json'),
appSrc: resolveApp('src'),
appTsConfig: resolveApp('tsconfig.json'),
appJsConfig: resolveApp('jsconfig.json'),
yarnLockFile: resolveApp('yarn.lock'),
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
proxySetup: resolveApp('src/setupProxy.js'),
appNodeModules: resolveApp('node_modules'),
publicUrl: getPublicUrl(resolveApp('package.json')),
servedPath: getServedPath(resolveApp('package.json')),
};
module.exports.moduleFileExtensions = moduleFileExtensions;

View File

@@ -0,0 +1,35 @@
'use strict';
const {resolveModuleName} = require('ts-pnp');
exports.resolveModuleName = (
typescript,
moduleName,
containingFile,
compilerOptions,
resolutionHost
) => {
return resolveModuleName(
moduleName,
containingFile,
compilerOptions,
resolutionHost,
typescript.resolveModuleName
);
};
exports.resolveTypeReferenceDirective = (
typescript,
moduleName,
containingFile,
compilerOptions,
resolutionHost
) => {
return resolveModuleName(
moduleName,
containingFile,
compilerOptions,
resolutionHost,
typescript.resolveTypeReferenceDirective
);
};

View File

@@ -0,0 +1,686 @@
'use strict';
// Fork Start
const ReactFlightWebpackPlugin = require('react-transport-dom-webpack/plugin');
// Fork End
const fs = require('fs');
const isWsl = require('is-wsl');
const path = require('path');
const webpack = require('webpack');
const resolve = require('resolve');
const PnpWebpackPlugin = require('pnp-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const safePostCssParser = require('postcss-safe-parser');
const ManifestPlugin = require('webpack-manifest-plugin');
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
const paths = require('./paths');
const modules = require('./modules');
const getClientEnvironment = require('./env');
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
const ForkTsCheckerWebpackPlugin = require('react-dev-utils/ForkTsCheckerWebpackPlugin');
const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
const eslint = require('eslint');
const postcssNormalize = require('postcss-normalize');
const appPackageJson = require(paths.appPackageJson);
// Source maps are resource heavy and can cause out of memory issue for large source files.
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
// Some apps do not need the benefits of saving a web request, so not inlining the chunk
// makes for a smoother build process.
const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
const imageInlineSizeLimit = parseInt(
process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'
);
// Check if TypeScript is setup
const useTypeScript = fs.existsSync(paths.appTsConfig);
// style files regexes
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
// This is the production and development configuration.
// It is focused on developer experience, fast rebuilds, and a minimal bundle.
module.exports = function(webpackEnv) {
const isEnvDevelopment = webpackEnv === 'development';
const isEnvProduction = webpackEnv === 'production';
// Variable used for enabling profiling in Production
// passed into alias object. Uses a flag if passed into the build command
const isEnvProductionProfile =
isEnvProduction && process.argv.includes('--profile');
// Webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
// In development, we always serve from the root. This makes config easier.
const publicPath = isEnvProduction
? paths.servedPath
: isEnvDevelopment && '/';
// Some apps do not use client-side routing with pushState.
// For these, "homepage" can be set to "." to enable relative asset paths.
const shouldUseRelativeAssetPaths = publicPath === './';
// `publicUrl` is just like `publicPath`, but we will provide it to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
const publicUrl = isEnvProduction
? publicPath.slice(0, -1)
: isEnvDevelopment && '';
// Get environment variables to inject into our app.
const env = getClientEnvironment(publicUrl);
// common function to get style loaders
const getStyleLoaders = (cssOptions, preProcessor) => {
const loaders = [
isEnvDevelopment && require.resolve('style-loader'),
isEnvProduction && {
loader: MiniCssExtractPlugin.loader,
options: shouldUseRelativeAssetPaths ? {publicPath: '../../'} : {},
},
{
loader: require.resolve('css-loader'),
options: cssOptions,
},
{
// Options for PostCSS as we reference these options twice
// Adds vendor prefixing based on your specified browser support in
// package.json
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebook/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
}),
// Adds PostCSS Normalize as the reset css with default options,
// so that it honors browserslist config in package.json
// which in turn let's users customize the target behavior as per their needs.
postcssNormalize(),
],
sourceMap: isEnvProduction && shouldUseSourceMap,
},
},
].filter(Boolean);
if (preProcessor) {
loaders.push(
{
loader: require.resolve('resolve-url-loader'),
options: {
sourceMap: isEnvProduction && shouldUseSourceMap,
},
},
{
loader: require.resolve(preProcessor),
options: {
sourceMap: true,
},
}
);
}
return loaders;
};
return {
mode: isEnvProduction ? 'production' : isEnvDevelopment && 'development',
// Stop compilation early in production
bail: isEnvProduction,
devtool: isEnvProduction
? shouldUseSourceMap
? 'source-map'
: false
: isEnvDevelopment && 'cheap-module-source-map',
// These are the "entry points" to our application.
// This means they will be the "root" imports that are included in JS bundle.
entry: [
// Include an alternative client for WebpackDevServer. A client's job is to
// connect to WebpackDevServer by a socket and get notified about changes.
// When you save a file, the client will either apply hot updates (in case
// of CSS changes), or refresh the page (in case of JS changes). When you
// make a syntax error, this client will display a syntax error overlay.
// Note: instead of the default WebpackDevServer client, we use a custom one
// to bring better experience for Create React App users. You can replace
// the line below with these two lines if you prefer the stock client:
// require.resolve('webpack-dev-server/client') + '?/',
// require.resolve('webpack/hot/dev-server'),
isEnvDevelopment &&
require.resolve('react-dev-utils/webpackHotDevClient'),
// Finally, this is your app's code:
paths.appIndexJs,
// We include the app code last so that if there is a runtime error during
// initialization, it doesn't blow up the WebpackDevServer client, and
// changing JS code would still trigger a refresh.
].filter(Boolean),
output: {
// The build folder.
path: isEnvProduction ? paths.appBuild : undefined,
// Add /* filename */ comments to generated require()s in the output.
pathinfo: isEnvDevelopment,
// There will be one main bundle, and one file per asynchronous chunk.
// In development, it does not produce real files.
filename: isEnvProduction
? 'static/js/[name].[contenthash:8].js'
: isEnvDevelopment && 'static/js/bundle.js',
// TODO: remove this when upgrading to webpack 5
futureEmitAssets: true,
// There are also additional JS chunk files if you use code splitting.
chunkFilename: isEnvProduction
? 'static/js/[name].[contenthash:8].chunk.js'
: isEnvDevelopment && 'static/js/[name].chunk.js',
// We inferred the "public path" (such as / or /my-project) from homepage.
// We use "/" in development.
publicPath: publicPath,
// Point sourcemap entries to original disk location (format as URL on Windows)
devtoolModuleFilenameTemplate: isEnvProduction
? info =>
path
.relative(paths.appSrc, info.absoluteResourcePath)
.replace(/\\/g, '/')
: isEnvDevelopment &&
(info => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')),
// Prevents conflicts when multiple Webpack runtimes (from different apps)
// are used on the same page.
jsonpFunction: `webpackJsonp${appPackageJson.name}`,
// this defaults to 'window', but by setting it to 'this' then
// module chunks which are built will work in web workers as well.
globalObject: 'this',
},
optimization: {
minimize: isEnvProduction,
minimizer: [
// This is only used in production mode
new TerserPlugin({
terserOptions: {
parse: {
// We want terser to parse ecma 8 code. However, we don't want it
// to apply any minification steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
// https://github.com/facebook/create-react-app/issues/2376
// Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false,
// Disabled because of an issue with Terser breaking valid code:
// https://github.com/facebook/create-react-app/issues/5250
// Pending further investigation:
// https://github.com/terser-js/terser/issues/120
inline: 2,
},
mangle: {
safari10: true,
},
// Added for profiling in devtools
keep_classnames: isEnvProductionProfile,
keep_fnames: isEnvProductionProfile,
output: {
ecma: 5,
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true,
},
},
// Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1
// Disabled on WSL (Windows Subsystem for Linux) due to an issue with Terser
// https://github.com/webpack-contrib/terser-webpack-plugin/issues/21
parallel: !isWsl,
// Enable file caching
cache: true,
sourceMap: shouldUseSourceMap,
}),
// This is only used in production mode
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
parser: safePostCssParser,
map: shouldUseSourceMap
? {
// `inline: false` forces the sourcemap to be output into a
// separate file
inline: false,
// `annotation: true` appends the sourceMappingURL to the end of
// the css file, helping the browser find the sourcemap
annotation: true,
}
: false,
},
}),
],
// Automatically split vendor and commons
// https://twitter.com/wSokra/status/969633336732905474
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
splitChunks: {
chunks: 'all',
name: false,
},
// Keep the runtime chunk separated to enable long term caching
// https://twitter.com/wSokra/status/969679223278505985
// https://github.com/facebook/create-react-app/issues/5358
runtimeChunk: {
name: entrypoint => `runtime-${entrypoint.name}`,
},
},
resolve: {
// This allows you to set a fallback for where Webpack should look for modules.
// We placed these paths second because we want `node_modules` to "win"
// if there are any conflicts. This matches Node resolution mechanism.
// https://github.com/facebook/create-react-app/issues/253
modules: ['node_modules', paths.appNodeModules].concat(
modules.additionalModulePaths || []
),
// These are the reasonable defaults supported by the Node ecosystem.
// We also include JSX as a common component filename extension to support
// some tools, although we do not recommend using it, see:
// https://github.com/facebook/create-react-app/issues/290
// `web` extension prefixes have been added for better support
// for React Native Web.
extensions: paths.moduleFileExtensions
.map(ext => `.${ext}`)
.filter(ext => useTypeScript || !ext.includes('ts')),
alias: {
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
'react-native': 'react-native-web',
// Allows for better profiling with ReactDevTools
...(isEnvProductionProfile && {
'react-dom$': 'react-dom/profiling',
'scheduler/tracing': 'scheduler/tracing-profiling',
}),
...(modules.webpackAliases || {}),
},
plugins: [
// Adds support for installing with Plug'n'Play, leading to faster installs and adding
// guards against forgotten dependencies and such.
PnpWebpackPlugin,
// Prevents users from importing files from outside of src/ (or node_modules/).
// This often causes confusion because we only process files within src/ with babel.
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
// please link the files into your node_modules/ and let module-resolution kick in.
// Make sure your source files are compiled, as they will not be processed in any way.
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
],
},
resolveLoader: {
plugins: [
// Also related to Plug'n'Play, but this time it tells Webpack to load its loaders
// from the current package.
PnpWebpackPlugin.moduleLoader(module),
],
},
module: {
strictExportPresence: true,
rules: [
// Disable require.ensure as it's not a standard language feature.
{parser: {requireEnsure: false}},
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
enforce: 'pre',
use: [
{
options: {
cache: true,
formatter: require.resolve('react-dev-utils/eslintFormatter'),
eslintPath: require.resolve('eslint'),
resolvePluginsRelativeTo: __dirname,
},
loader: require.resolve('eslint-loader'),
},
],
include: paths.appSrc,
},
{
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
// back to the "file" loader at the end of the loader list.
oneOf: [
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: imageInlineSizeLimit,
name: 'static/media/[name].[hash:8].[ext]',
},
},
// Process application JS with Babel.
// The preset includes JSX, Flow, TypeScript, and some ESnext features.
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
customize: require.resolve(
'babel-preset-react-app/webpack-overrides'
),
plugins: [
[
require.resolve('babel-plugin-named-asset-import'),
{
loaderMap: {
svg: {
ReactComponent:
'@svgr/webpack?-svgo,+titleProp,+ref![path]',
},
},
},
],
],
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
compact: isEnvProduction,
},
},
// Process any JS outside of the app with Babel.
// Unlike the application JS, we only compile the standard ES features.
{
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
presets: [
[
require.resolve('babel-preset-react-app/dependencies'),
{helpers: true},
],
],
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
// If an error happens in a package, it's possible to be
// because it was compiled. Thus, we don't want the browser
// debugger to show the original code. Instead, the code
// being evaluated would be much more helpful.
sourceMaps: false,
},
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader turns CSS into JS modules that inject <style> tags.
// In production, we use MiniCSSExtractPlugin to extract that CSS
// to a file, but in development "style" loader enables hot editing
// of CSS.
// By default we support CSS Modules with the extension .module.css
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction && shouldUseSourceMap,
}),
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true,
},
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
// using the extension .module.css
{
test: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction && shouldUseSourceMap,
modules: true,
getLocalIdent: getCSSModuleLocalIdent,
}),
},
// Opt-in support for SASS (using .scss or .sass extensions).
// By default we support SASS Modules with the
// extensions .module.scss or .module.sass
{
test: sassRegex,
exclude: sassModuleRegex,
use: getStyleLoaders(
{
importLoaders: 2,
sourceMap: isEnvProduction && shouldUseSourceMap,
},
'sass-loader'
),
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true,
},
// Adds support for CSS Modules, but using SASS
// using the extension .module.scss or .module.sass
{
test: sassModuleRegex,
use: getStyleLoaders(
{
importLoaders: 2,
sourceMap: isEnvProduction && shouldUseSourceMap,
modules: true,
getLocalIdent: getCSSModuleLocalIdent,
},
'sass-loader'
),
},
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
// This loader doesn't use a "test" so it will catch all modules
// that fall through the other loaders.
{
loader: require.resolve('file-loader'),
// Exclude `js` files to keep "css" loader working as it injects
// its runtime that would otherwise be processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
// ** STOP ** Are you adding a new loader?
// Make sure to add the new loader(s) before the "file" loader.
],
},
],
},
plugins: [
// Generates an `index.html` file with the <script> injected.
new HtmlWebpackPlugin(
Object.assign(
{},
{
inject: true,
template: paths.appHtml,
},
isEnvProduction
? {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}
: undefined
)
),
// Inlines the webpack runtime script. This script is too small to warrant
// a network request.
// https://github.com/facebook/create-react-app/issues/5358
isEnvProduction &&
shouldInlineRuntimeChunk &&
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime-.+[.]js/]),
// Makes some environment variables available in index.html.
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
// <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
// In production, it will be an empty string unless you specify "homepage"
// in `package.json`, in which case it will be the pathname of that URL.
// In development, this will be an empty string.
new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
// This gives some necessary context to module not found errors, such as
// the requesting resource.
new ModuleNotFoundPlugin(paths.appPath),
// Makes some environment variables available to the JS code, for example:
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
// It is absolutely essential that NODE_ENV is set to production
// during a production build.
// Otherwise React will be compiled in the very slow development mode.
new webpack.DefinePlugin(env.stringified),
// This is necessary to emit hot updates (currently CSS only):
isEnvDevelopment && new webpack.HotModuleReplacementPlugin(),
// Watcher doesn't work well if you mistype casing in a path so we use
// a plugin that prints an error when you attempt to do this.
// See https://github.com/facebook/create-react-app/issues/240
isEnvDevelopment && new CaseSensitivePathsPlugin(),
// If you require a missing module and then `npm install` it, you still have
// to restart the development server for Webpack to discover it. This plugin
// makes the discovery automatic so you don't have to restart.
// See https://github.com/facebook/create-react-app/issues/186
isEnvDevelopment &&
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
isEnvProduction &&
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: 'static/css/[name].[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
}),
// Generate an asset manifest file with the following content:
// - "files" key: Mapping of all asset filenames to their corresponding
// output file so that tools can pick it up without having to parse
// `index.html`
// - "entrypoints" key: Array of files which are included in `index.html`,
// can be used to reconstruct the HTML if necessary
new ManifestPlugin({
fileName: 'asset-manifest.json',
publicPath: publicPath,
generate: (seed, files, entrypoints) => {
const manifestFiles = files.reduce((manifest, file) => {
manifest[file.name] = file.path;
return manifest;
}, seed);
const entrypointFiles = entrypoints.main.filter(
fileName => !fileName.endsWith('.map')
);
return {
files: manifestFiles,
entrypoints: entrypointFiles,
};
},
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
isEnvProduction &&
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
importWorkboxFrom: 'cdn',
navigateFallback: publicUrl + '/index.html',
navigateFallbackBlacklist: [
// Exclude URLs starting with /_, as they're likely an API call
new RegExp('^/_'),
// Exclude any URLs whose last part seems to be a file extension
// as they're likely a resource and not a SPA route.
// URLs containing a "?" character won't be blacklisted as they're likely
// a route with query params (e.g. auth callbacks).
new RegExp('/[^/?]+\\.[^/]+$'),
],
}),
// TypeScript type checking
useTypeScript &&
new ForkTsCheckerWebpackPlugin({
typescript: resolve.sync('typescript', {
basedir: paths.appNodeModules,
}),
async: isEnvDevelopment,
useTypescriptIncrementalApi: true,
checkSyntacticErrors: true,
resolveModuleNameModule: process.versions.pnp
? `${__dirname}/pnpTs.js`
: undefined,
resolveTypeReferenceDirectiveModule: process.versions.pnp
? `${__dirname}/pnpTs.js`
: undefined,
tsconfig: paths.appTsConfig,
reportFiles: [
'**',
'!**/__tests__/**',
'!**/?(*.)(spec|test).*',
'!**/src/setupProxy.*',
'!**/src/setupTests.*',
],
silent: true,
// The formatter is invoked directly in WebpackDevServerUtils during development
formatter: isEnvProduction ? typescriptFormatter : undefined,
}),
// Fork Start
new ReactFlightWebpackPlugin({isServer: false}),
// Fork End
].filter(Boolean),
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
node: {
module: 'empty',
dgram: 'empty',
dns: 'mock',
fs: 'empty',
http2: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty',
},
// Turn off performance processing because we utilize
// our own hints via the FileSizeReporter
performance: false,
};
};

View File

@@ -0,0 +1,104 @@
'use strict';
const errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware');
const evalSourceMapMiddleware = require('react-dev-utils/evalSourceMapMiddleware');
const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware');
const ignoredFiles = require('react-dev-utils/ignoredFiles');
const paths = require('./paths');
const fs = require('fs');
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
const host = process.env.HOST || '0.0.0.0';
module.exports = function(proxy, allowedHost) {
return {
// WebpackDevServer 2.4.3 introduced a security fix that prevents remote
// websites from potentially accessing local content through DNS rebinding:
// https://github.com/webpack/webpack-dev-server/issues/887
// https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a
// However, it made several existing use cases such as development in cloud
// environment or subdomains in development significantly more complicated:
// https://github.com/facebook/create-react-app/issues/2271
// https://github.com/facebook/create-react-app/issues/2233
// While we're investigating better solutions, for now we will take a
// compromise. Since our WDS configuration only serves files in the `public`
// folder we won't consider accessing them a vulnerability. However, if you
// use the `proxy` feature, it gets more dangerous because it can expose
// remote code execution vulnerabilities in backends like Django and Rails.
// So we will disable the host check normally, but enable it if you have
// specified the `proxy` setting. Finally, we let you override it if you
// really know what you're doing with a special environment variable.
disableHostCheck:
!proxy || process.env.DANGEROUSLY_DISABLE_HOST_CHECK === 'true',
// Enable gzip compression of generated files.
compress: true,
// Silence WebpackDevServer's own logs since they're generally not useful.
// It will still show compile warnings and errors with this setting.
clientLogLevel: 'none',
// By default WebpackDevServer serves physical files from current directory
// in addition to all the virtual build products that it serves from memory.
// This is confusing because those files wont automatically be available in
// production build folder unless we copy them. However, copying the whole
// project directory is dangerous because we may expose sensitive files.
// Instead, we establish a convention that only files in `public` directory
// get served. Our build script will copy `public` into the `build` folder.
// In `index.html`, you can get URL of `public` folder with %PUBLIC_URL%:
// <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
// In JavaScript code, you can access it with `process.env.PUBLIC_URL`.
// Note that we only recommend to use `public` folder as an escape hatch
// for files like `favicon.ico`, `manifest.json`, and libraries that are
// for some reason broken when imported through Webpack. If you just want to
// use an image, put it in `src` and `import` it from JavaScript instead.
contentBase: paths.appPublic,
// By default files from `contentBase` will not trigger a page reload.
watchContentBase: true,
// Enable hot reloading server. It will provide /sockjs-node/ endpoint
// for the WebpackDevServer client so it can learn when the files were
// updated. The WebpackDevServer client is included as an entry point
// in the Webpack development configuration. Note that only changes
// to CSS are currently hot reloaded. JS changes will refresh the browser.
hot: true,
// It is important to tell WebpackDevServer to use the same "root" path
// as we specified in the config. In development, we always serve from /.
publicPath: '/',
// WebpackDevServer is noisy by default so we emit custom message instead
// by listening to the compiler events with `compiler.hooks[...].tap` calls above.
quiet: true,
// Reportedly, this avoids CPU overload on some systems.
// https://github.com/facebook/create-react-app/issues/293
// src/node_modules is not ignored to support absolute imports
// https://github.com/facebook/create-react-app/issues/1065
watchOptions: {
ignored: ignoredFiles(paths.appSrc),
},
// Enable HTTPS if the HTTPS environment variable is set to 'true'
https: protocol === 'https',
host,
overlay: false,
historyApiFallback: {
// Paths with dots should still use the history fallback.
// See https://github.com/facebook/create-react-app/issues/387.
disableDotRule: true,
},
public: allowedHost,
proxy,
before(app, server) {
if (fs.existsSync(paths.proxySetup)) {
// This registers user provided middleware for proxy reasons
require(paths.proxySetup)(app);
}
// This lets us fetch source contents from webpack for the error overlay
app.use(evalSourceMapMiddleware(server));
// This lets us open files from the runtime error overlay.
app.use(errorOverlayMiddleware());
// This service worker file is effectively a 'no-op' that will reset any
// previous service worker registered for the same host:port combination.
// We do this in development to avoid hitting the production cache if
// it used the same host and port.
// https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
app.use(noopServiceWorkerMiddleware());
},
};
};

View File

@@ -3,21 +3,72 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/core": "7.6.0",
"@babel/register": "^7.7.0",
"@svgr/webpack": "4.3.2",
"@typescript-eslint/eslint-plugin": "^2.2.0",
"@typescript-eslint/parser": "^2.2.0",
"babel-eslint": "10.0.3",
"babel-jest": "^24.9.0",
"babel-loader": "8.0.6",
"babel-plugin-named-asset-import": "^0.3.4",
"babel-preset-react-app": "^9.0.2",
"camelcase": "^5.2.0",
"case-sensitive-paths-webpack-plugin": "2.2.0",
"concurrently": "^5.0.0",
"css-loader": "2.1.1",
"dotenv": "6.2.0",
"dotenv-expand": "5.1.0",
"eslint": "^6.1.0",
"eslint-config-react-app": "^5.0.2",
"eslint-loader": "3.0.2",
"eslint-plugin-flowtype": "3.13.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.14.3",
"eslint-plugin-react-hooks": "^1.6.1",
"express": "^4.17.1",
"react-scripts": "3.2.0"
"file-loader": "3.0.1",
"fs-extra": "7.0.1",
"html-webpack-plugin": "4.0.0-beta.5",
"identity-obj-proxy": "3.0.0",
"is-wsl": "^1.1.0",
"jest": "24.9.0",
"jest-environment-jsdom-fourteen": "0.1.0",
"jest-resolve": "24.9.0",
"jest-watch-typeahead": "0.4.0",
"mini-css-extract-plugin": "0.8.0",
"optimize-css-assets-webpack-plugin": "5.0.3",
"pnp-webpack-plugin": "1.5.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-normalize": "7.0.1",
"postcss-preset-env": "6.7.0",
"postcss-safe-parser": "4.0.1",
"react-app-polyfill": "^1.0.4",
"react-dev-utils": "^9.1.0",
"resolve": "1.12.0",
"resolve-url-loader": "3.1.0",
"sass-loader": "7.2.0",
"semver": "6.3.0",
"style-loader": "1.0.0",
"terser-webpack-plugin": "1.4.1",
"ts-pnp": "1.1.4",
"url-loader": "2.1.0",
"webpack": "4.41.0",
"webpack-dev-server": "3.2.1",
"webpack-manifest-plugin": "2.1.1",
"workbox-webpack-plugin": "4.3.1"
},
"scripts": {
"prestart": "cp -r ../../build/node_modules/* ./node_modules/",
"prebuild": "cp -r ../../build/node_modules/* ./node_modules/",
"start": "concurrently \"npm run start:server\" \"npm run start:client\"",
"start:client": "react-scripts start",
"start:client": "node scripts/start.js",
"start:server": "NODE_ENV=development node server",
"start:prod": "react-scripts build && NODE_ENV=production node server",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
"start:prod": "node scripts/build.js && NODE_ENV=production node server",
"build": "node scripts/build.js",
"test": "node scripts/test.js --env=jsdom"
},
"eslintConfig": {
"extends": "react-app"
@@ -33,5 +84,58 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"jest": {
"roots": [
"<rootDir>/src"
],
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"setupFiles": [
"react-app-polyfill/jsdom"
],
"setupFilesAfterEnv": [],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"testEnvironment": "jest-environment-jsdom-fourteen",
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
"modulePaths": [],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
],
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
]
},
"babel": {
"presets": [
"react-app"
]
}
}

View File

@@ -0,0 +1,200 @@
'use strict';
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'production';
process.env.NODE_ENV = 'production';
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});
// Ensure environment variables are read.
require('../config/env');
const path = require('path');
const chalk = require('react-dev-utils/chalk');
const fs = require('fs-extra');
const webpack = require('webpack');
const configFactory = require('../config/webpack.config');
const paths = require('../config/paths');
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
const printBuildError = require('react-dev-utils/printBuildError');
const measureFileSizesBeforeBuild =
FileSizeReporter.measureFileSizesBeforeBuild;
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
const useYarn = fs.existsSync(paths.yarnLockFile);
// These sizes are pretty large. We'll warn for bundles exceeding them.
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
const isInteractive = process.stdout.isTTY;
// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
process.exit(1);
}
// Generate configuration
const config = configFactory('production');
// We require that you explicitly set browsers and do not fall back to
// browserslist defaults.
const {checkBrowsers} = require('react-dev-utils/browsersHelper');
checkBrowsers(paths.appPath, isInteractive)
.then(() => {
// First, read the current file sizes in build directory.
// This lets us display how much they changed later.
return measureFileSizesBeforeBuild(paths.appBuild);
})
.then(previousFileSizes => {
// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
fs.emptyDirSync(paths.appBuild);
// Merge with the public folder
copyPublicFolder();
// Start the webpack build
return build(previousFileSizes);
})
.then(
({stats, previousFileSizes, warnings}) => {
if (warnings.length) {
console.log(chalk.yellow('Compiled with warnings.\n'));
console.log(warnings.join('\n\n'));
console.log(
'\nSearch for the ' +
chalk.underline(chalk.yellow('keywords')) +
' to learn more about each warning.'
);
console.log(
'To ignore, add ' +
chalk.cyan('// eslint-disable-next-line') +
' to the line before.\n'
);
} else {
console.log(chalk.green('Compiled successfully.\n'));
}
console.log('File sizes after gzip:\n');
printFileSizesAfterBuild(
stats,
previousFileSizes,
paths.appBuild,
WARN_AFTER_BUNDLE_GZIP_SIZE,
WARN_AFTER_CHUNK_GZIP_SIZE
);
console.log();
const appPackage = require(paths.appPackageJson);
const publicUrl = paths.publicUrl;
const publicPath = config.output.publicPath;
const buildFolder = path.relative(process.cwd(), paths.appBuild);
printHostingInstructions(
appPackage,
publicUrl,
publicPath,
buildFolder,
useYarn
);
},
err => {
const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
if (tscCompileOnError) {
console.log(
chalk.yellow(
'Compiled with the following type errors (you may want to check these before deploying your app):\n'
)
);
printBuildError(err);
} else {
console.log(chalk.red('Failed to compile.\n'));
printBuildError(err);
process.exit(1);
}
}
)
.catch(err => {
if (err && err.message) {
console.log(err.message);
}
process.exit(1);
});
// Create the production build and print the deployment instructions.
function build(previousFileSizes) {
// We used to support resolving modules according to `NODE_PATH`.
// This now has been deprecated in favor of jsconfig/tsconfig.json
// This lets you use absolute paths in imports inside large monorepos:
if (process.env.NODE_PATH) {
console.log(
chalk.yellow(
'Setting NODE_PATH to resolve modules absolutely has been deprecated in favor of setting baseUrl in jsconfig.json (or tsconfig.json if you are using TypeScript) and will be removed in a future major release of create-react-app.'
)
);
console.log();
}
console.log('Creating an optimized production build...');
const compiler = webpack(config);
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
let messages;
if (err) {
if (!err.message) {
return reject(err);
}
messages = formatWebpackMessages({
errors: [err.message],
warnings: [],
});
} else {
messages = formatWebpackMessages(
stats.toJson({all: false, warnings: true, errors: true})
);
}
if (messages.errors.length) {
// Only keep the first error. Others are often indicative
// of the same problem, but confuse the reader with noise.
if (messages.errors.length > 1) {
messages.errors.length = 1;
}
return reject(new Error(messages.errors.join('\n\n')));
}
if (
process.env.CI &&
(typeof process.env.CI !== 'string' ||
process.env.CI.toLowerCase() !== 'false') &&
messages.warnings.length
) {
console.log(
chalk.yellow(
'\nTreating warnings as errors because process.env.CI = true.\n' +
'Most CI servers set it automatically.\n'
)
);
return reject(new Error(messages.warnings.join('\n\n')));
}
return resolve({
stats,
previousFileSizes,
warnings: messages.warnings,
});
});
});
}
function copyPublicFolder() {
fs.copySync(paths.appPublic, paths.appBuild, {
dereference: true,
filter: file => file !== paths.appHtml,
});
}

View File

@@ -0,0 +1,146 @@
'use strict';
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'development';
process.env.NODE_ENV = 'development';
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});
// Ensure environment variables are read.
require('../config/env');
const fs = require('fs');
const chalk = require('react-dev-utils/chalk');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const clearConsole = require('react-dev-utils/clearConsole');
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
const {
choosePort,
createCompiler,
prepareProxy,
prepareUrls,
} = require('react-dev-utils/WebpackDevServerUtils');
const openBrowser = require('react-dev-utils/openBrowser');
const paths = require('../config/paths');
const configFactory = require('../config/webpack.config');
const createDevServerConfig = require('../config/webpackDevServer.config');
const useYarn = fs.existsSync(paths.yarnLockFile);
const isInteractive = process.stdout.isTTY;
// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
process.exit(1);
}
// Tools like Cloud9 rely on this.
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
const HOST = process.env.HOST || '0.0.0.0';
if (process.env.HOST) {
console.log(
chalk.cyan(
`Attempting to bind to HOST environment variable: ${chalk.yellow(
chalk.bold(process.env.HOST)
)}`
)
);
console.log(
`If this was unintentional, check that you haven't mistakenly set it in your shell.`
);
console.log(
`Learn more here: ${chalk.yellow('https://bit.ly/CRA-advanced-config')}`
);
console.log();
}
// We require that you explicitly set browsers and do not fall back to
// browserslist defaults.
const {checkBrowsers} = require('react-dev-utils/browsersHelper');
checkBrowsers(paths.appPath, isInteractive)
.then(() => {
// We attempt to use the default port but if it is busy, we offer the user to
// run on a different port. `choosePort()` Promise resolves to the next free port.
return choosePort(HOST, DEFAULT_PORT);
})
.then(port => {
if (port == null) {
// We have not found a port.
return;
}
const config = configFactory('development');
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
const appName = require(paths.appPackageJson).name;
const useTypeScript = fs.existsSync(paths.appTsConfig);
const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';
const urls = prepareUrls(protocol, HOST, port);
const devSocket = {
warnings: warnings =>
devServer.sockWrite(devServer.sockets, 'warnings', warnings),
errors: errors =>
devServer.sockWrite(devServer.sockets, 'errors', errors),
};
// Create a webpack compiler that is configured with custom messages.
const compiler = createCompiler({
appName,
config,
devSocket,
urls,
useYarn,
useTypeScript,
tscCompileOnError,
webpack,
});
// Load proxy config
const proxySetting = require(paths.appPackageJson).proxy;
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
// Serve webpack assets generated by the compiler over a web server.
const serverConfig = createDevServerConfig(
proxyConfig,
urls.lanUrlForConfig
);
const devServer = new WebpackDevServer(compiler, serverConfig);
// Launch WebpackDevServer.
devServer.listen(port, HOST, err => {
if (err) {
return console.log(err);
}
if (isInteractive) {
clearConsole();
}
// We used to support resolving modules according to `NODE_PATH`.
// This now has been deprecated in favor of jsconfig/tsconfig.json
// This lets you use absolute paths in imports inside large monorepos:
if (process.env.NODE_PATH) {
console.log(
chalk.yellow(
'Setting NODE_PATH to resolve modules absolutely has been deprecated in favor of setting baseUrl in jsconfig.json (or tsconfig.json if you are using TypeScript) and will be removed in a future major release of create-react-app.'
)
);
console.log();
}
console.log(chalk.cyan('Starting the development server...\n'));
openBrowser(urls.localUrlForBrowser);
});
['SIGINT', 'SIGTERM'].forEach(function(sig) {
process.on(sig, function() {
devServer.close();
process.exit();
});
});
})
.catch(err => {
if (err && err.message) {
console.log(err.message);
}
process.exit(1);
});

View File

@@ -0,0 +1,51 @@
'use strict';
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'test';
process.env.NODE_ENV = 'test';
process.env.PUBLIC_URL = '';
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});
// Ensure environment variables are read.
require('../config/env');
const jest = require('jest');
const execSync = require('child_process').execSync;
let argv = process.argv.slice(2);
function isInGitRepository() {
try {
execSync('git rev-parse --is-inside-work-tree', {stdio: 'ignore'});
return true;
} catch (e) {
return false;
}
}
function isInMercurialRepository() {
try {
execSync('hg --cwd . root', {stdio: 'ignore'});
return true;
} catch (e) {
return false;
}
}
// Watch unless on CI or explicitly running all tests
if (
!process.env.CI &&
argv.indexOf('--watchAll') === -1 &&
argv.indexOf('--watchAll=false') === -1
) {
// https://github.com/facebook/create-react-app/issues/5210
const hasSourceControl = isInGitRepository() || isInMercurialRepository();
argv.push(hasSourceControl ? '--watch' : '--watchAll');
}
jest.run(argv);

View File

@@ -1,6 +1,6 @@
'use strict';
const ReactFlightDOMServer = require('react-flight-dom-webpack/server');
const ReactTransportDOMServer = require('react-transport-dom-webpack/server');
const React = require('react');
const Stream = require('stream');
@@ -20,9 +20,7 @@ function HTML() {
module.exports = function(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
let model = {
content: {
__html: <HTML />,
},
content: <HTML />,
};
ReactFlightDOMServer.pipeToNodeWritable(model, res);
ReactTransportDOMServer.pipeToNodeWritable(model, res);
};

View File

@@ -1,7 +1,7 @@
import React, {Suspense} from 'react';
function Content({data}) {
return <p dangerouslySetInnerHTML={data.model.content} />;
return data.readRoot().content;
}
function App({data}) {

View File

@@ -1,7 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
import ReactFlightDOMClient from 'react-flight-dom-webpack';
import ReactTransportDOMClient from 'react-transport-dom-webpack';
import App from './App';
let data = ReactFlightDOMClient.readFromFetch(fetch('http://localhost:3001'));
let data = ReactTransportDOMClient.createFromFetch(
fetch('http://localhost:3001')
);
ReactDOM.render(<App data={data} />, document.getElementById('root'));

View File

@@ -4267,10 +4267,6 @@ fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
fsevents@2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.0.7.tgz#382c9b443c6cbac4c57187cdda23aa3bf1ccfc2a"
fsevents@^1.2.2:
version "1.2.4"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.2.4.tgz#f41dcb1af2582af3692da36fc55cbd8e1041c426"
@@ -7806,66 +7802,6 @@ react-is@^16.8.1, react-is@^16.8.4:
version "16.11.0"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.11.0.tgz#b85dfecd48ad1ce469ff558a882ca8e8313928fa"
react-scripts@3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/react-scripts/-/react-scripts-3.2.0.tgz#58ccd6b4ffa27f1b4d2986cbdcaa916660e9e33c"
dependencies:
"@babel/core" "7.6.0"
"@svgr/webpack" "4.3.2"
"@typescript-eslint/eslint-plugin" "^2.2.0"
"@typescript-eslint/parser" "^2.2.0"
babel-eslint "10.0.3"
babel-jest "^24.9.0"
babel-loader "8.0.6"
babel-plugin-named-asset-import "^0.3.4"
babel-preset-react-app "^9.0.2"
camelcase "^5.2.0"
case-sensitive-paths-webpack-plugin "2.2.0"
css-loader "2.1.1"
dotenv "6.2.0"
dotenv-expand "5.1.0"
eslint "^6.1.0"
eslint-config-react-app "^5.0.2"
eslint-loader "3.0.2"
eslint-plugin-flowtype "3.13.0"
eslint-plugin-import "2.18.2"
eslint-plugin-jsx-a11y "6.2.3"
eslint-plugin-react "7.14.3"
eslint-plugin-react-hooks "^1.6.1"
file-loader "3.0.1"
fs-extra "7.0.1"
html-webpack-plugin "4.0.0-beta.5"
identity-obj-proxy "3.0.0"
is-wsl "^1.1.0"
jest "24.9.0"
jest-environment-jsdom-fourteen "0.1.0"
jest-resolve "24.9.0"
jest-watch-typeahead "0.4.0"
mini-css-extract-plugin "0.8.0"
optimize-css-assets-webpack-plugin "5.0.3"
pnp-webpack-plugin "1.5.0"
postcss-flexbugs-fixes "4.1.0"
postcss-loader "3.0.0"
postcss-normalize "7.0.1"
postcss-preset-env "6.7.0"
postcss-safe-parser "4.0.1"
react-app-polyfill "^1.0.4"
react-dev-utils "^9.1.0"
resolve "1.12.0"
resolve-url-loader "3.1.0"
sass-loader "7.2.0"
semver "6.3.0"
style-loader "1.0.0"
terser-webpack-plugin "1.4.1"
ts-pnp "1.1.4"
url-loader "2.1.0"
webpack "4.41.0"
webpack-dev-server "3.2.1"
webpack-manifest-plugin "2.1.1"
workbox-webpack-plugin "4.3.1"
optionalDependencies:
fsevents "2.0.7"
read-pkg-up@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be"

View File

@@ -7,6 +7,7 @@ module.exports = {
react: '../../../../build/node_modules/react/umd/react.development',
'react-dom':
'../../../../build/node_modules/react-dom/umd/react-dom.development',
schedule: '../../../../build/dist/schedule.development',
schedule:
'../../../../build/node_modules/scheduler/umd/schedule.development',
},
};

View File

@@ -7,6 +7,7 @@ module.exports = {
react: '../../../../build/node_modules/react/umd/react.production.min',
'react-dom':
'../../../../build/node_modules/react-dom/umd/react-dom.production.min',
schedule: '../../../../build/dist/schedule.development',
schedule:
'../../../../build/node_modules/scheduler/umd/schedule.development',
},
};

View File

@@ -3,6 +3,7 @@ System.config({
react: '../../../../build/node_modules/react/umd/react.development.js',
'react-dom':
'../../../../build/node_modules/react-dom/umd/react-dom.development.js',
schedule: '../../../../build/dist/schedule.development',
schedule:
'../../../../build/node_modules/scheduler/umd/schedule.development',
},
});

View File

@@ -3,6 +3,7 @@ System.config({
react: '../../../../build/node_modules/react/umd/react.production.min.js',
'react-dom':
'../../../../build/node_modules/react-dom/umd/react-dom.production.min.js',
schedule: '../../../../build/dist/schedule.development',
schedule:
'../../../../build/node_modules/scheduler/umd/schedule.development',
},
});

View File

@@ -0,0 +1,80 @@
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called"
);
}
return self;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true,
});
} else {
obj[key] = value;
}
return obj;
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
subClass.__proto__ = superClass;
}
// Compile this with Babel.
// babel --config-file ./babel.config.json BabelClasses.js --out-file BabelClasses-compiled.js --source-maps
let BabelClass = /*#__PURE__*/ (function(_React$Component) {
_inheritsLoose(BabelClass, _React$Component);
function BabelClass() {
return _React$Component.apply(this, arguments) || this;
}
var _proto = BabelClass.prototype;
_proto.render = function render() {
return this.props.children;
};
return BabelClass;
})(React.Component);
let BabelClassWithFields = /*#__PURE__*/ (function(_React$Component2) {
_inheritsLoose(BabelClassWithFields, _React$Component2);
function BabelClassWithFields(...args) {
var _this;
_this = _React$Component2.call(this, ...args) || this;
_defineProperty(
_assertThisInitialized(_assertThisInitialized(_this)),
'props',
void 0
);
_defineProperty(
_assertThisInitialized(_assertThisInitialized(_this)),
'state',
{}
);
return _this;
}
var _proto2 = BabelClassWithFields.prototype;
_proto2.render = function render() {
return this.props.children;
};
return BabelClassWithFields;
})(React.Component);
//# sourceMappingURL=BabelClasses-compiled.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["BabelClasses.js"],"names":[],"mappings":";;;;;;AAAA;AACA;IAEM,U;;;;;;;;;SACJ,M,qBAAS;AACP,WAAO,KAAK,KAAL,CAAW,QAAlB;AACD,G;;;EAHsB,KAAK,CAAC,S;;IAMzB,oB;;;;;;;;;;oFAGI,E;;;;;;;UACR,M,qBAAS;AACP,WAAO,KAAK,KAAL,CAAW,QAAlB;AACD,G;;;EANgC,KAAK,CAAC,S","file":"BabelClasses-compiled.js","sourcesContent":["// Compile this with Babel.\n// babel --config-file ./babel.config.json BabelClasses.js --out-file BabelClasses-compiled.js --source-maps\n\nclass BabelClass extends React.Component {\n render() {\n return this.props.children;\n }\n}\n\nclass BabelClassWithFields extends React.Component {\n // These compile to defineProperty which can break some interception techniques.\n props;\n state = {};\n render() {\n return this.props.children;\n }\n}\n"]}

View File

@@ -0,0 +1,17 @@
// Compile this with Babel.
// babel --config-file ./babel.config.json BabelClasses.js --out-file BabelClasses-compiled.js --source-maps
class BabelClass extends React.Component {
render() {
return this.props.children;
}
}
class BabelClassWithFields extends React.Component {
// These compile to defineProperty which can break some interception techniques.
props;
state = {};
render() {
return this.props.children;
}
}

View File

@@ -0,0 +1,30 @@
// Example
const Throw = React.lazy(() => {
throw new Error('Example');
});
const Component = React.memo(function Component({children}) {
return children;
});
function DisplayName({children}) {
return children;
}
DisplayName.displayName = 'Custom Name';
class NativeClass extends React.Component {
render() {
return this.props.children;
}
}
class FrozenClass extends React.Component {
constructor() {
super();
}
render() {
return this.props.children;
}
}
Object.freeze(FrozenClass.prototype);

View File

@@ -0,0 +1,67 @@
// Example
const x = React.createElement;
class ErrorBoundary extends React.Component {
static getDerivedStateFromError(error) {
return {
error: error,
};
}
componentDidCatch(error, errorInfo) {
console.log(error.message, errorInfo.componentStack);
this.setState({
componentStack: errorInfo.componentStack,
});
}
render() {
if (this.state && this.state.error) {
return x(
'div',
null,
x('h3', null, this.state.error.message),
x('pre', null, this.state.componentStack)
);
}
return this.props.children;
}
}
function Example() {
let state = React.useState(false);
return x(
ErrorBoundary,
null,
x(
DisplayName,
null,
x(
React.SuspenseList,
null,
x(
NativeClass,
null,
x(
FrozenClass,
null,
x(
BabelClass,
null,
x(
BabelClassWithFields,
null,
x(
React.Suspense,
null,
x('div', null, x(Component, null, x(Throw)))
)
)
)
)
)
)
)
);
}

View File

@@ -0,0 +1,6 @@
{
"plugins": [
["@babel/plugin-proposal-class-properties", {"loose": false}],
["@babel/plugin-transform-classes", {"loose": true}]
]
}

View File

@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Component Stacks</title>
<style>
html, body {
margin: 20px;
}
pre {
background: #eee;
border: 1px solid #ccc;
padding: 2px;
}
</style>
</head>
<body>
<div id="container">
<p>
To install React, follow the instructions on
<a href="https://github.com/facebook/react/">GitHub</a>.
</p>
<p>
If you can see this, React is <strong>not</strong> working right.
If you checked out the source from GitHub make sure to run <code>npm run build</code>.
</p>
</div>
<script src="../../build/node_modules/react/umd/react.production.min.js"></script>
<script src="../../build/node_modules/react-dom/umd/react-dom.production.min.js"></script>
<script src="./Components.js"></script>
<script src="./BabelClasses-compiled.js"></script>
<script src="./Example.js"></script>
<script>
const container = document.getElementById("container");
ReactDOM.render(React.createElement(Example), container);
</script>
<h3>The above stack should look something like this:</h3>
<pre>
at Lazy
at Component (/stacks/Component.js:7:1)
at div
at Suspense
at BabelClassWithFields (/stacks/BabelClasses-compiled.js:31:31)
at BabelClass (/stacks/BabelClass-compiled.js:13:29)
at FrozenClass (/stacks/Components.js:22:1)
at NativeClass (/stacks/Component.js:16:1)
at SuspenseList
at Custom Name (/stacks/Component.js:11:1)
at ErrorBoundary (/stacks/Example.js:5:1)
at Example (/stacks/Example.js:32:1)</pre>
</body>
</html>

View File

@@ -65,7 +65,7 @@
<script>
if (window.location.search.includes('puppeteer=true')) {
// Colocated calls to performance.now() often yield different values in Puppeteer.
// Collocated calls to performance.now() often yield different values in Puppeteer.
// This causes the Scheduler API test to fail.
// For the purposes of our automated release scripts,
// Coerce tests to use Date.now() instead to reduce the chances of a false positive.

View File

@@ -44,10 +44,9 @@
"core-js": "^3.6.4",
"coveralls": "^3.0.9",
"create-react-class": "^15.6.3",
"cross-env": "^6.0.3",
"danger": "^9.2.10",
"error-stack-parser": "^2.0.6",
"eslint": "^6.8.0",
"eslint": "^7.0.0",
"eslint-config-fbjs": "^1.1.1",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-babel": "^5.3.0",
@@ -56,19 +55,20 @@
"eslint-plugin-no-for-of-loops": "^1.0.0",
"eslint-plugin-react": "^6.7.1",
"eslint-plugin-react-internal": "link:./scripts/eslint-rules",
"fbjs-scripts": "0.8.3",
"fbjs-scripts": "1.2.0",
"filesize": "^6.0.1",
"flow-bin": "0.97",
"glob": "^7.1.6",
"glob-stream": "^6.1.0",
"google-closure-compiler": "^20200112.0.0",
"google-closure-compiler": "^20200517.0.0",
"gzip-size": "^5.1.1",
"jasmine-check": "^1.0.0-rc.0",
"jest": "^25.1.0",
"jest-diff": "^25.1.0",
"jest": "^25.2.7",
"jest-cli": "^25.2.7",
"jest-diff": "^25.2.6",
"jest-snapshot-serializer-raw": "^1.1.0",
"minimatch": "^3.0.4",
"minimist": "^1.2.0",
"minimist": "^1.2.3",
"mkdirp": "^0.5.1",
"ncp": "^2.0.0",
"object-assign": "^4.1.1",
@@ -90,37 +90,45 @@
"through2": "^3.0.1",
"tmp": "^0.1.0",
"typescript": "^3.7.5",
"webpack": "^4.41.2"
"webpack": "^4.41.2",
"yargs": "^15.3.1"
},
"devEngines": {
"node": "8.x || 9.x || 10.x || 11.x || 12.x || 13.x"
"node": "8.x || 9.x || 10.x || 11.x || 12.x || 13.x || 14.x"
},
"jest": {
"testRegex": "/scripts/jest/dont-run-jest-directly\\.js$"
},
"scripts": {
"build": "node ./scripts/rollup/build.js",
"build-for-devtools": "cross-env RELEASE_CHANNEL=experimental yarn build -- react/index,react-dom,react-is,react-debug-tools,scheduler,react-test-renderer --type=NODE",
"build-for-devtools": "cross-env RELEASE_CHANNEL=experimental yarn build react/index,react-dom,react-is,react-debug-tools,scheduler,react-test-renderer --type=NODE",
"linc": "node ./scripts/tasks/linc.js",
"lint": "node ./scripts/tasks/eslint.js",
"lint-build": "node ./scripts/rollup/validate/index.js",
"extract-errors": "yarn build --type=dev --extract-errors",
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json && node ./scripts/flow/createFlowConfigs.js && node ./scripts/yarn/downloadReactIsForPrettyFormat.js",
"debug-test": "cross-env NODE_ENV=development node --inspect-brk node_modules/jest/bin/jest.js --config ./scripts/jest/config.source.js --runInBand",
"test": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.source.js",
"test-persistent": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.source-persistent.js",
"debug-test-persistent": "cross-env NODE_ENV=development node --inspect-brk node_modules/jest/bin/jest.js --config ./scripts/jest/config.source-persistent.js --runInBand",
"test-prod": "cross-env NODE_ENV=production jest --config ./scripts/jest/config.source.js",
"test-prod-build": "yarn test-build-prod",
"test-build": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.build.js",
"test-build-prod": "cross-env NODE_ENV=production jest --config ./scripts/jest/config.build.js",
"test-build-devtools": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.build-devtools.js",
"debug-test-build-devtools": "cross-env NODE_ENV=development node --inspect-brk node_modules/jest/bin/jest.js --config ./scripts/jest/config.build-devtools.js",
"debug-test": "yarn test --debug",
"test": "node ./scripts/jest/jest-cli.js",
"test-www": "yarn test --release-channel=www-modern",
"test-www-variant": "yarn test --release-channel=www-modern --variant",
"test-prod-www": "yarn test --prod --release-channel=www-modern",
"test-prod-www-variant": "yarn test --prod --release-channel=www-modern --variant",
"test-persistent": "yarn test --persistent",
"debug-test-persistent": "yarn test --debug --persistent",
"test-prod": "yarn test --prod",
"debug-test-prod": "yarn test --debug --prod",
"test-prod-build": "yarn test --prod --build",
"test-build": "yarn test --build",
"test-build-prod": "yarn test --build --prod",
"test-build-devtools": "yarn test --build --project devtools",
"debug-test-build-devtools": "yarn test --debug --build --project devtools",
"test-dom-fixture": "cd fixtures/dom && yarn && yarn prestart && yarn test",
"flow": "node ./scripts/tasks/flow.js",
"flow-ci": "node ./scripts/tasks/flow-ci.js",
"prettier": "node ./scripts/prettier/index.js write-changed",
"prettier-all": "node ./scripts/prettier/index.js write",
"version-check": "node ./scripts/tasks/version-check.js"
"version-check": "node ./scripts/tasks/version-check.js",
"merge-fork": "node ./scripts/merge-fork/merge-fork.js",
"replace-fork": "node ./scripts/merge-fork/replace-fork.js"
}
}

View File

@@ -1,5 +0,0 @@
This package is intended to eventually replace the current `@babel/plugin-transform-react-jsx`, changing the JSX transform from targeting `React.createElement(type, props, children)` to `React.jsx(types, props, key)`.
https://github.com/reactjs/rfcs/blob/createlement-rfc/text/0000-create-element-changes.md
**This is experimental and not intended to be used directly.**

View File

@@ -1,870 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable quotes */
'use strict';
const babel = require('@babel/core');
const codeFrame = require('@babel/code-frame');
const {wrap} = require('jest-snapshot-serializer-raw');
function transform(input, pluginOpts, babelOpts) {
return wrap(
babel.transform(input, {
configFile: false,
sourceType: 'module',
plugins: [
'@babel/plugin-syntax-jsx',
'@babel/plugin-transform-arrow-functions',
...(pluginOpts && pluginOpts.development
? [
'@babel/plugin-transform-react-jsx-source',
'@babel/plugin-transform-react-jsx-self',
]
: []),
[
'./packages/babel-plugin-react-jsx',
{
useBuiltIns: true,
useCreateElement: false,
...pluginOpts,
},
],
],
...babelOpts,
}).code
);
}
describe('transform react to jsx', () => {
it('auto import pragma overrides regular pragma', () => {
expect(
transform(
`/** @jsxAutoImport defaultExport */
var x = <div><span /></div>
`,
{
autoImport: 'namespace',
importSource: 'foobar',
}
)
).toMatchSnapshot();
});
it('import source pragma overrides regular pragma', () => {
expect(
transform(
`/** @jsxImportSource baz */
var x = <div><span /></div>
`,
{
autoImport: 'namespace',
importSource: 'foobar',
}
)
).toMatchSnapshot();
});
it('multiple pragmas work', () => {
expect(
transform(
`/** Some comment here
* @jsxImportSource baz
* @jsxAutoImport defaultExport
*/
var x = <div><span /></div>
`,
{
autoImport: 'namespace',
importSource: 'foobar',
}
)
).toMatchSnapshot();
});
it('throws error when sourceType is module and autoImport is require', () => {
const code = `var x = <div><span /></div>`;
expect(() => {
transform(code, {
autoImport: 'require',
});
}).toThrow(
'Babel `sourceType` must be set to `script` for autoImport ' +
'to use `require` syntax. See Babel `sourceType` for details.\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 1}, end: {line: 1, column: 28}},
{highlightCode: true}
)
);
});
it('throws error when sourceType is script and autoImport is not require', () => {
const code = `var x = <div><span /></div>`;
expect(() => {
transform(
code,
{
autoImport: 'namespace',
},
{sourceType: 'script'}
);
}).toThrow(
'Babel `sourceType` must be set to `module` for autoImport ' +
'to use `namespace` syntax. See Babel `sourceType` for details.\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 1}, end: {line: 1, column: 28}},
{highlightCode: true}
)
);
});
it("auto import that doesn't exist should throw error", () => {
const code = `var x = <div><span /></div>`;
expect(() => {
transform(code, {
autoImport: 'foo',
});
}).toThrow(
'autoImport must be one of the following: none, require, namespace, defaultExport, namedExports\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 1}, end: {line: 1, column: 28}},
{highlightCode: true}
)
);
});
it('auto import can specify source', () => {
expect(
transform(`var x = <div><span /></div>`, {
autoImport: 'namespace',
importSource: 'foobar',
})
).toMatchSnapshot();
});
it('auto import require', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'require',
},
{
sourceType: 'script',
}
)
).toMatchSnapshot();
});
it('auto import namespace', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'namespace',
}
)
).toMatchSnapshot();
});
it('auto import default', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'defaultExport',
}
)
).toMatchSnapshot();
});
it('auto import named exports', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'namedExports',
}
)
).toMatchSnapshot();
});
it('auto import with no JSX', () => {
expect(
transform(
`var foo = "<div></div>"`,
{
autoImport: 'require',
},
{
sourceType: 'script',
}
)
).toMatchSnapshot();
});
it('complicated scope require', () => {
expect(
transform(
`
const Bar = () => {
const Foo = () => {
const Component = ({thing, ..._react}) => {
if (!thing) {
var _react2 = "something useless";
var b = _react3();
var c = _react5();
var jsx = 1;
var _jsx = 2;
return <div />;
};
return <span />;
};
}
}
`,
{
autoImport: 'require',
},
{
sourceType: 'script',
}
)
).toMatchSnapshot();
});
it('complicated scope named exports', () => {
expect(
transform(
`
const Bar = () => {
const Foo = () => {
const Component = ({thing, ..._react}) => {
if (!thing) {
var _react2 = "something useless";
var b = _react3();
var jsx = 1;
var _jsx = 2;
return <div />;
};
return <span />;
};
}
}
`,
{
autoImport: 'namedExports',
}
)
).toMatchSnapshot();
});
it('auto import in dev', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'namedExports',
development: true,
}
)
).toMatchSnapshot();
});
it('auto import none', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`,
{
autoImport: 'none',
}
)
).toMatchSnapshot();
});
it('auto import undefined', () => {
expect(
transform(
`var x = (
<>
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
</>
);`
)
).toMatchSnapshot();
});
it('auto import with namespaces already defined', () => {
expect(
transform(
`
import * as _react from "foo";
const react = _react(1);
const _react1 = react;
const _react2 = react;
var x = (
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
);`,
{
autoImport: 'namespace',
}
)
).toMatchSnapshot();
});
it('auto import with react already defined', () => {
expect(
transform(
`
import * as react from "react";
var y = react.createElement("div", {foo: 1});
var x = (
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
<div {...props} key="4" />
</div>
);`,
{
autoImport: 'namespace',
}
)
).toMatchSnapshot();
});
it('fragment with no children', () => {
expect(transform(`var x = <></>`)).toMatchSnapshot();
});
it('fragments', () => {
expect(transform(`var x = <><div /></>`)).toMatchSnapshot();
});
it('fragments to set keys', () => {
expect(
transform(`var x = <React.Fragment key="foo"></React.Fragment>`)
).toMatchSnapshot();
});
it('React.fragment to set keys and source', () => {
expect(
transform(`var x = <React.Fragment key='foo'></React.Fragment>`, {
development: true,
})
).toMatchSnapshot();
});
it('fragments in dev mode (no key and source)', () => {
expect(
transform(`var x = <><div /></>`, {
development: true,
})
).toMatchSnapshot();
});
it('nonStatic children', () => {
expect(
transform(
`var x = (
<div>
{[<span key={'0'} />, <span key={'1'} />]}
</div>
);
`,
{
development: true,
}
)
).toMatchSnapshot();
});
it('static children', () => {
expect(
transform(
`var x = (
<div>
<span />
{[<span key={'0'} />, <span key={'1'} />]}
</div>
);
`,
{
development: true,
}
)
).toMatchSnapshot();
});
it('uses jsxDEV instead of jsx in dev mode', () => {
expect(
transform(`var x = <span propOne="one">Hi</span>`, {development: true})
).toMatchSnapshot();
});
it('properly passes in source and self', () => {
expect(
transform(`var x = <div />;`, {development: true})
).toMatchSnapshot();
});
it('should properly handle potentially null variables', () => {
expect(
transform(`
var foo = null;
var x = <div {...foo} />;
`)
).toMatchSnapshot();
});
it('properly handles keys', () => {
expect(
transform(`var x = (
<div>
<div key="1" />
<div key="2" meow="wolf" />
<div key="3" />
</div>
);`)
).toMatchSnapshot();
});
it('uses createElement when the key comes after a spread', () => {
expect(
transform(`var x = (
<div {...props} key="1" foo="bar" />
);`)
).toMatchSnapshot();
});
it('uses jsx when the key comes before a spread', () => {
expect(
transform(`var x = (
<div key="1" {...props} foo="bar" />
);`)
).toMatchSnapshot();
});
it('should properly handle comments adjacent to children', () => {
expect(
transform(`
var x = (
<div>
{/* A comment at the beginning */}
{/* A second comment at the beginning */}
<span>
{/* A nested comment */}
</span>
{/* A sandwiched comment */}
<br />
{/* A comment at the end */}
{/* A second comment at the end */}
</div>
);
`)
).toMatchSnapshot();
});
it('adds appropriate new lines when using spread attribute', () => {
expect(transform(`<Component {...props} sound="moo" />`)).toMatchSnapshot();
});
it('arrow functions', () => {
expect(
transform(`
var foo = function () {
return () => <this />;
};
var bar = function () {
return () => <this.foo />;
};
`)
).toMatchSnapshot();
});
it('assignment', () => {
expect(
transform(`var div = <Component {...props} foo="bar" />`)
).toMatchSnapshot();
});
it('concatenates adjacent string literals', () => {
expect(
transform(`
var x =
<div>
foo
{"bar"}
baz
<div>
buz
bang
</div>
qux
{null}
quack
</div>
`)
).toMatchSnapshot();
});
it('should allow constructor as prop', () => {
expect(transform(`<Component constructor="foo" />;`)).toMatchSnapshot();
});
it('should allow deeper js namespacing', () => {
expect(
transform(`<Namespace.DeepNamespace.Component />;`)
).toMatchSnapshot();
});
it('should allow elements as attributes', () => {
expect(transform(`<div attr=<div /> />`)).toMatchSnapshot();
});
it('should allow js namespacing', () => {
expect(transform(`<Namespace.Component />;`)).toMatchSnapshot();
});
it('should allow nested fragments', () => {
expect(
transform(`
<div>
< >
<>
<span>Hello</span>
<span>world</span>
</>
<>
<span>Goodbye</span>
<span>world</span>
</>
</>
</div>
`)
).toMatchSnapshot();
});
it('should avoid wrapping in extra parens if not needed', () => {
expect(
transform(`
var x = <div>
<Component />
</div>;
var x = <div>
{props.children}
</div>;
var x = <Composite>
{props.children}
</Composite>;
var x = <Composite>
<Composite2 />
</Composite>;
`)
).toMatchSnapshot();
});
it('should convert simple tags', () => {
expect(transform(`var x = <div></div>;`)).toMatchSnapshot();
});
it('should convert simple text', () => {
expect(transform(`var x = <div>text</div>;`)).toMatchSnapshot();
});
it('should disallow spread children', () => {
let _error;
const code = `<div>{...children}</div>;`;
try {
transform(code);
} catch (error) {
_error = error;
}
expect(_error).toEqual(
new SyntaxError(
'unknown: Spread children are not supported in React.' +
'\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 6}, end: {line: 1, column: 19}},
{highlightCode: true}
)
)
);
});
it('should escape xhtml jsxattribute', () => {
expect(
transform(`
<div id="wôw" />;
<div id="\w" />;
<div id="w &lt; w" />;
`)
).toMatchSnapshot();
});
it('should escape xhtml jsxtext', () => {
/* eslint-disable no-irregular-whitespace */
expect(
transform(`
<div>wow</div>;
<div>wôw</div>;
<div>w & w</div>;
<div>w &amp; w</div>;
<div>w &nbsp; w</div>;
<div>this should not parse as unicode: \u00a0</div>;
<div>this should parse as nbsp:   </div>;
<div>this should parse as unicode: {'\u00a0 '}</div>;
<div>w &lt; w</div>;
`)
).toMatchSnapshot();
/*eslint-enable */
});
it('should handle attributed elements', () => {
expect(
transform(`
var HelloMessage = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
React.render(<HelloMessage name={
<span>
Sebastian
</span>
} />, mountNode);
`)
).toMatchSnapshot();
});
it('should handle has own property correctly', () => {
expect(
transform(`<hasOwnProperty>testing</hasOwnProperty>;`)
).toMatchSnapshot();
});
it('should have correct comma in nested children', () => {
expect(
transform(`
var x = <div>
<div><br /></div>
<Component>{foo}<br />{bar}</Component>
<br />
</div>;
`)
).toMatchSnapshot();
});
it('should insert commas after expressions before whitespace', () => {
expect(
transform(`
var x =
<div
attr1={
"foo" + "bar"
}
attr2={
"foo" + "bar" +
"baz" + "bug"
}
attr3={
"foo" + "bar" +
"baz" + "bug"
}
attr4="baz">
</div>
`)
).toMatchSnapshot();
});
it('should not add quotes to identifier names', () => {
expect(
transform(`var e = <F aaa new const var default foo-bar/>;`)
).toMatchSnapshot();
});
it('should not strip nbsp even couple with other whitespace', () => {
expect(transform(`<div>&nbsp; </div>;`)).toMatchSnapshot();
});
it('should not strip tags with a single child of nbsp', () => {
expect(transform(`<div>&nbsp;</div>;`)).toMatchSnapshot();
});
it('should properly handle comments between props', () => {
expect(
transform(`
var x = (
<div
/* a multi-line
comment */
attr1="foo">
<span // a double-slash comment
attr2="bar"
/>
</div>
);
`)
).toMatchSnapshot();
});
it('should quote jsx attributes', () => {
expect(
transform(`<button data-value='a value'>Button</button>`)
).toMatchSnapshot();
});
it('should support xml namespaces if flag', () => {
expect(
transform('<f:image n:attr />', {throwIfNamespace: false})
).toMatchSnapshot();
});
it('should throw error namespaces if not flag', () => {
let _error;
const code = `<f:image />`;
try {
transform(code);
} catch (error) {
_error = error;
}
expect(_error).toEqual(
new SyntaxError(
"unknown: Namespace tags are not supported by default. React's " +
"JSX doesn't support namespace tags. You can turn on the " +
"'throwIfNamespace' flag to bypass this warning." +
'\n' +
codeFrame.codeFrameColumns(
code,
{start: {line: 1, column: 2}, end: {line: 1, column: 9}},
{highlightCode: true}
)
)
);
});
it('should transform known hyphenated tags', () => {
expect(transform(`<font-face />`)).toMatchSnapshot();
});
it('wraps props in react spread for first spread attributes', () => {
expect(transform(`<Component {...x} y={2} z />`)).toMatchSnapshot();
});
it('wraps props in react spread for last spread attributes', () => {
expect(transform(`<Component y={2} z { ... x } />`)).toMatchSnapshot();
});
it('wraps props in react spread for middle spread attributes', () => {
expect(transform(`<Component y={2} { ... x } z />`)).toMatchSnapshot();
});
it('useBuiltIns false uses extend instead of Object.assign', () => {
expect(
transform(`<Component y={2} {...x} />`, {useBuiltIns: false})
).toMatchSnapshot();
});
it('duplicate children prop should transform into sequence expression with actual children', () => {
expect(
transform(`<Component children={1}>2</Component>`)
).toMatchSnapshot();
});
it('duplicate children prop should transform into sequence expression with next prop', () => {
expect(
transform(`<Component children={1} foo={3}>2</Component>`)
).toMatchSnapshot();
});
it('duplicate children props should transform into sequence expression with next prop', () => {
expect(
transform(`<Component children={1} children={4} foo={3}>2</Component>`)
).toMatchSnapshot();
});
it('duplicate children prop should transform into sequence expression with spread', () => {
expect(
transform(`<Component children={1} {...x}>2</Component>`)
).toMatchSnapshot();
});
});

View File

@@ -1,645 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`transform react to jsx React.fragment to set keys and source 1`] = `
var _jsxFileName = "";
var x = React.jsxDEV(React.Fragment, {}, "foo", false, {
fileName: _jsxFileName,
lineNumber: 1
}, this);
`;
exports[`transform react to jsx adds appropriate new lines when using spread attribute 1`] = `
React.jsx(Component, Object.assign({}, props, {
sound: "moo"
}));
`;
exports[`transform react to jsx arrow functions 1`] = `
var foo = function () {
var _this = this;
return function () {
return React.jsx(_this, {});
};
};
var bar = function () {
var _this2 = this;
return function () {
return React.jsx(_this2.foo, {});
};
};
`;
exports[`transform react to jsx assignment 1`] = `
var div = React.jsx(Component, Object.assign({}, props, {
foo: "bar"
}));
`;
exports[`transform react to jsx auto import can specify source 1`] = `
import * as _foobar from "foobar";
var x = _foobar.jsx("div", {
children: _foobar.jsx("span", {})
});
`;
exports[`transform react to jsx auto import default 1`] = `
import _default from "react";
var x = _default.jsx(_default.Fragment, {
children: _default.jsxs("div", {
children: [_default.jsx("div", {}, "1"), _default.jsx("div", {
meow: "wolf"
}, "2"), _default.jsx("div", {}, "3"), _default.createElement("div", Object.assign({}, props, {
key: "4"
}))]
})
});
`;
exports[`transform react to jsx auto import in dev 1`] = `
import { createElement as _createElement } from "react";
import { jsxDEV as _jsxDEV } from "react";
import { Fragment as _Fragment } from "react";
var _jsxFileName = "";
var x = _jsxDEV(_Fragment, {
children: _jsxDEV("div", {
children: [_jsxDEV("div", {}, "1", false, {
fileName: _jsxFileName,
lineNumber: 4
}, this), _jsxDEV("div", {
meow: "wolf"
}, "2", false, {
fileName: _jsxFileName,
lineNumber: 5
}, this), _jsxDEV("div", {}, "3", false, {
fileName: _jsxFileName,
lineNumber: 6
}, this), _createElement("div", Object.assign({}, props, {
key: "4",
__source: {
fileName: _jsxFileName,
lineNumber: 7
},
__self: this
}))]
}, undefined, true, {
fileName: _jsxFileName,
lineNumber: 3
}, this)
}, undefined, false);
`;
exports[`transform react to jsx auto import named exports 1`] = `
import { createElement as _createElement } from "react";
import { jsx as _jsx } from "react";
import { jsxs as _jsxs } from "react";
import { Fragment as _Fragment } from "react";
var x = _jsx(_Fragment, {
children: _jsxs("div", {
children: [_jsx("div", {}, "1"), _jsx("div", {
meow: "wolf"
}, "2"), _jsx("div", {}, "3"), _createElement("div", Object.assign({}, props, {
key: "4"
}))]
})
});
`;
exports[`transform react to jsx auto import namespace 1`] = `
import * as _react from "react";
var x = _react.jsx(_react.Fragment, {
children: _react.jsxs("div", {
children: [_react.jsx("div", {}, "1"), _react.jsx("div", {
meow: "wolf"
}, "2"), _react.jsx("div", {}, "3"), _react.createElement("div", Object.assign({}, props, {
key: "4"
}))]
})
});
`;
exports[`transform react to jsx auto import none 1`] = `
var x = React.jsx(React.Fragment, {
children: React.jsxs("div", {
children: [React.jsx("div", {}, "1"), React.jsx("div", {
meow: "wolf"
}, "2"), React.jsx("div", {}, "3"), React.createElement("div", Object.assign({}, props, {
key: "4"
}))]
})
});
`;
exports[`transform react to jsx auto import pragma overrides regular pragma 1`] = `
import _default from "foobar";
/** @jsxAutoImport defaultExport */
var x = _default.jsx("div", {
children: _default.jsx("span", {})
});
`;
exports[`transform react to jsx auto import require 1`] = `
var _react = require("react");
var x = _react.jsx(_react.Fragment, {
children: _react.jsxs("div", {
children: [_react.jsx("div", {}, "1"), _react.jsx("div", {
meow: "wolf"
}, "2"), _react.jsx("div", {}, "3"), _react.createElement("div", Object.assign({}, props, {
key: "4"
}))]
})
});
`;
exports[`transform react to jsx auto import undefined 1`] = `
var x = React.jsx(React.Fragment, {
children: React.jsxs("div", {
children: [React.jsx("div", {}, "1"), React.jsx("div", {
meow: "wolf"
}, "2"), React.jsx("div", {}, "3"), React.createElement("div", Object.assign({}, props, {
key: "4"
}))]
})
});
`;
exports[`transform react to jsx auto import with namespaces already defined 1`] = `
import * as _react3 from "react";
import * as _react from "foo";
const react = _react(1);
const _react1 = react;
const _react2 = react;
var x = _react3.jsxs("div", {
children: [_react3.jsx("div", {}, "1"), _react3.jsx("div", {
meow: "wolf"
}, "2"), _react3.jsx("div", {}, "3"), _react3.createElement("div", Object.assign({}, props, {
key: "4"
}))]
});
`;
exports[`transform react to jsx auto import with no JSX 1`] = `var foo = "<div></div>";`;
exports[`transform react to jsx auto import with react already defined 1`] = `
import * as _react from "react";
import * as react from "react";
var y = react.createElement("div", {
foo: 1
});
var x = _react.jsxs("div", {
children: [_react.jsx("div", {}, "1"), _react.jsx("div", {
meow: "wolf"
}, "2"), _react.jsx("div", {}, "3"), _react.createElement("div", Object.assign({}, props, {
key: "4"
}))]
});
`;
exports[`transform react to jsx complicated scope named exports 1`] = `
import { jsx as _jsx2 } from "react";
const Bar = function () {
const Foo = function () {
const Component = function ({
thing,
..._react
}) {
if (!thing) {
var _react2 = "something useless";
var b = _react3();
var jsx = 1;
var _jsx = 2;
return _jsx2("div", {});
}
;
return _jsx2("span", {});
};
};
};
`;
exports[`transform react to jsx complicated scope require 1`] = `
var _react4 = require("react");
const Bar = function () {
const Foo = function () {
const Component = function ({
thing,
..._react
}) {
if (!thing) {
var _react2 = "something useless";
var b = _react3();
var c = _react5();
var jsx = 1;
var _jsx = 2;
return _react4.jsx("div", {});
}
;
return _react4.jsx("span", {});
};
};
};
`;
exports[`transform react to jsx concatenates adjacent string literals 1`] = `
var x = React.jsxs("div", {
children: ["foo", "bar", "baz", React.jsx("div", {
children: "buz bang"
}), "qux", null, "quack"]
});
`;
exports[`transform react to jsx duplicate children prop should transform into sequence expression with actual children 1`] = `
React.jsx(Component, {
children: (1, "2")
});
`;
exports[`transform react to jsx duplicate children prop should transform into sequence expression with next prop 1`] = `
React.jsx(Component, {
foo: (1, 3),
children: "2"
});
`;
exports[`transform react to jsx duplicate children prop should transform into sequence expression with spread 1`] = `
React.jsx(Component, Object.assign({}, (1, x), {
children: "2"
}));
`;
exports[`transform react to jsx duplicate children props should transform into sequence expression with next prop 1`] = `
React.jsx(Component, {
foo: (1, 4, 3),
children: "2"
});
`;
exports[`transform react to jsx fragment with no children 1`] = `var x = React.jsx(React.Fragment, {});`;
exports[`transform react to jsx fragments 1`] = `
var x = React.jsx(React.Fragment, {
children: React.jsx("div", {})
});
`;
exports[`transform react to jsx fragments in dev mode (no key and source) 1`] = `
var _jsxFileName = "";
var x = React.jsxDEV(React.Fragment, {
children: React.jsxDEV("div", {}, undefined, false, {
fileName: _jsxFileName,
lineNumber: 1
}, this)
}, undefined, false);
`;
exports[`transform react to jsx fragments to set keys 1`] = `var x = React.jsx(React.Fragment, {}, "foo");`;
exports[`transform react to jsx import source pragma overrides regular pragma 1`] = `
import * as _baz from "baz";
/** @jsxImportSource baz */
var x = _baz.jsx("div", {
children: _baz.jsx("span", {})
});
`;
exports[`transform react to jsx multiple pragmas work 1`] = `
import _default from "baz";
/** Some comment here
* @jsxImportSource baz
* @jsxAutoImport defaultExport
*/
var x = _default.jsx("div", {
children: _default.jsx("span", {})
});
`;
exports[`transform react to jsx nonStatic children 1`] = `
var _jsxFileName = "";
var x = React.jsxDEV("div", {
children: [React.jsxDEV("span", {}, '0', false, {
fileName: _jsxFileName,
lineNumber: 3
}, this), React.jsxDEV("span", {}, '1', false, {
fileName: _jsxFileName,
lineNumber: 3
}, this)]
}, undefined, false, {
fileName: _jsxFileName,
lineNumber: 2
}, this);
`;
exports[`transform react to jsx properly handles keys 1`] = `
var x = React.jsxs("div", {
children: [React.jsx("div", {}, "1"), React.jsx("div", {
meow: "wolf"
}, "2"), React.jsx("div", {}, "3")]
});
`;
exports[`transform react to jsx properly passes in source and self 1`] = `
var _jsxFileName = "";
var x = React.jsxDEV("div", {}, undefined, false, {
fileName: _jsxFileName,
lineNumber: 1
}, this);
`;
exports[`transform react to jsx should allow constructor as prop 1`] = `
React.jsx(Component, {
constructor: "foo"
});
`;
exports[`transform react to jsx should allow deeper js namespacing 1`] = `React.jsx(Namespace.DeepNamespace.Component, {});`;
exports[`transform react to jsx should allow elements as attributes 1`] = `
React.jsx("div", {
attr: React.jsx("div", {})
});
`;
exports[`transform react to jsx should allow js namespacing 1`] = `React.jsx(Namespace.Component, {});`;
exports[`transform react to jsx should allow nested fragments 1`] = `
React.jsx("div", {
children: React.jsxs(React.Fragment, {
children: [React.jsxs(React.Fragment, {
children: [React.jsx("span", {
children: "Hello"
}), React.jsx("span", {
children: "world"
})]
}), React.jsxs(React.Fragment, {
children: [React.jsx("span", {
children: "Goodbye"
}), React.jsx("span", {
children: "world"
})]
})]
})
});
`;
exports[`transform react to jsx should avoid wrapping in extra parens if not needed 1`] = `
var x = React.jsx("div", {
children: React.jsx(Component, {})
});
var x = React.jsx("div", {
children: props.children
});
var x = React.jsx(Composite, {
children: props.children
});
var x = React.jsx(Composite, {
children: React.jsx(Composite2, {})
});
`;
exports[`transform react to jsx should convert simple tags 1`] = `var x = React.jsx("div", {});`;
exports[`transform react to jsx should convert simple text 1`] = `
var x = React.jsx("div", {
children: "text"
});
`;
exports[`transform react to jsx should escape xhtml jsxattribute 1`] = `
React.jsx("div", {
id: "w\\xF4w"
});
React.jsx("div", {
id: "w"
});
React.jsx("div", {
id: "w < w"
});
`;
exports[`transform react to jsx should escape xhtml jsxtext 1`] = `
React.jsx("div", {
children: "wow"
});
React.jsx("div", {
children: "w\\xF4w"
});
React.jsx("div", {
children: "w & w"
});
React.jsx("div", {
children: "w & w"
});
React.jsx("div", {
children: "w \\xA0 w"
});
React.jsx("div", {
children: "this should not parse as unicode: \\xA0"
});
React.jsx("div", {
children: "this should parse as nbsp: \\xA0 "
});
React.jsxs("div", {
children: ["this should parse as unicode: ", '  ']
});
React.jsx("div", {
children: "w < w"
});
`;
exports[`transform react to jsx should handle attributed elements 1`] = `
var HelloMessage = React.createClass({
render: function () {
return React.jsxs("div", {
children: ["Hello ", this.props.name]
});
}
});
React.render(React.jsx(HelloMessage, {
name: React.jsx("span", {
children: "Sebastian"
})
}), mountNode);
`;
exports[`transform react to jsx should handle has own property correctly 1`] = `
React.jsx("hasOwnProperty", {
children: "testing"
});
`;
exports[`transform react to jsx should have correct comma in nested children 1`] = `
var x = React.jsxs("div", {
children: [React.jsx("div", {
children: React.jsx("br", {})
}), React.jsxs(Component, {
children: [foo, React.jsx("br", {}), bar]
}), React.jsx("br", {})]
});
`;
exports[`transform react to jsx should insert commas after expressions before whitespace 1`] = `
var x = React.jsx("div", {
attr1: "foo" + "bar",
attr2: "foo" + "bar" + "baz" + "bug",
attr3: "foo" + "bar" + "baz" + "bug",
attr4: "baz"
});
`;
exports[`transform react to jsx should not add quotes to identifier names 1`] = `
var e = React.jsx(F, {
aaa: true,
new: true,
const: true,
var: true,
default: true,
"foo-bar": true
});
`;
exports[`transform react to jsx should not strip nbsp even couple with other whitespace 1`] = `
React.jsx("div", {
children: "\\xA0 "
});
`;
exports[`transform react to jsx should not strip tags with a single child of nbsp 1`] = `
React.jsx("div", {
children: "\\xA0"
});
`;
exports[`transform react to jsx should properly handle comments adjacent to children 1`] = `
var x = React.jsxs("div", {
children: [React.jsx("span", {}), React.jsx("br", {})]
});
`;
exports[`transform react to jsx should properly handle comments between props 1`] = `
var x = React.jsx("div", {
/* a multi-line
comment */
attr1: "foo",
children: React.jsx("span", {
// a double-slash comment
attr2: "bar"
})
});
`;
exports[`transform react to jsx should properly handle potentially null variables 1`] = `
var foo = null;
var x = React.jsx("div", Object.assign({}, foo));
`;
exports[`transform react to jsx should quote jsx attributes 1`] = `
React.jsx("button", {
"data-value": "a value",
children: "Button"
});
`;
exports[`transform react to jsx should support xml namespaces if flag 1`] = `
React.jsx("f:image", {
"n:attr": true
});
`;
exports[`transform react to jsx should transform known hyphenated tags 1`] = `React.jsx("font-face", {});`;
exports[`transform react to jsx static children 1`] = `
var _jsxFileName = "";
var x = React.jsxDEV("div", {
children: [React.jsxDEV("span", {}, undefined, false, {
fileName: _jsxFileName,
lineNumber: 3
}, this), [React.jsxDEV("span", {}, '0', false, {
fileName: _jsxFileName,
lineNumber: 4
}, this), React.jsxDEV("span", {}, '1', false, {
fileName: _jsxFileName,
lineNumber: 4
}, this)]]
}, undefined, true, {
fileName: _jsxFileName,
lineNumber: 2
}, this);
`;
exports[`transform react to jsx useBuiltIns false uses extend instead of Object.assign 1`] = `
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
React.jsx(Component, _extends({
y: 2
}, x));
`;
exports[`transform react to jsx uses createElement when the key comes after a spread 1`] = `
var x = React.createElement("div", Object.assign({}, props, {
key: "1",
foo: "bar"
}));
`;
exports[`transform react to jsx uses jsx when the key comes before a spread 1`] = `
var x = React.jsx("div", Object.assign({}, props, {
foo: "bar"
}), "1");
`;
exports[`transform react to jsx uses jsxDEV instead of jsx in dev mode 1`] = `
var _jsxFileName = "";
var x = React.jsxDEV("span", {
propOne: "one",
children: "Hi"
}, undefined, false, {
fileName: _jsxFileName,
lineNumber: 1
}, this);
`;
exports[`transform react to jsx wraps props in react spread for first spread attributes 1`] = `
React.jsx(Component, Object.assign({}, x, {
y: 2,
z: true
}));
`;
exports[`transform react to jsx wraps props in react spread for last spread attributes 1`] = `
React.jsx(Component, Object.assign({
y: 2,
z: true
}, x));
`;
exports[`transform react to jsx wraps props in react spread for middle spread attributes 1`] = `
React.jsx(Component, Object.assign({
y: 2
}, x, {
z: true
}));
`;

View File

@@ -1,3 +0,0 @@
'use strict';
module.exports = require('./src/TransformJSXToReactBabelPlugin');

View File

@@ -1,7 +0,0 @@
'use strict';
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react-jsx-babel.production.min.js');
} else {
module.exports = require('./cjs/react-jsx-babel.development.js');
}

View File

@@ -1,18 +0,0 @@
{
"name": "babel-plugin-react-jsx",
"version": "0.1.0",
"private": true,
"description": "@babel/plugin-transform-react-jsx",
"main": "index.js",
"dependencies": {
"@babel/helper-module-imports": "^7.0.0",
"esutils": "^2.0.0"
},
"files": [
"README.md",
"index.js",
"build-info.json",
"cjs/",
"umd/"
]
}

View File

@@ -1,799 +0,0 @@
// MIT License
// Copyright (c) 2014-present Sebastian McKenzie and other contributors
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// © 2019 GitHub, Inc.
'use strict';
const esutils = require('esutils');
const {
isModule,
addNamespace,
addNamed,
addDefault,
} = require('@babel/helper-module-imports');
// These are all the valid auto import types (under the config autoImport)
// that a user can specific
const IMPORT_TYPES = {
none: 'none', // default option. Will not import anything
require: 'require', // var _react = require("react");
namespace: 'namespace', // import * as _react from "react";
defaultExport: 'defaultExport', // import _default from "react";
namedExports: 'namedExports', // import { jsx } from "react";
};
const JSX_AUTO_IMPORT_ANNOTATION_REGEX = /\*?\s*@jsxAutoImport\s+([^\s]+)/;
const JSX_IMPORT_SOURCE_ANNOTATION_REGEX = /\*?\s*@jsxImportSource\s+([^\s]+)/;
// We want to use React.createElement, even in the case of
// jsx, for <div {...props} key={key} /> to distinguish it
// from <div key={key} {...props} />. This is an intermediary
// step while we deprecate key spread from props. Afterwards,
// we will remove createElement entirely
function shouldUseCreateElement(path, types) {
const openingPath = path.get('openingElement');
const attributes = openingPath.node.attributes;
let seenPropsSpread = false;
for (let i = 0; i < attributes.length; i++) {
const attr = attributes[i];
if (
seenPropsSpread &&
types.isJSXAttribute(attr) &&
attr.name.name === 'key'
) {
return true;
} else if (types.isJSXSpreadAttribute(attr)) {
seenPropsSpread = true;
}
}
return false;
}
function helper(babel, opts) {
const {types: t} = babel;
const visitor = {};
visitor.JSXNamespacedName = function(path, state) {
const throwIfNamespace =
state.opts.throwIfNamespace === undefined
? true
: !!state.opts.throwIfNamespace;
if (throwIfNamespace) {
throw path.buildCodeFrameError(
`Namespace tags are not supported by default. React's JSX doesn't support namespace tags. \
You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
);
}
};
visitor.JSXSpreadChild = function(path) {
throw path.buildCodeFrameError(
'Spread children are not supported in React.',
);
};
visitor.JSXElement = {
exit(path, file) {
let callExpr;
if (shouldUseCreateElement(path, t)) {
callExpr = buildCreateElementCall(path, file);
} else {
callExpr = buildJSXElementCall(path, file);
}
if (callExpr) {
path.replaceWith(t.inherits(callExpr, path.node));
}
},
};
visitor.JSXFragment = {
exit(path, file) {
if (opts.compat) {
throw path.buildCodeFrameError(
'Fragment tags are only supported in React 16 and up.',
);
}
let callExpr = buildJSXFragmentCall(path, file);
if (callExpr) {
path.replaceWith(t.inherits(callExpr, path.node));
}
},
};
return visitor;
function convertJSXIdentifier(node, parent) {
if (t.isJSXIdentifier(node)) {
if (node.name === 'this' && t.isReferenced(node, parent)) {
return t.thisExpression();
} else if (esutils.keyword.isIdentifierNameES6(node.name)) {
node.type = 'Identifier';
} else {
return t.stringLiteral(node.name);
}
} else if (t.isJSXMemberExpression(node)) {
return t.memberExpression(
convertJSXIdentifier(node.object, node),
convertJSXIdentifier(node.property, node),
);
} else if (t.isJSXNamespacedName(node)) {
/**
* If there is flag "throwIfNamespace"
* print XMLNamespace like string literal
*/
return t.stringLiteral(`${node.namespace.name}:${node.name.name}`);
}
return node;
}
function convertAttributeValue(node) {
if (t.isJSXExpressionContainer(node)) {
return node.expression;
} else {
return node;
}
}
function convertAttribute(node, duplicateChildren) {
let value = convertAttributeValue(node.value || t.booleanLiteral(true));
if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(node.value)) {
value.value = value.value.replace(/\n\s+/g, ' ');
// "raw" JSXText should not be used from a StringLiteral because it needs to be escaped.
if (value.extra && value.extra.raw) {
delete value.extra.raw;
}
}
if (duplicateChildren && duplicateChildren.length > 0) {
value = t.sequenceExpression([...duplicateChildren, value]);
}
if (t.isJSXNamespacedName(node.name)) {
node.name = t.stringLiteral(
node.name.namespace.name + ':' + node.name.name.name,
);
} else if (esutils.keyword.isIdentifierNameES6(node.name.name)) {
node.name.type = 'Identifier';
} else {
node.name = t.stringLiteral(node.name.name);
}
return t.inherits(t.objectProperty(node.name, value), node);
}
// Builds JSX into:
// Production: React.jsx(type, arguments, key)
// Development: React.jsxDEV(type, arguments, key, isStaticChildren, source, self)
function buildJSXElementCall(path, file) {
if (opts.filter && !opts.filter(path.node, file)) {
return;
}
const openingPath = path.get('openingElement');
openingPath.parent.children = t.react.buildChildren(openingPath.parent);
const tagExpr = convertJSXIdentifier(
openingPath.node.name,
openingPath.node,
);
const args = [];
let tagName;
if (t.isIdentifier(tagExpr)) {
tagName = tagExpr.name;
} else if (t.isLiteral(tagExpr)) {
tagName = tagExpr.value;
}
const state = {
tagExpr: tagExpr,
tagName: tagName,
args: args,
};
if (opts.pre) {
opts.pre(state, file);
}
let attribs = [];
let key;
let source;
let self;
// for React.jsx, key, __source (dev), and __self (dev) is passed in as
// a separate argument rather than in the args object. We go through the
// props and filter out these three keywords so we can pass them in
// as separate arguments later
for (let i = 0; i < openingPath.node.attributes.length; i++) {
const attr = openingPath.node.attributes[i];
if (t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name)) {
if (attr.name.name === 'key') {
key = convertAttribute(attr).value;
} else if (attr.name.name === '__source') {
source = convertAttribute(attr).value;
} else if (attr.name.name === '__self') {
self = convertAttribute(attr).value;
} else {
attribs.push(attr);
}
} else {
attribs.push(attr);
}
}
if (attribs.length || path.node.children.length) {
attribs = buildJSXOpeningElementAttributes(
attribs,
file,
path.node.children,
);
} else {
// attributes should never be null
attribs = t.objectExpression([]);
}
args.push(attribs);
if (!file.opts.development) {
if (key !== undefined) {
args.push(key);
}
} else {
// isStaticChildren, __source, and __self are only used in development
args.push(
key === undefined ? t.identifier('undefined') : key,
t.booleanLiteral(path.node.children.length > 1),
source === undefined ? t.identifier('undefined') : source,
self === undefined ? t.identifier('undefined') : self,
);
}
if (opts.post) {
opts.post(state, file);
}
return (
state.call ||
t.callExpression(
path.node.children.length > 1 ? state.staticCallee : state.callee,
args,
)
);
}
function isChildrenProp(prop) {
return (
t.isJSXAttribute(prop) &&
t.isJSXIdentifier(prop.name) &&
prop.name.name === 'children'
);
}
// Builds props for React.jsx. This function adds children into the props
// and ensures that props is always an object
function buildJSXOpeningElementAttributes(attribs, file, children) {
let _props = [];
const objs = [];
// In order to avoid having duplicate "children" keys, we avoid
// pushing the "children" prop if we have actual children. However,
// the children prop may have side effects, so to be certain
// these side effects are evaluated, we add them to the following prop
// as a sequence expression to preserve order. So:
// <div children={x++} foo={y}>{child}</div> becomes
// React.jsx('div', {foo: (x++, y), children: child});
// duplicateChildren contains the extra children prop values
let duplicateChildren = [];
const hasChildren = children && children.length > 0;
const useBuiltIns = file.opts.useBuiltIns || false;
if (typeof useBuiltIns !== 'boolean') {
throw new Error(
'transform-react-jsx currently only accepts a boolean option for ' +
'useBuiltIns (defaults to false)',
);
}
while (attribs.length) {
const prop = attribs.shift();
if (hasChildren && isChildrenProp(prop)) {
duplicateChildren.push(convertAttributeValue(prop.value));
} else if (t.isJSXSpreadAttribute(prop)) {
_props = pushProps(_props, objs);
if (duplicateChildren.length > 0) {
objs.push(
t.sequenceExpression([...duplicateChildren, prop.argument]),
);
duplicateChildren = [];
} else {
objs.push(prop.argument);
}
} else {
_props.push(convertAttribute(prop, duplicateChildren));
if (duplicateChildren.length > 0) {
duplicateChildren = [];
}
}
}
// In React.JSX, children is no longer a separate argument, but passed in
// through the argument object
if (hasChildren) {
if (children.length === 1) {
_props.push(
t.objectProperty(
t.identifier('children'),
duplicateChildren.length > 0
? t.sequenceExpression([...duplicateChildren, children[0]])
: children[0],
),
);
} else {
_props.push(
t.objectProperty(
t.identifier('children'),
duplicateChildren.length > 0
? t.sequenceExpression([
...duplicateChildren,
t.arrayExpression(children),
])
: t.arrayExpression(children),
),
);
}
}
pushProps(_props, objs);
if (objs.length === 1) {
// only one object
if (!t.isObjectExpression(objs[0])) {
// if the prop object isn't an object, use Object.assign or _extends
// to ensure that the prop will always be an object (as opposed to a variable
// that could be null at some point)
const expressionHelper = useBuiltIns
? t.memberExpression(t.identifier('Object'), t.identifier('assign'))
: file.addHelper('extends');
attribs = t.callExpression(expressionHelper, [
t.objectExpression([]),
objs[0],
]);
} else {
attribs = objs[0];
}
} else {
// looks like we have multiple objects
if (!t.isObjectExpression(objs[0])) {
objs.unshift(t.objectExpression([]));
}
const expressionHelper = useBuiltIns
? t.memberExpression(t.identifier('Object'), t.identifier('assign'))
: file.addHelper('extends');
// spread it
attribs = t.callExpression(expressionHelper, objs);
}
return attribs;
}
// Builds JSX Fragment <></> into
// Production: React.jsx(type, arguments)
// Development: React.jsxDEV(type, { children})
function buildJSXFragmentCall(path, file) {
if (opts.filter && !opts.filter(path.node, file)) {
return;
}
const openingPath = path.get('openingElement');
openingPath.parent.children = t.react.buildChildren(openingPath.parent);
const args = [];
const tagName = null;
const tagExpr = file.get('jsxFragIdentifier')();
const state = {
tagExpr: tagExpr,
tagName: tagName,
args: args,
};
if (opts.pre) {
opts.pre(state, file);
}
let childrenNode;
if (path.node.children.length > 0) {
if (path.node.children.length === 1) {
childrenNode = path.node.children[0];
} else {
childrenNode = t.arrayExpression(path.node.children);
}
}
args.push(
t.objectExpression(
childrenNode !== undefined
? [t.objectProperty(t.identifier('children'), childrenNode)]
: [],
),
);
if (file.opts.development) {
args.push(
t.identifier('undefined'),
t.booleanLiteral(path.node.children.length > 1),
);
}
if (opts.post) {
opts.post(state, file);
}
return (
state.call ||
t.callExpression(
path.node.children.length > 1 ? state.staticCallee : state.callee,
args,
)
);
}
// Builds JSX into:
// Production: React.createElement(type, arguments, children)
// Development: React.createElement(type, arguments, children, source, self)
function buildCreateElementCall(path, file) {
if (opts.filter && !opts.filter(path.node, file)) {
return;
}
const openingPath = path.get('openingElement');
openingPath.parent.children = t.react.buildChildren(openingPath.parent);
const tagExpr = convertJSXIdentifier(
openingPath.node.name,
openingPath.node,
);
const args = [];
let tagName;
if (t.isIdentifier(tagExpr)) {
tagName = tagExpr.name;
} else if (t.isLiteral(tagExpr)) {
tagName = tagExpr.value;
}
const state = {
tagExpr: tagExpr,
tagName: tagName,
args: args,
};
if (opts.pre) {
opts.pre(state, file);
}
let attribs = openingPath.node.attributes;
if (attribs.length) {
attribs = buildCreateElementOpeningElementAttributes(attribs, file);
} else {
attribs = t.nullLiteral();
}
args.push(attribs, ...path.node.children);
if (opts.post) {
opts.post(state, file);
}
return state.call || t.callExpression(state.oldCallee, args);
}
function pushProps(_props, objs) {
if (!_props.length) {
return _props;
}
objs.push(t.objectExpression(_props));
return [];
}
/**
* The logic for this is quite terse. It's because we need to
* support spread elements. We loop over all attributes,
* breaking on spreads, we then push a new object containing
* all prior attributes to an array for later processing.
*/
function buildCreateElementOpeningElementAttributes(attribs, file) {
let _props = [];
const objs = [];
const useBuiltIns = file.opts.useBuiltIns || false;
if (typeof useBuiltIns !== 'boolean') {
throw new Error(
'transform-react-jsx currently only accepts a boolean option for ' +
'useBuiltIns (defaults to false)',
);
}
while (attribs.length) {
const prop = attribs.shift();
if (t.isJSXSpreadAttribute(prop)) {
_props = pushProps(_props, objs);
objs.push(prop.argument);
} else {
const attr = convertAttribute(prop);
_props.push(attr);
}
}
pushProps(_props, objs);
if (objs.length === 1) {
// only one object
attribs = objs[0];
} else {
// looks like we have multiple objects
if (!t.isObjectExpression(objs[0])) {
objs.unshift(t.objectExpression([]));
}
const expressionHelper = useBuiltIns
? t.memberExpression(t.identifier('Object'), t.identifier('assign'))
: file.addHelper('extends');
// spread it
attribs = t.callExpression(expressionHelper, objs);
}
return attribs;
}
}
module.exports = function(babel) {
const {types: t} = babel;
const createIdentifierParser = id => () => {
return id
.split('.')
.map(name => t.identifier(name))
.reduce((object, property) => t.memberExpression(object, property));
};
const visitor = helper(babel, {
pre(state) {
const tagName = state.tagName;
const args = state.args;
if (t.react.isCompatTag(tagName)) {
args.push(t.stringLiteral(tagName));
} else {
args.push(state.tagExpr);
}
},
post(state, pass) {
state.callee = pass.get('jsxIdentifier')();
state.staticCallee = pass.get('jsxStaticIdentifier')();
state.oldCallee = pass.get('oldJSXIdentifier')();
},
});
const createIdentifierName = (path, autoImport, name, importName) => {
if (autoImport === IMPORT_TYPES.none) {
return `React.${name}`;
} else if (autoImport === IMPORT_TYPES.namedExports) {
if (importName) {
const identifierName = `${importName[name]}`;
return identifierName;
}
} else {
return `${importName}.${name}`;
}
};
function getImportNames(parentPath, state) {
const imports = {};
parentPath.traverse({
JSXElement(path) {
if (shouldUseCreateElement(path, t)) {
imports.createElement = true;
} else if (path.node.children.length > 1) {
const importName = state.development ? 'jsxDEV' : 'jsxs';
imports[importName] = true;
} else {
const importName = state.development ? 'jsxDEV' : 'jsx';
imports[importName] = true;
}
},
JSXFragment(path) {
imports.Fragment = true;
},
});
return imports;
}
function hasJSX(parentPath) {
let fileHasJSX = false;
parentPath.traverse({
JSXElement(path) {
fileHasJSX = true;
path.stop();
},
JSXFragment(path) {
fileHasJSX = true;
path.stop();
},
});
return fileHasJSX;
}
function addAutoImports(path, state) {
if (state.autoImport === IMPORT_TYPES.none) {
return;
}
if (IMPORT_TYPES[state.autoImport] === undefined) {
throw path.buildCodeFrameError(
'autoImport must be one of the following: ' +
Object.keys(IMPORT_TYPES).join(', '),
);
}
if (state.autoImport === IMPORT_TYPES.require && isModule(path)) {
throw path.buildCodeFrameError(
'Babel `sourceType` must be set to `script` for autoImport ' +
'to use `require` syntax. See Babel `sourceType` for details.',
);
}
if (state.autoImport !== IMPORT_TYPES.require && !isModule(path)) {
throw path.buildCodeFrameError(
'Babel `sourceType` must be set to `module` for autoImport to use `' +
state.autoImport +
'` syntax. See Babel `sourceType` for details.',
);
}
// import {jsx} from "react";
// import {createElement} from "react";
if (state.autoImport === IMPORT_TYPES.namedExports) {
const imports = getImportNames(path, state);
const importMap = {};
Object.keys(imports).forEach(importName => {
importMap[importName] = addNamed(path, importName, state.source).name;
});
return importMap;
}
// add import to file and get the import name
let name;
if (state.autoImport === IMPORT_TYPES.require) {
// var _react = require("react");
name = addNamespace(path, state.source, {
importedInterop: 'uncompiled',
}).name;
} else if (state.autoImport === IMPORT_TYPES.namespace) {
// import * as _react from "react";
name = addNamespace(path, state.source).name;
} else if (state.autoImport === IMPORT_TYPES.defaultExport) {
// import _default from "react";
name = addDefault(path, state.source).name;
}
return name;
}
visitor.Program = {
enter(path, state) {
if (hasJSX(path)) {
let autoImport = state.opts.autoImport || IMPORT_TYPES.none;
let source = state.opts.importSource || 'react';
const {file} = state;
if (file.ast.comments) {
for (let i = 0; i < file.ast.comments.length; i++) {
const comment = file.ast.comments[i];
const jsxAutoImportMatches = JSX_AUTO_IMPORT_ANNOTATION_REGEX.exec(
comment.value,
);
if (jsxAutoImportMatches) {
autoImport = jsxAutoImportMatches[1];
}
const jsxImportSourceMatches = JSX_IMPORT_SOURCE_ANNOTATION_REGEX.exec(
comment.value,
);
if (jsxImportSourceMatches) {
source = jsxImportSourceMatches[1];
}
}
}
const importName = addAutoImports(path, {
...state.opts,
autoImport,
source,
});
state.set(
'oldJSXIdentifier',
createIdentifierParser(
createIdentifierName(path, autoImport, 'createElement', importName),
),
);
state.set(
'jsxIdentifier',
createIdentifierParser(
createIdentifierName(
path,
autoImport,
state.opts.development ? 'jsxDEV' : 'jsx',
importName,
),
),
);
state.set(
'jsxStaticIdentifier',
createIdentifierParser(
createIdentifierName(
path,
autoImport,
state.opts.development ? 'jsxDEV' : 'jsxs',
importName,
),
),
);
state.set(
'jsxFragIdentifier',
createIdentifierParser(
createIdentifierName(path, autoImport, 'Fragment', importName),
),
);
}
},
};
visitor.JSXAttribute = function(path) {
if (t.isJSXElement(path.node.value)) {
path.node.value = t.jsxExpressionContainer(path.node.value);
}
};
return {
name: 'transform-react-jsx',
visitor,
};
};

View File

@@ -1,7 +1,7 @@
{
"name": "create-subscription",
"description": "utility for subscribing to external data sources inside React components",
"version": "16.13.0",
"version": "16.13.1",
"repository": {
"type": "git",
"url": "https://github.com/facebook/react.git",

View File

@@ -11,7 +11,6 @@
let createSubscription;
let BehaviorSubject;
let ReactFeatureFlags;
let React;
let ReactNoop;
let Scheduler;
@@ -21,8 +20,7 @@ describe('createSubscription', () => {
beforeEach(() => {
jest.resetModules();
createSubscription = require('create-subscription').createSubscription;
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');

View File

@@ -0,0 +1,43 @@
## 4.0.3
* Remove the heuristic that checks all Hooks ending with `Effect` due to too many false positives. ([@gaearon](https://github.com/gaearon) in [#19004](https://github.com/facebook/react/pull/19004))
## 4.0.2
* Prevent Hooks that have `Effect` in the middle from being considered effects. ([@surgeboris](https://github.com/surgeboris) in [#18907](https://github.com/facebook/react/pull/18907))
## 4.0.1
* Declare support for ESLint 7. ([@MichaelDeBoey](https://github.com/MichaelDeBoey) in [#18878](https://github.com/facebook/react/pull/18878))
## 4.0.0
* **New Violations:** Consider `PascalCase.useFoo()` calls as Hooks. ([@cyan33](https://github.com/cyan33) in [#18722](https://github.com/facebook/react/pull/18722))
* **New Violations:** Check callback body when it's not written inline. ([@gaearon](https://github.com/gaearon) in [#18435](https://github.com/facebook/react/pull/18435))
* **New Violations:** Check dependencies for all Hooks ending with `Effect`. ([@airjp73](https://github.com/airjp73) in [#18580](https://github.com/facebook/react/pull/18580))
* Add a way to enable the dangerous autofix. ([@gaearon](https://github.com/gaearon) in [#18437](https://github.com/facebook/react/pull/18437))
* Offer a more sensible suggestion when encountering an assignment. ([@Zzzen](https://github.com/Zzzen) in [#16784](https://github.com/facebook/react/pull/16784))
* Consider TypeScript casts of `useRef` as constant. ([@sophiebits](https://github.com/sophiebits) in [#18496](https://github.com/facebook/react/pull/18496))
* Add documentation. ([@ghmcadams](https://github.com/ghmcadams) in [#16607](https://github.com/facebook/react/pull/16607))
## 3.0.0
* **New Violations:** Forbid calling Hooks from classes. ([@ianobermiller](https://github.com/ianobermiller) in [#18341](https://github.com/facebook/react/pull/18341))
* Add a recommended config. ([@SimenB](https://github.com/SimenB) in [#14762](https://github.com/facebook/react/pull/14762))
## 2.5.0
* Fix a misleading error message in loops. ([@M-Izadmehr](https://github.com/M-Izadmehr) in [#16853](https://github.com/facebook/react/pull/16853))
## 2.4.0
* **New Violations:** Run checks for functions passed to `forwardRef`. ([@dprgarner](https://github.com/dprgarner) in [#17255](https://github.com/facebook/react/pull/17255))
* **New Violations:** Check for ref usage in any Hook containing the word `Effect`. ([@gaearon](https://github.com/gaearon) in [#17663](https://github.com/facebook/react/pull/17663))
* Disable dangerous autofix and use ESLint Suggestions API instead. ([@wdoug](https://github.com/wdoug) in [#17385](https://github.com/facebook/react/pull/17385))
## 2.0.0
* **New Violations:** Forbid calling Hooks at the top level. ([@gaearon](https://github.com/gaearon) in [#16455](https://github.com/facebook/react/pull/16455))
* Fix a crash when referencing arguments in arrow functions. ([@hristo-kanchev](https://github.com/hristo-kanchev) in [#16356](https://github.com/facebook/react/pull/16356))
## 1.x
The 1.x releases arent noted in this changelog, but you can find them in the [commit history](https://github.com/facebook/react/commits/master/packages/eslint-plugin-react-hooks).

View File

@@ -18,7 +18,20 @@ npm install eslint-plugin-react-hooks --save-dev
yarn add eslint-plugin-react-hooks --dev
```
Then add it to your ESLint configuration:
Then extend the recommended eslint config:
```js
{
"extends": [
// ...
"plugin:react-hooks/recommended"
]
}
```
### Custom Configuration
If you want more fine-grained configuration, you can instead add a snippet like this to your ESLint configuration file:
```js
{
@@ -34,12 +47,29 @@ Then add it to your ESLint configuration:
}
```
## Advanced Configuration
`exhaustive-deps` can be configured to validate dependencies of custom Hooks with the `additionalHooks` option.
This option accepts a regex to match the names of custom Hooks that have dependencies.
```js
{
"rules": {
// ...
"react-hooks/exhaustive-deps": ["warn", {
"additionalHooks": "(useMyCustomHook|useMyOtherCustomHook)"
}]
}
}
```
We suggest to use this option **very sparingly, if at all**. Generally saying, we recommend most custom Hooks to not use the dependencies argument, and instead provide a higher-level API that is more focused around a specific use case.
## Valid and Invalid Examples
Please refer to the [Rules of Hooks](https://reactjs.org/docs/hooks-rules.html) documentation and the [Hooks FAQ](https://reactjs.org/docs/hooks-faq.html#what-exactly-do-the-lint-rules-enforce) to learn more about this rule.
For feedback about the `exhaustive-deps` rule, please post in [this thread](https://github.com/facebook/react/issues/14920).
## License
MIT

View File

@@ -11,14 +11,6 @@ const ESLintTester = require('eslint').RuleTester;
const ReactHooksESLintPlugin = require('eslint-plugin-react-hooks');
const ReactHooksESLintRule = ReactHooksESLintPlugin.rules['exhaustive-deps'];
ESLintTester.setDefaultConfig({
parser: require.resolve('babel-eslint'),
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
},
});
/**
* A string template tag that removes padding from the left side of multi-line strings
* @param {Array} strings array of code strings (only one expected)
@@ -259,6 +251,182 @@ const tests = {
}
`,
},
// Nullish coalescing and optional chaining
{
code: normalizeIndent`
function MyComponent(props) {
useEffect(() => {
console.log(props.foo?.bar?.baz ?? null);
}, [props.foo]);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useEffect(() => {
console.log(props.foo?.bar);
}, [props.foo?.bar]);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useEffect(() => {
console.log(props.foo);
console.log(props.foo?.bar);
}, [props.foo]);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useEffect(() => {
console.log(props.foo?.toString());
}, [props.foo]);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useMemo(() => {
console.log(props.foo?.toString());
}, [props.foo]);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.toString());
}, [props.foo]);
}
`,
},
{
code: normalizeIndent`
function MyComponent() {
const myEffect = () => {
// Doesn't use anything
};
useEffect(myEffect, []);
}
`,
},
{
code: normalizeIndent`
const local = {};
function MyComponent() {
const myEffect = () => {
console.log(local);
};
useEffect(myEffect, []);
}
`,
},
{
code: normalizeIndent`
const local = {};
function MyComponent() {
function myEffect() {
console.log(local);
}
useEffect(myEffect, []);
}
`,
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
function myEffect() {
console.log(local);
}
useEffect(myEffect, [local]);
}
`,
},
{
code: normalizeIndent`
function MyComponent() {
function myEffect() {
console.log(global);
}
useEffect(myEffect, []);
}
`,
},
{
code: normalizeIndent`
const local = {};
function MyComponent() {
const myEffect = () => {
otherThing()
}
const otherThing = () => {
console.log(local);
}
useEffect(myEffect, []);
}
`,
},
{
// Valid because even though we don't inspect the function itself,
// at least it's passed as a dependency.
code: normalizeIndent`
function MyComponent({delay}) {
const local = {};
const myEffect = debounce(() => {
console.log(local);
}, delay);
useEffect(myEffect, [myEffect]);
}
`,
},
{
code: normalizeIndent`
function MyComponent({myEffect}) {
useEffect(myEffect, [,myEffect]);
}
`,
},
{
code: normalizeIndent`
function MyComponent({myEffect}) {
useEffect(myEffect, [,myEffect,,]);
}
`,
},
{
code: normalizeIndent`
let local = {};
function myEffect() {
console.log(local);
}
function MyComponent() {
useEffect(myEffect, []);
}
`,
},
{
code: normalizeIndent`
function MyComponent({myEffect}) {
useEffect(myEffect, [myEffect]);
}
`,
},
{
// Valid because has no deps.
code: normalizeIndent`
function MyComponent({myEffect}) {
useEffect(myEffect);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
@@ -289,6 +457,24 @@ const tests = {
`,
options: [{additionalHooks: 'useAnotherEffect'}],
},
{
code: normalizeIndent`
function MyComponent(props) {
useWithoutEffectSuffix(() => {
console.log(props.foo);
}, []);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
return renderHelperConfusedWithEffect(() => {
console.log(props.foo);
}, []);
}
`,
},
{
// Valid because we don't care about hooks outside of components.
code: normalizeIndent`
@@ -478,6 +664,16 @@ const tests = {
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
let foo = {}
useEffect(() => {
foo.bar.baz = 43;
}, [foo.bar]);
}
`,
},
{
// Valid because we assign ref.current
// ourselves. Therefore it's likely not
@@ -1057,6 +1253,35 @@ const tests = {
},
],
invalid: [
{
code: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.toString());
}, []);
}
`,
errors: [
{
message:
"React Hook useCallback has a missing dependency: 'props.foo?.toString'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc:
'Update the dependencies array to be: [props.foo?.toString]',
output: normalizeIndent`
function MyComponent(props) {
useCallback(() => {
console.log(props.foo?.toString());
}, [props.foo?.toString]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
@@ -1175,6 +1400,29 @@ const tests = {
},
],
},
{
// Invalid because they don't have a meaning without deps.
code: normalizeIndent`
function MyComponent({ fn1, fn2 }) {
const value = useMemo(fn1);
const fn = useCallback(fn2);
}
`,
errors: [
{
message:
'React Hook useMemo does nothing when called with only one argument. ' +
'Did you forget to pass an array of dependencies?',
suggestions: undefined,
},
{
message:
'React Hook useCallback does nothing when called with only one argument. ' +
'Did you forget to pass an array of dependencies?',
suggestions: undefined,
},
],
},
{
// Regression test
code: normalizeIndent`
@@ -1606,6 +1854,38 @@ const tests = {
},
],
},
{
code: normalizeIndent`
function MyComponent({ history }) {
useEffect(() => {
return [
history?.foo
];
}, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'history?.foo'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [history?.foo]',
output: normalizeIndent`
function MyComponent({ history }) {
useEffect(() => {
return [
history?.foo
];
}, [history?.foo]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
@@ -5998,6 +6278,356 @@ const tests = {
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
function myEffect() {
console.log(local);
}
useEffect(myEffect, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'local'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [local]',
output: normalizeIndent`
function MyComponent() {
const local = {};
function myEffect() {
console.log(local);
}
useEffect(myEffect, [local]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = () => {
console.log(local);
};
useEffect(myEffect, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'local'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [local]',
output: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = () => {
console.log(local);
};
useEffect(myEffect, [local]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = function() {
console.log(local);
};
useEffect(myEffect, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'local'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [local]',
output: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = function() {
console.log(local);
};
useEffect(myEffect, [local]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = () => {
otherThing();
};
const otherThing = () => {
console.log(local);
};
useEffect(myEffect, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'otherThing'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [otherThing]',
output: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = () => {
otherThing();
};
const otherThing = () => {
console.log(local);
};
useEffect(myEffect, [otherThing]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = debounce(() => {
console.log(local);
}, delay);
useEffect(myEffect, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'myEffect'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [myEffect]',
output: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = debounce(() => {
console.log(local);
}, delay);
useEffect(myEffect, [myEffect]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = debounce(() => {
console.log(local);
}, delay);
useEffect(myEffect, [local]);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'myEffect'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [myEffect]',
output: normalizeIndent`
function MyComponent() {
const local = {};
const myEffect = debounce(() => {
console.log(local);
}, delay);
useEffect(myEffect, [myEffect]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent({myEffect}) {
useEffect(myEffect, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'myEffect'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [myEffect]',
output: normalizeIndent`
function MyComponent({myEffect}) {
useEffect(myEffect, [myEffect]);
}
`,
},
],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
useEffect(debounce(() => {
console.log(local);
}, delay), []);
}
`,
errors: [
{
message:
'React Hook useEffect received a function whose dependencies ' +
'are unknown. Pass an inline function instead.',
suggestions: [],
},
],
},
{
code: normalizeIndent`
function MyComponent() {
const local = {};
useEffect(() => {
console.log(local);
}, []);
}
`,
// Dangerous autofix is enabled due to the option:
output: normalizeIndent`
function MyComponent() {
const local = {};
useEffect(() => {
console.log(local);
}, [local]);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'local'. " +
'Either include it or remove the dependency array.',
},
],
// Keep this until major IDEs and VS Code FB ESLint plugin support Suggestions API.
options: [{enableDangerousAutofixThisMayCauseInfiniteLoops: true}],
},
{
code: normalizeIndent`
function MyComponent(props) {
let foo = {}
useEffect(() => {
foo.bar.baz = 43;
props.foo.bar.baz = 1;
}, []);
}
`,
errors: [
{
message:
"React Hook useEffect has missing dependencies: 'foo.bar' and 'props.foo.bar'. " +
'Either include them or remove the dependency array.',
suggestions: [
{
desc:
'Update the dependencies array to be: [foo.bar, props.foo.bar]',
output: normalizeIndent`
function MyComponent(props) {
let foo = {}
useEffect(() => {
foo.bar.baz = 43;
props.foo.bar.baz = 1;
}, [foo.bar, props.foo.bar]);
}
`,
},
],
},
],
},
],
};
const testsTypescript = {
valid: [
{
// `ref` is still constant, despite the cast.
code: normalizeIndent`
function MyComponent() {
const ref = useRef() as React.MutableRefObject<HTMLDivElement>;
useEffect(() => {
console.log(ref.current);
}, []);
}
`,
},
],
invalid: [
{
// `local` is still non-constant, despite the cast.
code: normalizeIndent`
function MyComponent() {
const local = {} as string;
useEffect(() => {
console.log(local);
}, []);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'local'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [local]',
output: normalizeIndent`
function MyComponent() {
const local = {} as string;
useEffect(() => {
console.log(local);
}, [local]);
}
`,
},
],
},
],
},
],
};
@@ -6005,7 +6635,12 @@ const tests = {
if (!process.env.CI) {
let only = [];
let skipped = [];
[...tests.valid, ...tests.invalid].forEach(t => {
[
...tests.valid,
...tests.invalid,
...testsTypescript.valid,
...testsTypescript.invalid,
].forEach(t => {
if (t.skip) {
delete t.skip;
skipped.push(t);
@@ -6026,7 +6661,21 @@ if (!process.env.CI) {
};
tests.valid = tests.valid.filter(predicate);
tests.invalid = tests.invalid.filter(predicate);
testsTypescript.valid = testsTypescript.valid.filter(predicate);
testsTypescript.invalid = testsTypescript.invalid.filter(predicate);
}
const eslintTester = new ESLintTester();
eslintTester.run('react-hooks', ReactHooksESLintRule, tests);
const parserOptions = {
ecmaVersion: 6,
sourceType: 'module',
};
new ESLintTester({
parser: require.resolve('babel-eslint'),
parserOptions,
}).run('react-hooks', ReactHooksESLintRule, tests);
new ESLintTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions,
}).run('react-hooks', ReactHooksESLintRule, testsTypescript);

View File

@@ -106,6 +106,7 @@ const tests = {
({useHook() { useState(); }});
const {useHook3 = () => { useState(); }} = {};
({useHook = () => { useState(); }} = {});
Namespace.useHook = () => { useState(); };
`,
`
// Valid because hooks can call hooks.
@@ -191,29 +192,6 @@ const tests = {
}
}
`,
`
// Currently valid.
// We *could* make this invalid if we want, but it creates false positives
// (see the FooStore case).
class C {
m() {
This.useHook();
Super.useHook();
}
}
`,
`
// Valid although we *could* consider these invalid.
// But it doesn't bring much benefit since it's an immediate runtime error anyway.
// So might as well allow it.
Hook.use();
Hook._use();
Hook.useState();
Hook._useState();
Hook.use42();
Hook.useHook();
Hook.use_hook();
`,
`
// Valid -- this is a regression test.
jest.useFakeTimers();
@@ -221,56 +199,6 @@ const tests = {
jest.useRealTimers();
})
`,
`
// Valid because that's a false positive we've seen quite a bit.
// This is a regression test.
class Foo extends Component {
render() {
if (cond) {
FooStore.useFeatureFlag();
}
}
}
`,
`
// Currently valid because we found this to be a common pattern
// for feature flag checks in existing components.
// We *could* make it invalid but that produces quite a few false positives.
// Why does it make sense to ignore it? Firstly, because using
// hooks in a class would cause a runtime error anyway.
// But why don't we care about the same kind of false positive in a functional
// component? Because even if it was a false positive, it would be confusing
// anyway. So it might make sense to rename a feature flag check in that case.
class ClassComponentWithFeatureFlag extends React.Component {
render() {
if (foo) {
useFeatureFlag();
}
}
}
`,
`
// Currently valid because we don't check for hooks in classes.
// See ClassComponentWithFeatureFlag for rationale.
// We *could* make it invalid if we don't regress that false positive.
class ClassComponentWithHook extends React.Component {
render() {
React.useState();
}
}
`,
`
// Currently valid.
// These are variations capturing the current heuristic--
// we only allow hooks in PascalCase, useFoo functions,
// or classes (due to common false positives and because they error anyway).
// We *could* make some of these invalid.
// They probably don't matter much.
(class {useHook = () => { useState(); }});
(class {useHook() { useState(); }});
(class {h = () => { useState(); }});
(class {i() { useState(); }});
`,
`
// Valid because they're not matching use[A-Z].
fooState();
@@ -278,16 +206,8 @@ const tests = {
_use();
_useState();
use_hook();
`,
`
// This is grey area.
// Currently it's valid (although React.useCallback would fail here).
// We could also get stricter and disallow it, just like we did
// with non-namespace use*() top-level calls.
const History = require('history-2.1.2');
const browserHistory = History.useBasename(History.createHistory)({
basename: '/',
});
// also valid because it's not matching the PascalCase namespace
jest.useFakeTimer()
`,
`
// Regression test for some internal code.
@@ -431,6 +351,59 @@ const tests = {
`,
errors: [conditionalError('useConditionalHook')],
},
{
code: `
Hook.use();
Hook._use();
Hook.useState();
Hook._useState();
Hook.use42();
Hook.useHook();
Hook.use_hook();
`,
errors: [
topLevelError('Hook.useState'),
topLevelError('Hook.use42'),
topLevelError('Hook.useHook'),
],
},
{
code: `
class C {
m() {
This.useHook();
Super.useHook();
}
}
`,
errors: [classError('This.useHook'), classError('Super.useHook')],
},
{
code: `
// This is a false positive (it's valid) that unfortunately
// we cannot avoid. Prefer to rename it to not start with "use"
class Foo extends Component {
render() {
if (cond) {
FooStore.useFeatureFlag();
}
}
}
`,
errors: [classError('FooStore.useFeatureFlag')],
},
{
code: `
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
function ComponentWithConditionalHook() {
if (cond) {
Namespace.useConditionalHook();
}
}
`,
errors: [conditionalError('Namespace.useConditionalHook')],
},
{
code: `
// Invalid because it's dangerous and might not warn otherwise.
@@ -870,6 +843,52 @@ const tests = {
`,
errors: [topLevelError('useBasename')],
},
{
code: `
class ClassComponentWithFeatureFlag extends React.Component {
render() {
if (foo) {
useFeatureFlag();
}
}
}
`,
errors: [classError('useFeatureFlag')],
},
{
code: `
class ClassComponentWithHook extends React.Component {
render() {
React.useState();
}
}
`,
errors: [classError('React.useState')],
},
{
code: `
(class {useHook = () => { useState(); }});
`,
errors: [classError('useState')],
},
{
code: `
(class {useHook() { useState(); }});
`,
errors: [classError('useState')],
},
{
code: `
(class {h = () => { useState(); }});
`,
errors: [classError('useState')],
},
{
code: `
(class {i() { useState(); }});
`,
errors: [classError('useState')],
},
],
};
@@ -897,7 +916,8 @@ function functionError(hook, fn) {
return {
message:
`React Hook "${hook}" is called in function "${fn}" that is neither ` +
'a React function component nor a custom React Hook function.',
'a React function component nor a custom React Hook function.' +
' React component names must start with an uppercase letter.',
};
}
@@ -919,6 +939,15 @@ function topLevelError(hook) {
};
}
function classError(hook) {
return {
message:
`React Hook "${hook}" cannot be called in a class component. React Hooks ` +
'must be called in a React function component or a custom React ' +
'Hook function.',
};
}
// For easier local testing
if (!process.env.CI) {
let only = [];

View File

@@ -5,6 +5,4 @@
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('./src/index');
export * from './src/index';

View File

@@ -1,7 +1,7 @@
{
"name": "eslint-plugin-react-hooks",
"description": "ESLint rules for React Hooks",
"version": "2.5.0",
"version": "4.0.5",
"repository": {
"type": "git",
"url": "https://github.com/facebook/react.git",
@@ -25,10 +25,13 @@
"url": "https://github.com/facebook/react/issues"
},
"engines": {
"node": ">=7"
"node": ">=10"
},
"homepage": "https://reactjs.org/",
"peerDependencies": {
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
},
"devDependencies": {
"@typescript-eslint/parser": "^2.26.0"
}
}

View File

@@ -11,14 +11,27 @@
export default {
meta: {
type: 'suggestion',
docs: {
description:
'verifies the list of dependencies for Hooks like useEffect and similar',
category: 'Best Practices',
recommended: true,
url: 'https://github.com/facebook/react/issues/14920',
},
fixable: 'code',
schema: [
{
type: 'object',
additionalProperties: false,
enableDangerousAutofixThisMayCauseInfiniteLoops: false,
properties: {
additionalHooks: {
type: 'string',
},
enableDangerousAutofixThisMayCauseInfiniteLoops: {
type: 'boolean',
},
},
},
],
@@ -31,13 +44,36 @@ export default {
context.options[0].additionalHooks
? new RegExp(context.options[0].additionalHooks)
: undefined;
const options = {additionalHooks};
const enableDangerousAutofixThisMayCauseInfiniteLoops =
(context.options &&
context.options[0] &&
context.options[0].enableDangerousAutofixThisMayCauseInfiniteLoops) ||
false;
const options = {
additionalHooks,
enableDangerousAutofixThisMayCauseInfiniteLoops,
};
function reportProblem(problem) {
if (enableDangerousAutofixThisMayCauseInfiniteLoops) {
// Used to enable legacy behavior. Dangerous.
// Keep this as an option until major IDEs upgrade (including VSCode FB ESLint extension).
if (Array.isArray(problem.suggest) && problem.suggest.length > 0) {
problem.fix = problem.suggest[0].fix;
}
}
context.report(problem);
}
const scopeManager = context.getSourceCode().scopeManager;
// Should be shared between visitors.
let setStateCallSites = new WeakMap();
let stateVariables = new WeakSet();
let staticKnownValueCache = new WeakMap();
let functionWithoutCapturedValueCache = new WeakMap();
const setStateCallSites = new WeakMap();
const stateVariables = new WeakSet();
const staticKnownValueCache = new WeakMap();
const functionWithoutCapturedValueCache = new WeakMap();
function memoizeWithWeakMap(fn, map) {
return function(arg) {
if (map.has(arg)) {
@@ -52,41 +88,24 @@ export default {
}
return {
FunctionExpression: visitFunctionExpression,
ArrowFunctionExpression: visitFunctionExpression,
CallExpression: visitCallExpression,
};
/**
* Visitor for both function expressions and arrow function expressions.
*/
function visitFunctionExpression(node) {
// We only want to lint nodes which are reactive hook callbacks.
if (
(node.type !== 'FunctionExpression' &&
node.type !== 'ArrowFunctionExpression') ||
node.parent.type !== 'CallExpression'
) {
function visitCallExpression(node) {
const callbackIndex = getReactiveHookCallbackIndex(node.callee, options);
if (callbackIndex === -1) {
// Not a React Hook call that needs deps.
return;
}
const callbackIndex = getReactiveHookCallbackIndex(
node.parent.callee,
options,
);
if (node.parent.arguments[callbackIndex] !== node) {
return;
}
// Get the reactive hook node.
const reactiveHook = node.parent.callee;
const callback = node.arguments[callbackIndex];
const reactiveHook = node.callee;
const reactiveHookName = getNodeWithoutReactNamespace(reactiveHook).name;
const declaredDependenciesNode = node.arguments[callbackIndex + 1];
const isEffect = /Effect($|[^a-z])/g.test(reactiveHookName);
// Get the declared dependencies for this reactive hook. If there is no
// Check the declared dependencies for this reactive hook. If there is no
// second argument then the reactive callback will re-run on every render.
// So no need to check for dependency inclusion.
const depsIndex = callbackIndex + 1;
const declaredDependenciesNode = node.parent.arguments[depsIndex];
if (!declaredDependenciesNode && !isEffect) {
// These are only used for optimization.
if (
@@ -94,8 +113,8 @@ export default {
reactiveHookName === 'useCallback'
) {
// TODO: Can this have a suggestion?
context.report({
node: node.parent.callee,
reportProblem({
node: reactiveHook,
message:
`React Hook ${reactiveHookName} does nothing when called with ` +
`only one argument. Did you forget to pass an array of ` +
@@ -105,8 +124,128 @@ export default {
return;
}
switch (callback.type) {
case 'FunctionExpression':
case 'ArrowFunctionExpression':
visitFunctionWithDependencies(
callback,
declaredDependenciesNode,
reactiveHook,
reactiveHookName,
isEffect,
);
return; // Handled
case 'Identifier':
if (!declaredDependenciesNode) {
// No deps, no problems.
return; // Handled
}
// The function passed as a callback is not written inline.
// But perhaps it's in the dependencies array?
if (
declaredDependenciesNode.elements &&
declaredDependenciesNode.elements.some(
el => el && el.type === 'Identifier' && el.name === callback.name,
)
) {
// If it's already in the list of deps, we don't care because
// this is valid regardless.
return; // Handled
}
// We'll do our best effort to find it, complain otherwise.
const variable = context.getScope().set.get(callback.name);
if (variable == null || variable.defs == null) {
// If it's not in scope, we don't care.
return; // Handled
}
// The function passed as a callback is not written inline.
// But it's defined somewhere in the render scope.
// We'll do our best effort to find and check it, complain otherwise.
const def = variable.defs[0];
if (!def || !def.node) {
break; // Unhandled
}
if (def.type !== 'Variable' && def.type !== 'FunctionName') {
// Parameter or an unusual pattern. Bail out.
break; // Unhandled
}
switch (def.node.type) {
case 'FunctionDeclaration':
// useEffect(() => { ... }, []);
visitFunctionWithDependencies(
def.node,
declaredDependenciesNode,
reactiveHook,
reactiveHookName,
isEffect,
);
return; // Handled
case 'VariableDeclarator':
const init = def.node.init;
if (!init) {
break; // Unhandled
}
switch (init.type) {
// const effectBody = () => {...};
// useEffect(effectBody, []);
case 'ArrowFunctionExpression':
case 'FunctionExpression':
// We can inspect this function as if it were inline.
visitFunctionWithDependencies(
init,
declaredDependenciesNode,
reactiveHook,
reactiveHookName,
isEffect,
);
return; // Handled
}
break; // Unhandled
}
break; // Unhandled
default:
// useEffect(generateEffectBody(), []);
reportProblem({
node: reactiveHook,
message:
`React Hook ${reactiveHookName} received a function whose dependencies ` +
`are unknown. Pass an inline function instead.`,
});
return; // Handled
}
// Something unusual. Fall back to suggesting to add the body itself as a dep.
reportProblem({
node: reactiveHook,
message:
`React Hook ${reactiveHookName} has a missing dependency: '${callback.name}'. ` +
`Either include it or remove the dependency array.`,
suggest: [
{
desc: `Update the dependencies array to be: [${callback.name}]`,
fix(fixer) {
return fixer.replaceText(
declaredDependenciesNode,
`[${callback.name}]`,
);
},
},
],
});
}
/**
* Visitor for both function expressions and arrow function expressions.
*/
function visitFunctionWithDependencies(
node,
declaredDependenciesNode,
reactiveHook,
reactiveHookName,
isEffect,
) {
if (isEffect && node.async) {
context.report({
reportProblem({
node: node,
message:
`Effect callbacks are synchronous to prevent race conditions. ` +
@@ -124,7 +263,7 @@ export default {
}
// Get the current scope.
const scope = context.getScope();
const scope = scopeManager.acquire(node);
// Find all our "pure scopes". On every re-render of a component these
// pure scopes may have changes to the variables declared within. So all
@@ -177,10 +316,13 @@ export default {
if (def.node.type !== 'VariableDeclarator') {
return false;
}
const init = def.node.init;
let init = def.node.init;
if (init == null) {
return false;
}
while (init.type === 'TSAsExpression') {
init = init.expression;
}
// Detect primitive constants
// const foo = 42
let declaration = def.node.parent;
@@ -278,12 +420,12 @@ export default {
// Search the direct component subscopes for
// top-level function definitions matching this reference.
const fnNode = def.node;
let childScopes = componentScope.childScopes;
const childScopes = componentScope.childScopes;
let fnScope = null;
let i;
for (i = 0; i < childScopes.length; i++) {
let childScope = childScopes[i];
let childScopeBlock = childScope.block;
const childScope = childScopes[i];
const childScopeBlock = childScope.block;
if (
// function handleChange() {}
(fnNode.type === 'FunctionDeclaration' &&
@@ -461,7 +603,7 @@ export default {
if (foundCurrentAssignment) {
return;
}
context.report({
reportProblem({
node: dependencyNode.parent.property,
message:
`The ref value '${dependency}.current' will likely have ` +
@@ -475,13 +617,13 @@ export default {
// Warn about assigning to variables in the outer scope.
// Those are usually bugs.
let staleAssignments = new Set();
const staleAssignments = new Set();
function reportStaleAssignment(writeExpr, key) {
if (staleAssignments.has(key)) {
return;
}
staleAssignments.add(key);
context.report({
reportProblem({
node: writeExpr,
message:
`Assignments to the '${key}' variable from inside React Hook ` +
@@ -542,15 +684,15 @@ export default {
});
});
if (setStateInsideEffectWithoutDeps) {
let {suggestedDependencies} = collectRecommendations({
const {suggestedDependencies} = collectRecommendations({
dependencies,
declaredDependencies: [],
optionalDependencies,
externalDependencies: new Set(),
isEffect: true,
});
context.report({
node: node.parent.callee,
reportProblem({
node: reactiveHook,
message:
`React Hook ${reactiveHookName} contains a call to '${setStateInsideEffectWithoutDeps}'. ` +
`Without a list of dependencies, this can lead to an infinite chain of updates. ` +
@@ -581,7 +723,7 @@ export default {
// If the declared dependencies are not an array expression then we
// can't verify that the user provided the correct dependencies. Tell
// the user this in an error.
context.report({
reportProblem({
node: declaredDependenciesNode,
message:
`React Hook ${context.getSource(reactiveHook)} was passed a ` +
@@ -597,7 +739,7 @@ export default {
}
// If we see a spread element then add a special warning.
if (declaredDependencyNode.type === 'SpreadElement') {
context.report({
reportProblem({
node: declaredDependencyNode,
message:
`React Hook ${context.getSource(reactiveHook)} has a spread ` +
@@ -616,7 +758,7 @@ export default {
if (/Unsupported node type/.test(error.message)) {
if (declaredDependencyNode.type === 'Literal') {
if (dependencies.has(declaredDependencyNode.value)) {
context.report({
reportProblem({
node: declaredDependencyNode,
message:
`The ${declaredDependencyNode.raw} literal is not a valid dependency ` +
@@ -624,7 +766,7 @@ export default {
`Did you mean to include ${declaredDependencyNode.value} in the array instead?`,
});
} else {
context.report({
reportProblem({
node: declaredDependencyNode,
message:
`The ${declaredDependencyNode.raw} literal is not a valid dependency ` +
@@ -632,7 +774,7 @@ export default {
});
}
} else {
context.report({
reportProblem({
node: declaredDependencyNode,
message:
`React Hook ${context.getSource(reactiveHook)} has a ` +
@@ -667,7 +809,7 @@ export default {
});
}
let {
const {
suggestedDependencies,
unnecessaryDependencies,
missingDependencies,
@@ -680,6 +822,8 @@ export default {
isEffect,
});
let suggestedDeps = suggestedDependencies;
const problemCount =
duplicateDependencies.size +
missingDependencies.size +
@@ -732,7 +876,7 @@ export default {
}
// TODO: What if the function needs to change on every render anyway?
// Should we suggest removing effect deps as an appropriate fix too?
context.report({
reportProblem({
// TODO: Why not report this at the dependency site?
node: fn.node,
message,
@@ -749,7 +893,7 @@ export default {
// for effects though because those have legit
// use cases for over-specifying deps.
if (!isEffect && missingDependencies.size > 0) {
suggestedDependencies = collectRecommendations({
suggestedDeps = collectRecommendations({
dependencies,
declaredDependencies: [], // Pretend we don't know
optionalDependencies,
@@ -768,7 +912,7 @@ export default {
return declaredDepKeys.join(',') === sortedDeclaredDepKeys.join(',');
}
if (areDeclaredDepsAlphabetized()) {
suggestedDependencies.sort();
suggestedDeps.sort();
}
function getWarningMessage(deps, singlePrefix, label, fixVerb) {
@@ -823,7 +967,7 @@ export default {
// a `this` value. This warning can be confusing.
// So if we're going to show it, append a clarification.
if (!extraWarning && missingDependencies.has('props')) {
let propDep = dependencies.get('props');
const propDep = dependencies.get('props');
if (propDep == null) {
return;
}
@@ -1003,7 +1147,7 @@ export default {
}
}
context.report({
reportProblem({
node: declaredDependenciesNode,
message:
`React Hook ${context.getSource(reactiveHook)} has ` +
@@ -1024,14 +1168,14 @@ export default {
extraWarning,
suggest: [
{
desc: `Update the dependencies array to be: [${suggestedDependencies.join(
desc: `Update the dependencies array to be: [${suggestedDeps.join(
', ',
)}]`,
fix(fixer) {
// TODO: consider preserving the comments or formatting?
return fixer.replaceText(
declaredDependenciesNode,
`[${suggestedDependencies.join(', ')}]`,
`[${suggestedDeps.join(', ')}]`,
);
},
},
@@ -1091,10 +1235,10 @@ function collectRecommendations({
// Tree manipulation helpers.
function getOrCreateNodeByPath(rootNode, path) {
let keys = path.split('.');
const keys = path.split('.');
let node = rootNode;
for (let key of keys) {
let child = node.children.get(key);
for (const key of keys) {
let child = getChildByKey(node, key);
if (!child) {
child = createDepTree();
node.children.set(key, child);
@@ -1104,10 +1248,10 @@ function collectRecommendations({
return node;
}
function markAllParentsByPath(rootNode, path, fn) {
let keys = path.split('.');
const keys = path.split('.');
let node = rootNode;
for (let key of keys) {
let child = node.children.get(key);
for (const key of keys) {
const child = getChildByKey(node, key);
if (!child) {
return;
}
@@ -1116,9 +1260,24 @@ function collectRecommendations({
}
}
/**
* Match key with optional chaining
* key -> key
* key? -> key
* key -> key?
* Otherwise undefined.
*/
function getChildByKey(node, key) {
return (
node.children.get(key) ||
node.children.get(key.split('?')[0]) ||
node.children.get(key + '?')
);
}
// Now we can learn which dependencies are missing or necessary.
let missingDependencies = new Set();
let satisfyingDependencies = new Set();
const missingDependencies = new Set();
const satisfyingDependencies = new Set();
scanTreeRecursively(
depTree,
missingDependencies,
@@ -1128,10 +1287,13 @@ function collectRecommendations({
function scanTreeRecursively(node, missingPaths, satisfyingPaths, keyToPath) {
node.children.forEach((child, key) => {
const path = keyToPath(key);
// For analyzing dependencies, we want the "normalized" path, without any optional chaining ("?.") operator
// foo?.bar -> foo.bar
const normalizedPath = path.replace(/\?$/, '');
if (child.isSatisfiedRecursively) {
if (child.hasRequiredNodesBelow) {
// Remember this dep actually satisfied something.
satisfyingPaths.add(path);
satisfyingPaths.add(normalizedPath);
}
// It doesn't matter if there's something deeper.
// It would be transitively satisfied since we assume immutability.
@@ -1140,7 +1302,7 @@ function collectRecommendations({
}
if (child.isRequired) {
// Remember that no declared deps satisfied this node.
missingPaths.add(path);
missingPaths.add(normalizedPath);
// If we got here, nothing in its subtree was satisfied.
// No need to search further.
return;
@@ -1155,9 +1317,9 @@ function collectRecommendations({
}
// Collect suggestions in the order they were originally specified.
let suggestedDependencies = [];
let unnecessaryDependencies = new Set();
let duplicateDependencies = new Set();
const suggestedDependencies = [];
const unnecessaryDependencies = new Set();
const duplicateDependencies = new Set();
declaredDependencies.forEach(({key}) => {
// Does this declared dep satisfy a real need?
if (satisfyingDependencies.has(key)) {
@@ -1215,7 +1377,7 @@ function scanForDeclaredBareFunctions({
if (fnRef == null) {
return null;
}
let fnNode = fnRef.defs[0];
const fnNode = fnRef.defs[0];
if (fnNode == null) {
return null;
}
@@ -1285,7 +1447,8 @@ function scanForDeclaredBareFunctions({
*/
function getDependency(node) {
if (
node.parent.type === 'MemberExpression' &&
(node.parent.type === 'MemberExpression' ||
node.parent.type === 'OptionalMemberExpression') &&
node.parent.object === node &&
node.parent.property.name !== 'current' &&
!node.parent.computed &&
@@ -1296,6 +1459,12 @@ function getDependency(node) {
)
) {
return getDependency(node.parent);
} else if (
node.type === 'MemberExpression' &&
node.parent &&
node.parent.type === 'AssignmentExpression'
) {
return node.object;
} else {
return node;
}
@@ -1306,6 +1475,7 @@ function getDependency(node) {
* (foo) -> 'foo'
* foo.(bar) -> 'foo.bar'
* foo.bar.(baz) -> 'foo.bar.baz'
* foo?.(bar) -> 'foo?.bar'
* Otherwise throw.
*/
function toPropertyAccessString(node) {
@@ -1315,6 +1485,10 @@ function toPropertyAccessString(node) {
const object = toPropertyAccessString(node.object);
const property = toPropertyAccessString(node.property);
return `${object}.${property}`;
} else if (node.type === 'OptionalMemberExpression' && !node.computed) {
const object = toPropertyAccessString(node.object);
const property = toPropertyAccessString(node.property);
return `${object}?.${property}`;
} else {
throw new Error(`Unsupported node type: ${node.type}`);
}
@@ -1339,9 +1513,9 @@ function getNodeWithoutReactNamespace(node, options) {
// 1 for useImperativeHandle(ref, fn).
// For additionally configured Hooks, assume that they're like useEffect (0).
function getReactiveHookCallbackIndex(calleeNode, options) {
let node = getNodeWithoutReactNamespace(calleeNode);
const node = getNodeWithoutReactNamespace(calleeNode);
if (node.type !== 'Identifier') {
return null;
return -1;
}
switch (node.name) {
case 'useEffect':
@@ -1385,7 +1559,7 @@ function getReactiveHookCallbackIndex(calleeNode, options) {
* - agnostic to AST node types, it looks for `{ type: string, ... }`
*/
function fastFindReferenceWithParent(start, target) {
let queue = [start];
const queue = [start];
let item = null;
while (queue.length) {
@@ -1399,7 +1573,7 @@ function fastFindReferenceWithParent(start, target) {
continue;
}
for (let [key, value] of Object.entries(item)) {
for (const [key, value] of Object.entries(item)) {
if (key === 'parent') {
continue;
}

View File

@@ -31,10 +31,9 @@ function isHook(node) {
!node.computed &&
isHook(node.property)
) {
// Only consider React.useFoo() to be namespace hooks for now to avoid false positives.
// We can expand this check later.
const obj = node.object;
return obj.type === 'Identifier' && obj.name === 'React';
const isPascalCaseNameSpace = /^[A-Z].*/;
return obj.type === 'Identifier' && isPascalCaseNameSpace.test(obj.name);
} else {
return false;
}
@@ -106,6 +105,15 @@ function isInsideComponentOrHook(node) {
}
export default {
meta: {
type: 'problem',
docs: {
description: 'enforces the Rules of Hooks',
category: 'Possible Errors',
recommended: true,
url: 'https://reactjs.org/docs/hooks-rules.html',
},
},
create(context) {
const codePathReactHooksMapStack = [];
const codePathSegmentStack = [];
@@ -226,7 +234,7 @@ export default {
function countPathsToEnd(segment, pathHistory) {
const {cache} = countPathsToEnd;
let paths = cache.get(segment.id);
let pathList = new Set(pathHistory);
const pathList = new Set(pathHistory);
// If `pathList` includes the current segment then we've found a cycle!
// We need to fill `cyclic` with all segments inside cycle
@@ -461,18 +469,20 @@ export default {
codePathNode.parent.type === 'ClassProperty') &&
codePathNode.parent.value === codePathNode
) {
// Ignore class methods for now because they produce too many
// false positives due to feature flag checks. We're less
// sensitive to them in classes because hooks would produce
// runtime errors in classes anyway, and because a use*()
// call in a class, if it works, is unambiguously *not* a hook.
// Custom message for hooks inside a class
const message =
`React Hook "${context.getSource(hook)}" cannot be called ` +
'in a class component. React Hooks must be called in a ' +
'React function component or a custom React Hook function.';
context.report({node: hook, message});
} else if (codePathFunctionName) {
// Custom message if we found an invalid function name.
const message =
`React Hook "${context.getSource(hook)}" is called in ` +
`function "${context.getSource(codePathFunctionName)}" ` +
'that is neither a React function component nor a custom ' +
'React Hook function.';
'React Hook function.' +
' React component names must start with an uppercase letter.';
context.report({node: hook, message});
} else if (codePathNode.type === 'Program') {
// These are dangerous if you have inline requires enabled.
@@ -526,7 +536,7 @@ export default {
* easy. For anonymous function expressions it is much harder. If you search for
* `IsAnonymousFunctionDefinition()` in the ECMAScript spec you'll find places
* where JS gives anonymous function expressions names. We roughly detect the
* same AST nodes with some exceptions to better fit our usecase.
* same AST nodes with some exceptions to better fit our use case.
*/
function getFunctionName(node) {

View File

@@ -7,10 +7,20 @@
'use strict';
import RuleOfHooks from './RulesOfHooks';
import RulesOfHooks from './RulesOfHooks';
import ExhaustiveDeps from './ExhaustiveDeps';
export const configs = {
recommended: {
plugins: ['react-hooks'],
rules: {
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
},
},
};
export const rules = {
'rules-of-hooks': RuleOfHooks,
'rules-of-hooks': RulesOfHooks,
'exhaustive-deps': ExhaustiveDeps,
};

View File

@@ -1,6 +1,6 @@
{
"name": "jest-react",
"version": "0.11.0",
"version": "0.11.1",
"description": "Jest matchers and utilities for testing React components.",
"main": "index.js",
"repository": {
@@ -23,6 +23,9 @@
"react": "^16.0.0",
"react-test-renderer": "^16.0.0"
},
"dependencies": {
"object-assign": "^4.1.1"
},
"files": [
"LICENSE",
"README.md",

View File

@@ -99,7 +99,7 @@ function jsonChildrenToJSXChildren(jsonChildren) {
if (jsonChildren.length === 1) {
return jsonChildToJSXChild(jsonChildren[0]);
} else if (jsonChildren.length > 1) {
let jsxChildren = [];
const jsxChildren = [];
let allJSXChildrenAreStrings = true;
let jsxChildrenString = '';
for (let i = 0; i < jsonChildren.length; i++) {

View File

@@ -11,12 +11,18 @@ import type {DispatchConfig} from './ReactSyntheticEventType';
import type {
AnyNativeEvent,
PluginName,
PluginModule,
LegacyPluginModule,
ModernPluginModule,
} from './PluginModuleType';
import invariant from 'shared/invariant';
type NamesToPlugins = {[key: PluginName]: PluginModule<AnyNativeEvent>, ...};
type NamesToPlugins = {
[key: PluginName]:
| LegacyPluginModule<AnyNativeEvent>
| ModernPluginModule<AnyNativeEvent>,
...,
};
type EventPluginOrder = null | Array<PluginName>;
/**
@@ -84,7 +90,9 @@ function recomputePluginOrdering(): void {
*/
function publishEventForPlugin(
dispatchConfig: DispatchConfig,
pluginModule: PluginModule<AnyNativeEvent>,
pluginModule:
| LegacyPluginModule<AnyNativeEvent>
| ModernPluginModule<AnyNativeEvent>,
eventName: string,
): boolean {
invariant(
@@ -128,7 +136,9 @@ function publishEventForPlugin(
*/
function publishRegistrationName(
registrationName: string,
pluginModule: PluginModule<AnyNativeEvent>,
pluginModule:
| LegacyPluginModule<AnyNativeEvent>
| ModernPluginModule<AnyNativeEvent>,
eventName: string,
): void {
invariant(
@@ -241,3 +251,20 @@ export function injectEventPluginsByName(
recomputePluginOrdering();
}
}
export function injectEventPlugins(
eventPlugins: [ModernPluginModule<AnyNativeEvent>],
): void {
for (let i = 0; i < eventPlugins.length; i++) {
const pluginModule = eventPlugins[i];
plugins.push(pluginModule);
const publishedEvents = pluginModule.eventTypes;
for (const eventName in publishedEvents) {
publishEventForPlugin(
publishedEvents[eventName],
pluginModule,
eventName,
);
}
}
}

View File

@@ -1,130 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
getParentInstance,
traverseTwoPhase,
traverseEnterLeave,
} from 'shared/ReactTreeTraversal';
import getListener from 'legacy-events/getListener';
import accumulateInto from './accumulateInto';
import forEachAccumulated from './forEachAccumulated';
type PropagationPhases = 'bubbled' | 'captured';
/**
* Some event types have a notion of different registration names for different
* "phases" of propagation. This finds listeners by a given phase.
*/
function listenerAtPhase(inst, event, propagationPhase: PropagationPhases) {
const registrationName =
event.dispatchConfig.phasedRegistrationNames[propagationPhase];
return getListener(inst, registrationName);
}
/**
* A small set of propagation patterns, each of which will accept a small amount
* of information, and generate a set of "dispatch ready event objects" - which
* are sets of events that have already been annotated with a set of dispatched
* listener functions/ids. The API is designed this way to discourage these
* propagation strategies from actually executing the dispatches, since we
* always want to collect the entire set of dispatches before executing even a
* single one.
*/
/**
* Tags a `SyntheticEvent` with dispatched listeners. Creating this function
* here, allows us to not have to bind or create functions for each event.
* Mutating the event's members allows us to not have to create a wrapping
* "dispatch" object that pairs the event with the listener.
*/
function accumulateDirectionalDispatches(inst, phase, event) {
if (__DEV__) {
if (!inst) {
console.error('Dispatching inst must not be null');
}
}
const listener = listenerAtPhase(inst, event, phase);
if (listener) {
event._dispatchListeners = accumulateInto(
event._dispatchListeners,
listener,
);
event._dispatchInstances = accumulateInto(event._dispatchInstances, inst);
}
}
/**
* Collect dispatches (must be entirely collected before dispatching - see unit
* tests). Lazily allocate the array to conserve memory. We must loop through
* each event and perform the traversal for each one. We cannot perform a
* single traversal for the entire collection of events because each event may
* have a different target.
*/
function accumulateTwoPhaseDispatchesSingle(event) {
if (event && event.dispatchConfig.phasedRegistrationNames) {
traverseTwoPhase(event._targetInst, accumulateDirectionalDispatches, event);
}
}
/**
* Same as `accumulateTwoPhaseDispatchesSingle`, but skips over the targetID.
*/
function accumulateTwoPhaseDispatchesSingleSkipTarget(event) {
if (event && event.dispatchConfig.phasedRegistrationNames) {
const targetInst = event._targetInst;
const parentInst = targetInst ? getParentInstance(targetInst) : null;
traverseTwoPhase(parentInst, accumulateDirectionalDispatches, event);
}
}
/**
* Accumulates without regard to direction, does not look for phased
* registration names. Same as `accumulateDirectDispatchesSingle` but without
* requiring that the `dispatchMarker` be the same as the dispatched ID.
*/
function accumulateDispatches(inst, ignoredDirection, event) {
if (inst && event && event.dispatchConfig.registrationName) {
const registrationName = event.dispatchConfig.registrationName;
const listener = getListener(inst, registrationName);
if (listener) {
event._dispatchListeners = accumulateInto(
event._dispatchListeners,
listener,
);
event._dispatchInstances = accumulateInto(event._dispatchInstances, inst);
}
}
}
/**
* Accumulates dispatches on an `SyntheticEvent`, but only for the
* `dispatchMarker`.
* @param {SyntheticEvent} event
*/
function accumulateDirectDispatchesSingle(event) {
if (event && event.dispatchConfig.registrationName) {
accumulateDispatches(event._targetInst, null, event);
}
}
export function accumulateTwoPhaseDispatches(events) {
forEachAccumulated(events, accumulateTwoPhaseDispatchesSingle);
}
export function accumulateTwoPhaseDispatchesSkipTarget(events) {
forEachAccumulated(events, accumulateTwoPhaseDispatchesSingleSkipTarget);
}
export function accumulateEnterLeaveDispatches(leave, enter, from, to) {
traverseEnterLeave(from, to, accumulateDispatches, leave, enter);
}
export function accumulateDirectDispatches(events) {
forEachAccumulated(events, accumulateDirectDispatchesSingle);
}

View File

@@ -7,29 +7,59 @@
* @flow
*/
import type {Fiber} from 'react-reconciler/src/ReactFiber';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import type {
DispatchConfig,
ReactSyntheticEvent,
} from './ReactSyntheticEventType';
import type {TopLevelType} from './TopLevelEventTypes';
import type {EventSystemFlags} from 'legacy-events/EventSystemFlags';
export type EventTypes = {[key: string]: DispatchConfig, ...};
export type AnyNativeEvent = Event | KeyboardEvent | MouseEvent | Touch;
export type AnyNativeEvent = Event | KeyboardEvent | MouseEvent | TouchEvent;
export type PluginName = string;
export type PluginModule<NativeEvent> = {
export type EventSystemFlags = number;
export type LegacyPluginModule<NativeEvent> = {
eventTypes: EventTypes,
extractEvents: (
topLevelType: TopLevelType,
targetInst: null | Fiber,
nativeTarget: NativeEvent,
nativeEventTarget: null | EventTarget,
eventSystemFlags: EventSystemFlags,
container?: Document | Element | Node,
eventSystemFlags?: number,
container?: null | EventTarget,
) => ?ReactSyntheticEvent,
tapMoveThreshold?: number,
};
export type DispatchQueueItemPhaseEntry = {|
instance: null | Fiber,
listener: Function,
currentTarget: EventTarget,
|};
export type DispatchQueueItemPhase = Array<DispatchQueueItemPhaseEntry>;
export type DispatchQueueItem = {|
event: ReactSyntheticEvent,
capture: DispatchQueueItemPhase,
bubble: DispatchQueueItemPhase,
|};
export type DispatchQueue = Array<DispatchQueueItem>;
export type ModernPluginModule<NativeEvent> = {
eventTypes: EventTypes,
extractEvents: (
dispatchQueue: DispatchQueue,
topLevelType: TopLevelType,
targetInst: null | Fiber,
nativeTarget: NativeEvent,
nativeEventTarget: null | EventTarget,
eventSystemFlags: number,
container: null | EventTarget,
) => void,
};

View File

@@ -5,14 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/
import {
needsStateRestore,
restoreStateIfNeeded,
} from './ReactControlledComponent';
import {enableDeprecatedFlareAPI} from 'shared/ReactFeatureFlags';
import {invokeGuardedCallbackAndCatchFirstError} from 'shared/ReactErrorUtils';
// Used as a way to call batchedUpdates when we don't have a reference to
// the renderer. Such as when we're dispatching events or if third party
// libraries need to call batchedUpdates. Eventually, this API will go away when
@@ -32,21 +24,6 @@ let batchedEventUpdatesImpl = batchedUpdatesImpl;
let isInsideEventHandler = false;
let isBatchingEventUpdates = false;
function finishEventHandler() {
// Here we wait until all updates have propagated, which is important
// when using controlled components within layers:
// https://github.com/facebook/react/issues/1698
// Then we restore state of any controlled component.
const controlledComponentsHavePendingUpdates = needsStateRestore();
if (controlledComponentsHavePendingUpdates) {
// If a controlled event was fired, we may need to restore the state of
// the DOM node back to the controlled value. This is necessary when React
// bails out of the update without touching the DOM.
flushDiscreteUpdatesImpl();
restoreStateIfNeeded();
}
}
export function batchedUpdates(fn, bookkeeping) {
if (isInsideEventHandler) {
// If we are currently inside another batch, we need to wait until it
@@ -58,7 +35,6 @@ export function batchedUpdates(fn, bookkeeping) {
return batchedUpdatesImpl(fn, bookkeeping);
} finally {
isInsideEventHandler = false;
finishEventHandler();
}
}
@@ -73,19 +49,6 @@ export function batchedEventUpdates(fn, a, b) {
return batchedEventUpdatesImpl(fn, a, b);
} finally {
isBatchingEventUpdates = false;
finishEventHandler();
}
}
// This is for the React Flare event system
export function executeUserEventHandler(fn: any => void, value: any): void {
const previouslyInEventHandler = isInsideEventHandler;
try {
isInsideEventHandler = true;
const type = typeof value === 'object' && value !== null ? value.type : '';
invokeGuardedCallbackAndCatchFirstError(type, fn, undefined, value);
} finally {
isInsideEventHandler = previouslyInEventHandler;
}
}
@@ -97,32 +60,12 @@ export function discreteUpdates(fn, a, b, c, d) {
} finally {
isInsideEventHandler = prevIsInsideEventHandler;
if (!isInsideEventHandler) {
finishEventHandler();
}
}
}
let lastFlushedEventTimeStamp = 0;
export function flushDiscreteUpdatesIfNeeded(timeStamp: number) {
// event.timeStamp isn't overly reliable due to inconsistencies in
// how different browsers have historically provided the time stamp.
// Some browsers provide high-resolution time stamps for all events,
// some provide low-resolution time stamps for all events. FF < 52
// even mixes both time stamps together. Some browsers even report
// negative time stamps or time stamps that are 0 (iOS9) in some cases.
// Given we are only comparing two time stamps with equality (!==),
// we are safe from the resolution differences. If the time stamp is 0
// we bail-out of preventing the flush, which can affect semantics,
// such as if an earlier flush removes or adds event listeners that
// are fired in the subsequent flush. However, this is the same
// behaviour as we had before this change, so the risks are low.
if (
!isInsideEventHandler &&
(!enableDeprecatedFlareAPI ||
timeStamp === 0 ||
lastFlushedEventTimeStamp !== timeStamp)
) {
lastFlushedEventTimeStamp = timeStamp;
if (!isInsideEventHandler) {
flushDiscreteUpdatesImpl();
}
}

Some files were not shown because too many files have changed in this diff Show More