Skip to content

Commit

Permalink
feat: improve types to fix parent type of string Unicode properties (
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Aug 25, 2023
1 parent 6a8c538 commit bc63c4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
6 changes: 6 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ export interface CharacterUnicodePropertyCharacterSet
/** StringsUnicodePropertyCharacterSet is Unicode property escape with property of strings. */
export interface StringsUnicodePropertyCharacterSet
extends BaseUnicodePropertyCharacterSet {
parent:
| Alternative
| ClassIntersection
| ClassSubtraction
| Quantifier
| UnicodeSetsCharacterClass
strings: true
value: null
negate: false
Expand Down
35 changes: 10 additions & 25 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
ClassSubtraction,
UnicodeSetsCharacterClassElement,
ClassSetOperand,
UnicodePropertyCharacterSet,
UnicodeSetsCharacterClass,
ExpressionCharacterClass,
StringAlternative,
Expand Down Expand Up @@ -413,45 +412,31 @@ class RegExpParserState {
strings: boolean,
): void {
const parent = this._node
if (
(parent.type !== "Alternative" &&
parent.type !== "CharacterClass") ||
(strings && (negate || value))
) {
if (parent.type !== "Alternative" && parent.type !== "CharacterClass") {
throw new Error("UnknownError")
}

const base = {
type: "CharacterSet",
parent,
start,
end,
raw: this.source.slice(start, end),
kind,
strings,
key,
} as const
const node: UnicodePropertyCharacterSet = strings
? {
...base,
value: null,
negate: false,
strings: true,
}
: {
...base,
value,
negate,
strings: false,
}

if (node.strings) {
if (parent.type === "CharacterClass" && !parent.unicodeSets) {
if (strings) {
if (
(parent.type === "CharacterClass" && !parent.unicodeSets) ||
negate ||
value !== null
) {
throw new Error("UnknownError")
}
parent.elements.push(node)

parent.elements.push({ ...base, parent, strings, value, negate })
} else {
parent.elements.push(node)
parent.elements.push({ ...base, parent, strings, value, negate })
}
}

Expand Down

0 comments on commit bc63c4c

Please sign in to comment.