Skip to content

Commit

Permalink
Fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jclem committed Oct 20, 2021
1 parent ce69edc commit 580a820
Show file tree
Hide file tree
Showing 8 changed files with 3,903 additions and 1,164 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root: true
parser: '@typescript-eslint/parser'
plugins: ['@typescript-eslint']
extends:
- eslint:recommended
- plugin:@typescript-eslint/eslint-recommended
- plugin:@typescript-eslint/recommended
- prettier
rules:
'@typescript-eslint/no-unused-vars':
- warn
- argsIgnorePattern: '^_'
varsIgnorePattern: '^_'
ignorePatterns:
- dist/
- node_modules/
4 changes: 2 additions & 2 deletions lib/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export function decode(line: string): Encodeable {
let tokenType: TokenType = TokenType.Garbage
let ch: string | undefined

let key: string = ''
let value: string = ''
let key = ''
let value = ''

while ((ch = scanner.next())) {
switch (tokenType) {
Expand Down
4 changes: 2 additions & 2 deletions lib/encode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Encodeable = {[key: string]: any}
export type Encodeable = {[key: string]: unknown}

export type EncodeOptions = {
encodeKeys?: boolean
Expand All @@ -22,7 +22,7 @@ export function encode(obj: Encodeable, opts: EncodeOptions = {}): string {
if (value == null) {
encodedValue = ''
} else {
encodedValue = encodeString(value.toString())
encodedValue = encodeString(String(value))
}

const keyString = opts.encodeKeys ? encodeString(key) : key
Expand Down
22 changes: 12 additions & 10 deletions lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {Encodeable} from '.'
import {encode} from './encode'

type LoggerOpts = {
stream?: NodeJS.WritableStream
context?: LoggerContext
context?: Encodeable
}

type LoggerContext = {[key: string]: any}

type Timers = {[key: string]: number}

/**
* An object with optional context state that logs to a stream
*/
export class Logger {
private context: LoggerContext
private context: Encodeable
private timers: Timers = {}
private readonly stream: NodeJS.WritableStream

Expand All @@ -22,7 +21,10 @@ export class Logger {
this.context = context || {}
}

static log(data: object, opts: {stream?: NodeJS.WritableStream} = {}): void {
static log(
data: Encodeable,
opts: {stream?: NodeJS.WritableStream} = {}
): void {
const stream = opts.stream || process.stdout
const encodedData = encode(data)
stream.write(`${encodedData}\n`)
Expand All @@ -31,14 +33,14 @@ export class Logger {
/**
* Append new data to the logger's context.
*/
appendContext(newContext: object): void {
appendContext(newContext: Encodeable): void {
this.context = this.mergeContext(newContext)
}

/**
* Log a message to the logger's stream.
*/
log(data: object = {}): void {
log(data: Encodeable = {}): void {
const timersObj = this.getTimersState()
const encodedData = encode(this.mergeContext(timersObj, data))
this.stream.write(`${encodedData}\n`)
Expand All @@ -53,7 +55,7 @@ export class Logger {
* Note that this creates a new logger and immediately logs to this logger's
* stream. Running timers, etc. are not included.
*/
logError(error: Error, data: object = {}): void {
logError(error: Error, data: Encodeable = {}): void {
const context = this.merge(this.context, data)
const logger = new Logger({stream: this.stream, context})
const errorId = this.pseudorandomId()
Expand Down Expand Up @@ -93,11 +95,11 @@ export class Logger {
return timers
}

private merge(...data: object[]): object {
private merge(...data: Encodeable[]): Encodeable {
return Object.assign({}, ...data)
}

private mergeContext(...data: object[]): object {
private mergeContext(...data: Encodeable[]): Encodeable {
return Object.assign({}, this.context, ...data)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/string-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Scans over a string, character by character
*/
export default class StringScanner {
pos: number = 0
pos = 0

constructor(private readonly data: string) {}

Expand Down
Loading

0 comments on commit 580a820

Please sign in to comment.