Skip to content

Commit

Permalink
perf: code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
kabeep committed May 1, 2024
1 parent 62c8cdc commit b4962f1
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/core/PaletteError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
})
Expand Down
3 changes: 1 addition & 2 deletions src/core/PrintError.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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}`;

Expand Down
2 changes: 1 addition & 1 deletion src/core/TraceError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<anonymous>';
name = 'anonymous';
}

address = normalizePath(address);
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/is-nil.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/is-string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function isString(value: any): value is string {
function isString(value: unknown): value is string {
return typeof value === 'string';
}

Expand Down
7 changes: 3 additions & 4 deletions test/core/PaletteError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
1 change: 0 additions & 1 deletion test/core/PrintError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ test('PrintError.print - should generate correct trace information', () => {
name: 'anotherFunction',
packageName: '[current]',
},
undefined,
];

const expected =
Expand Down
4 changes: 2 additions & 2 deletions test/core/TraceError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test('trace - should return correct anonymous trace options array', () => {
},
{
original: 'at example/file.js:3:4',
name: '<anonymous>',
name: 'anonymous',
address: 'example/file.js:3:4',
file: 'file.js',
line: 3,
Expand All @@ -47,7 +47,7 @@ test('trace - should return correct partial trace options array', () => {
const expected = [
{
original: 'at example/file.js',
name: '<anonymous>',
name: 'anonymous',
address: 'example/file.js',
file: undefined,
line: undefined,
Expand Down

0 comments on commit b4962f1

Please sign in to comment.