EnumDeclaration()

in packages/babel-plugin-transform-flow-enums/index.js [31:82]


      EnumDeclaration(path, state) {
        const opts = state.opts;
        const enumModule =
          opts.getRuntime != null
            ? opts.getRuntime(t)
            : t.callExpression(t.identifier('require'), [
                t.stringLiteral('flow-enums-runtime'),
              ]);

        const body = path.node.body;
        const members = body.members;
        const mirrored =
          body.type === 'EnumStringBody' &&
          (!members.length || members[0].type === 'EnumDefaultedMember');
        const enumExpression = mirrored
          ? t.callExpression(
              t.memberExpression(enumModule, t.identifier('Mirrored')),
              [
                t.arrayExpression(
                  members.map(member => t.stringLiteral(member.id.name)),
                ),
              ],
            )
          : t.callExpression(enumModule, [
              t.objectExpression(
                members.map(member => {
                  return t.objectProperty(
                    member.id,
                    memberInit(t, body.type, member),
                  );
                }),
              ),
            ]);

        const enumDeclaration = t.variableDeclaration('const', [
          t.variableDeclarator(path.node.id, enumExpression),
        ]);

        // Default exports do not support variable declaration statements as
        // children, instead we need to replace the statement and append a
        // default export of the identifier. e.g.
        //   export default enum A {} -> const A = ...; export default A;
        if (t.isExportDefaultDeclaration(path.parentPath)) {
          path.parentPath.replaceWithMultiple([
            enumDeclaration,
            t.exportDefaultDeclaration(t.identifier(path.node.id.name)),
          ]);
          return;
        }

        path.replaceWith(enumDeclaration);
      },