2016-02-08 23:00:42 +00:00
|
|
|
/**
|
2018-03-08 20:02:45 +00:00
|
|
|
* Copyright (c) 2004-present, Facebook, Inc.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-02-08 23:00:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
'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) {
|
2016-11-23 17:27:36 +00:00
|
|
|
if (!isAppropriateMember(path)) {
|
2016-02-08 23:00:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:27:36 +00:00
|
|
|
let node = path.node;
|
|
|
|
|
2016-02-08 23:00:42 +00:00
|
|
|
path.replaceWith(
|
|
|
|
t.conditionalExpression(
|
|
|
|
t.binaryExpression(
|
|
|
|
'===',
|
2017-07-13 02:22:43 +00:00
|
|
|
t.unaryExpression(
|
|
|
|
'typeof',
|
|
|
|
t.identifier('Symbol'),
|
|
|
|
true
|
|
|
|
),
|
|
|
|
t.stringLiteral('function')
|
2016-02-08 23:00:42 +00:00
|
|
|
),
|
|
|
|
node,
|
2017-07-13 02:22:43 +00:00
|
|
|
t.stringLiteral(`@@${node.property.name}`)
|
|
|
|
)
|
2016-02-08 23:00:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// We should stop to avoid infinite recursion, since Babel
|
|
|
|
// traverses replaced path, and again would hit our transform.
|
|
|
|
path.stop();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-11-23 17:27:36 +00:00
|
|
|
function isAppropriateMember(path) {
|
|
|
|
let node = path.node;
|
|
|
|
|
2017-07-13 02:22:43 +00:00
|
|
|
return path.parentPath.type !== 'AssignmentExpression' &&
|
2016-11-23 17:27:36 +00:00
|
|
|
node.object.type === 'Identifier' &&
|
2016-02-08 23:00:42 +00:00
|
|
|
node.object.name === 'Symbol' &&
|
2017-07-13 02:22:43 +00:00
|
|
|
node.property.type === 'Identifier';
|
2016-02-08 23:00:42 +00:00
|
|
|
}
|