Skip to content

Commit

Permalink
Adds inline support for native variant in Platform.select
Browse files Browse the repository at this point in the history
  • Loading branch information
koke committed Nov 5, 2019
1 parent 907d6af commit 2decb55
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,39 @@ describe('inline constants', () => {
});
});

it("inlines Platform.select in the code if Platform is a global and the argument doesn't have target platform in it keys but has native", () => {
const code = `
function a() {
var a = Platform.select({ios: 1, native: 2});
var b = a.Platform.select({ios: 1, native: 2});
}
`;

compare([inlinePlugin], code, code.replace(/Platform\.select[^;]+/, '2'), {
inlinePlatform: 'true',
platform: 'android',
});
});

it("doesn't inline Platform.select in the code if Platform is a global and the argument only has an unknown platform in its keys", () => {
const code = `
function a() {
var a = Platform.select({web: 2});
var b = a.Platform.select({native: 2});
}
`;

compare(
[inlinePlugin],
code,
code.replace(/Platform\.select[^;]+/, 'undefined'),
{
inlinePlatform: 'true',
platform: 'android',
},
);
});

it('inlines Platform.select in the code when using string keys', () => {
const code = `
function a() {
Expand Down
4 changes: 3 additions & 1 deletion packages/metro/src/JSTransformer/worker/inline-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ function inlinePlugin(
) {
if (hasStaticProperties(arg)) {
const fallback = () =>
findProperty(arg, 'default', () => t.identifier('undefined'));
findProperty(arg, 'native', () =>
findProperty(arg, 'default', () => t.identifier('undefined')),
);

path.replaceWith(findProperty(arg, opts.platform, fallback));
}
Expand Down

0 comments on commit 2decb55

Please sign in to comment.