diagnostics: Remove unused advice, and eliminate unknown normalization
MODIFY
packages/@romejs/diagnostics/normalize.ts
packages/@romejs/diagnostics/normalize.ts
* LICENSE file in the root directory of this source tree.
*/
-import {UnknownObject} from '@romejs/typescript-helpers';
import {Position} from '@romejs/parser-core';
-import {Diffs, Diff} from '@romejs/string-diff';
import {MarkupFormatOptions} from '@romejs/string-markup';
import {
Diagnostic,
@@ -15,31 +13,30 @@ import {
DiagnosticAdvice,
Diagnostics,
DiagnosticLanguage,
- DiagnosticLogCategory,
DiagnosticAdviceStackFrame,
- DiagnosticOrigin,
- DiagnosticSourceType,
DiagnosticDependencies,
+ PartialDiagnostic,
+ PartialDiagnosticAdvice,
+ PartialDiagnosticAdviceItem,
+ PartialDiagnosticAdviceStackFrame,
+ PartialDiagnostics,
} from './types';
import {coerce1, coerce0, number1, number0, number0Neg1} from '@romejs/ob1';
const DEFAULT_LIST_TRUNCATE = 100;
const DEFAULT_STACKTRACE_TRUNCATE = 10;
-function normalizeLanguage(language: unknown): DiagnosticLanguage {
- if (
- language === 'js' ||
- language === 'json' ||
- language === 'shell' ||
- language === 'url'
- ) {
+function normalizeLanguage(
+ language: undefined | DiagnosticLanguage,
+): DiagnosticLanguage {
+ if (language === undefined) {
+ return 'unknown';
+ } else {
return language;
}
-
- return 'unknown';
}
-function normalizePositionAssert(position: unknown): Position {
+function normalizePositionAssert(position: undefined | Position): Position {
const normalized = normalizePosition(position);
if (normalized === undefined) {
return {
@@ -52,8 +49,14 @@ function normalizePositionAssert(position: unknown): Position {
}
}
-export function normalizePosition(position: unknown): undefined | Position {
- const {line, column, index} = normalizeDiagnosticsObject(position);
+export function normalizePosition(
+ position: undefined | Position,
+): undefined | Position {
+ if (position === undefined) {
+ return undefined;
+ }
+
+ const {line, column, index} = position;
if (
typeof line !== 'number' ||
@@ -70,76 +73,19 @@ export function normalizePosition(position: unknown): undefined | Position {
};
}
-function normalizeLogCategory(value: unknown): DiagnosticLogCategory {
- if (
- value === 'none' ||
- value === 'info' ||
- value === 'warn' ||
- value === 'error'
- ) {
- return value;
- }
-
- return 'error';
-}
-
-function normalizeStringArray(value: unknown): Array<string> {
- if (Array.isArray(value)) {
- return value.map(item => normalizeString(item, 'Nothing provided'));
- } else {
- return [];
- }
-}
-
-function normalizeOptionalNumber(value: unknown): undefined | number {
- if (typeof value === 'number') {
- return value;
- } else {
- return undefined;
- }
-}
-
-function normalizeNumber(value: unknown, def: number): number {
- if (typeof value === 'number') {
- return value;
- } else {
+function normalizeValue<T>(value: undefined | T, def: T): T {
+ if (value === undefined) {
return def;
- }
-}
-
-function normalizeOptionalString(value: unknown): undefined | string {
- if (typeof value === 'string') {
- return value;
} else {
- return undefined;
- }
-}
-
-function normalizeString(value: unknown, def: string): string {
- if (typeof value === 'string') {
return value;
- } else {
- return def;
- }
-}
-
-function normalizeBoolean(value: unknown, def: boolean): boolean {
- if (typeof value === 'boolean') {
- return value;
- } else {
- return def;
}
}
export function normalizeDiagnostics(
- diagnostics: unknown,
+ diagnostics: PartialDiagnostics,
opts: MarkupFormatOptions,
): Diagnostics {
- if (Array.isArray(diagnostics)) {
return diagnostics.map(diag => normalizeDiagnostic(diag, opts));
- } else {
- return [];
- }
}
function normalizeFilename(
@@ -155,47 +101,24 @@ function normalizeFilename(
}
}
-function normalizeDiagnosticsObject(obj: unknown): UnknownObject {
- if (typeof obj !== 'object' || obj == null || Array.isArray(obj)) {
- return {};
- } else {
- // @ts-ignore
- return obj;
- }
-}
-
-function normalizeSourceType(val: unknown): DiagnosticSourceType {
- if (val === 'module' || val === 'script' || val === 'template') {
- return val;
- } else {
- return 'unknown';
- }
-}
-
export function normalizeDiagnostic(
- rawDiagnostic: unknown,
+ diagnostic: PartialDiagnostic,
opts: MarkupFormatOptions,
): Diagnostic {
- // Ensure that it's an object
- const diagnostic = normalizeDiagnosticsObject(rawDiagnostic);
-
- const filename = normalizeFilename(
- normalizeOptionalString(diagnostic.filename),
- opts,
- );
- const mtime = normalizeOptionalNumber(diagnostic.mtime);
- const origins = normalizeOrigins(diagnostic.origins);
+ const filename = normalizeFilename(diagnostic.filename, opts);
+ const mtime = diagnostic.mtime;
+ const origins = normalizeValue(diagnostic.origins, []);
const start = normalizePosition(diagnostic.start);
const end = normalizePosition(diagnostic.end);
- const fixable = normalizeBoolean(diagnostic.fixable, false);
- const sourceText = normalizeOptionalString(diagnostic.sourceText);
+ const fixable = normalizeValue(diagnostic.fixable, false);
+ const sourceText = diagnostic.sourceText;
const language = normalizeLanguage(diagnostic.language);
- const sourceType = normalizeSourceType(diagnostic.sourceType);
- const category = normalizeString(diagnostic.category, 'unknown');
- const message = normalizeString(diagnostic.message, 'No message provided');
- const marker = normalizeOptionalString(diagnostic.marker);
+ const sourceType = normalizeValue(diagnostic.sourceType, 'unknown');
+ const category = normalizeValue(diagnostic.category, 'unknown');
+ const message = normalizeValue(diagnostic.message, 'No message provided');
+ const marker = diagnostic.marker;
const dependencies = normalizeDependencies(diagnostic.dependencies, opts);
const diag: Diagnostic = {
@@ -224,10 +147,12 @@ export function normalizeDiagnostic(
}
function normalizeDependencies(
- value: unknown,
+ value: PartialDiagnostic['dependencies'],
opts: MarkupFormatOptions,
): DiagnosticDependencies {
- if (Array.isArray(value)) {
+ if (value === undefined) {
+ return [];
+ } else {
const deps: DiagnosticDependencies = [];
for (const elem of value) {
@@ -245,48 +170,28 @@ function normalizeDependencies(
}
return deps;
- } else {
- return [];
- }
-}
-
-function normalizeOrigins(value: unknown): Array<DiagnosticOrigin> {
- if (Array.isArray(value)) {
- return value.map(elem => {
- if (typeof elem === 'object' && elem != null) {
- return {
- category:
- typeof elem.category === 'string' ? elem.category : 'unknown',
- message: typeof elem.message === 'string' ? elem.message : undefined,
- };
- } else {
- return {category: 'unknown'};
- }
- });
- } else {
- return [];
}
}
function normalizeAdviceStackFrame(
- value: UnknownObject,
+ value: PartialDiagnosticAdviceStackFrame,
opts: MarkupFormatOptions,
): DiagnosticAdviceStackFrame {
return {
- prefix: normalizeOptionalString(value.prefix),
- suffix: normalizeOptionalString(value.suffix),
- object: normalizeOptionalString(value.object),
- property: normalizeOptionalString(value.property),
- filename: normalizeFilename(normalizeOptionalString(value.filename), opts),
- line: coerce1(normalizeOptionalNumber(value.line)),
- column: coerce0(normalizeOptionalNumber(value.column)),
- sourceText: normalizeOptionalString(value.sourceText),
+ prefix: value.prefix,
+ suffix: value.suffix,
+ object: value.object,
+ property: value.property,
+ filename: normalizeFilename(value.filename, opts),
+ line: value.line,
+ column: value.column,
+ sourceText: value.sourceText,
language: normalizeLanguage(value.language),
};
}
function normalizeAdviceStackFrames(
- value: unknown,
+ value: Array<PartialDiagnosticAdviceStackFrame>,
opts: MarkupFormatOptions,
): Array<DiagnosticAdviceStackFrame> {
if (Array.isArray(value)) {
@@ -298,7 +203,7 @@ function normalizeAdviceStackFrames(
function normalizeDiagnosticAdvice(
diag: Diagnostic,
- value: unknown,
+ value: undefined | PartialDiagnosticAdvice,
opts: MarkupFormatOptions,
): DiagnosticAdvice {
if (Array.isArray(value)) {
@@ -310,58 +215,50 @@ function normalizeDiagnosticAdvice(
export function normalizeDiagnosticAdviceItem(
diag: Diagnostic,
- rawPart: unknown,
+ part: PartialDiagnosticAdviceItem,
opts: MarkupFormatOptions,
): DiagnosticAdviceItem {
- const part = normalizeDiagnosticsObject(rawPart);
-
- if (typeof part.type !== 'string') {
- return {
- type: 'log',
- category: 'error',
- message: `Unsupported diagnostics part with no type`,
- compact: false,
- };
- }
-
switch (part.type) {
case 'log':
return {
type: 'log',
- category: normalizeLogCategory(part.category),
- message: normalizeString(part.message, 'No message provided'),
- compact: normalizeBoolean(part.compact, false),
+ category: part.category,
+ message: normalizeValue(part.message, 'No message provided'),
+ compact: normalizeValue(part.compact, false),
};
case 'list':
return {
type: 'list',
- list: normalizeStringArray(part.list),
- truncate: normalizeNumber(part.truncate, DEFAULT_LIST_TRUNCATE),
- reverse: normalizeBoolean(part.reverse, false),
- ordered: normalizeBoolean(part.ordered, false),
+ list: part.list,
+ truncate: normalizeValue(part.truncate, DEFAULT_LIST_TRUNCATE),
+ reverse: normalizeValue(part.reverse, false),
+ ordered: normalizeValue(part.ordered, false),
};
case 'code':
return {
type: 'code',
- code: normalizeString(part.code, ''),
+ code: normalizeValue(part.code, ''),
language: normalizeLanguage(part.language || diag.language),
- sourceType: normalizeSourceType(part.sourceType || diag.sourceType),
+ sourceType: normalizeValue(
+ part.sourceType || diag.sourceType,
+ 'unknown',
+ ),
};
case 'frame':
return {
type: 'frame',
- sourceType: normalizeSourceType(part.sourceType || diag.sourceType),
- language: normalizeLanguage(part.language || diag.language),
- sourceText: normalizeOptionalString(part.sourceText),
- marker: normalizeOptionalString(part.marker),
- filename: normalizeFilename(
- normalizeOptionalString(part.filename),
- opts,
+ sourceType: normalizeValue(
+ part.sourceType || diag.sourceType,
+ 'unknown',
),
- mtime: normalizeOptionalNumber(part.mtime),
+ language: normalizeLanguage(part.language || diag.language),
+ sourceText: part.sourceText,
+ marker: part.marker,
+ filename: normalizeFilename(part.filename, opts),
+ mtime: part.mtime,
start: normalizePositionAssert(part.start),
end: normalizePositionAssert(part.end),
};
@@ -369,23 +266,15 @@ export function normalizeDiagnosticAdviceItem(
case 'diff':
return {
type: 'diff',
- diff: normalizeAdviceDiff(part.diff),
- };
-
- case 'action':
- return {
- type: 'action',
- message: normalizeString(part.message, 'No message provided'),
- cancelable: normalizeBoolean(part.cancelable, false),
- buttons: normalizeAdviceActionButtons(part.buttons),
+ diff: part.diff,
};
case 'stacktrace':
return {
type: 'stacktrace',
- title: normalizeOptionalString(part.title),
+ title: part.title,
frames: normalizeAdviceStackFrames(part.frames, opts),
- truncate: normalizeNumber(part.truncate, DEFAULT_STACKTRACE_TRUNCATE),
+ truncate: normalizeValue(part.truncate, DEFAULT_STACKTRACE_TRUNCATE),
};
case 'inspect':
@@ -394,58 +283,5 @@ export function normalizeDiagnosticAdviceItem(
// @ts-ignore TODO
data: part.data as any,
};
-
- default:
- return {
- type: 'log',
- category: 'error',
- message: `Unsupported diagnostics part ${part.type}`,
- compact: false,
- };
- }
-}
-
-function normalizeAdviceDiffEntry([op, text]: Array<unknown>): Diff {
- if (typeof text === 'string' && (op === -1 || op === 0 || op === 1)) {
- return [op, text];
- } else {
- // Maybe we should error here?
- return [0, ''];
- }
-}
-
-function normalizeAdviceDiff(value: unknown): Diffs {
- if (Array.isArray(value)) {
- return value.map(item => {
- if (Array.isArray(item)) {
- return normalizeAdviceDiffEntry(item);
- } else {
- return normalizeAdviceDiffEntry([]);
- }
- });
- } else {
- return [];
- }
-}
-
-function normalizeAdviceActionButtons(
- value: unknown,
-): Array<{
- text: string;
- command: string;
-}> {
- if (Array.isArray(value)) {
- return value.map(item => {
- if (typeof item !== 'object' || item == null) {
- item = {};
- }
-
- return {
- text: normalizeString(item.text, 'No text provided'),
- command: normalizeString(item.command, ''),
- };
- });
- } else {
- return [];
}
}

MODIFY
packages/@romejs/diagnostics/types.ts
packages/@romejs/diagnostics/types.ts
@@ -96,16 +96,6 @@ export type DiagnosticAdviceItemDiff = {
diff: Diffs;
};
-export type DiagnosticAdviceItemAction = {
- type: 'action';
- message: string;
- cancelable: boolean;
- buttons: Array<{
- text: string;
- command: string;
- }>;
-};
-
export type DiagnosticAdviceItemStacktrace = {
type: 'stacktrace';
title: undefined | string;
@@ -240,15 +230,6 @@ export type PartialDiagnosticAdviceItem =
type: 'diff';
diff: Diffs;
}
- | {
- type: 'action';
- message: string;
- cancelable: boolean;
- buttons: Array<{
- text: string;
- command: string;
- }>;
- }
| {
type: 'stacktrace';
title?: string;

diagnostics: Remove unused advice, and eliminate unknown normalization