mirror of
https://github.com/status-im/metro.git
synced 2025-02-10 18:17:12 +00:00
Add inline support code for PlatformOS
Reviewed By: mjesun Differential Revision: D6491061 fbshipit-source-id: e51b50e8a5b1c497946f8dfa3a95bd7599877018
This commit is contained in:
parent
1a37164dac
commit
d2075f3743
@ -12,6 +12,7 @@
|
||||
'use strict';
|
||||
|
||||
const inline = require('../inline');
|
||||
|
||||
const {transform, transformFromAst} = require('babel-core');
|
||||
|
||||
const babelOptions = {
|
||||
@ -484,3 +485,76 @@ describe('inline constants', () => {
|
||||
expect(transformed).toEqual('__d(()=>{const a=true;});');
|
||||
});
|
||||
});
|
||||
|
||||
describe('inline PlatformOS.OS', () => {
|
||||
it('replaces PlatformOS.OS in the code if PlatformOS is a top level import', () => {
|
||||
const code = `
|
||||
var PlatformOS = require('PlatformOS');
|
||||
function a() {
|
||||
if (PlatformOS.OS === 'android') a = function() {};
|
||||
var b = a.PlatformOS.OS;
|
||||
}`;
|
||||
const {ast} = inline('arbitrary.js', {code}, {platform: 'ios'});
|
||||
expect(toString(ast)).toEqual(
|
||||
normalize(code.replace(/PlatformOS\.OS/, '"ios"')),
|
||||
);
|
||||
});
|
||||
|
||||
it('replaces require("PlatformOS").OS in the code', () => {
|
||||
const code = `function a() {
|
||||
var a = require('PlatformOS').OS;
|
||||
var b = a.require('PlatformOS').OS;
|
||||
}`;
|
||||
const {ast} = inline('arbitrary.js', {code}, {platform: 'android'});
|
||||
expect(toString(ast)).toEqual(
|
||||
normalize(code.replace(/require\('PlatformOS'\)\.OS/, '"android"')),
|
||||
);
|
||||
});
|
||||
|
||||
it(`doesn't replace PlatformOS.OS in the code if PlatformOS is the left hand side of an assignment expression`, () => {
|
||||
const code = `function a() {
|
||||
PlatformOS.OS = "test"
|
||||
}`;
|
||||
const {ast} = inline('arbitrary.js', {code}, {platform: 'ios'});
|
||||
|
||||
expect(toString(ast)).toEqual(normalize(code));
|
||||
});
|
||||
|
||||
it('replaces PlatformOS.OS in the code if PlatformOS is the right hand side of an assignment expression', () => {
|
||||
const code = `function a() {
|
||||
var a;
|
||||
a = PlatformOS.OS;
|
||||
}`;
|
||||
const {ast} = inline('arbitrary.js', {code}, {platform: 'ios'});
|
||||
|
||||
expect(toString(ast)).toEqual(
|
||||
normalize(code.replace(/PlatformOS\.OS/, '"ios"')),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('inline PlatformOS.select', () => {
|
||||
it('replaces PlatformOS.select in the code if PlatformOS is a top level import', () => {
|
||||
const code = `
|
||||
var PlatformOS = require('PlatformOS');
|
||||
function a() {
|
||||
PlatformOS.select({ios: 1, android: 2});
|
||||
var b = a.PlatformOS.select({});
|
||||
}`;
|
||||
const {ast} = inline('arbitrary.js', {code}, {platform: 'android'});
|
||||
expect(toString(ast)).toEqual(
|
||||
normalize(code.replace(/PlatformOS\.select\([^;]+/, '2')),
|
||||
);
|
||||
});
|
||||
|
||||
it('replaces require("PlatformOS").select in the code', () => {
|
||||
const code = `function a() {
|
||||
var a = require('PlatformOS').select({ios: 1, android: 2});
|
||||
var b = a.require('PlatformOS').select({});
|
||||
}`;
|
||||
const {ast} = inline('arbitrary.js', {code}, {platform: 'android'});
|
||||
expect(toString(ast)).toEqual(
|
||||
normalize(code.replace(/PlatformOS\.select\([^;]+/, '2')),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -17,11 +17,28 @@ const babel = require('babel-core');
|
||||
const t = babel.types;
|
||||
const importMap = new Map([['ReactNative', 'react-native']]);
|
||||
|
||||
const isPlatformOS = (node: any, scope: any, isWrappedModule: boolean) =>
|
||||
const isPlatformNode = (
|
||||
node: Object,
|
||||
scope: Object,
|
||||
isWrappedModule: boolean,
|
||||
) =>
|
||||
isPlatformOS(node, scope, isWrappedModule) ||
|
||||
isReactPlatformOS(node, scope, isWrappedModule) ||
|
||||
isPlatformOSOS(node, scope, isWrappedModule);
|
||||
|
||||
const isPlatformSelectNode = (
|
||||
node: Object,
|
||||
scope: Object,
|
||||
isWrappedModule: boolean,
|
||||
) =>
|
||||
isPlatformSelect(node, scope, isWrappedModule) ||
|
||||
isReactPlatformSelect(node, scope, isWrappedModule);
|
||||
|
||||
const isPlatformOS = (node, scope, isWrappedModule) =>
|
||||
t.isIdentifier(node.property, {name: 'OS'}) &&
|
||||
isImportOrGlobal(node.object, scope, [{name: 'Platform'}], isWrappedModule);
|
||||
|
||||
const isReactPlatformOS = (node: any, scope: any, isWrappedModule: boolean) =>
|
||||
const isReactPlatformOS = (node, scope, isWrappedModule) =>
|
||||
t.isIdentifier(node.property, {name: 'OS'}) &&
|
||||
t.isMemberExpression(node.object) &&
|
||||
t.isIdentifier(node.object.property, {name: 'Platform'}) &&
|
||||
@ -32,7 +49,11 @@ const isReactPlatformOS = (node: any, scope: any, isWrappedModule: boolean) =>
|
||||
isWrappedModule,
|
||||
);
|
||||
|
||||
const isPlatformSelect = (node: any, scope: any, isWrappedModule: boolean) =>
|
||||
const isPlatformOSOS = (node, scope, isWrappedModule) =>
|
||||
t.isIdentifier(node.property, {name: 'OS'}) &&
|
||||
isImportOrGlobal(node.object, scope, [{name: 'PlatformOS'}], isWrappedModule);
|
||||
|
||||
const isPlatformSelect = (node, scope, isWrappedModule) =>
|
||||
t.isMemberExpression(node.callee) &&
|
||||
t.isIdentifier(node.callee.object, {name: 'Platform'}) &&
|
||||
t.isIdentifier(node.callee.property, {name: 'select'}) &&
|
||||
@ -43,11 +64,7 @@ const isPlatformSelect = (node: any, scope: any, isWrappedModule: boolean) =>
|
||||
isWrappedModule,
|
||||
);
|
||||
|
||||
const isReactPlatformSelect = (
|
||||
node: any,
|
||||
scope: any,
|
||||
isWrappedModule: boolean,
|
||||
) =>
|
||||
const isReactPlatformSelect = (node, scope, isWrappedModule) =>
|
||||
t.isMemberExpression(node.callee) &&
|
||||
t.isIdentifier(node.callee.property, {name: 'select'}) &&
|
||||
t.isMemberExpression(node.callee.object) &&
|
||||
@ -59,6 +76,35 @@ const isReactPlatformSelect = (
|
||||
isWrappedModule,
|
||||
);
|
||||
|
||||
const isPlatformOSSelect = (
|
||||
node: Object,
|
||||
scope: Object,
|
||||
isWrappedModule: boolean,
|
||||
) =>
|
||||
t.isMemberExpression(node.callee) &&
|
||||
t.isIdentifier(node.callee.object, {name: 'PlatformOS'}) &&
|
||||
t.isIdentifier(node.callee.property, {name: 'select'}) &&
|
||||
isImportOrGlobal(
|
||||
node.callee.object,
|
||||
scope,
|
||||
[{name: 'PlatformOS'}],
|
||||
isWrappedModule,
|
||||
);
|
||||
|
||||
const getReplacementForPlatformOSSelect = (node: Object, platform: string) => {
|
||||
const matchingProperty = node.arguments[0].properties.find(
|
||||
p => p.key.name === platform,
|
||||
);
|
||||
|
||||
if (!matchingProperty) {
|
||||
throw new Error(
|
||||
'No matching property was found for PlatformOS.select:\n' +
|
||||
JSON.stringify(node),
|
||||
);
|
||||
}
|
||||
return matchingProperty.value;
|
||||
};
|
||||
|
||||
const isGlobal = binding => !binding;
|
||||
|
||||
const isRequireCall = (node, dependencyId, scope) =>
|
||||
@ -97,8 +143,8 @@ const isToplevelBinding = (binding, isWrappedModule) =>
|
||||
(isWrappedModule && !binding.scope.parent.parent);
|
||||
|
||||
module.exports = {
|
||||
isPlatformOS,
|
||||
isReactPlatformOS,
|
||||
isPlatformSelect,
|
||||
isReactPlatformSelect,
|
||||
isPlatformNode,
|
||||
isPlatformSelectNode,
|
||||
isPlatformOSSelect,
|
||||
getReplacementForPlatformOSSelect,
|
||||
};
|
||||
|
@ -65,10 +65,7 @@ const inlinePlugin = {
|
||||
const opts = state.opts;
|
||||
|
||||
if (!isLeftHandSideOfAssignmentExpression(node, path.parent)) {
|
||||
if (
|
||||
inlinePlatform.isPlatformOS(node, scope, opts.isWrapped) ||
|
||||
inlinePlatform.isReactPlatformOS(node, scope, opts.isWrapped)
|
||||
) {
|
||||
if (inlinePlatform.isPlatformNode(node, scope, opts.isWrapped)) {
|
||||
path.replaceWith(t.stringLiteral(opts.platform));
|
||||
} else if (isProcessEnvNodeEnv(node, scope)) {
|
||||
path.replaceWith(
|
||||
@ -83,10 +80,7 @@ const inlinePlugin = {
|
||||
const arg = node.arguments[0];
|
||||
const opts = state.opts;
|
||||
|
||||
if (
|
||||
inlinePlatform.isPlatformSelect(node, scope, opts.isWrapped) ||
|
||||
inlinePlatform.isReactPlatformSelect(node, scope, opts.isWrapped)
|
||||
) {
|
||||
if (inlinePlatform.isPlatformSelectNode(node, scope, opts.isWrapped)) {
|
||||
const fallback = () =>
|
||||
findProperty(arg, 'default', () => t.identifier('undefined'));
|
||||
const replacement = t.isObjectExpression(arg)
|
||||
@ -94,6 +88,12 @@ const inlinePlugin = {
|
||||
: node;
|
||||
|
||||
path.replaceWith(replacement);
|
||||
} else if (
|
||||
inlinePlatform.isPlatformOSSelect(node, scope, opts.isWrapped)
|
||||
) {
|
||||
path.replaceWith(
|
||||
inlinePlatform.getReplacementForPlatformOSSelect(node, opts.platform),
|
||||
);
|
||||
}
|
||||
},
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user