Skip to content

Commit 8f642b1

Browse files
committed
Add ability to strip out Flow type annotations
1 parent 71d0cf4 commit 8f642b1

File tree

9 files changed

+134
-26
lines changed

9 files changed

+134
-26
lines changed

src/React.MSBuild/TransformJsx.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public class TransformJsx : Task
3636
/// <returns><c>true</c> if support for es6 syntax should be rewritten.</returns>
3737
public bool UseHarmony { get; set; }
3838

39+
/// <summary>
40+
/// Gets or sets whether Flow types should be stripped.
41+
/// </summary>
42+
public bool StripTypes { get; set; }
43+
3944
/// <summary>
4045
/// Executes the task.
4146
/// </summary>
@@ -45,6 +50,7 @@ public override bool Execute()
4550
MSBuildHost.EnsureInitialized();
4651
var config = React.AssemblyRegistration.Container.Resolve<IReactSiteConfiguration>();
4752
config.UseHarmony = UseHarmony;
53+
config.StripTypes = StripTypes;
4854

4955
_environment = React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
5056

src/React.Sample.Mvc4/App_Start/ReactConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static void Configure()
1818
ReactSiteConfiguration.Configuration
1919
.SetUseHarmony(true)
2020
.SetReuseJavaScriptEngines(true)
21+
.SetStripTypes(true)
2122
.AddScript("~/Content/Sample.jsx");
2223
}
2324
}

src/React.Sample.Mvc4/TransformJsx.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="TransformJsx">
33
<UsingTask AssemblyFile="..\..\bin\$(Configuration)\React.MSBuild\React.MSBuild.dll" TaskName="TransformJsx" />
44
<Target Name="TransformJsx">
5-
<TransformJsx SourceDir="$(MSBuildProjectDirectory)" UseHarmony="true" />
5+
<TransformJsx SourceDir="$(MSBuildProjectDirectory)" UseHarmony="true" StripTypes="true" />
66
</Target>
77
</Project>

src/React.Tests/Core/JsxTransformerTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public void ShouldTransformJsx()
5353
_environment.Verify(x => x.ExecuteWithLargerStackIfRequired<string>(
5454
"ReactNET_transform",
5555
"<div>Hello World</div>",
56-
It.IsAny<bool>()
56+
It.IsAny<bool?>(),
57+
It.IsAny<bool?>()
5758
));
5859
}
5960

@@ -63,7 +64,8 @@ public void ShouldWrapExceptionsInJsxExeption()
6364
_environment.Setup(x => x.ExecuteWithLargerStackIfRequired<string>(
6465
"ReactNET_transform",
6566
"<div>Hello World</div>",
66-
It.IsAny<bool>()
67+
It.IsAny<bool?>(),
68+
It.IsAny<bool?>()
6769
)).Throws(new Exception("Something broke..."));
6870

6971
const string input = "<div>Hello World</div>";
@@ -116,7 +118,8 @@ public void ShouldTransformJsxIfFileCacheHashInvalid()
116118
_environment.Setup(x => x.ExecuteWithLargerStackIfRequired<JavaScriptWithSourceMap>(
117119
"ReactNET_transform_sourcemap",
118120
It.IsAny<string>(),
119-
It.IsAny<bool>()
121+
It.IsAny<bool?>(),
122+
It.IsAny<bool?>()
120123
)).Returns(new JavaScriptWithSourceMap { Code = "React.DOM.div('Hello World')" });
121124

122125
var result = _jsxTransformer.TransformJsxFile("foo.jsx");
@@ -132,7 +135,8 @@ public void ShouldTransformJsxIfNoCache()
132135
_environment.Setup(x => x.ExecuteWithLargerStackIfRequired<JavaScriptWithSourceMap>(
133136
"ReactNET_transform_sourcemap",
134137
It.IsAny<string>(),
135-
It.IsAny<bool>()
138+
It.IsAny<bool?>(),
139+
It.IsAny<bool?>()
136140
)).Returns(new JavaScriptWithSourceMap { Code = "React.DOM.div('Hello World')" });
137141

138142
var result = _jsxTransformer.TransformJsxFile("foo.jsx");
@@ -146,7 +150,8 @@ public void ShouldSaveTransformationResult()
146150
_environment.Setup(x => x.ExecuteWithLargerStackIfRequired<JavaScriptWithSourceMap>(
147151
"ReactNET_transform_sourcemap",
148152
It.IsAny<string>(),
149-
It.IsAny<bool>()
153+
It.IsAny<bool?>(),
154+
It.IsAny<bool?>()
150155
)).Returns(new JavaScriptWithSourceMap { Code = "React.DOM.div('Hello World')" });
151156

152157
string result = null;

src/React/IJsxTransformer.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ public interface IJsxTransformer
1919
/// </summary>
2020
/// <param name="filename">Name of the file to load</param>
2121
/// <param name="useHarmony"><c>true</c> if support for ES6 syntax should be enabled</param>
22+
/// <param name="stripTypes">
23+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
24+
/// configuration.
25+
/// </param>
2226
/// <returns>JavaScript</returns>
23-
string TransformJsxFile(string filename, bool? useHarmony = null);
27+
string TransformJsxFile(string filename, bool? useHarmony = null, bool? stripTypes = null);
2428

2529
/// <summary>
2630
/// Transforms a JSX file to regular JavaScript and also returns a source map to map the
@@ -32,35 +36,56 @@ public interface IJsxTransformer
3236
/// <c>true</c> to re-transform the file if a cached version with no source map is available
3337
/// </param>
3438
/// <param name="useHarmony"><c>true</c> if support for ES6 syntax should be enabled</param>
39+
/// <param name="stripTypes">
40+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
41+
/// configuration.
42+
/// </param>
3543
/// <returns>JavaScript and source map</returns>
36-
JavaScriptWithSourceMap TransformJsxFileWithSourceMap(string filename, bool forceGenerateSourceMap = false, bool? useHarmony = null);
44+
JavaScriptWithSourceMap TransformJsxFileWithSourceMap(
45+
string filename,
46+
bool forceGenerateSourceMap = false,
47+
bool? useHarmony = null,
48+
bool? stripTypes = null
49+
);
3750

3851
/// <summary>
3952
/// Transforms JSX into regular JavaScript. The result is not cached. Use
4053
/// <see cref="TransformJsxFile"/> if loading from a file since this will cache the result.
4154
/// </summary>
4255
/// <param name="input">JSX</param>
4356
/// <param name="useHarmony"><c>true</c> if support for ES6 syntax should be enabled</param>
57+
/// <param name="stripTypes">
58+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
59+
/// configuration.
60+
/// </param>
4461
/// <returns>JavaScript</returns>
45-
string TransformJsx(string input, bool? useHarmony = null);
62+
string TransformJsx(string input, bool? useHarmony = null, bool? stripTypes = null);
4663

4764
/// <summary>
4865
/// Transforms JSX to regular JavaScript and also returns a source map to map the compiled
4966
/// source to the original version. The result is not cached.
5067
/// </summary>
5168
/// <param name="input">JSX</param>
5269
/// <param name="useHarmony"><c>true</c> if support for ES6 syntax should be enabled</param>
70+
/// <param name="stripTypes">
71+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
72+
/// configuration.
73+
/// </param>
5374
/// <returns>JavaScript and source map</returns>
54-
JavaScriptWithSourceMap TransformJsxWithSourceMap(string input, bool? useHarmony = null);
75+
JavaScriptWithSourceMap TransformJsxWithSourceMap(string input, bool? useHarmony = null, bool? stripTypes = null);
5576

5677
/// <summary>
5778
/// Transforms a JSX file to JavaScript, and saves the result into a ".generated.js" file
5879
/// alongside the original file.
5980
/// </summary>
6081
/// <param name="filename">Name of the file to load</param>
6182
/// <param name="useHarmony"><c>true</c> if support for ES6 syntax should be enabled</param>
83+
/// <param name="stripTypes">
84+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
85+
/// configuration.
86+
/// </param>
6287
/// <returns>File contents</returns>
63-
string TransformAndSaveJsxFile(string filename, bool? useHarmony = null);
88+
string TransformAndSaveJsxFile(string filename, bool? useHarmony = null, bool? stripTypes = null);
6489

6590
/// <summary>
6691
/// Returns the path the specified JSX file's compilation will be cached to

src/React/IReactSiteConfiguration.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,14 @@ public interface IReactSiteConfiguration
6868
/// </remarks>
6969
/// <param name="settings">The settings.</param>
7070
IReactSiteConfiguration SetJsonSerializerSettings(JsonSerializerSettings settings);
71+
72+
/// <summary>
73+
/// Gets or sets whether Flow types should be stripped out.
74+
/// </summary>
75+
bool StripTypes { get; set; }
76+
/// <summary>
77+
/// Sets whether Flow types should be stripped out.
78+
/// </summary>
79+
IReactSiteConfiguration SetStripTypes(bool stripTypes);
7180
}
7281
}

src/React/JsxTransformer.cs

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ public JsxTransformer(IReactEnvironment environment, ICache cache, IFileSystem f
7979
/// </summary>
8080
/// <param name="filename">Name of the file to load</param>
8181
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
82+
/// <param name="stripTypes">
83+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
84+
/// configuration.
85+
/// </param>
8286
/// <returns>JavaScript</returns>
83-
public virtual string TransformJsxFile(string filename, bool? useHarmony = null)
87+
public virtual string TransformJsxFile(string filename, bool? useHarmony = null, bool? stripTypes = null)
8488
{
85-
return TransformJsxFileWithSourceMap(filename, false, useHarmony).Code;
89+
return TransformJsxFileWithSourceMap(filename, false, useHarmony, stripTypes).Code;
8690
}
8791

8892
/// <summary>
@@ -95,8 +99,17 @@ public virtual string TransformJsxFile(string filename, bool? useHarmony = null)
9599
/// <c>true</c> to re-transform the file if a cached version with no source map is available
96100
/// </param>
97101
/// <param name="useHarmony"><c>true</c> if support for ES6 syntax should be enabled</param>
102+
/// <param name="stripTypes">
103+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
104+
/// configuration.
105+
/// </param>
98106
/// <returns>JavaScript and source map</returns>
99-
public virtual JavaScriptWithSourceMap TransformJsxFileWithSourceMap(string filename, bool forceGenerateSourceMap = false, bool? useHarmony = null)
107+
public virtual JavaScriptWithSourceMap TransformJsxFileWithSourceMap(
108+
string filename,
109+
bool forceGenerateSourceMap = false,
110+
bool? useHarmony = null,
111+
bool? stripTypes = null
112+
)
100113
{
101114
var cacheKey = string.Format(JSX_CACHE_KEY, filename);
102115

@@ -118,7 +131,7 @@ public virtual JavaScriptWithSourceMap TransformJsxFileWithSourceMap(string file
118131
// 3. Not cached, perform the transformation
119132
try
120133
{
121-
output = TransformJsxWithHeader(filename, contents, hash, useHarmony);
134+
output = TransformJsxWithHeader(filename, contents, hash, useHarmony, stripTypes);
122135
}
123136
catch (JsxException ex)
124137
{
@@ -211,15 +224,25 @@ protected virtual JavaScriptWithSourceMap LoadJsxFromFileCache(string filename,
211224
/// <param name="contents">Contents of the input file</param>
212225
/// <param name="hash">Hash of the input. If null, it will be calculated</param>
213226
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
227+
/// <param name="stripTypes">
228+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
229+
/// configuration.
230+
/// </param>
214231
/// <returns>JavaScript</returns>
215-
protected virtual JavaScriptWithSourceMap TransformJsxWithHeader(string filename, string contents, string hash = null, bool? useHarmony = null)
232+
protected virtual JavaScriptWithSourceMap TransformJsxWithHeader(
233+
string filename,
234+
string contents,
235+
string hash = null,
236+
bool? useHarmony = null,
237+
bool? stripTypes = null
238+
)
216239
{
217240
if (string.IsNullOrEmpty(hash))
218241
{
219242
hash = _fileCacheHash.CalculateHash(contents);
220243
}
221244
var header = GetFileHeader(hash);
222-
var result = TransformJsxWithSourceMap(header + contents, useHarmony);
245+
var result = TransformJsxWithSourceMap(header + contents, useHarmony, stripTypes);
223246
result.Hash = hash;
224247
if (result.SourceMap != null)
225248
{
@@ -240,16 +263,21 @@ protected virtual JavaScriptWithSourceMap TransformJsxWithHeader(string filename
240263
/// </summary>
241264
/// <param name="input">JSX</param>
242265
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
266+
/// <param name="stripTypes">
267+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
268+
/// configuration.
269+
/// </param>
243270
/// <returns>JavaScript</returns>
244-
public virtual string TransformJsx(string input, bool? useHarmony = null)
271+
public virtual string TransformJsx(string input, bool? useHarmony = null, bool? stripTypes = null)
245272
{
246273
EnsureJsxTransformerSupported();
247274
try
248275
{
249276
var output = _environment.ExecuteWithLargerStackIfRequired<string>(
250277
"ReactNET_transform",
251278
input,
252-
useHarmony ?? _config.UseHarmony
279+
useHarmony ?? _config.UseHarmony,
280+
stripTypes ?? _config.StripTypes
253281
);
254282
return output;
255283
}
@@ -265,16 +293,25 @@ public virtual string TransformJsx(string input, bool? useHarmony = null)
265293
/// </summary>
266294
/// <param name="input">JSX</param>
267295
/// <param name="useHarmony"><c>true</c> if support for ES6 syntax should be enabled</param>
296+
/// <param name="stripTypes">
297+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
298+
/// configuration.
299+
/// </param>
268300
/// <returns>JavaScript and source map</returns>
269-
public virtual JavaScriptWithSourceMap TransformJsxWithSourceMap(string input, bool? useHarmony)
301+
public virtual JavaScriptWithSourceMap TransformJsxWithSourceMap(
302+
string input,
303+
bool? useHarmony = null,
304+
bool? stripTypes = null
305+
)
270306
{
271307
EnsureJsxTransformerSupported();
272308
try
273309
{
274310
return _environment.ExecuteWithLargerStackIfRequired<JavaScriptWithSourceMap>(
275311
"ReactNET_transform_sourcemap",
276312
input,
277-
useHarmony ?? _config.UseHarmony
313+
useHarmony ?? _config.UseHarmony,
314+
stripTypes ?? _config.StripTypes
278315
);
279316
}
280317
catch (Exception ex)
@@ -331,13 +368,21 @@ public virtual string GetSourceMapOutputPath(string path)
331368
/// </summary>
332369
/// <param name="filename">Name of the file to load</param>
333370
/// <param name="useHarmony"><c>true</c> if support for es6 syntax should be rewritten.</param>
371+
/// <param name="stripTypes">
372+
/// Whether Flow types should be stripped out. Defaults to the value set in the site
373+
/// configuration.
374+
/// </param>
334375
/// <returns>File contents</returns>
335-
public virtual string TransformAndSaveJsxFile(string filename, bool? useHarmony = null)
376+
public virtual string TransformAndSaveJsxFile(
377+
string filename,
378+
bool? useHarmony = null,
379+
bool? stripTypes = null
380+
)
336381
{
337382
var outputPath = GetJsxOutputPath(filename);
338383
var sourceMapPath = GetSourceMapOutputPath(filename);
339384
var contents = _fileSystem.ReadAsString(filename);
340-
var result = TransformJsxWithHeader(filename, contents, useHarmony: useHarmony);
385+
var result = TransformJsxWithHeader(filename, contents, null, useHarmony, stripTypes);
341386
_fileSystem.WriteAsString(outputPath, result.Code);
342387
_fileSystem.WriteAsString(sourceMapPath, result.SourceMap == null ? string.Empty : result.SourceMap.ToJson());
343388
return outputPath;

src/React/ReactSiteConfiguration.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,18 @@ public IReactSiteConfiguration SetReuseJavaScriptEngines(bool value)
111111
ReuseJavaScriptEngines = value;
112112
return this;
113113
}
114+
115+
/// <summary>
116+
/// Gets or sets whether Flow types should be stripped.
117+
/// </summary>
118+
public bool StripTypes { get; set; }
119+
/// <summary>
120+
/// Sets whether Flow types should be stripped
121+
/// </summary>
122+
public IReactSiteConfiguration SetStripTypes(bool stripTypes)
123+
{
124+
StripTypes = stripTypes;
125+
return this;
126+
}
114127
}
115128
}

src/React/Resources/shims.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ if (!Object.freeze) {
1919
Object.freeze = function() { };
2020
}
2121

22-
function ReactNET_transform(input, harmony) {
22+
function ReactNET_transform(input, harmony, stripTypes) {
2323
try {
24-
return global.JSXTransformer.transform(input, { harmony: !!harmony }).code;
24+
return global.JSXTransformer.transform(input, {
25+
harmony: !!harmony,
26+
stripTypes: !!stripTypes
27+
}).code;
2528
} catch (ex) {
2629
throw new Error(ex.message + " (at line " + ex.lineNumber + " column " + ex.column + ")");
2730
}
2831
}
2932

30-
function ReactNET_transform_sourcemap(input, harmony) {
33+
function ReactNET_transform_sourcemap(input, harmony, stripTypes) {
3134
try {
3235
var result = global.JSXTransformer.transform(input, {
3336
harmony: !!harmony,
37+
stripTypes: !!stripTypes,
3438
sourceMap: true
3539
});
3640
if (!result.sourceMap) {

0 commit comments

Comments
 (0)