Skip to content

Commit

Permalink
Print diagnostic related information (#867)
Browse files Browse the repository at this point in the history
* Fix tab issue when printing diagnostics

* include related info in printed diagnostics

* Fix "....and X more" loop
  • Loading branch information
TwitchBronBron authored Aug 2, 2023
1 parent b2fc10e commit a027ad2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
16 changes: 10 additions & 6 deletions package-lock.json

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

14 changes: 13 additions & 1 deletion src/ProgramBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as diagnosticUtils from './diagnosticUtils';
import * as fsExtra from 'fs-extra';
import * as requireRelative from 'require-relative';
import { Throttler } from './Throttler';
import { URI } from 'vscode-uri';

/**
* A runner class that handles
Expand Down Expand Up @@ -310,8 +311,19 @@ export class ProgramBuilder {
for (let diagnostic of sortedDiagnostics) {
//default the severity to error if undefined
let severity = typeof diagnostic.severity === 'number' ? diagnostic.severity : DiagnosticSeverity.Error;
let relatedInformation = (diagnostic.relatedInformation ?? []).map(x => {
let relatedInfoFilePath = URI.parse(x.location.uri).fsPath;
if (!emitFullPaths) {
relatedInfoFilePath = path.relative(cwd, relatedInfoFilePath);
}
return {
filePath: relatedInfoFilePath,
range: x.location.range,
message: x.message
};
});
//format output
diagnosticUtils.printDiagnostic(options, severity, filePath, lines, diagnostic);
diagnosticUtils.printDiagnostic(options, severity, filePath, lines, diagnostic, relatedInformation);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bscPlugin/validation/ScopeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export class ScopeValidator {
if (isXmlScope(this.event.scope) && this.event.scope.xmlFile?.srcPath) {
info.location = util.createLocation(
URI.file(this.event.scope.xmlFile.srcPath).toString(),
util.createRange(0, 0, 0, 10)
this.event.scope?.xmlFile?.ast?.component?.getAttribute('name')?.value.range ?? util.createRange(0, 0, 0, 10)
);
} else {
info.location = util.createLocation(
Expand Down
2 changes: 1 addition & 1 deletion src/diagnosticUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('diagnosticUtils', () => {
diagnosticUtils.getDiagnosticLine({ range: range } as any, '1'.repeat(lineLength), color)
).to.eql([
chalk.bgWhite(' ' + chalk.black((range.start.line + 1).toString()) + ' ') + ' ' + '1'.repeat(lineLength),
chalk.bgWhite(' ' + chalk.white('_'.repeat((range.start.line + 1).toString().length)) + ' ') + ' ' + squigglyText.padEnd(lineLength, ' ')
chalk.bgWhite(' ' + chalk.white(' '.repeat((range.start.line + 1).toString().length)) + ' ') + ' ' + squigglyText.padEnd(lineLength, ' ')
].join('\n'));
}

Expand Down
31 changes: 29 additions & 2 deletions src/diagnosticUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chalk from 'chalk';
import type { BsConfig } from './BsConfig';
import { DiagnosticSeverity } from 'vscode-languageserver';
import type { BsDiagnostic } from '.';
import type { Range } from 'vscode-languageserver';

/**
* Prepare print diagnostic formatting options
Expand Down Expand Up @@ -57,7 +58,8 @@ export function printDiagnostic(
severity: DiagnosticSeverity,
filePath: string,
lines: string[],
diagnostic: BsDiagnostic
diagnostic: BsDiagnostic,
relatedInformation?: Array<{ range: Range; filePath: string; message: string }>
) {
let { includeDiagnostic, severityTextMap, typeColor } = options;

Expand Down Expand Up @@ -91,6 +93,31 @@ export function printDiagnostic(
console.log(
getDiagnosticLine(diagnostic, diagnosticLine, typeColor[severity])
);

//print related information if present (only first few rows)
const relatedInfoList = relatedInformation ?? [];
let indent = ' ';
for (let i = 0; i < relatedInfoList.length; i++) {
let relatedInfo = relatedInfoList[i];
//only show the first 5 relatedInfo links
if (i < 5) {
console.log('');
console.log(
indent,
chalk.cyan(relatedInfo.filePath ?? '<unknown file>') +
':' +
chalk.yellow(
relatedInfo.range
? (relatedInfo.range.start.line + 1) + ':' + (relatedInfo.range.start.character + 1)
: 'line?:col?'
)
);
console.log(indent, relatedInfo.message);
} else {
console.log('\n', indent, `...and ${relatedInfoList.length - i + 1} more`);
break;
}
}
console.log('');
}

Expand All @@ -100,7 +127,7 @@ export function getDiagnosticLine(diagnostic: BsDiagnostic, diagnosticLine: stri
//only print the line information if we have some
if (diagnostic.range && diagnosticLine) {
const lineNumberText = chalk.bgWhite(' ' + chalk.black((diagnostic.range.start.line + 1).toString()) + ' ') + ' ';
const blankLineNumberText = chalk.bgWhite(' ' + chalk.white('_'.repeat((diagnostic.range.start.line + 1).toString().length)) + ' ') + ' ';
const blankLineNumberText = chalk.bgWhite(' ' + chalk.white(' '.repeat((diagnostic.range.start.line + 1).toString().length)) + ' ') + ' ';

//remove tabs in favor of spaces to make diagnostic printing more consistent
let leadingText = diagnosticLine.slice(0, diagnostic.range.start.character);
Expand Down

0 comments on commit a027ad2

Please sign in to comment.