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

[logger] Add CompilerDiagnostic category, thread Logger into environment

Mofei Zhang committed Mar 20, 2024 at 13:48 UTC b35779c6a442fd98473706a8d552ecb5a9db573a
7 files changed +34 -17
compiler/apps/playground/components/Editor/EditorImpl.tsx
+1
@@ -203,6 +203,7 @@ function compile(source: string): CompilerOutput {
203 },
204 getReactFunctionType(id),
205 null,
206 + null,
207 )) {
208 const fnName = fn.node.id?.name ?? null;
209 switch (result.kind) {
compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts
+1 -1
@@ -168,7 +168,7 @@ function ReactForgetFunctionTransform() {
168 }
169 }
170
171 - const compiled = compile(fn, forgetOptions, "Other", null);
171 + const compiled = compile(fn, forgetOptions, "Other", null, null);
172 compiledFns.add(compiled);
173
174 const fun = t.functionDeclaration(
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts
+5
@@ -149,6 +149,11 @@ export type LoggerEvent =
149 fnLoc: t.SourceLocation | null;
150 detail: CompilerErrorDetailOptions;
151 }
152 + | {
153 + kind: "CompileDiagnostic";
154 + fnLoc: t.SourceLocation | null;
155 + detail: Omit<Omit<CompilerErrorDetailOptions, "severity">, "suggestions">;
156 + }
157 | {
158 kind: "CompileSuccess";
159 fnLoc: t.SourceLocation | null;
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts
+15 -10
@@ -8,6 +8,7 @@
8 import { NodePath } from "@babel/traverse";
9 import * as t from "@babel/types";
10 import prettyFormat from "pretty-format";
11 +import { Logger } from ".";
12 import {
13 HIRFunction,
14 ReactiveFunction,
@@ -97,16 +98,24 @@ export function* run(
98 >,
99 config: EnvironmentConfig,
100 fnType: ReactFunctionType,
101 +
102 + logger: Logger | null,
103 filename: string | null
104 ): Generator<CompilerPipelineValue, CodegenFunction> {
105 const contextIdentifiers = findContextIdentifiers(func);
103 - const env = new Environment(fnType, config, contextIdentifiers);
106 + const env = new Environment(
107 + fnType,
108 + config,
109 + contextIdentifiers,
110 + logger,
111 + filename
112 + );
113 yield {
114 kind: "debug",
115 name: "EnvironmentConfig",
116 value: prettyFormat(env.config),
117 };
109 - const ast = yield* runWithEnvironment(func, env, filename);
118 + const ast = yield* runWithEnvironment(func, env);
119 return ast;
120 }
121
@@ -118,8 +127,7 @@ function* runWithEnvironment(
127 func: NodePath<
128 t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression
129 >,
121 - env: Environment,
122 - filename: string | null
130 + env: Environment
131 ): Generator<CompilerPipelineValue, CodegenFunction> {
132 const hir = lower(func, env).unwrap();
133 yield log({ kind: "hir", name: "HIR", value: hir });
@@ -381,11 +389,7 @@ function* runWithEnvironment(
389 validatePreservedManualMemoization(reactiveFunction);
390 }
391
384 - const ast = codegenFunction(
385 - reactiveFunction,
386 - uniqueIdentifiers,
387 - filename
388 - ).unwrap();
392 + const ast = codegenFunction(reactiveFunction, uniqueIdentifiers).unwrap();
393 yield log({ kind: "ast", name: "Codegen", value: ast });
394
395 /**
@@ -406,9 +410,10 @@ export function compileFn(
410 >,
411 config: EnvironmentConfig,
412 fnType: ReactFunctionType,
413 + logger: Logger | null,
414 filename: string | null
415 ): CodegenFunction {
411 - let generator = run(func, config, fnType, filename);
416 + let generator = run(func, config, fnType, logger, filename);
417 while (true) {
418 const next = generator.next();
419 if (next.done) {
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+2 -2
@@ -269,8 +269,8 @@ export function compileProgram(
269 }
270 const config = environment.unwrap();
271
272 - compiledFn = compileFn(fn, config, fnType, pass.filename);
273 - pass.opts.logger?.logEvent(pass.filename, {
272 + compiledFn = compileFn(fn, config, fnType, options.logger, pass.filename);
273 + options.logger?.logEvent(pass.filename, {
274 kind: "CompileSuccess",
275 fnLoc: fn.node.loc ?? null,
276 fnName: compiledFn.id?.name ?? null,
compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts
+8 -1
@@ -9,6 +9,7 @@ import * as t from "@babel/types";
9 import { ZodError, z } from "zod";
10 import { fromZodError } from "zod-validation-error";
11 import { CompilerError } from "../CompilerError";
12 +import { Logger } from "../Entrypoint";
13 import { Err, Ok, Result } from "../Utils/Result";
14 import { log } from "../Utils/logger";
15 import {
@@ -423,6 +424,8 @@ export class Environment {
424 #nextIdentifer: number = 0;
425 #nextBlock: number = 0;
426 #nextScope: number = 0;
427 + logger: Logger | null;
428 + filename: string | null;
429 config: EnvironmentConfig;
430 fnType: ReactFunctionType;
431
@@ -432,10 +435,14 @@ export class Environment {
435 constructor(
436 fnType: ReactFunctionType,
437 config: EnvironmentConfig,
435 - contextIdentifiers: Set<t.Identifier>
438 + contextIdentifiers: Set<t.Identifier>,
439 + logger: Logger | null,
440 + filename: string | null
441 ) {
442 this.fnType = fnType;
443 this.config = config;
444 + this.filename = filename;
445 + this.logger = logger;
446 this.#shapes = new Map(DEFAULT_SHAPES);
447 this.#globals = new Map(DEFAULT_GLOBALS);
448
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+2 -3
@@ -70,8 +70,7 @@ export type CodegenFunction = {
70
71 export function codegenFunction(
72 fn: ReactiveFunction,
73 - uniqueIdentifiers: Set<string>,
74 - filename: string | null
73 + uniqueIdentifiers: Set<string>
74 ): Result<CodegenFunction, CompilerError> {
75 const cx = new Context(
76 fn.env,
@@ -144,7 +143,7 @@ export function codegenFunction(
143 t.expressionStatement(
144 t.callExpression(
145 t.identifier(emitInstrumentForget.fn.importSpecifierName),
147 - [t.stringLiteral(fn.id), t.stringLiteral(filename ?? "")]
146 + [t.stringLiteral(fn.id), t.stringLiteral(fn.env.filename ?? "")]
147 )
148 )
149 );