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

src/index.ts

Lines changed: 1 addition & 0 deletions
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
/**

src/regexp-syntax-error.ts

Lines changed: 28 additions & 21 deletions
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+
}

src/validator.ts

Lines changed: 2 additions & 2 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy