[logger] Add CompilerDiagnostic category, thread Logger into environment

This commit is contained in:
Mofei Zhang
2024-03-20 13:48:18 -04:00
parent 346f6c13a8
commit b35779c6a4
7 changed files with 34 additions and 17 deletions
@@ -203,6 +203,7 @@ function compile(source: string): CompilerOutput {
},
getReactFunctionType(id),
null,
null,
)) {
const fnName = fn.node.id?.name ?? null;
switch (result.kind) {
@@ -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(
@@ -149,6 +149,11 @@ export type LoggerEvent =
fnLoc: t.SourceLocation | null;
detail: CompilerErrorDetailOptions;
}
| {
kind: "CompileDiagnostic";
fnLoc: t.SourceLocation | null;
detail: Omit<Omit<CompilerErrorDetailOptions, "severity">, "suggestions">;
}
| {
kind: "CompileSuccess";
fnLoc: t.SourceLocation | null;
@@ -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<CompilerPipelineValue, CodegenFunction> {
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<CompilerPipelineValue, CodegenFunction> {
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) {
@@ -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,
@@ -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<t.Identifier>
contextIdentifiers: Set<t.Identifier>,
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);
@@ -70,8 +70,7 @@ export type CodegenFunction = {
export function codegenFunction(
fn: ReactiveFunction,
uniqueIdentifiers: Set<string>,
filename: string | null
uniqueIdentifiers: Set<string>
): Result<CodegenFunction, CompilerError> {
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 ?? "")]
)
)
);