Skip to content

Commit 65805b1

Browse files
committed
Fix duplicate Platform.select key inlining
1 parent 2fa055f commit 65805b1

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ describe('Platform.select', () => {
479479
expect(select('{ios() { return 1; }}')).toContain('function');
480480
});
481481

482+
test('uses the last definition of a duplicate key', () => {
483+
expect(select('{ios: 1, ios: 2}')).toContain('const value=2');
484+
});
485+
482486
test('does not inline computed keys', () => {
483487
expect(select('{[key]: 1, default: 2}')).toContain('Platform.select');
484488
});

packages/react-native-babel-preset/src/inline-platform-plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ module.exports = function inlinePlatformPlugin(
430430
key /*: string */,
431431
fallback /*: () => Node */,
432432
) /*: Node */ {
433-
for (const property of objectExpression.properties) {
433+
// Object literal evaluation keeps the last definition of a duplicate key.
434+
for (let i = objectExpression.properties.length - 1; i >= 0; i--) {
435+
const property = objectExpression.properties[i];
434436
if (!t.isObjectProperty(property) && !t.isObjectMethod(property)) {
435437
continue;
436438
}

0 commit comments

Comments
 (0)