Skip to content
Open
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 @@ -483,6 +483,16 @@ describe('Platform.select', () => {
expect(select('{ios: 1, ios: 2}')).toContain('const value=2');
});

test('does not discard impure initializers', () => {
expectUnchanged(`
const value = require('react-native').Platform.select({
ios: first(),
android: android(),
ios: last(),
});
`);
});

test('does not inline computed keys', () => {
expect(select('{[key]: 1, default: 2}')).toContain('Platform.select');
});
Expand Down
20 changes: 15 additions & 5 deletions packages/react-native-babel-preset/src/inline-platform-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,23 @@ module.exports = function inlinePlatformPlugin(
return;
}

path.replaceWith(
findProperty(spec, platform, () =>
findProperty(spec, 'native', () =>
findProperty(spec, 'default', () => t.identifier('undefined')),
),
const replacement = findProperty(spec, platform, () =>
findProperty(spec, 'native', () =>
findProperty(spec, 'default', () => t.identifier('undefined')),
),
);
// Inlining must not drop side effects from discarded property values.
if (
spec.properties.some(
property =>
t.isObjectProperty(property) &&
property.value !== replacement &&
!path.scope.isPure(property.value),
)
) {
return;
}
path.replaceWith(replacement);
},
},
};
Expand Down
Loading