Skip to content

Commit 2e8f1af

Browse files
authored
feat: export RegExpSyntaxError (#144)
1 parent 3cb60cc commit 2e8f1af

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

Diff for: src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RegExpParser } from "./parser"
33
import { RegExpValidator } from "./validator"
44
import { RegExpVisitor } from "./visitor"
55

6+
export { RegExpSyntaxError } from "./regexp-syntax-error"
67
export { AST, RegExpParser, RegExpValidator }
78

89
/**

Diff for: src/regexp-syntax-error.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,34 @@ import type { RegExpValidatorSourceContext } from "./validator"
33
export class RegExpSyntaxError extends SyntaxError {
44
public index: number
55

6-
public constructor(
7-
srcCtx: RegExpValidatorSourceContext,
8-
flags: { unicode: boolean; unicodeSets: boolean },
9-
index: number,
10-
message: string,
11-
) {
12-
let source = ""
13-
if (srcCtx.kind === "literal") {
14-
const literal = srcCtx.source.slice(srcCtx.start, srcCtx.end)
15-
if (literal) {
16-
source = `: ${literal}`
17-
}
18-
} else if (srcCtx.kind === "pattern") {
19-
const pattern = srcCtx.source.slice(srcCtx.start, srcCtx.end)
20-
const flagsText = `${flags.unicode ? "u" : ""}${
21-
flags.unicodeSets ? "v" : ""
22-
}`
23-
source = `: /${pattern}/${flagsText}`
24-
}
25-
26-
super(`Invalid regular expression${source}: ${message}`)
6+
public constructor(message: string, index: number) {
7+
super(message)
278
this.index = index
289
}
2910
}
11+
12+
export function newRegExpSyntaxError(
13+
srcCtx: RegExpValidatorSourceContext,
14+
flags: { unicode: boolean; unicodeSets: boolean },
15+
index: number,
16+
message: string,
17+
): RegExpSyntaxError {
18+
let source = ""
19+
if (srcCtx.kind === "literal") {
20+
const literal = srcCtx.source.slice(srcCtx.start, srcCtx.end)
21+
if (literal) {
22+
source = `: ${literal}`
23+
}
24+
} else if (srcCtx.kind === "pattern") {
25+
const pattern = srcCtx.source.slice(srcCtx.start, srcCtx.end)
26+
const flagsText = `${flags.unicode ? "u" : ""}${
27+
flags.unicodeSets ? "v" : ""
28+
}`
29+
source = `: /${pattern}/${flagsText}`
30+
}
31+
32+
return new RegExpSyntaxError(
33+
`Invalid regular expression${source}: ${message}`,
34+
index,
35+
)
36+
}

Diff for: src/validator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { EcmaVersion } from "./ecma-versions"
22
import { latestEcmaVersion } from "./ecma-versions"
33
import { Reader } from "./reader"
4-
import { RegExpSyntaxError } from "./regexp-syntax-error"
4+
import { newRegExpSyntaxError } from "./regexp-syntax-error"
55
import {
66
ASTERISK,
77
BACKSPACE,
@@ -1246,7 +1246,7 @@ export class RegExpValidator {
12461246
message: string,
12471247
context?: { index?: number; unicode?: boolean; unicodeSets?: boolean },
12481248
): never {
1249-
throw new RegExpSyntaxError(
1249+
throw newRegExpSyntaxError(
12501250
this._srcCtx!,
12511251
{
12521252
unicode:

0 commit comments

Comments
 (0)