main
ts 52 lines 1.13 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 import {HIRFunction, isPropsType} from '../HIR';
9
10 /**
11 * Converts method calls into regular calls where the receiver is the props object:
12 *
13 * Example:
14 *
15 * ```
16 * // INPUT
17 * props.foo();
18 *
19 * // OUTPUT
20 * const t0 = props.foo;
21 * t0();
22 * ```
23 *
24 * Counter example:
25 *
26 * Here the receiver is `props.foo`, not the props object, so we don't rewrite it:
27 *
28 * // INPUT
29 * props.foo.bar();
30 *
31 * // OUTPUT
32 * props.foo.bar();
33 * ```
34 */
35 export function optimizePropsMethodCalls(fn: HIRFunction): void {
36 for (const [, block] of fn.body.blocks) {
37 for (let i = 0; i < block.instructions.length; i++) {
38 const instr = block.instructions[i]!;
39 if (
40 instr.value.kind === 'MethodCall' &&
41 isPropsType(instr.value.receiver.identifier)
42 ) {
43 instr.value = {
44 kind: 'CallExpression',
45 callee: instr.value.property,
46 args: instr.value.args,
47 loc: instr.value.loc,
48 };
49 }
50 }
51 }
52 }