Skip to content

feat: export RegExpSyntaxError #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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