Skip to content

Commit

Permalink
Merge pull request #1420 from samchon/feat/website-compiler
Browse files Browse the repository at this point in the history
Report `TransformError` in website playground.
  • Loading branch information
samchon authored Dec 9, 2024
2 parents 97589c7 + e1932e6 commit 30f1300
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
33 changes: 31 additions & 2 deletions website/src/compilers/TypeScriptCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export namespace TypeScriptCompiler {
//----
const output = { value: "" };
const diagnostics: ts.Diagnostic[] = [];
const returnFailure = (): ICompilerService.IErrorOutput => ({
success: false,
error: {
name: "Error",
message: "TypeScript Compilation failed.",
diagnostics: diagnostics.map((d) => ({
code: d.code,
category: d.category,
text: d.messageText,
})),
} as Error,
});

// CREATE PROGRAM
const program = ts.createProgram(["main.ts"], COMPILER_OPTIONS, {
Expand All @@ -66,6 +78,8 @@ export namespace TypeScriptCompiler {
(self as any).checker = program.getTypeChecker();
(self as any).source = source;

// diagnostics.push(...ts.getPreEmitDiagnostics(program));

// TRANSFORMATION
try {
if (target === "javascript") {
Expand All @@ -78,6 +92,7 @@ export namespace TypeScriptCompiler {
),
],
});
if (diagnostics.length) return returnFailure();
return {
success: true,
target,
Expand All @@ -91,11 +106,15 @@ export namespace TypeScriptCompiler {
transform(
program,
{},
{ addDiagnostic: (input) => diagnostics.push(input) },
{
addDiagnostic: (input) => diagnostics.push(input),
},
),
],
program.getCompilerOptions(),
);
if (diagnostics.length) return returnFailure();

const printer: ts.Printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed,
});
Expand All @@ -107,7 +126,17 @@ export namespace TypeScriptCompiler {
};
}
} catch (err: unknown) {
return { success: false, error: err as Error };
return {
success: false,
error:
err instanceof Error
? {
...err,
message: err.message,
name: err.name,
}
: (err as Error),
};
}
};
}
8 changes: 0 additions & 8 deletions website/src/components/playground/OutputViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import Editor from "@monaco-editor/react";

import { Singleton } from "tstl";

import { typia_packageJson } from "../../../raw/typia/packageJson";

const version = new Singleton(
() => typia_packageJson.split(`"version": "`)[1].split(`"`)[0],
);

const OutputViewer = (props: {
language: "typescript" | "javascript";
content: string;
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ const Playground = () => {
})
.join("\n\n")
: output.content
: output.error.message
: JSON.stringify(output.error, null, 2)
}
/>
<div
Expand Down

0 comments on commit 30f1300

Please sign in to comment.