diff --git a/packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js b/packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js index 92bb4aac0739..2e182e5f763b 100644 --- a/packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js +++ b/packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js @@ -479,6 +479,10 @@ describe('Platform.select', () => { expect(select('{ios() { return 1; }}')).toContain('function'); }); + test('uses the last definition of a duplicate key', () => { + expect(select('{ios: 1, ios: 2}')).toContain('const value=2'); + }); + test('does not inline computed keys', () => { expect(select('{[key]: 1, default: 2}')).toContain('Platform.select'); }); diff --git a/packages/react-native-babel-preset/src/inline-platform-plugin.js b/packages/react-native-babel-preset/src/inline-platform-plugin.js index c818ff3331d5..f70a24b3e95e 100644 --- a/packages/react-native-babel-preset/src/inline-platform-plugin.js +++ b/packages/react-native-babel-preset/src/inline-platform-plugin.js @@ -430,7 +430,9 @@ module.exports = function inlinePlatformPlugin( key /*: string */, fallback /*: () => Node */, ) /*: Node */ { - for (const property of objectExpression.properties) { + // Object literal evaluation keeps the last definition of a duplicate key. + for (let i = objectExpression.properties.length - 1; i >= 0; i--) { + const property = objectExpression.properties[i]; if (!t.isObjectProperty(property) && !t.isObjectMethod(property)) { continue; }