Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ describe('inline constants', () => {
});
});

test('does not discard impure Platform.select initializers', () => {
const code = `
var value = Platform.select({
ios: selected(),
android: discarded(),
});
`;

compare([inlinePlugin], code, code, {
inlinePlatform: true,
platform: 'ios',
});
});

test('inlines Platform.select in the code when using an ObjectMethod', () => {
const code = `
function a() {
Expand Down
13 changes: 11 additions & 2 deletions packages/metro-transform-plugins/src/inline-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,17 @@ export default function inlinePlugin(
findProperty(arg, 'native', () =>
findProperty(arg, 'default', () => t.identifier('undefined')),
);

path.replaceWith(findProperty(arg, opts.platform, fallback));
const selected = findProperty(arg, opts.platform, fallback);

if (
arg.properties.every(
property =>
(isObjectProperty(property) && property.value === selected) ||
scope.isPure(property),
)
) {
path.replaceWith(selected);
}
}
}
},
Expand Down