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 @@ -399,6 +399,17 @@ describe('inline constants', () => {
});
});

test('uses the last definition when Platform.select has duplicate keys', () => {
const code = `
var value = Platform.select({ios: 1, ios: 2});
`;

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

test('inlines Platform.select in the code when using an ObjectMethod', () => {
const code = `
function a() {
Expand Down
14 changes: 6 additions & 8 deletions packages/metro-transform-plugins/src/inline-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export default function inlinePlugin(
key: string,
fallback: () => Node,
): Node {
let value = null;

for (const p 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 p = objectExpression.properties[i];
if (!isObjectProperty(p) && !isObjectMethod(p)) {
continue;
}
Expand All @@ -101,16 +101,14 @@ export default function inlinePlugin(
(isStringLiteral(p.key) && p.key.value === key)
) {
if (isObjectProperty(p)) {
value = p.value;
break;
return p.value;
} else if (isObjectMethod(p)) {
value = t.toExpression(p);
break;
return t.toExpression(p);
}
}
}

return value ?? fallback();
return fallback();
}

function hasStaticProperties(objectExpression: ObjectExpression): boolean {
Expand Down
Loading