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

test("doesn't replace Platform.OS in other write targets", () => {
const code = `
Platform.OS++;
delete Platform.OS;
[Platform.OS] = values;
({os: Platform.OS} = value);
for (Platform.OS in object) {}
for ([Platform.OS] in nestedObject) {}
for (Platform.OS of values) {}
for ([Platform.OS] of nestedValues) {}
`;

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

test('replaces Platform.OS when it is read inside a write target', () => {
const code = `
target[Platform.OS] = value;
[target[Platform.OS]] = values;
({[Platform.OS]: target} = value);
`;

compare([inlinePlugin], code, code.replaceAll('Platform.OS', '"ios"'), {
inlinePlatform: true,
platform: 'ios',
});
});

test('replaces Platform.OS in the code if Platform is the right hand side of an assignment expression', () => {
const code = `
function a() {
Expand Down
47 changes: 41 additions & 6 deletions packages/metro-transform-plugins/src/inline-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default function inlinePlugin(
options: Options,
): PluginObj<State> {
const {
isAssignmentExpression,
isIdentifier,
isMemberExpression,
isObjectExpression,
Expand All @@ -69,10 +68,46 @@ export default function inlinePlugin(
return !binding || isFlowDeclared(binding);
}

const isLeftHandSideOfAssignmentExpression = (
node: Node,
parent: Node,
): boolean => isAssignmentExpression(parent) && parent.left === node;
function isWriteTarget(path: NodePath<MemberExpression>): boolean {
let child: Node = path.node;
let parentPath = path.parentPath;

while (parentPath != null) {
const parent = parentPath.node;
if (
(parent.type === 'AssignmentExpression' ||
parent.type === 'ForInStatement' ||
parent.type === 'ForOfStatement') &&
parent.left === child
) {
return true;
}
if (parent.type === 'UpdateExpression' && parent.argument === child) {
return true;
}
if (
parent.type === 'UnaryExpression' &&
parent.operator === 'delete' &&
parent.argument === child
) {
return true;
}

const nestedWriteTarget =
parent.type === 'ArrayPattern' ||
parent.type === 'ObjectPattern' ||
(parent.type === 'ObjectProperty' && parent.value === child) ||
(parent.type === 'RestElement' && parent.argument === child) ||
(parent.type === 'AssignmentPattern' && parent.left === child);
if (!nestedWriteTarget) {
return false;
}

child = parent;
parentPath = parentPath.parentPath;
}
return false;
}

const isProcessEnvNodeEnv = (node: MemberExpression, scope: Scope): boolean =>
isIdentifier(node.property, nodeEnv) &&
Expand Down Expand Up @@ -138,7 +173,7 @@ export default function inlinePlugin(
const scope = path.scope;
const opts = state.opts;

if (!isLeftHandSideOfAssignmentExpression(node, path.parent)) {
if (!isWriteTarget(path)) {
if (
opts.inlinePlatform &&
isPlatformNode(node, scope, !!opts.isWrapped)
Expand Down