Skip to content

Commit d5ed811

Browse files
skevybestander
authored andcommitted
Adding 'transform-symbol-member' transform to preset.
Summary: Turns out, even after discussion that was had in #5294 (comment), we really do need this transform. I've just included it in the preset...let me know if you all would rather publish to npm. The actual reason why this is necessary is because in the latest sync from FB, fbjs was updated to use the `Symbol.iterator` express in it's isEmpty function: facebook/fbjs@064a484 We use this in RN in the ListView...and this change (once #5084 is merged) will cause ListView to break on older JSC context's. This resolves that, and is probably something we should have had all along. Closes #5824 Reviewed By: svcscm Differential Revision: D2913315 Pulled By: vjeux fb-gh-sync-id: abaf484a9431b3111e8118d01db8d2c0d2dd73ca shipit-source-id: abaf484a9431b3111e8118d01db8d2c0d2dd73ca
1 parent 4ad141d commit d5ed811

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

babel-preset/configs/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = {
3636
'transform-react-jsx',
3737
'transform-regenerator',
3838
['transform-es2015-for-of', { loose: true }],
39+
require('../transforms/transform-symbol-member'),
3940
]),
4041
retainLines: true,
4142
sourceMaps: false,

babel-preset/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-preset-react-native",
3-
"version": "1.2.4",
3+
"version": "1.4.0",
44
"description": "Babel preset for React Native applications",
55
"main": "index.js",
66
"repository": "https://github.com/facebook/react-native/tree/master/babel-preset",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright 2004-present Facebook. All Rights Reserved.
3+
*/
4+
5+
'use strict';
6+
7+
/*eslint consistent-return: 0*/
8+
9+
/**
10+
* Transforms function properties of the `Symbol` into
11+
* the presence check, and fallback string "@@<name>".
12+
*
13+
* Example:
14+
*
15+
* Symbol.iterator;
16+
*
17+
* Transformed to:
18+
*
19+
* typeof Symbol.iterator === 'function' ? Symbol.iterator : '@@iterator';
20+
*/
21+
module.exports = function symbolMember(babel) {
22+
const t = babel.types;
23+
24+
return {
25+
visitor: {
26+
MemberExpression(path) {
27+
let node = path.node;
28+
29+
if (!isAppropriateMember(node)) {
30+
return;
31+
}
32+
33+
path.replaceWith(
34+
t.conditionalExpression(
35+
t.binaryExpression(
36+
'===',
37+
t.unaryExpression(
38+
'typeof',
39+
t.identifier('Symbol'),
40+
true
41+
),
42+
t.stringLiteral('function')
43+
),
44+
node,
45+
t.stringLiteral(`@@${node.property.name}`)
46+
)
47+
);
48+
49+
// We should stop to avoid infinite recursion, since Babel
50+
// traverses replaced path, and again would hit our transform.
51+
path.stop();
52+
},
53+
},
54+
};
55+
};
56+
57+
function isAppropriateMember(node) {
58+
return node.object.type === 'Identifier' &&
59+
node.object.name === 'Symbol' &&
60+
node.property.type === 'Identifier';
61+
}

0 commit comments

Comments
 (0)