From 2a163f92bd1715db1f8d91fb25c263d33b7e8248 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sat, 23 Nov 2024 08:49:42 +0530 Subject: [PATCH] feat: create type for the parser options --- src/parser.ts | 12 ++++++++++-- src/types.ts | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 1caeb70..6c28a92 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -14,7 +14,15 @@ import { parse, StackFrame as ESFrame } from 'error-stack-parser-es' import debug from './debug.js' import { SourceFile } from './source_file.js' -import type { Chunk, ParsedError, Parser, SourceLoader, StackFrame, Transformer } from './types.js' +import type { + Chunk, + Parser, + StackFrame, + Transformer, + ParsedError, + SourceLoader, + YouchParserOptions, +} from './types.js' /** * ErrorParser exposes the API to parse an thrown value and extract @@ -81,7 +89,7 @@ export class ErrorParser { */ #transformers: Transformer[] = [] - constructor(options?: { offset?: number; frameSourceBuffer?: number }) { + constructor(options?: YouchParserOptions) { options = options ?? {} this.#offset = options.offset } diff --git a/src/types.ts b/src/types.ts index fb29a90..a5ac9c3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -120,3 +120,22 @@ export type Parser = (source: unknown) => any * error value. */ export type Transformer = (error: ParsedError, source: unknown) => void | Promise + +/** + * Options accepted by the Youch parser + */ +export type YouchParserOptions = { + /** + * Define the offset to skip certain stack frames from + * the top + */ + offset?: number + + /** + * Number of lines of code to display for the error stack frame. + * For example: If you set the frameSourceBuffer=7, then 3 lines + * above the error line and 3 lines after the error line will + * be displayed. + */ + frameSourceBuffer?: number +}