Skip to content

Commit

Permalink
feat: export RegExpSyntaxError (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Oct 25, 2023
1 parent 3cb60cc commit 2e8f1af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

/**
Expand Down
49 changes: 28 additions & 21 deletions src/regexp-syntax-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
4 changes: 2 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2e8f1af

Please sign in to comment.