mirror of https://github.com/status-im/metro.git
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:
parent
886c9b986c
commit
a62eaa0d3b
|
@ -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', () => {
|
it('replaces Platform.select in the code if Platform is a top level import', () => {
|
||||||
const code = `
|
const code = `
|
||||||
var Platform = require('Platform');
|
var Platform = require('Platform');
|
||||||
|
|
|
@ -49,7 +49,17 @@ const isDev = (node, parent, scope) =>
|
||||||
!t.isMemberExpression(parent);
|
!t.isMemberExpression(parent);
|
||||||
|
|
||||||
function findProperty(objectExpression, key, fallback) {
|
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();
|
return property ? property.value : fallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue