From b4962f190ac9b11cf84678c576511420e33fd468 Mon Sep 17 00:00:00 2001 From: kabeep Date: Thu, 2 May 2024 01:25:18 +0800 Subject: [PATCH] perf: code quality --- src/core/PaletteError.ts | 5 ++--- src/core/PrintError.ts | 3 +-- src/core/TraceError.ts | 2 +- src/helpers/is-nil.ts | 2 +- src/helpers/is-string.ts | 2 +- test/core/PaletteError.spec.ts | 7 +++---- test/core/PrintError.spec.ts | 1 - test/core/TraceError.spec.ts | 4 ++-- 8 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/core/PaletteError.ts b/src/core/PaletteError.ts index 7b2c771..62468dc 100644 --- a/src/core/PaletteError.ts +++ b/src/core/PaletteError.ts @@ -14,7 +14,7 @@ export default class PaletteError extends Error { * @param {(string | Error)} message - The error message or an Error object. */ constructor(message: string | Error) { - super((message as Error)?.message || (message as string)); + super(isString(message) ? message : message?.message || ''); /** * The name of the error. @@ -44,8 +44,7 @@ export default class PaletteError extends Error { if (isNil(chain)) chain = chalk; const rgb = normalizeRgb(style, 'bg'); - const operator = _isBgRgb ? 'bgRgb' : 'rgb'; - chain = chain[operator](...rgb); + chain = _isRgb ? chain.rgb(...rgb) : chain.bgRgb(...rgb); return undefined; }) diff --git a/src/core/PrintError.ts b/src/core/PrintError.ts index 50367d3..51326d9 100644 --- a/src/core/PrintError.ts +++ b/src/core/PrintError.ts @@ -1,5 +1,6 @@ import { basename } from 'node:path'; import process from 'node:process'; +import { ChalkInstance } from 'chalk'; import type { TraceOption } from '../shared/index.js'; import TraceError from './TraceError.js'; @@ -57,8 +58,6 @@ export default class PrintError extends TraceError { return track .map((item, index) => { - if (!item) return; - const title = this.palette('yellowBright')(`${item.file}:${item.line}`); const summary = `- ${title} ${item.name}`; diff --git a/src/core/TraceError.ts b/src/core/TraceError.ts index 8807db8..0ceddce 100644 --- a/src/core/TraceError.ts +++ b/src/core/TraceError.ts @@ -77,7 +77,7 @@ export default class TraceError extends PaletteError { name = isEval ? 'eval' : text.slice(0, Math.max(0, text.length - address.length - 2)).trim(); } else { address = text; - name = ''; + name = 'anonymous'; } address = normalizePath(address); diff --git a/src/helpers/is-nil.ts b/src/helpers/is-nil.ts index 004b317..79c8b9b 100644 --- a/src/helpers/is-nil.ts +++ b/src/helpers/is-nil.ts @@ -1,4 +1,4 @@ -function isNil(value: any): value is undefined { +function isNil(value: unknown): value is undefined { // eslint-disable-next-line eqeqeq, no-eq-null return value == null; } diff --git a/src/helpers/is-string.ts b/src/helpers/is-string.ts index 3e39d35..c381844 100644 --- a/src/helpers/is-string.ts +++ b/src/helpers/is-string.ts @@ -1,4 +1,4 @@ -function isString(value: any): value is string { +function isString(value: unknown): value is string { return typeof value === 'string'; } diff --git a/test/core/PaletteError.spec.ts b/test/core/PaletteError.spec.ts index d76bc4c..388cb88 100644 --- a/test/core/PaletteError.spec.ts +++ b/test/core/PaletteError.spec.ts @@ -2,10 +2,9 @@ import chalk from 'chalk'; import { expect, test } from 'vitest'; import PaletteError from '../../src/core/PaletteError'; -test('PaletteError - should create an instance with correct error message', () => { - const errorMessage = 'Test error message'; - const error = new PaletteError(errorMessage); - expect(error.message).toBe(errorMessage); +test('PaletteError - should create an instance with uncorrected error message', () => { + const error = new PaletteError(undefined); + expect(error.message).toBe(''); }); test('PaletteError - should create an instance with Error object', () => { diff --git a/test/core/PrintError.spec.ts b/test/core/PrintError.spec.ts index 89553df..2e19557 100644 --- a/test/core/PrintError.spec.ts +++ b/test/core/PrintError.spec.ts @@ -68,7 +68,6 @@ test('PrintError.print - should generate correct trace information', () => { name: 'anotherFunction', packageName: '[current]', }, - undefined, ]; const expected = diff --git a/test/core/TraceError.spec.ts b/test/core/TraceError.spec.ts index ea2f394..d57489a 100644 --- a/test/core/TraceError.spec.ts +++ b/test/core/TraceError.spec.ts @@ -27,7 +27,7 @@ test('trace - should return correct anonymous trace options array', () => { }, { original: 'at example/file.js:3:4', - name: '', + name: 'anonymous', address: 'example/file.js:3:4', file: 'file.js', line: 3, @@ -47,7 +47,7 @@ test('trace - should return correct partial trace options array', () => { const expected = [ { original: 'at example/file.js', - name: '', + name: 'anonymous', address: 'example/file.js', file: undefined, line: undefined,