mirror of
https://github.com/status-im/react-native.git
synced 2025-02-10 16:36:25 +00:00
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:
parent
2f92ee8a9c
commit
68c77395b1
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"version": "0.5.0",
|
"version": "0.5.1",
|
||||||
"name": "react-native-packager",
|
"name": "react-native-packager",
|
||||||
"description": "Build native apps with React!",
|
"description": "Build native apps with React!",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
@ -306,4 +306,32 @@ describe('inline constants', () => {
|
|||||||
expect(toString(ast)).toEqual(
|
expect(toString(ast)).toEqual(
|
||||||
normalize(code.replace(/require\([^)]+\)\.Platform\.OS/, '"android"')));
|
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;});');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -34,6 +34,12 @@ const importMap = new Map([['ReactNative', 'react-native']]);
|
|||||||
|
|
||||||
const isGlobal = binding => !binding;
|
const isGlobal = binding => !binding;
|
||||||
|
|
||||||
|
const isFlowDeclared = binding =>
|
||||||
|
t.isDeclareVariable(binding.path);
|
||||||
|
|
||||||
|
const isGlobalOrFlowDeclared = binding =>
|
||||||
|
isGlobal(binding) || isFlowDeclared(binding);
|
||||||
|
|
||||||
const isToplevelBinding = (binding, isWrappedModule) =>
|
const isToplevelBinding = (binding, isWrappedModule) =>
|
||||||
isGlobal(binding) ||
|
isGlobal(binding) ||
|
||||||
!binding.scope.parent ||
|
!binding.scope.parent ||
|
||||||
@ -93,7 +99,7 @@ const isReactPlatformSelect = (node, scope, isWrappedModule) =>
|
|||||||
|
|
||||||
const isDev = (node, parent, scope) =>
|
const isDev = (node, parent, scope) =>
|
||||||
t.isIdentifier(node, dev) &&
|
t.isIdentifier(node, dev) &&
|
||||||
isGlobal(scope.getBinding(dev.name)) &&
|
isGlobalOrFlowDeclared(scope.getBinding(dev.name)) &&
|
||||||
!(t.isMemberExpression(parent));
|
!(t.isMemberExpression(parent));
|
||||||
|
|
||||||
function findProperty(objectExpression, key, fallback) {
|
function findProperty(objectExpression, key, fallback) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user