From 722cb2da00002e5f1cb53131b4b34baa1443926c Mon Sep 17 00:00:00 2001 From: ezolenko Date: Fri, 24 Mar 2017 23:08:10 -0600 Subject: [PATCH] - error codes and types --- src/host.ts | 1 + src/index.ts | 73 ++++++++++++++++++++++++++++++-------------------- src/tscache.ts | 16 ++++++----- 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/host.ts b/src/host.ts index f6b7a53..7e4f670 100644 --- a/src/host.ts +++ b/src/host.ts @@ -34,6 +34,7 @@ export class LanguageServiceHost implements ts.LanguageServiceHost if (fs.existsSync(fileName)) { this.snapshots[fileName] = ts.ScriptSnapshot.fromString(ts.sys.readFile(fileName)); + this.versions[fileName] = (this.versions[fileName] || 0) + 1; return this.snapshots[fileName]; } diff --git a/src/index.ts b/src/index.ts index 587ca27..5df4c91 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,7 +48,7 @@ function parseTsConfig(context: IContext) if (result.error) { - printDiagnostics(context, convertDiagnostic([result.error])); + printDiagnostics(context, convertDiagnostic("config", [result.error])); throw new Error(`failed to parse ${fileName}`); } @@ -63,27 +63,34 @@ function printDiagnostics(context: IContext, diagnostics: IDiagnostics[]) { let print; let color; + let category; switch (diagnostic.category) { case ts.DiagnosticCategory.Message: print = context.info; color = colors.white; + category = ""; break; case ts.DiagnosticCategory.Error: print = context.error; color = colors.red; + category = "error"; break; case ts.DiagnosticCategory.Warning: default: print = context.warn; color = colors.yellow; + category = "warning"; break; } + // const type = ""; + const type = diagnostic.type + " "; + if (diagnostic.fileLine) - print.call(context, [`${diagnostic.fileLine}: ${color(diagnostic.flatMessage)}`]); + print.call(context, [`${diagnostic.fileLine}: ${type}${category} TS${diagnostic.code} ${color(diagnostic.flatMessage)}`]); else - print.call(context, [color(diagnostic.flatMessage)]); + print.call(context, [`${type}${category} TS${diagnostic.code} ${color(diagnostic.flatMessage)}`]); }); }; @@ -141,7 +148,7 @@ export default function typescript (options: IOptions) // printing compiler option errors if (options.check) - printDiagnostics(context, convertDiagnostic(service.getCompilerOptionsDiagnostics())); + printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); return { @@ -155,6 +162,7 @@ export default function typescript (options: IOptions) importer = importer.split("\\").join("/"); + // TODO: use module resolution cache const result = ts.nodeModuleNameResolver(importee, importer, parsedConfig.options, ts.sys); if (result.resolvedModule && result.resolvedModule.resolvedFileName) @@ -205,13 +213,16 @@ export default function typescript (options: IOptions) noErrors = false; // always checking on fatal errors, even if options.check is set to false - const diagnostics = cache.getSyntacticDiagnostics(id, snapshot, () => - { - return service.getSyntacticDiagnostics(id); - }).concat(cache.getSemanticDiagnostics(id, snapshot, () => - { - return service.getSemanticDiagnostics(id); - })); + const diagnostics = _.concat( + cache.getSyntacticDiagnostics(id, snapshot, () => + { + return service.getSyntacticDiagnostics(id); + }), + cache.getSemanticDiagnostics(id, snapshot, () => + { + return service.getSemanticDiagnostics(id); + }), + ); printDiagnostics(contextWrapper, diagnostics); // since no output was generated, aborting compilation @@ -229,15 +240,18 @@ export default function typescript (options: IOptions) if (options.check) { - const diagnostics = cache.getSyntacticDiagnostics(id, snapshot, () => - { - return service.getSyntacticDiagnostics(id); - }).concat(cache.getSemanticDiagnostics(id, snapshot, () => - { - return service.getSemanticDiagnostics(id); - })); + const diagnostics = _.concat( + cache.getSyntacticDiagnostics(id, snapshot, () => + { + return service.getSyntacticDiagnostics(id); + }), + cache.getSemanticDiagnostics(id, snapshot, () => + { + return service.getSemanticDiagnostics(id); + }), + ); - if (diagnostics.length !== 0) + if (diagnostics.length > 0) noErrors = false; printDiagnostics(contextWrapper, diagnostics); @@ -248,15 +262,14 @@ export default function typescript (options: IOptions) ongenerate(bundleOptions: any): void { - if (_.isArray(bundleOptions.targets)) - targetCount = bundleOptions.targets.length; + targetCount = _.get(bundleOptions, "targets.length", 1); if (round >= targetCount) // ongenerate() is called for each target { watchMode = true; round = 0; } - context.debug(`generating target ${round} of ${bundleOptions.targets.length}`); + context.debug(`generating target ${round + 1} of ${targetCount}`); if (watchMode && round === 0) { @@ -268,7 +281,10 @@ export default function typescript (options: IOptions) cache.walkTree((id) => { - const diagnostics = convertDiagnostic(service.getSyntacticDiagnostics(id)).concat(convertDiagnostic(service.getSemanticDiagnostics(id))); + const diagnostics = _.concat( + convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), + convertDiagnostic("semantic", service.getSemanticDiagnostics(id)), + ); if (diagnostics.length > 0) noErrors = false; @@ -276,12 +292,11 @@ export default function typescript (options: IOptions) printDiagnostics(context, diagnostics); }); - } - - if (!noErrors) - { - noErrors = true; - context.info(colors.yellow("there were errors or warnings above.")); + if (!noErrors) + { + noErrors = true; + context.info(colors.yellow("there were errors or warnings above.")); + } } cache.done(); diff --git a/src/tscache.ts b/src/tscache.ts index 67e0b20..5c7b900 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -24,6 +24,8 @@ export interface IDiagnostics flatMessage: string; fileLine?: string; category: ts.DiagnosticCategory; + code: number; + type: string; } interface ITypeSnapshot @@ -32,7 +34,7 @@ interface ITypeSnapshot snapshot: ts.IScriptSnapshot | undefined; } -export function convertDiagnostic(data: ts.Diagnostic[]): IDiagnostics[] +export function convertDiagnostic(type: string, data: ts.Diagnostic[]): IDiagnostics[] { return _.map(data, (diagnostic) => { @@ -40,6 +42,8 @@ export function convertDiagnostic(data: ts.Diagnostic[]): IDiagnostics[] { flatMessage: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), category: diagnostic.category, + code: diagnostic.code, + type, }; if (diagnostic.file) @@ -54,7 +58,7 @@ export function convertDiagnostic(data: ts.Diagnostic[]): IDiagnostics[] export class TsCache { - private cacheVersion = "3"; + private cacheVersion = "4"; private dependencyTree: graph.Graph; private ambientTypes: ITypeSnapshot[]; private ambientTypesDirty = false; @@ -157,12 +161,12 @@ export class TsCache public getSyntacticDiagnostics(id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[] { - return this.getDiagnostics(this.syntacticDiagnosticsCache, id, snapshot, check); + return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check); } public getSemanticDiagnostics(id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[] { - return this.getDiagnostics(this.semanticDiagnosticsCache, id, snapshot, check); + return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check); } private checkAmbientTypes(): void @@ -184,7 +188,7 @@ export class TsCache _.each(typeNames, (name) => this.typesCache.touch(name)); } - private getDiagnostics(cache: ICache, id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[] + private getDiagnostics(type: string, cache: ICache, id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[] { const name = this.makeName(id, snapshot); @@ -194,7 +198,7 @@ export class TsCache { this.context.debug(colors.yellow(" cache miss")); - const data = convertDiagnostic(check()); + const data = convertDiagnostic(type, check()); cache.write(name, data); this.markAsDirty(id, snapshot); return data;