Make the inline platform transform support string keys

Summary:
This diff makes the platform inline transformer support string keys, for example:

```
Platform.select({
  'ios': 'ios',
  'default': 'default',
});
```

This fixes https://github.com/facebook/metro/issues/134

Reviewed By: mjesun

Differential Revision: D7079013

fbshipit-source-id: 3e47bb4a28f2ac776475842982e089d5954d6521
This commit is contained in:
Rafael Oleza 2018-02-25 10:31:06 -08:00 committed by Facebook Github Bot
parent 886c9b986c
commit a62eaa0d3b
2 changed files with 21 additions and 1 deletions

View File

@ -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');

View File

@ -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();
}