From bc63c4c5e4f7b1b5f7f5e62868aa732ae33c2f70 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Fri, 25 Aug 2023 09:03:59 +0200 Subject: [PATCH] feat: improve types to fix `parent` type of string Unicode properties (#127) --- src/ast.ts | 6 ++++++ src/parser.ts | 35 ++++++++++------------------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/ast.ts b/src/ast.ts index fcbba40..361a4fc 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -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 diff --git a/src/parser.ts b/src/parser.ts index b7a6560..dd273a2 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -16,7 +16,6 @@ import type { ClassSubtraction, UnicodeSetsCharacterClassElement, ClassSetOperand, - UnicodePropertyCharacterSet, UnicodeSetsCharacterClass, ExpressionCharacterClass, StringAlternative, @@ -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 }) } }