From b35779c6a442fd98473706a8d552ecb5a9db573a Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Wed, 20 Mar 2024 13:48:18 -0400 Subject: [PATCH] [logger] Add CompilerDiagnostic category, thread Logger into environment --- .../components/Editor/EditorImpl.tsx | 1 + .../scripts/jest/makeTransform.ts | 2 +- .../src/Entrypoint/Options.ts | 5 ++++ .../src/Entrypoint/Pipeline.ts | 25 +++++++++++-------- .../src/Entrypoint/Program.ts | 4 +-- .../src/HIR/Environment.ts | 9 ++++++- .../ReactiveScopes/CodegenReactiveFunction.ts | 5 ++-- 7 files changed, 34 insertions(+), 17 deletions(-) diff --git a/compiler/apps/playground/components/Editor/EditorImpl.tsx b/compiler/apps/playground/components/Editor/EditorImpl.tsx index 3907330eff..91c4d8bb14 100644 --- a/compiler/apps/playground/components/Editor/EditorImpl.tsx +++ b/compiler/apps/playground/components/Editor/EditorImpl.tsx @@ -203,6 +203,7 @@ function compile(source: string): CompilerOutput { }, getReactFunctionType(id), null, + null, )) { const fnName = fn.node.id?.name ?? null; switch (result.kind) { diff --git a/compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts b/compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts index b13fe67121..b32acb8174 100644 --- a/compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts +++ b/compiler/packages/babel-plugin-react-forget/scripts/jest/makeTransform.ts @@ -168,7 +168,7 @@ function ReactForgetFunctionTransform() { } } - const compiled = compile(fn, forgetOptions, "Other", null); + const compiled = compile(fn, forgetOptions, "Other", null, null); compiledFns.add(compiled); const fun = t.functionDeclaration( diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts index 74d71bc82d..86fcea9d39 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts @@ -149,6 +149,11 @@ export type LoggerEvent = fnLoc: t.SourceLocation | null; detail: CompilerErrorDetailOptions; } + | { + kind: "CompileDiagnostic"; + fnLoc: t.SourceLocation | null; + detail: Omit, "suggestions">; + } | { kind: "CompileSuccess"; fnLoc: t.SourceLocation | null; diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts index 7415ef4257..28c12b99b6 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts @@ -8,6 +8,7 @@ import { NodePath } from "@babel/traverse"; import * as t from "@babel/types"; import prettyFormat from "pretty-format"; +import { Logger } from "."; import { HIRFunction, ReactiveFunction, @@ -97,16 +98,24 @@ export function* run( >, config: EnvironmentConfig, fnType: ReactFunctionType, + + logger: Logger | null, filename: string | null ): Generator { const contextIdentifiers = findContextIdentifiers(func); - const env = new Environment(fnType, config, contextIdentifiers); + const env = new Environment( + fnType, + config, + contextIdentifiers, + logger, + filename + ); yield { kind: "debug", name: "EnvironmentConfig", value: prettyFormat(env.config), }; - const ast = yield* runWithEnvironment(func, env, filename); + const ast = yield* runWithEnvironment(func, env); return ast; } @@ -118,8 +127,7 @@ function* runWithEnvironment( func: NodePath< t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression >, - env: Environment, - filename: string | null + env: Environment ): Generator { const hir = lower(func, env).unwrap(); yield log({ kind: "hir", name: "HIR", value: hir }); @@ -381,11 +389,7 @@ function* runWithEnvironment( validatePreservedManualMemoization(reactiveFunction); } - const ast = codegenFunction( - reactiveFunction, - uniqueIdentifiers, - filename - ).unwrap(); + const ast = codegenFunction(reactiveFunction, uniqueIdentifiers).unwrap(); yield log({ kind: "ast", name: "Codegen", value: ast }); /** @@ -406,9 +410,10 @@ export function compileFn( >, config: EnvironmentConfig, fnType: ReactFunctionType, + logger: Logger | null, filename: string | null ): CodegenFunction { - let generator = run(func, config, fnType, filename); + let generator = run(func, config, fnType, logger, filename); while (true) { const next = generator.next(); if (next.done) { diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts index 2d5a362d87..441ae7e77a 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts @@ -269,8 +269,8 @@ export function compileProgram( } const config = environment.unwrap(); - compiledFn = compileFn(fn, config, fnType, pass.filename); - pass.opts.logger?.logEvent(pass.filename, { + compiledFn = compileFn(fn, config, fnType, options.logger, pass.filename); + options.logger?.logEvent(pass.filename, { kind: "CompileSuccess", fnLoc: fn.node.loc ?? null, fnName: compiledFn.id?.name ?? null, diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts index f3355cab6a..fff345cdd8 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts @@ -9,6 +9,7 @@ import * as t from "@babel/types"; import { ZodError, z } from "zod"; import { fromZodError } from "zod-validation-error"; import { CompilerError } from "../CompilerError"; +import { Logger } from "../Entrypoint"; import { Err, Ok, Result } from "../Utils/Result"; import { log } from "../Utils/logger"; import { @@ -423,6 +424,8 @@ export class Environment { #nextIdentifer: number = 0; #nextBlock: number = 0; #nextScope: number = 0; + logger: Logger | null; + filename: string | null; config: EnvironmentConfig; fnType: ReactFunctionType; @@ -432,10 +435,14 @@ export class Environment { constructor( fnType: ReactFunctionType, config: EnvironmentConfig, - contextIdentifiers: Set + contextIdentifiers: Set, + logger: Logger | null, + filename: string | null ) { this.fnType = fnType; this.config = config; + this.filename = filename; + this.logger = logger; this.#shapes = new Map(DEFAULT_SHAPES); this.#globals = new Map(DEFAULT_GLOBALS); diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 61651c2f0d..c50205b5f7 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -70,8 +70,7 @@ export type CodegenFunction = { export function codegenFunction( fn: ReactiveFunction, - uniqueIdentifiers: Set, - filename: string | null + uniqueIdentifiers: Set ): Result { const cx = new Context( fn.env, @@ -144,7 +143,7 @@ export function codegenFunction( t.expressionStatement( t.callExpression( t.identifier(emitInstrumentForget.fn.importSpecifierName), - [t.stringLiteral(fn.id), t.stringLiteral(filename ?? "")] + [t.stringLiteral(fn.id), t.stringLiteral(fn.env.filename ?? "")] ) ) );