Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform keyMirror to object literal #7505

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"transform-es2015-modules-commonjs",
"transform-es3-member-expression-literals",
"transform-es3-property-literals",
"./scripts/babel/key-mirror-to-literal",
"./scripts/babel/transform-object-assign-require",
"transform-react-jsx-source"
]
Expand Down
64 changes: 64 additions & 0 deletions scripts/babel/key-mirror-to-literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

module.exports = function keyMirrorToLiteral(babel) {
const t = babel.types;

function isKeyMirrorRequireCall(node) {
return (
node &&
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'require' &&
node.arguments.length === 1 &&
node.arguments[0].type === 'StringLiteral' &&
(
node.arguments[0].value === 'fbjs/lib/keyMirror' ||
node.arguments[0].value === 'keyMirror'
)
);
}

return {
visitor: {
Program(path, file) {
const binding = path.scope.getBinding('keyMirror');
if (binding == null) {
return;
}
const initNode = binding.path.get('init').node;
if (!isKeyMirrorRequireCall(initNode)) {
return;
}
for (const reference of binding.referencePaths) {
if (!reference.parentPath.isCallExpression()) {
throw reference.buildCodeFrameError('Expected keyMirror function call.');
}
const argPath = reference.parentPath.get('arguments.0');
if (!argPath.isObjectExpression()) {
throw argPath.buildCodeFrameError('Expected object literal.');
}
for (const prop of argPath.get('properties')) {
const value = prop.get('value');
if (value.isNullLiteral()) {
const name = prop.get('key.name').node;
value.replaceWith(t.stringLiteral(name));
Copy link
Collaborator

@gaearon gaearon Aug 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I’m wrong but I think this goes against the purpose of keyMirror.
It exists for Google Closure Compiler property mangling to work.
React is used by Om (and other libraries) from ClojureScript, which in turn relies on GCC.

AFAIK it will break Google Closure Compiler optimizations because

keyMirror({ hello: true })

will get transpiled to

{ hello: 'hello' }

with this plugin (correct me if I’m wrong).

GCC will then mangle this to

{ a: 'hello' }

which is broken.

The intention of keyMirror is that

keyMirror({ hello: true })

gets mangled by GCC to

keyMirror({ a: true })

which will still work (because it produces { a: 'a' }).

} else {
throw value.buildCodeFrameError('Expected null value for key.');
}
}
reference.parentPath.replaceWith(argPath);
}
binding.path.remove();
},
},
};
};