Skip to content

Commit

Permalink
small runtime optimization and output emit diagnostics on compile
Browse files Browse the repository at this point in the history
  • Loading branch information
jahudka committed Sep 8, 2023
1 parent d3d760a commit c7218ca
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"ioc",
"inversion of control"
],
"version": "0.0.38",
"version": "0.0.39",
"license": "MIT",
"author": {
"name": "Dan Kadera",
Expand Down
31 changes: 29 additions & 2 deletions core/cli/src/checker.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
}
}
12 changes: 7 additions & 5 deletions core/cli/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ export class Compiler {
}

private writeHeader(sources: Map<SourceFile, string>): void {
const imports = [...sources].map(([source, name]) => [
name,
this.output.getRelativePathAsModuleSpecifierTo(source),
]);

this.output.addImportDeclaration({
moduleSpecifier: 'dicc',
namedImports: [
{ name: 'Container' },
],
});

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 });
}
}

Expand Down
1 change: 1 addition & 0 deletions core/cli/src/dicc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ export class Dicc {

this.helper.destroy();
await output.save();
this.checker.checkOutput(output);
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c7218ca

Please sign in to comment.