Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch up with base #9

Merged
merged 10 commits into from
Mar 24, 2020
118 changes: 67 additions & 51 deletions packages/@romejs/cli-diagnostics/DiagnosticsPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
*/

import {
Diagnostic,
Diagnostics,
PartialDiagnostics,
PartialDiagnostic,
DiagnosticAdvice,
Diagnostic,
DiagnosticOrigin,
DiagnosticLanguage,
DiagnosticSourceType,
PartialDiagnosticAdvice,
DiagnosticAdvice,
} from '@romejs/diagnostics';
import {Reporter} from '@romejs/cli-reporter';
import {
Expand All @@ -25,7 +22,6 @@ import {
} from './types';
import {DiagnosticsProcessor} from '@romejs/diagnostics';
import {
normalizeDiagnosticAdviceItem,
deriveRootAdviceFromDiagnostic,
getDiagnosticHeader,
} from '@romejs/diagnostics';
Expand Down Expand Up @@ -55,8 +51,8 @@ type Banner = {
};

type PositionLike = {
line: undefined | Number1;
column: undefined | Number0;
line?: undefined | Number1;
column?: undefined | Number0;
};

export function readDiagnosticsFileLocal(
Expand Down Expand Up @@ -110,8 +106,8 @@ type ReferenceFileDependency = {
type: 'reference';
path: UnknownFilePath;
mtime: undefined | number;
sourceType: DiagnosticSourceType;
language: DiagnosticLanguage;
sourceType: undefined | DiagnosticSourceType;
language: undefined | DiagnosticLanguage;
};

type FileDependency = ChangeFileDependency | ReferenceFileDependency;
Expand Down Expand Up @@ -164,6 +160,20 @@ export default class DiagnosticsPrinter extends Error {
filteredCount: number;
truncatedCount: number;

createFilePath(filename: undefined | string): UnknownFilePath {
if (filename === undefined) {
filename = 'unknown';
}

const {normalizeFilename} = this.reporter.markupOptions;

if (normalizeFilename === undefined) {
return createUnknownFilePath(filename);
} else {
return createUnknownFilePath(normalizeFilename(filename));
}
}

throwIfAny() {
if (this.hasDiagnostics()) {
throw this;
Expand All @@ -188,16 +198,14 @@ export default class DiagnosticsPrinter extends Error {
}

getDiagnostics(): Diagnostics {
return this.processor.getCompleteSortedDiagnostics(
this.reporter.markupOptions,
);
return this.processor.getSortedDiagnostics();
}

isFocused(diag: Diagnostic): boolean {
const focusFlag = this.flags.focus;
const focusEnabled = focusFlag !== undefined && focusFlag !== '';

const {filename, start, end} = diag;
const {filename, start, end} = diag.location;

// If focus is enabled, exclude locationless errors
if (focusEnabled && (filename === undefined || start === undefined)) {
Expand Down Expand Up @@ -237,7 +245,8 @@ export default class DiagnosticsPrinter extends Error {
}

// Match against the supplied grep pattern
let ignored = diag.message.toLowerCase().includes(grep) === false;
let ignored =
diag.description.message.value.toLowerCase().includes(grep) === false;
if (inverseGrep) {
ignored = !ignored;
}
Expand All @@ -264,43 +273,47 @@ export default class DiagnosticsPrinter extends Error {
const deps: Array<FileDependency> = [];

for (const {
advice,
filename,
dependencies,
language,
sourceType,
mtime,
description: {advice},
location: {language, sourceType, mtime, filename},
} of diagnostics) {
if (filename !== undefined) {
deps.push({
type: 'reference',
path: createUnknownFilePath(filename),
path: this.createFilePath(filename),
mtime,
language,
sourceType,
});
}

for (const {filename, mtime} of dependencies) {
deps.push({
type: 'change',
path: createUnknownFilePath(filename),
mtime,
});
}

for (const item of advice) {
if (item.type === 'frame' && item.filename !== undefined &&
item.sourceText === undefined) {
if (dependencies !== undefined) {
for (const {filename, mtime} of dependencies) {
deps.push({
type: 'reference',
path: createUnknownFilePath(item.filename),
language: item.language,
sourceType: item.sourceType,
mtime: item.mtime,
type: 'change',
path: this.createFilePath(filename),
mtime,
});
}
}

if (advice !== undefined) {
for (const item of advice) {
if (item.type === 'frame') {
const {location} = item;
if (location.filename !== undefined && location.sourceText ===
undefined) {
deps.push({
type: 'reference',
path: this.createFilePath(location.filename),
language: location.language,
sourceType: location.sourceType,
mtime: location.mtime,
});
}
}
}
}
}

const depsMap: UnknownFilePathMap<FileDependency> = new UnknownFilePathMap();
Expand Down Expand Up @@ -351,11 +364,11 @@ export default class DiagnosticsPrinter extends Error {
}
}

addDiagnostic(partialDiagnostic: PartialDiagnostic, origin?: DiagnosticOrigin) {
addDiagnostic(partialDiagnostic: Diagnostic, origin?: DiagnosticOrigin) {
this.addDiagnostics([partialDiagnostic], origin);
}

addDiagnostics(partials: PartialDiagnostics, origin?: DiagnosticOrigin) {
addDiagnostics(partials: Diagnostics, origin?: DiagnosticOrigin) {
if (partials.length === 0) {
return;
}
Expand All @@ -379,18 +392,22 @@ export default class DiagnosticsPrinter extends Error {

displayDiagnostic(diag: Diagnostic) {
const {reporter} = this;
const {start, end, filename} = diag;
const {start, end, filename} = diag.location;
const {advice} = diag.description;

// Determine if we should skip showing the frame at the top of the diagnostic output

// We check if there are any frame advice entries that match us exactly, this is

// useful for stuff like reporting call stacks
let skipFrame = false;
if (start !== undefined && end !== undefined) {
adviceLoop: for (const item of diag.advice) {
if (item.type === 'frame' && item.filename === filename &&
equalPosition(item.start, start) && equalPosition(item.end, end)) {
if (start !== undefined && end !== undefined && advice !== undefined) {
adviceLoop: for (const item of advice) {
if (item.type === 'frame' && item.location.filename === filename &&
equalPosition(item.location.start, start) && equalPosition(
item.location.end,
end,
)) {
skipFrame = true;
break;
}
Expand Down Expand Up @@ -419,7 +436,7 @@ export default class DiagnosticsPrinter extends Error {
}
}

const outdatedAdvice: PartialDiagnosticAdvice = [];
const outdatedAdvice: DiagnosticAdvice = [];
const isOutdated = outdatedFiles.size > 0;
if (isOutdated) {
const outdatedFilesArr = Array.from(outdatedFiles, (path) => path.join());
Expand Down Expand Up @@ -456,17 +473,16 @@ export default class DiagnosticsPrinter extends Error {

reporter.indent(() => {
// Concat all the advice together
const derivedAdvice: DiagnosticAdvice = [
const advice: DiagnosticAdvice = [
...derived.advice,
...outdatedAdvice,
].map((item) =>
normalizeDiagnosticAdviceItem(diag, item, this.reporter.markupOptions)
);
const advice: DiagnosticAdvice = derivedAdvice.concat(diag.advice);
...(diag.description.advice || []),
];

// Print advice
for (const item of advice) {
const noSpacer = printAdvice(item, {
printer: this,
flags: this.flags,
missingFileSources: this.missingFileSources,
fileSources: this.fileSources,
Expand All @@ -482,7 +498,7 @@ export default class DiagnosticsPrinter extends Error {
if (this.flags.verboseDiagnostics) {
const {origins} = diag;

if (origins.length > 0) {
if (origins !== undefined && origins.length > 0) {
reporter.spacer();
reporter.info('Why are you seeing this diagnostic?');
reporter.forceSpacer();
Expand Down
9 changes: 4 additions & 5 deletions packages/@romejs/cli-diagnostics/ansiHighlightCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const FILE_SIZE_MAX = 100_000;
export type AnsiHighlightOptions = {
path: UnknownFilePath;
input: string;
sourceType: DiagnosticSourceType;
language: DiagnosticLanguage;
sourceType: undefined | DiagnosticSourceType;
language: undefined | DiagnosticLanguage;
};

export default function ansiHighlightCode(opts: AnsiHighlightOptions): string {
Expand All @@ -30,9 +30,8 @@ export default function ansiHighlightCode(opts: AnsiHighlightOptions): string {

if (opts.language === 'js') {
// js-parser does not accept an "unknown" sourceType
return ansiHighlightJS(opts.input, opts.sourceType === 'unknown'
? 'script' : opts.sourceType
);
return ansiHighlightJS(opts.input, opts.sourceType === undefined ||
opts.sourceType === 'unknown' ? 'script' : opts.sourceType);
}

if (opts.language === 'json') {
Expand Down
8 changes: 6 additions & 2 deletions packages/@romejs/cli-diagnostics/buildMessageCodeFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ import {

export default function buildMessageCodeFrame(
allLines: Array<string>,
start: Position,
end: Position,
start: undefined | Position,
end: undefined | Position,
maybeMarkerMessage?: string,
): string {
let markerMessage: string = maybeMarkerMessage === undefined
? '' : maybeMarkerMessage;

if (start === undefined || end === undefined) {
return CODE_FRAME_INDENT + markerMessage;
}

const startLineIndex = coerce1to0(start.line);

let endLineIndex = coerce1to0(end.line);
Expand Down
8 changes: 4 additions & 4 deletions packages/@romejs/cli-diagnostics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {PartialDiagnostics} from '@romejs/diagnostics';
import {Diagnostics} from '@romejs/diagnostics';
import {DiagnosticsPrinterOptions} from './types';
import {Reporter, ReporterStream} from '@romejs/cli-reporter';
import DiagnosticsPrinter from './DiagnosticsPrinter';
Expand All @@ -24,7 +24,7 @@ export * from './types';

// Simple wrappers around DiagnosticsPrinter
export async function printDiagnostics(
diagnostics: PartialDiagnostics,
diagnostics: Diagnostics,
opts: DiagnosticsPrinterOptions,
): Promise<DiagnosticsPrinter> {
const printer = new DiagnosticsPrinter(opts);
Expand All @@ -34,7 +34,7 @@ export async function printDiagnostics(
}

export function printDiagnosticsSync(
diagnostics: PartialDiagnostics,
diagnostics: Diagnostics,
opts: DiagnosticsPrinterOptions,
): DiagnosticsPrinter {
const printer = new DiagnosticsPrinter(opts);
Expand All @@ -44,7 +44,7 @@ export function printDiagnosticsSync(
}

export function printDiagnosticsToString(
diagnostics: PartialDiagnostics,
diagnostics: Diagnostics,
opts: Omit<DiagnosticsPrinterOptions, 'reporter'> = {},
format: ReporterStream['format'] = 'none',
): string {
Expand Down
Loading