From 3db0ae39da811386f66f5be6ca97cf1f91182555 Mon Sep 17 00:00:00 2001 From: Nicolas Charpentier Date: Wed, 11 Dec 2019 19:15:51 -0500 Subject: [PATCH] Add mac as a platform in Platform.select inline --- .../worker/__tests__/inline-plugin-test.js | 39 +++++++++++++++++++ .../src/JSTransformer/worker/inline-plugin.js | 32 ++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/packages/metro/src/JSTransformer/worker/__tests__/inline-plugin-test.js b/packages/metro/src/JSTransformer/worker/__tests__/inline-plugin-test.js index fbadadd8c9..438c56da26 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/inline-plugin-test.js +++ b/packages/metro/src/JSTransformer/worker/__tests__/inline-plugin-test.js @@ -296,6 +296,19 @@ describe('inline constants', () => { }); }); + it('inlines Platform.select in the code to ios and mac if both are present and running on ios platform', () => { + const code = ` + function a() { + var a = Platform.select({ios: 1, android: 2, mac: 3}); + } + `; + + compare([inlinePlugin], code, code.replace(' android: 2,', ''), { + inlinePlatform: 'true', + platform: 'ios', + }); + }); + it('does not inline Platform.select in the code when some of the properties are dynamic', () => { const code = ` function a() { @@ -337,6 +350,32 @@ describe('inline constants', () => { }); }); + it('does not inline Platform.select if the current platform is ios, and ios and mac are in the properties', () => { + const code = ` + function a() { + var a = Platform.select({ios: 1, mac: 2}); + } + `; + + compare([inlinePlugin], code, code, { + inlinePlatform: true, + platform: 'ios', + }); + }); + + it('does not inline Platform.select if the current platform is ios and mac is in the properties, but ios is not', () => { + const code = ` + function a() { + var a = Platform.select({mac: 1, default: 2}); + } + `; + + compare([inlinePlugin], code, code, { + inlinePlatform: true, + platform: 'ios', + }); + }); + it('replaces Platform.select in the code if Platform is a top level import', () => { const code = ` var Platform = require('Platform'); diff --git a/packages/metro/src/JSTransformer/worker/inline-plugin.js b/packages/metro/src/JSTransformer/worker/inline-plugin.js index cafbeb1ee0..ee07f800e9 100644 --- a/packages/metro/src/JSTransformer/worker/inline-plugin.js +++ b/packages/metro/src/JSTransformer/worker/inline-plugin.js @@ -103,6 +103,27 @@ function inlinePlugin( }); } + function hasMacProperty(objectExpression): boolean { + return findProperty(objectExpression, 'mac', () => false); + } + + function hasIOSProperty(objectExpression): boolean { + return findProperty(objectExpression, 'ios', () => false); + } + + function removePlatformsOutsideOfIOSAndMac(objectExpression): boolean { + return objectExpression.properties.filter(p => { + if ( + t.isIdentifier(p.key) && + (p.key.name === 'ios' || p.key.name === 'mac') + ) { + return true; + } + + return false; + }); + } + return { visitor: { Identifier(path: Path, state: State): void { @@ -144,7 +165,16 @@ function inlinePlugin( findProperty(arg, 'default', () => t.identifier('undefined')), ); - path.replaceWith(findProperty(arg, opts.platform, fallback)); + if (opts.platform === 'ios' && hasMacProperty(arg)) { + if (hasIOSProperty(arg)) { + node.arguments[0] = t.objectExpression( + removePlatformsOutsideOfIOSAndMac(arg), + ); + path.replaceWith(node); + } + } else { + path.replaceWith(findProperty(arg, opts.platform, fallback)); + } } } },