From 2b5819ab52321415d93aea0413fc396786122332 Mon Sep 17 00:00:00 2001 From: Sam Vervaeck Date: Sat, 23 May 2020 22:48:24 +0200 Subject: [PATCH] Fix errors in src/diagnostics.ts --- src/diagnostics.ts | 57 +++++++++++++++++++++++++--------------------- src/text.ts | 18 ++++++++++++--- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/diagnostics.ts b/src/diagnostics.ts index d2b2e2c2e..5c9c9cd49 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -10,6 +10,8 @@ export const E_TOO_FEW_ARGUMENTS_FOR_FUNCTION_CALL = "Too few arguments for func export const E_TOO_MANY_ARGUMENTS_FOR_FUNCTION_CALL = "Too many arguments for function call. Expected {expected} but got {actual}."; export const E_INVALID_ARGUMENTS = "Invalid arguments passed to function '{name}'." +const DIAG_NUM_EXTRA_LINES = 1; + export interface Diagnostic { message: string; severity: string; @@ -30,40 +32,43 @@ export class DiagnosticPrinter { public add(diagnostic: Diagnostic): void { let out = '' + if (diagnostic.node !== undefined) { + const span = diagnostic.node.span!; + const content = span.file.getText(); + const startLine = Math.max(0, span.start.line-1-DIAG_NUM_EXTRA_LINES) + const lines = content.split('\n') + const endLine = Math.min(lines.length-1, (span.end !== undefined ? span.end.line : startLine)+DIAG_NUM_EXTRA_LINES) + const gutterWidth = Math.max(2, countDigits(endLine+1)) + for (let i = startLine; i < endLine; i++) { + out += ' '+chalk.bgWhite.black(' '.repeat(gutterWidth-countDigits(i+1))+(i+1).toString())+' '+lines[i]+'\n' + const gutter = ' '+chalk.bgWhite.black(' '.repeat(gutterWidth))+' ' + if (span.end !== undefined) { + if (i === span.start.line-1 && i === span.end.line-1) { + out += gutter+' '.repeat(span.start.column-1)+chalk.red('~'.repeat(span.end.column-span.start.column)) + '\n' + } else if (i === span.start.line-1) { + out += gutter+' '.repeat(span.start.column-1)+chalk.red('~'.repeat(lines[i].length-span.start.column+1)) + '\n' + } else if (i === span.end.line-1) { + out += gutter+chalk.red('~'.repeat(span.end.column-1)) + '\n' + } else if (i > span.start.line-1 && i < span.end.line-1) { + out += gutter+chalk.red('~'.repeat(lines[i].length)) + '\n' + } + } + } + out += '\n' + out += chalk.bold.yellow(`${span.file.origPath}:${span.start.line}: `); + } switch (diagnostic.severity) { case 'error': this.hasErrors = true; out += chalk.bold.red('error: '); } - if (diagnostic.node !== undefined) { - // message = this.fileManager.getFileName(diag.location.fileId)+': '+message - const content = this.fileManager.getContent(diag.location.fileId) - const startLine = Math.max(0, diag.location.start.line-1-DIAG_NUM_EXTRA_LINES) - const lines = content.split('\n') - const endLine = Math.min(lines.length-1, (diag.location.end !== undefined ? diag.location.end.line : startLine)+DIAG_NUM_EXTRA_LINES) - const gutterWidth = Math.max(2, countDigits(endLine+1)) - for (let i = startLine; i < endLine; i++) { - console.error(' '+chalk.bgWhite.black(' '.repeat(gutterWidth-countDigits(i+1))+(i+1).toString())+' '+lines[i]) - const gutter = ' '+chalk.bgWhite.black(' '.repeat(gutterWidth))+' ' - if (diag.location.end !== undefined) { - if (i === diag.location.start.line-1 && i === diag.location.end.line-1) { - console.error(gutter+' '.repeat(diag.location.start.column-1)+chalk.red('~'.repeat(diag.location.end.column-diag.location.start.column))) - } else if (i === diag.location.start.line-1) { - console.error(gutter+' '.repeat(diag.location.start.column-1)+chalk.red('~'.repeat(lines[i].length-diag.location.start.column+1))) - } else if (i === diag.location.end.line-1) { - console.error(gutter+chalk.red('~'.repeat(diag.location.end.column-1))) - } else if (i > diag.location.start.line-1 && i < diag.location.end.line-1) { - console.error(gutter+chalk.red('~'.repeat(lines[i].length))) - } - } - } - } if (diagnostic.args !== undefined) { - out += format(diagnostic.message, diagnostic.args); + out += format(diagnostic.message, diagnostic.args) + '\n'; } else { - out += diagnostic.message; + out += diagnostic.message + '\n'; } - process.stderr.write(out + '\n'); + out += '\n' + process.stderr.write(out); } } diff --git a/src/text.ts b/src/text.ts index 1d7ba6d87..81be23f5f 100644 --- a/src/text.ts +++ b/src/text.ts @@ -1,16 +1,28 @@ import * as path from "path" +import * as fs from "fs" export class TextFile { + private cachedText: string | null = null; + constructor(public origPath: string) { } - get fullPath() { + public get fullPath() { return path.resolve(this.origPath) } + public getText(): string { + if (this.cachedText !== null) { + return this.cachedText; + } + const text = fs.readFileSync(this.fullPath, 'utf8'); + this.cachedText = text; + return text + } + } export class TextPos { @@ -23,7 +35,7 @@ export class TextPos { } - clone() { + public clone() { return new TextPos(this.offset, this.line, this.column) } @@ -39,7 +51,7 @@ export class TextSpan { } - clone() { + public clone() { return new TextSpan(this.file, this.start.clone(), this.end.clone()); }