Skip to content

Commit

Permalink
Adding 'transform-symbol-member' transform to preset.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
skevy authored and facebook-github-bot-0 committed Feb 8, 2016
1 parent 5a109ae commit 194092e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions babel-preset/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
'transform-react-jsx',
'transform-regenerator',
['transform-es2015-for-of', { loose: true }],
require('../transforms/transform-symbol-member'),
]),
retainLines: true,
sourceMaps: false,
Expand Down
2 changes: 1 addition & 1 deletion babel-preset/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-preset-react-native",
"version": "1.2.4",
"version": "1.4.0",
"description": "Babel preset for React Native applications",
"main": "index.js",
"repository": "https://github.com/facebook/react-native/tree/master/babel-preset",
Expand Down
61 changes: 61 additions & 0 deletions babel-preset/transforms/transform-symbol-member.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*/

'use strict';

/*eslint consistent-return: 0*/

/**
* Transforms function properties of the `Symbol` into
* the presence check, and fallback string "@@<name>".
*
* Example:
*
* Symbol.iterator;
*
* Transformed to:
*
* typeof Symbol.iterator === 'function' ? Symbol.iterator : '@@iterator';
*/
module.exports = function symbolMember(babel) {
const t = babel.types;

return {
visitor: {
MemberExpression(path) {
let node = path.node;

if (!isAppropriateMember(node)) {
return;
}

path.replaceWith(
t.conditionalExpression(
t.binaryExpression(
'===',
t.unaryExpression(
'typeof',
t.identifier('Symbol'),
true
),
t.stringLiteral('function')
),
node,
t.stringLiteral(`@@${node.property.name}`)
)
);

// We should stop to avoid infinite recursion, since Babel
// traverses replaced path, and again would hit our transform.
path.stop();
},
},
};
};

function isAppropriateMember(node) {
return node.object.type === 'Identifier' &&
node.object.name === 'Symbol' &&
node.property.type === 'Identifier';
}

0 comments on commit 194092e

Please sign in to comment.