Skip to content
This repository was archived by the owner on Aug 4, 2018. It is now read-only.

Commit 08bb948

Browse files
Merge pull request reactjs#571 from JoshuaKGoldberg/global-name-error
Returned a list of missing globals in shims.js
2 parents c415f60 + 2b17ed4 commit 08bb948

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/React.Core/JavaScriptEngineFactory.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,14 @@ private void LoadUserScripts(IJsEngine engine)
180180
/// <param name="engine">Engine to check</param>
181181
private static void EnsureReactLoaded(IJsEngine engine)
182182
{
183-
var result = engine.CallFunction<bool>("ReactNET_initReact");
184-
if (!result)
183+
var result = engine.CallFunction<string[]>("ReactNET_initReact");
184+
if (result.Length != 0)
185185
{
186186
throw new ReactNotInitialisedException(
187-
"React has not been loaded correctly. Please expose your version of React as global " +
188-
"variables named 'React', 'ReactDOM', and 'ReactDOMServer', or enable the " +
189-
"'LoadReact' configuration option to use the built-in version of React."
187+
$"React has not been loaded correctly: missing ({string.Join(", ", result)})." +
188+
"Please expose your version of React as global variables named " +
189+
"'React', 'ReactDOM', and 'ReactDOMServer', or enable the 'LoadReact'" +
190+
"configuration option to use the built-in version of React."
190191
);
191192
}
192193
}

src/React.Core/Resources/shims.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (c) 2014-Present, Facebook, Inc.
33
* All rights reserved.
44
*
@@ -46,26 +46,37 @@ if (!Object.freeze) {
4646
/**
4747
* Finds a user-supplied version of React and ensures it's exposed globally.
4848
*
49-
* @return {bool}
49+
* @return {string[]} Which globals are missing, if any.
5050
*/
5151
function ReactNET_initReact() {
52-
if (
53-
typeof React !== 'undefined' &&
54-
typeof ReactDOM !== 'undefined' &&
55-
typeof ReactDOMServer !== 'undefined'
56-
) {
57-
// React is already a global, woohoo
58-
return true;
52+
var missing = [];
53+
54+
if (typeof React === 'undefined') {
55+
if (global.React) {
56+
React = global.React;
57+
} else {
58+
missing.push('React');
59+
}
60+
}
61+
62+
if (typeof ReactDOM === 'undefined') {
63+
if (global.ReactDOM) {
64+
ReactDOM = global.ReactDOM;
65+
} else {
66+
missing.push('ReactDOM');
67+
}
5968
}
6069

61-
if (global.React && global.ReactDOM && global.ReactDOMServer) {
62-
React = global.React;
63-
ReactDOM = global.ReactDOM;
64-
ReactDOMServer = global.ReactDOMServer;
65-
return true;
70+
if (typeof ReactDOMServer === 'undefined') {
71+
if (global.ReactDOMServer) {
72+
ReactDOMServer = global.ReactDOMServer;
73+
}
74+
else {
75+
missing.push('ReactDOMServer');
76+
}
6677
}
67-
// :'(
68-
return false;
78+
79+
return missing;
6980
}
7081

7182
setTimeout = setTimeout || global.setTimeout;

tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void ShouldHandleLoadingExternalReactVersion()
125125
{
126126
var jsEngine = new Mock<IJsEngine>();
127127
jsEngine.Setup(x => x.Evaluate<int>("1 + 1")).Returns(2);
128-
jsEngine.Setup(x => x.CallFunction<bool>("ReactNET_initReact")).Returns(true);
128+
jsEngine.Setup(x => x.CallFunction<string[]>("ReactNET_initReact")).Returns(new string[] { });
129129
var config = new Mock<IReactSiteConfiguration>();
130130
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string>());
131131
config.Setup(x => x.LoadReact).Returns(false);
@@ -134,15 +134,15 @@ public void ShouldHandleLoadingExternalReactVersion()
134134

135135
factory.GetEngineForCurrentThread();
136136

137-
jsEngine.Verify(x => x.CallFunction<bool>("ReactNET_initReact"));
137+
jsEngine.Verify(x => x.CallFunction<string[]>("ReactNET_initReact"));
138138
}
139139

140140
[Fact]
141141
public void ShouldThrowIfReactVersionNotLoaded()
142142
{
143143
var jsEngine = new Mock<IJsEngine>();
144144
jsEngine.Setup(x => x.Evaluate<int>("1 + 1")).Returns(2);
145-
jsEngine.Setup(x => x.CallFunction<bool>("ReactNET_initReact")).Returns(false);
145+
jsEngine.Setup(x => x.CallFunction<string[]>("ReactNET_initReact")).Returns(new string[] { "React" });
146146
var config = new Mock<IReactSiteConfiguration>();
147147
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string>());
148148
config.Setup(x => x.LoadReact).Returns(false);

0 commit comments

Comments
 (0)