Allow flow-declared variables when inlining

Summary: The logic of the `inline` babel transform regarded identifiers as global if no binding exists for them. We extend that logic to also accept flow-declared variables.

Reviewed By: arcanis

Differential Revision: D4737620

fbshipit-source-id: e71cfdf77c7b7751265cfa4412430b4f29e9e853
This commit is contained in:
David Aurelio 2017-03-20 07:57:58 -07:00 committed by Facebook Github Bot
parent 80faeb9034
commit f19e5215cf
3 changed files with 36 additions and 2 deletions

View File

@ -1,5 +1,5 @@
{
"version": "0.5.0",
"version": "0.5.1",
"name": "react-native-packager",
"description": "Build native apps with React!",
"repository": {

View File

@ -306,4 +306,32 @@ describe('inline constants', () => {
expect(toString(ast)).toEqual(
normalize(code.replace(/require\([^)]+\)\.Platform\.OS/, '"android"')));
});
it('works with flow-declared variables', () => {
const stripFlow = require('babel-plugin-transform-flow-strip-types');
const code = `declare var __DEV__;
const a: boolean = __DEV__;`;
const transformed = transform(
code,
{...babelOptions, plugins: [stripFlow, [inline.plugin, {dev: false}]]},
).code;
expect(transformed).toEqual('const a=false;');
});
it('works with flow-declared variables in wrapped modules', () => {
const stripFlow = require('babel-plugin-transform-flow-strip-types');
const code = `__d(() => {
declare var __DEV__;
const a: boolean = __DEV__;
});`;
const transformed = transform(
code,
{...babelOptions, plugins: [stripFlow, [inline.plugin, {dev: true}]]},
).code;
expect(transformed).toEqual('__d(()=>{const a=true;});');
});
});

View File

@ -34,6 +34,12 @@ const importMap = new Map([['ReactNative', 'react-native']]);
const isGlobal = binding => !binding;
const isFlowDeclared = binding =>
t.isDeclareVariable(binding.path);
const isGlobalOrFlowDeclared = binding =>
isGlobal(binding) || isFlowDeclared(binding);
const isToplevelBinding = (binding, isWrappedModule) =>
isGlobal(binding) ||
!binding.scope.parent ||
@ -93,7 +99,7 @@ const isReactPlatformSelect = (node, scope, isWrappedModule) =>
const isDev = (node, parent, scope) =>
t.isIdentifier(node, dev) &&
isGlobal(scope.getBinding(dev.name)) &&
isGlobalOrFlowDeclared(scope.getBinding(dev.name)) &&
!(t.isMemberExpression(parent));
function findProperty(objectExpression, key, fallback) {