@samitouri / QOS-React-1 / commits / ec44c4789b

Add ComponentDeclaration util

Lauren Tan committed Aug 10, 2023 at 17:24 UTC ec44c4789b6db4f8d06f23dffc808a38d46a8d85
2 files changed +20 -3
compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+2 -3
@@ -14,6 +14,7 @@ import {
14 ErrorSeverity,
15 } from "../CompilerError";
16 import { GeneratedSource } from "../HIR";
17 +import { isComponentDeclaration } from "../Utils/ComponentDeclaration";
18 import { getOrInsertDefault } from "../Utils/utils";
19 import { addInstrumentForget } from "./Instrumentation";
20 import { ExternalFunction, PluginOptions, parsePluginOptions } from "./Options";
@@ -330,9 +331,7 @@ function shouldVisitNode(
331 pass: CompilerPass
332 ): boolean {
333 if (pass.opts.enableOnlyOnReactScript) {
333 - // Typescript gets angry about lookup the magic prop
334 - let fnAny = fn as any;
335 - if (!fnAny.node.__componentDeclaration) {
334 + if (!isComponentDeclaration(fn.node)) {
335 return false;
336 }
337 }
compiler/forget/packages/babel-plugin-react-forget/src/Utils/ComponentDeclaration.ts new
+18
@@ -0,0 +1,18 @@
1 +import * as t from "@babel/types";
2 +
3 +type FunctionDeclOrExpr = t.FunctionDeclaration | t.ArrowFunctionExpression;
4 +type ComponentDeclaration = FunctionDeclOrExpr & {
5 + __componentDeclaration: boolean;
6 +};
7 +
8 +export function isComponentDeclaration(
9 + node: FunctionDeclOrExpr
10 +): node is ComponentDeclaration {
11 + return Object.prototype.hasOwnProperty.call(node, "__componentDeclaration");
12 +}
13 +
14 +export function parseComponentDeclaration(
15 + node: FunctionDeclOrExpr
16 +): ComponentDeclaration | null {
17 + return isComponentDeclaration(node) ? node : null;
18 +}