diff --git a/Libraries/Utilities/Platform.android.js b/Libraries/Utilities/Platform.android.js index 9536b6315..f5e18626e 100644 --- a/Libraries/Utilities/Platform.android.js +++ b/Libraries/Utilities/Platform.android.js @@ -22,7 +22,7 @@ const Platform = { const constants = require('NativeModules').AndroidConstants; return constants && constants.isTesting; }, - select: (obj: Object) => obj.android, + select: (obj: Object) => 'android' in obj ? obj.android : obj.default, }; module.exports = Platform; diff --git a/Libraries/Utilities/Platform.ios.js b/Libraries/Utilities/Platform.ios.js index 9896c88c7..d95e9e1ae 100644 --- a/Libraries/Utilities/Platform.ios.js +++ b/Libraries/Utilities/Platform.ios.js @@ -26,7 +26,7 @@ const Platform = { const constants = require('NativeModules').IOSConstants; return constants && constants.isTesting; }, - select: (obj: Object) => obj.ios, + select: (obj: Object) => 'ios' in obj ? obj.ios : obj.default, }; module.exports = Platform; diff --git a/packager/src/JSTransformer/worker/__tests__/inline-test.js b/packager/src/JSTransformer/worker/__tests__/inline-test.js index c97b7a974..821aaf4cf 100644 --- a/packager/src/JSTransformer/worker/__tests__/inline-test.js +++ b/packager/src/JSTransformer/worker/__tests__/inline-test.js @@ -150,6 +150,15 @@ describe('inline constants', () => { expect(toString(ast)).toEqual(normalize(code.replace(/Platform\.select[^;]+/, '1'))); }); + it('inlines Platform.select in the code if Platform is a global and the argument doesn\'t have target platform in it\'s keys', () => { + const code = `function a() { + var a = Platform.select({ios: 1, default: 2}); + var b = a.Platform.select({ios: 1, default: 2}); + }`; + const {ast} = inline('arbitrary.js', {code}, {platform: 'android'}); + expect(toString(ast)).toEqual(normalize(code.replace(/Platform\.select[^;]+/, '2'))); + }); + it('replaces Platform.select in the code if Platform is a top level import', () => { const code = ` var Platform = require('Platform'); diff --git a/packager/src/JSTransformer/worker/inline.js b/packager/src/JSTransformer/worker/inline.js index c9bdee038..6b2bf5fb0 100644 --- a/packager/src/JSTransformer/worker/inline.js +++ b/packager/src/JSTransformer/worker/inline.js @@ -96,9 +96,9 @@ const isDev = (node, parent, scope) => isGlobal(scope.getBinding(dev.name)) && !(t.isMemberExpression(parent)); -function findProperty(objectExpression, key) { +function findProperty(objectExpression, key, fallback) { const property = objectExpression.properties.find(p => p.key.name === key); - return property ? property.value : t.identifier('undefined'); + return property ? property.value : fallback(); } const inlinePlugin = { @@ -133,8 +133,10 @@ const inlinePlugin = { isPlatformSelect(node, scope, opts.isWrapped) || isReactPlatformSelect(node, scope, opts.isWrapped) ) { + const fallback = () => + findProperty(arg, 'default', () => t.identifier('undefined')); const replacement = t.isObjectExpression(arg) - ? findProperty(arg, opts.platform) + ? findProperty(arg, opts.platform, fallback) : node; path.replaceWith(replacement);