From c7218ca5523d991344115cb52f80d031c9dfb7ed Mon Sep 17 00:00:00 2001 From: Dan Kadera Date: Fri, 8 Sep 2023 19:59:26 +0200 Subject: [PATCH] small runtime optimization and output emit diagnostics on compile --- core/cli/package.json | 2 +- core/cli/src/checker.ts | 31 +++++++++++++++++++++++++++++-- core/cli/src/compiler.ts | 12 +++++++----- core/cli/src/dicc.ts | 1 + package-lock.json | 2 +- 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/core/cli/package.json b/core/cli/package.json index b96bdeb..2568fea 100644 --- a/core/cli/package.json +++ b/core/cli/package.json @@ -9,7 +9,7 @@ "ioc", "inversion of control" ], - "version": "0.0.38", + "version": "0.0.39", "license": "MIT", "author": { "name": "Dan Kadera", diff --git a/core/cli/src/checker.ts b/core/cli/src/checker.ts index a564aa0..d496fe9 100644 --- a/core/cli/src/checker.ts +++ b/core/cli/src/checker.ts @@ -1,5 +1,5 @@ -import { Logger } from '@debugr/core'; -import { Node } from 'ts-morph'; +import { Logger, LogLevel } from '@debugr/core'; +import { DiagnosticCategory, DiagnosticMessageChain, Node, SourceFile } from 'ts-morph'; import { ServiceRegistry } from './serviceRegistry'; import { TypeHelper } from './typeHelper'; import { TypeFlag } from './types'; @@ -71,4 +71,31 @@ export class Checker { } } } + + checkOutput(output: SourceFile): void { + for (const diagnostic of output.getPreEmitDiagnostics()) { + this.logger.log( + this.getDiagnosticCategoryLogLevel(diagnostic.getCategory()), + this.formatDiagnostic(diagnostic.getLineNumber(), diagnostic.getMessageText()), + ); + } + } + + private getDiagnosticCategoryLogLevel(category: DiagnosticCategory): LogLevel { + switch (category) { + case DiagnosticCategory.Warning: return LogLevel.WARNING; + case DiagnosticCategory.Error: return LogLevel.ERROR; + default: return LogLevel.INFO; + } + } + + private formatDiagnostic(line?: number, ...messages: (DiagnosticMessageChain | string)[]): string { + return messages.map((message) => { + if (typeof message === 'string') { + return line !== undefined ? `L${line}: ${message}` : message; + } + + return this.formatDiagnostic(line, message.getMessageText(), ...message.getNext() ?? []); + }).join('\n'); + } } diff --git a/core/cli/src/compiler.ts b/core/cli/src/compiler.ts index c051faa..27005c1 100644 --- a/core/cli/src/compiler.ts +++ b/core/cli/src/compiler.ts @@ -46,6 +46,11 @@ export class Compiler { } private writeHeader(sources: Map): void { + const imports = [...sources].map(([source, name]) => [ + name, + this.output.getRelativePathAsModuleSpecifierTo(source), + ]); + this.output.addImportDeclaration({ moduleSpecifier: 'dicc', namedImports: [ @@ -53,11 +58,8 @@ export class Compiler { ], }); - for (const [source, name] of sources) { - this.output.addImportDeclaration({ - moduleSpecifier: this.output.getRelativePathAsModuleSpecifierTo(source), - namespaceImport: name, - }); + for (const [namespaceImport, moduleSpecifier] of imports) { + this.output.addImportDeclaration({ moduleSpecifier, namespaceImport }); } } diff --git a/core/cli/src/dicc.ts b/core/cli/src/dicc.ts index c0820e3..70df228 100644 --- a/core/cli/src/dicc.ts +++ b/core/cli/src/dicc.ts @@ -50,5 +50,6 @@ export class Dicc { this.helper.destroy(); await output.save(); + this.checker.checkOutput(output); } } diff --git a/package-lock.json b/package-lock.json index 33c5938..fc1e436 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ }, "core/cli": { "name": "dicc-cli", - "version": "0.0.38", + "version": "0.0.39", "license": "MIT", "dependencies": { "@debugr/console": "^3.0.0-rc.10",