Skip to content
Closed
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
19 changes: 16 additions & 3 deletions src/react_compiler/lowering/build_hir/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,22 @@ pub(crate) fn lower_expression(
.as_ref()
.ok_or_else(|| cold_todo("object property without key", loc))?;
let computed = prop.flags.contains(PF::IsComputed);
let key = match lower_object_property_key(builder, key_expr, computed)? {
Some(k) => k,
None => continue,
// `{["__proto__"]: v}` and `{__proto__}` define an own property; the
// static key they would otherwise lower to is emitted as
// `__proto__: v`, which sets the prototype instead. Complement of the
// parser's prototype-setter predicate in visit_expr.rs (methods were
// diverted above and are never setters).
let own_proto_key = (computed || prop.flags.contains(PF::WasShorthand))
&& matches!(&key_expr.data, Data::EString(s) if s.eql_comptime(b"__proto__"));
let key = if own_proto_key {
ObjectPropertyKey::Computed {
name: lower_expression_to_temporary(builder, key_expr)?,
}
} else {
match lower_object_property_key(builder, key_expr, computed)? {
Some(k) => k,
None => continue,
}
};
let value_expr = prop
.value
Expand Down
51 changes: 51 additions & 0 deletions test/bundler/transpiler/react-compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,4 +1063,55 @@ describe("bundler", () => {
expect(out).toMatch(/__MEMO_CACHE_SENTINEL\)\s*\{[^}]*globalFn\(\)/);
},
});

// `{ __proto__: v }` sets the prototype, while `{ ["__proto__"]: v }` and the
// shorthand `{ __proto__ }` define an own property. Lowering used to turn the
// latter two into a static key, which codegen prints in the setter form.
itBundled("react-compiler/OwnProtoKeyStaysOwnProperty", {
files: {
"/entry.tsx": /* tsx */ `
function Comp({ id, proto }: { id: string; proto: object }) {
const __proto__ = "own";
const objects = {
computed: { ["__proto__"]: id },
template: { [\`__proto__\`]: id },
shorthand: { __proto__ },
method: { ["__proto__"]() {} },
setter: { __proto__: proto },
quotedSetter: { "__proto__": proto },
};
const shapes: Record<string, [string[], boolean]> = {};
for (const [name, o] of Object.entries(objects)) {
shapes[name] = [Object.keys(o), Object.getPrototypeOf(o) === proto];
}
return <div>{JSON.stringify(shapes)}</div>;
}
const proto = {};
console.log(Comp({ id: "v", proto }).p.children);
`,
...stubReact,
},
reactCompiler: true,
target: "browser",
backend: "api",
run: {
stdout: JSON.stringify({
computed: [["__proto__"], false],
template: [["__proto__"], false],
shorthand: [["__proto__"], false],
method: [["__proto__"], false],
setter: [[], true],
quotedSetter: [[], true],
}),
},
onAfterBundle(api) {
const out = api.readFile("/out.js");
// Comp must actually be compiled for the codegen path to be on trial.
expect(out).toContain("react.memo_cache_sentinel");
// computed, template and shorthand are emitted with a computed key; the
// two setters keep the bare key. (The method prints as `__proto__() {`.)
expect(out.match(/\[(["'`])__proto__\1\]/g) ?? []).toHaveLength(3);
expect(out.match(/(?<!\[)(["']?)__proto__\1\s*:/g) ?? []).toHaveLength(2);
},
});
});
Loading