diff --git a/src/index.ts b/src/index.ts index 235452a..e199813 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import { RegExpParser } from "./parser" import { RegExpValidator } from "./validator" import { RegExpVisitor } from "./visitor" +export { RegExpSyntaxError } from "./regexp-syntax-error" export { AST, RegExpParser, RegExpValidator } /** diff --git a/src/regexp-syntax-error.ts b/src/regexp-syntax-error.ts index ccd51cf..19a707c 100644 --- a/src/regexp-syntax-error.ts +++ b/src/regexp-syntax-error.ts @@ -3,27 +3,34 @@ import type { RegExpValidatorSourceContext } from "./validator" export class RegExpSyntaxError extends SyntaxError { public index: number - public constructor( - srcCtx: RegExpValidatorSourceContext, - flags: { unicode: boolean; unicodeSets: boolean }, - index: number, - message: string, - ) { - let source = "" - if (srcCtx.kind === "literal") { - const literal = srcCtx.source.slice(srcCtx.start, srcCtx.end) - if (literal) { - source = `: ${literal}` - } - } else if (srcCtx.kind === "pattern") { - const pattern = srcCtx.source.slice(srcCtx.start, srcCtx.end) - const flagsText = `${flags.unicode ? "u" : ""}${ - flags.unicodeSets ? "v" : "" - }` - source = `: /${pattern}/${flagsText}` - } - - super(`Invalid regular expression${source}: ${message}`) + public constructor(message: string, index: number) { + super(message) this.index = index } } + +export function newRegExpSyntaxError( + srcCtx: RegExpValidatorSourceContext, + flags: { unicode: boolean; unicodeSets: boolean }, + index: number, + message: string, +): RegExpSyntaxError { + let source = "" + if (srcCtx.kind === "literal") { + const literal = srcCtx.source.slice(srcCtx.start, srcCtx.end) + if (literal) { + source = `: ${literal}` + } + } else if (srcCtx.kind === "pattern") { + const pattern = srcCtx.source.slice(srcCtx.start, srcCtx.end) + const flagsText = `${flags.unicode ? "u" : ""}${ + flags.unicodeSets ? "v" : "" + }` + source = `: /${pattern}/${flagsText}` + } + + return new RegExpSyntaxError( + `Invalid regular expression${source}: ${message}`, + index, + ) +} diff --git a/src/validator.ts b/src/validator.ts index 7f73cde..41901c9 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -1,7 +1,7 @@ import type { EcmaVersion } from "./ecma-versions" import { latestEcmaVersion } from "./ecma-versions" import { Reader } from "./reader" -import { RegExpSyntaxError } from "./regexp-syntax-error" +import { newRegExpSyntaxError } from "./regexp-syntax-error" import { ASTERISK, BACKSPACE, @@ -1246,7 +1246,7 @@ export class RegExpValidator { message: string, context?: { index?: number; unicode?: boolean; unicodeSets?: boolean }, ): never { - throw new RegExpSyntaxError( + throw newRegExpSyntaxError( this._srcCtx!, { unicode: