Skip to content

Instantly share code, notes, and snippets.

@alvinthen
Created November 12, 2016 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvinthen/21be4baacbe56c1f25c423797cb6951c to your computer and use it in GitHub Desktop.
Save alvinthen/21be4baacbe56c1f25c423797cb6951c to your computer and use it in GitHub Desktop.
Replace node_modules/babel-preset-react-native/transforms/transform-symbol-member.js to transform RxJS v5 on React Native
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*/
/* eslint-disable */
'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) {
if (!isAppropriateMember(path)) {
return;
}
let node = path.node;
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(path) {
let node = path.node;
return path.parentPath.type !== 'AssignmentExpression' &&
node.object.type === 'Identifier' &&
node.object.name === 'Symbol' &&
node.property.type === 'Identifier';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment