1 | /**
|
2 | * Copyright (c) 2004-present, Facebook, Inc.
|
3 | *
|
4 | * This source code is licensed under the MIT license found in the
|
5 | * LICENSE file in the root directory of this source tree.
|
6 | *
|
7 | * @format
|
8 | */
|
9 |
|
10 | ;
|
11 |
|
12 | /*eslint consistent-return: 0*/
|
13 |
|
14 | /**
|
15 | * Transforms function properties of the `Symbol` into
|
16 | * the presence check, and fallback string "@@<name>".
|
17 | *
|
18 | * Example:
|
19 | *
|
20 | * Symbol.iterator;
|
21 | *
|
22 | * Transformed to:
|
23 | *
|
24 | * typeof Symbol.iterator === 'function' ? Symbol.iterator : '@@iterator';
|
25 | */
|
26 | module.exports = function symbolMember(babel) {
|
27 | const t = babel.types;
|
28 |
|
29 | return {
|
30 | visitor: {
|
31 | MemberExpression(path) {
|
32 | if (!isAppropriateMember(path)) {
|
33 | return;
|
34 | }
|
35 |
|
36 | let node = path.node;
|
37 |
|
38 | path.replaceWith(
|
39 | t.conditionalExpression(
|
40 | t.binaryExpression(
|
41 | '===',
|
42 | t.unaryExpression('typeof', t.identifier('Symbol'), true),
|
43 | t.stringLiteral('function'),
|
44 | ),
|
45 | node,
|
46 | t.stringLiteral(`@@${node.property.name}`),
|
47 | ),
|
48 | );
|
49 |
|
50 | // We should stop to avoid infinite recursion, since Babel
|
51 | // traverses replaced path, and again would hit our transform.
|
52 | path.stop();
|
53 | },
|
54 | },
|
55 | };
|
56 | };
|
57 |
|
58 | function isAppropriateMember(path) {
|
59 | let node = path.node;
|
60 |
|
61 | return (
|
62 | path.parentPath.type !== 'AssignmentExpression' &&
|
63 | node.object.type === 'Identifier' &&
|
64 | node.object.name === 'Symbol' &&
|
65 | node.property.type === 'Identifier'
|
66 | );
|
67 | }
|