/
githubmr
/
facebook-react
Обзор
Документация
Войти
/
githubmr
/
facebook-react
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
compiler/packages/babel-plugin-react-compiler/src/Optimization/OptimizePropsMethodCalls.ts
52 строки
1 KB
Joseph Savona
Optimize method calls w props receiver (#31775)
14 дек 2024, 04:10
Не верифицирован
14 дек 2024, 04:10
a1b3bd0
Код
Авторство
О чём код?
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import {HIRFunction, isPropsType} from '../HIR'; /** * Converts method calls into regular calls where the receiver is the props object: * * Example: * * ``` * // INPUT * props.foo(); * * // OUTPUT * const t0 = props.foo; * t0(); * ``` * * Counter example: * * Here the receiver is `props.foo`, not the props object, so we don't rewrite it: * * // INPUT * props.foo.bar(); * * // OUTPUT * props.foo.bar(); * ``` */ export function optimizePropsMethodCalls(fn: HIRFunction): void { for (const [, block] of fn.body.blocks) { for (let i = 0; i < block.instructions.length; i++) { const instr = block.instructions[i]!; if ( instr.value.kind === 'MethodCall' && isPropsType(instr.value.receiver.identifier) ) { instr.value = { kind: 'CallExpression', callee: instr.value.property, args: instr.value.args, loc: instr.value.loc, }; } } } }