dillon1000/react
Commit
Browse files [rfc] Dont convert ArrowFunction to FunctionDecl
This PR changes the way we compile ArrowFunctionExpression to allow compiling more cases, such as within `React.forwardRef()`. We no longer convert arrow functions to function declarations. Instead, CodeGenerator emits a generic `CodegenFunction` type, and `Program.ts` is responsible for converting that to the appropriate type. The rule is basically: * Retain the original node type by default. Function declaration in, function declaration out. Arrow function in, arrow function out. * When gating is enabled, we emit a ConditionalExpression instead of creating a temporary variable. If the original (and hence compiled) functions are function declarations, we force them into FunctionExpressions only here, since we need an expression for each branch of the conditional. Then the rules are: * If this is a `export function Foo` ie a named export, replace it with a variable declaration with the conditional expression as the initializer, and the function name as the variable name. * Else, just replace the original function node with the conditional. This works for all other cases. I'm open to feedback but this seems like a pretty robust approach and will allow us to support a lot of real-world cases that we didn't yet, so i think we need _something_ in this direction.
Changed paths24 files
First-parent comparisoncompiler/.github/workflows/react.yml ModifiedM compiler/apps/playground/components/Editor/index.tsx ModifiedM compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/Entrypoint/Gating.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/Entrypoint/Instrumentation.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/index.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/arrow-function-expr-gating-test.expect.md ModifiedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/codegen-instrument-forget-gating-test.expect.md ModifiedD compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error._todo.multi-arrow-expr-export-default-gating-test.expect.md DeletedD compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo.infer-function-assignment.expect.md DeletedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/gating-test-export-default-function.expect.md ModifiedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/gating-test-export-function-and-default.expect.md ModifiedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/gating-test-export-function.expect.md ModifiedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/gating-test.expect.md ModifiedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-function-assignment.expect.md AddedR compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo.infer-function-assignment.js →compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/infer-function-assignment.js RenamedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-default-gating-test.expect.md AddedR compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error._todo.multi-arrow-expr-export-default-gating-test.js →compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-default-gating-test.js RenamedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-export-gating-test.expect.md ModifiedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/multi-arrow-expr-gating-test.expect.md ModifiedM compiler/packages/sprout/src/SproutTodoFilter.ts ModifiedPatch
Files changed
Rendering syntax-highlighted changes…