diff --git a/packages/metro/src/JSTransformer/worker/__tests__/inline-test.js b/packages/metro/src/JSTransformer/worker/__tests__/inline-test.js index bf84c525..a6c1a85e 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/inline-test.js +++ b/packages/metro/src/JSTransformer/worker/__tests__/inline-test.js @@ -186,6 +186,16 @@ describe('inline constants', () => { ); }); + it('inlines Platform.select in the code when using string keys', () => { + const code = `function a() { + var a = Platform.select({'ios': 1, 'android': 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/packages/metro/src/JSTransformer/worker/inline.js b/packages/metro/src/JSTransformer/worker/inline.js index 2c188c39..d46cdf45 100644 --- a/packages/metro/src/JSTransformer/worker/inline.js +++ b/packages/metro/src/JSTransformer/worker/inline.js @@ -49,7 +49,17 @@ const isDev = (node, parent, scope) => !t.isMemberExpression(parent); function findProperty(objectExpression, key, fallback) { - const property = objectExpression.properties.find(p => p.key.name === key); + const property = objectExpression.properties.find(p => { + if (t.isIdentifier(p.key) && p.key.name === key) { + return true; + } + + if (t.isStringLiteral(p.key) && p.key.value === key) { + return true; + } + + return false; + }); return property ? property.value : fallback(); }