forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding 'transform-symbol-member' transform to preset.
Summary: Turns out, even after discussion that was had in facebook#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 facebook#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 facebook#5824 Reviewed By: svcscm Differential Revision: D2913315 Pulled By: vjeux fb-gh-sync-id: abaf484a9431b3111e8118d01db8d2c0d2dd73ca shipit-source-id: abaf484a9431b3111e8118d01db8d2c0d2dd73ca
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |