Skip to content

Commit

Permalink
refactor: fixes all eslint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stacy committed Sep 10, 2024
1 parent 1fe0274 commit 1a8b632
Show file tree
Hide file tree
Showing 36 changed files with 257 additions and 200 deletions.
31 changes: 26 additions & 5 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@ import tseslint from 'typescript-eslint';
import eslintConfigPrettier from 'eslint-config-prettier';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
ignores: ['dist/*', 'docs/*'],
languageOptions: {
parserOptions: {
project: './tsconfig.prod.json',
tsconfigRootDir: import.meta.dirname,
},
},
extends: [
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
...tseslint.configs.stylisticTypeChecked,
eslintConfigPrettier,
],
rules: {
'@typescript-eslint/restrict-template-expressions': [
'error',
{
allowNumber: true,
allowBoolean: true,
allowAny: true,
allowNullish: true,
},
],
},
},
eslintConfigPrettier
{
ignores: ['dist/', 'docs/'],
}
);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"scripts": {
"prebuild": "npm run clean",
"build": "tsc --project tsconfig.prod.json && tsc-alias && rm ./dist/demo_funcs.js && rm ./dist/demo_funcs.d.ts",
"build": "tsc --project tsconfig.prod.json && tsc-alias && rm -f ./dist/demo_funcs.js && rm -f ./dist/demo_funcs.d.ts",
"build:demo": "tsc --project tsconfig.json && tsc-alias",
"clean": "rm -rf ./dist",
"docs:build": "vuepress build docs",
Expand Down
10 changes: 4 additions & 6 deletions src/_types/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type Format = (typeof formats)[number];
/**
* Configuration data points that are also emitted with log data.
*/
export interface ConfigurationData<Meta extends Record<string, any> = Record<string, any>> {
export interface ConfigurationData<Meta extends Record<string, unknown> = Record<string, unknown>> {
/**
* The level of logs to render.
*
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface ConfigurationData<Meta extends Record<string, any> = Record<str
/**
* Configuration for the logger.
*/
export interface Configuration<Meta extends Record<string, any> = Record<string, any>>
export interface Configuration<Meta extends Record<string, unknown> = Record<string, unknown>>
extends ConfigurationData<Meta> {
/**
* Applies middleware to execute along with the log.
Expand Down Expand Up @@ -115,7 +115,6 @@ export interface Filters {
* The log levels to filter.
*/
export type LevelSelector =
| '*'
| string
| number
| string[]
Expand Down Expand Up @@ -145,9 +144,8 @@ export interface FilterConfig<T = string[]> {
/**
* Partial configuration provided by the user.
*/
export type UserConfiguration<Meta extends Record<string, any> = Record<string, any>> = Partial<
Configuration<Meta>
>;
export type UserConfiguration<Meta extends Record<string, unknown> = Record<string, unknown>> =
Partial<Configuration<Meta>>;

/**
* Extended configuration if the user specifies a the formatter as "common".
Expand Down
8 changes: 2 additions & 6 deletions src/_types/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,12 @@ export type IObject = Record<string, unknown>;
/**
* Type for the constructor of a Formatter class.
*/
export interface FormatterConstructor {
new (cfg: Configuration, level: LevelConfiguration): Formatter;
}
export type FormatterConstructor = new (cfg: Configuration, level: LevelConfiguration) => Formatter;

/**
* Type for the constructor of an adze class.
*/
export interface AdzeConstructor {
new (cfg: UserConfiguration, modifierData?: ModifierData): adze;
}
export type AdzeConstructor = new (cfg: UserConfiguration, modifierData?: ModifierData) => adze;

export interface ModifierData {
method?: Method;
Expand Down
13 changes: 3 additions & 10 deletions src/adze-global.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Configuration,
Label,
LabelMap,
LevelSelector,
LogListener,
UserConfiguration,
} from './_types';
import { Label, LabelMap, LevelSelector, LogListener, UserConfiguration } from './_types';
import { defaultConfiguration } from './constants';
import { normalizeLevelSelector } from './functions';
import Log from './log';
Expand All @@ -18,7 +11,7 @@ import Tools from './tools';
*/
type ListenersMap = Map<number, Map<number, LogListener>>;

export default class AdzeGlobal<Meta extends Record<string, any> = Record<string, any>> {
export default class AdzeGlobal<Meta extends Record<string, unknown> = Record<string, unknown>> {
/**
* Global Adze configuration overrides.
*/
Expand Down Expand Up @@ -122,7 +115,7 @@ export default class AdzeGlobal<Meta extends Record<string, any> = Record<string
{ ...defaultConfiguration.levels, ...(this.config.levels ?? {}) },
levels
);
normalizedLevels.forEach((level) => {
normalizedLevels.forEach((level: number) => {
if (this._levelsToListeners.has(level)) {
const levelContainer = this._levelsToListeners.get(level) as Map<number, LogListener>;
levelContainer.set(id, listener);
Expand Down
32 changes: 16 additions & 16 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,103 +30,103 @@ export class Configuration implements IConfiguration {
}

public get activeLevel(): Level | number {
return this.glblCfg?.activeLevel ?? this.logCfg?.activeLevel ?? dfltCfg.activeLevel;
return this.glblCfg?.activeLevel ?? this.logCfg.activeLevel ?? dfltCfg.activeLevel;
}

public set activeLevel(level: Level | number) {
this.logCfg.activeLevel = level;
}

public get cache(): boolean {
return this.glblCfg?.cache ?? this.logCfg?.cache ?? dfltCfg.cache;
return this.glblCfg?.cache ?? this.logCfg.cache ?? dfltCfg.cache;
}

public set cache(value: boolean) {
this.logCfg.cache = value;
}

public get cacheSize(): number {
return this.glblCfg?.cacheSize ?? this.logCfg?.cacheSize ?? dfltCfg.cacheSize;
return this.glblCfg?.cacheSize ?? this.logCfg.cacheSize ?? dfltCfg.cacheSize;
}

public set cacheSize(size: number) {
this.logCfg.cacheSize = size;
}

public get dump(): boolean {
return this.glblCfg?.dump ?? this.logCfg?.dump ?? dfltCfg.dump;
return this.glblCfg?.dump ?? this.logCfg.dump ?? dfltCfg.dump;
}

public set dump(value: boolean) {
this.logCfg.dump = value;
}

public get meta(): Record<string, any> {
return { ...this.logCfg?.meta, ...this.glblCfg?.meta };
public get meta(): Record<string, unknown> {
return { ...this.logCfg.meta, ...this.glblCfg?.meta };
}

public set meta(value: Record<string, any>) {
public set meta(value: Record<string, unknown>) {
this.logCfg.meta = value;
}

public get silent(): boolean {
return this.glblCfg?.silent ?? this.logCfg?.silent ?? dfltCfg.silent;
return this.glblCfg?.silent ?? this.logCfg.silent ?? dfltCfg.silent;
}

public set silent(value: boolean) {
this.logCfg.silent = value;
}

public get showTimestamp(): boolean {
return this.glblCfg?.showTimestamp ?? this.logCfg?.showTimestamp ?? dfltCfg.showTimestamp;
return this.glblCfg?.showTimestamp ?? this.logCfg.showTimestamp ?? dfltCfg.showTimestamp;
}

public set showTimestamp(value: boolean) {
this.logCfg.showTimestamp = value;
}

public get withEmoji(): boolean {
return this.glblCfg?.withEmoji ?? this.logCfg?.withEmoji ?? dfltCfg.withEmoji;
return this.glblCfg?.withEmoji ?? this.logCfg.withEmoji ?? dfltCfg.withEmoji;
}

public set withEmoji(value: boolean) {
this.logCfg.withEmoji = value;
}

public get format(): string {
return this.glblCfg?.format ?? this.logCfg?.format ?? dfltCfg.format;
return this.glblCfg?.format ?? this.logCfg.format ?? dfltCfg.format;
}

public set format(value: string) {
this.logCfg.format = value;
}

public get levels(): Record<string, LevelConfiguration> {
return { ...dfltCfg.levels, ...(this.logCfg?.levels ?? {}), ...(this.glblCfg?.levels ?? {}) };
return { ...dfltCfg.levels, ...(this.logCfg.levels ?? {}), ...(this.glblCfg?.levels ?? {}) };
}

public set levels(value: Record<string, LevelConfiguration>) {
this.logCfg.levels = value;
}

public get middleware(): Middleware[] | undefined {
return [...(this.glblCfg?.middleware ?? []), ...(this.logCfg?.middleware ?? [])];
return [...(this.glblCfg?.middleware ?? []), ...(this.logCfg.middleware ?? [])];
}

public set middleware(value: Middleware[] | undefined) {
this.logCfg.middleware = value;
}

public get filters(): Filters | undefined {
return this.glblCfg?.filters ?? this.logCfg?.filters;
return this.glblCfg?.filters ?? this.logCfg.filters;
}

public set filters(value: Filters | undefined) {
this.logCfg.filters = value;
}

public get timestampFormatter(): TimestampFormatter | undefined {
return this.glblCfg?.timestampFormatter ?? this.logCfg?.timestampFormatter;
return this.glblCfg?.timestampFormatter ?? this.logCfg.timestampFormatter;
}

public set timestampFormatter(value: TimestampFormatter | undefined) {
Expand All @@ -136,7 +136,7 @@ export class Configuration implements IConfiguration {
public get formatters(): Record<string, FormatterConstructor> {
return {
...dfltCfg.formatters,
...(this.logCfg?.formatters ?? {}),
...(this.logCfg.formatters ?? {}),
...(this.glblCfg?.formatters ?? {}),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CommonFormatter from './formatters/common';
import JsonFormatter from './formatters/json';
import PrettyFormatter from './formatters/pretty';
import StandardFormatter from './formatters/standard/standard';
import { isChrome, isSafari } from './functions/global';
import { isChrome, isSafari } from './functions';

/**
* All valid log terminators. These are the terminators that can be called to end a log chain.
Expand Down
19 changes: 14 additions & 5 deletions src/demo_funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import adze, {
isBrowser,
} from './index';
import { StandardLogFormatMeta } from './formatters/standard/types';
import Log from './log';

if (isBrowser()) {
// @ts-ignore
// @ts-expect-error Setting adze to the window if in the browser
window.adze = adze;
}

Expand Down Expand Up @@ -60,6 +61,7 @@ async function runDemo() {
listener();
}

// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
async function performance() {
setup({
withEmoji: true,
Expand Down Expand Up @@ -95,7 +97,7 @@ async function performance() {
teardown();
}

async function defaultLevels() {
function defaultLevels() {
setup({
withEmoji: true,
activeLevel: 1337,
Expand Down Expand Up @@ -350,13 +352,19 @@ function counting() {

function tests() {
// @ts-expect-error Intentional error to test the assertion
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
adze.assert(2 === 4).log('This is a failed assertion!');
// @ts-expect-error Intentional error to test the assertion
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
adze.withEmoji.assert(2 === 4).log('This is a failed assertion with emoji!');
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
adze.assert(4 === 4).log('This passed so it should not show!');
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
adze.if(2 === 2).log('This condition passed!');
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
adze.withEmoji.if(2 === 2).log('This condition passed with emoji!');
// @ts-expect-error Intentional error to test the assertion
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
adze.if(2 === 4).log('This condition failed so it should not show!');
}

Expand Down Expand Up @@ -533,7 +541,7 @@ async function json() {
},
req_id: '12345',
req: await serializeRequest(request, true),
res: await serializeResponse(response),
res: serializeResponse(response),
latency: 4444444,
})
.ns('foobar', 'baz')
Expand All @@ -552,8 +560,8 @@ async function json() {

function listener() {
const store = setup();
const id = store.addListener('*', (log) => {
// console.log(log.data);
const id = store.addListener('*', (log: Log) => {
console.log('Log level logged from listener', log.data?.level);
});
adze.withEmoji.log('This is a log');
adze.ns('derp').log('This is a namespaced log');
Expand All @@ -562,6 +570,7 @@ function listener() {
teardown();
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function time() {
setup();
adze.timeNow.log('This is a time log');
Expand Down
18 changes: 7 additions & 11 deletions src/formatters/common/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Formatter from '../formatter';
import { Configuration } from '../../configuration';
import { LevelConfiguration, ModifierData } from '../../_types';
import { ModifierData } from '../../_types';
import { format } from 'date-fns/format';

/**
Expand All @@ -18,10 +17,6 @@ export default class CommonFormatter extends Formatter {
protected timestampFormatFunction: (date: Date) => string = (date: Date) =>
format(date, 'dd/MMM/yyyy:HH:mm:ss xx');

constructor(cfg: Configuration, level: LevelConfiguration) {
super(cfg, level);
}

/**
* Format the log message for the browser.
*/
Expand All @@ -42,16 +37,17 @@ export default class CommonFormatter extends Formatter {
* **Example:** 127.0.0.1 user-identifier frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326
*/
private formatMessage(_: ModifierData, timestamp: string, args: unknown[]): unknown[] {
if (this.cfg.meta?.hostname === undefined) {
if (this.cfg.meta.hostname === undefined) {
console.warn(
new Error(
"Adze: 'hostname' is required for the common log format. Please provide this value in your log's meta data."
)
);
}
const hostname = this.cfg.meta?.hostname;
const ident = this.cfg.meta?.ident ?? '-';
const user = this.cfg.meta?.user ?? '-';
return [`${hostname} ${ident} ${user} [${timestamp}] ${args[0]}`];
const hostname = this.cfg.meta.hostname as string;
const ident = (this.cfg.meta.ident as string | undefined) ?? '-';
const user = (this.cfg.meta.user as string | undefined) ?? '-';
const firstArg = args[0] as string;
return [`${hostname} ${ident} ${user} [${timestamp}] ${firstArg}`];
}
}
Loading

0 comments on commit 1a8b632

Please sign in to comment.