From c486366abd0da0207319bc268c3f8fd2904541fb Mon Sep 17 00:00:00 2001 From: Jitse De Smet Date: Wed, 15 Jan 2025 15:49:16 +0100 Subject: [PATCH] remove ts expect errors in core lib --- .../engine-sparql-1-2/lib/Sparql12parser.ts | 1 + .../core/lib/grammar-builder/builderTypes.ts | 5 +- .../core/lib/grammar-builder/parserBuilder.ts | 138 ++++++++---------- .../grammar-builder/parserBuilder.types.ts | 14 +- 4 files changed, 74 insertions(+), 84 deletions(-) diff --git a/engines/engine-sparql-1-2/lib/Sparql12parser.ts b/engines/engine-sparql-1-2/lib/Sparql12parser.ts index e762c8c..8c8c383 100644 --- a/engines/engine-sparql-1-2/lib/Sparql12parser.ts +++ b/engines/engine-sparql-1-2/lib/Sparql12parser.ts @@ -56,6 +56,7 @@ export const sparql12ParserBuilder = Builder.createBuilder(sparql11ParserBuilder .patchRule(S12.builtInCall) .patchRule(S12.rdfLiteral); + export class Sparql12Parser implements ISparqlParser { private readonly parser: { queryOrUpdate: (input: string) => SparqlQuery; diff --git a/packages/core/lib/grammar-builder/builderTypes.ts b/packages/core/lib/grammar-builder/builderTypes.ts index 0c0407f..a68235b 100644 --- a/packages/core/lib/grammar-builder/builderTypes.ts +++ b/packages/core/lib/grammar-builder/builderTypes.ts @@ -12,14 +12,15 @@ export type CheckOverlap = T & U extends never ? V : W; */ export type RuleListToObject< T extends readonly RuleDef[], + Names extends string = RuleNamesFromList, Agg extends Record = Record, > = T extends readonly [infer First, ...infer Rest] ? ( First extends RuleDef ? ( Rest extends readonly RuleDef[] ? ( - RuleListToObject + RuleListToObject ) : never ) : never -) : Agg; +) : RuleDefMap & Agg; export type ParserFromRules> = { [K in Names]: RuleDefs[K] extends RuleDef ? (...args: ARGS) => RET : never diff --git a/packages/core/lib/grammar-builder/parserBuilder.ts b/packages/core/lib/grammar-builder/parserBuilder.ts index 8b8a7e0..fbca022 100644 --- a/packages/core/lib/grammar-builder/parserBuilder.ts +++ b/packages/core/lib/grammar-builder/parserBuilder.ts @@ -1,4 +1,3 @@ -/* eslint-disable ts/ban-ts-comment */ import type { ILexerConfig, IParserConfig } from '@chevrotain/types'; import type {IToken, TokenType, TokenVocabulary} from 'chevrotain'; import { EmbeddedActionsParser, Lexer } from 'chevrotain'; @@ -22,18 +21,19 @@ function listToRuleDefMap(rules: T): RuleListToObj } export class Builder> { + /** + * Create a builder with from some initial grammar rules or an existing builder. + * If a builder is provided, a new copy will be created. + */ public static createBuilder< Rules extends readonly RuleDef[] = RuleDef[], Names extends string = RuleNamesFromList, - // @ts-expect-error TS2344 RuleDefs extends RuleDefMap = RuleListToObject, -// eslint-disable-next-line antfu/consistent-list-newline >(start: Rules | Builder): Builder { if (start instanceof Builder) { return new Builder({ ...start.rules }); } - // @ts-expect-error TS2344 - return new Builder(listToRuleDefMap(start)); + return > new Builder(listToRuleDefMap(start)); } private rules: RuleDefs; @@ -42,50 +42,61 @@ export class Builder> { this.rules = startRules; } + /** + * Change the implementation of an existing grammar rule. + */ public patchRule(patch: RuleDef): - // @ts-expect-error TS2344 - Builder : RuleDefs[Key] }> { - // @ts-expect-error TS2322 - this.rules[patch.name] = patch; - // @ts-expect-error TS2344 - return & Record>>> this; + Builder : (RuleDefs[Key] extends RuleDef ? RuleDefs[Key] : never) }> { + const self = : (RuleDefs[Key] extends RuleDef ? RuleDefs[Key] : never) }>> + this; + self.rules[patch.name] = patch; + return self; } + /** + * Add a rule to the grammar. If the rule already exists, but the implementation differs, an error will be thrown. + */ public addRuleRedundant(rule: RuleDef): - // @ts-expect-error TS2344 - Builder : RuleDefs[K] }> { - const rules = > this.rules; + Builder : ( K extends Names ? (RuleDefs[K] extends RuleDef ? RuleDefs[K] : never ) : never) }> { + const self = : ( K extends Names ? (RuleDefs[K] extends RuleDef ? RuleDefs[K] : never ) : never) }>> + this; + const rules = > self.rules; if (rules[rule.name] !== undefined && rules[rule.name] !== rule) { throw new Error(`Rule ${rule.name} already exists in the builder`); } - // @ts-expect-error TS2536 - this.rules[rule.name] = rule; - // @ts-expect-error TS2536 - return >>> this; + rules[rule.name] = rule; + return self; } + /** + * Add a rule to the grammar. Will raise a typescript error if the rule already exists in the grammar. + */ public addRule( rule: CheckOverlap>, - // @ts-expect-error TS2536 - ): Builder : RuleDefs[K] }> { + ): Builder : ( K extends Names ? (RuleDefs[K] extends RuleDef ? RuleDefs[K] : never ) : never) }> { return this.addRuleRedundant(rule); } public addMany( ...rules: CheckOverlap, Names, U> - // @ts-expect-error TS2536 - ): Builder, RuleDefs & RuleListToObject> { + ): Builder< + Names | RuleNamesFromList, + {[K in Names | RuleNamesFromList]: + K extends keyof RuleListToObject ? ( + RuleListToObject[K] extends RuleDef ? RuleListToObject[K] : never + ) : ( + K extends Names ? (RuleDefs[K] extends RuleDef ? RuleDefs[K] : never) : never + ) + }> { this.rules = { ...this.rules, ...listToRuleDefMap(rules) }; - // @ts-expect-error TS2536 - return , RuleDefs & RuleListToObject>> this; + return this; } public deleteRule(ruleName: U): - // @ts-expect-error TS2536 - Builder, Omit> { + Builder, { [K in Exclude]: RuleDefs[K] extends RuleDef ? RuleDefs[K] : never }> { delete this.rules[ruleName]; - // @ts-expect-error TS2536 - return , Omit>> this; + return , { [K in Exclude]: RuleDefs[K] extends RuleDef ? RuleDefs[K] : never }>> + this; } public merge, OW extends readonly RuleDef[]>( @@ -94,11 +105,14 @@ export class Builder> { ): Builder< Names | OtherNames | RuleNamesFromList, - // @ts-expect-error TS2344 - {[K in Names | OtherNames | RuleNamesFromList]: K extends Names | OtherNames ? ( - K extends Names ? RuleDefs[K] : (K extends OtherNames ? OtherRules[K] : never) - ) : (K extends keyof RuleListToObject ? RuleListToObject[K] : never) } - > { + {[K in Names | OtherNames | RuleNamesFromList]: + K extends keyof RuleListToObject ? ( + RuleListToObject[K] extends RuleDef ? RuleListToObject[K] : never + ) + : ( + K extends Names ? (RuleDefs[K] extends RuleDef ? RuleDefs[K] : never) + : K extends OtherNames ? (OtherRules[K] extends RuleDef ? OtherRules[K] : never) : never + )}> { // Assume the other grammar is bigger than yours. So start from that one and add this one const otherRules: Record = { ...builder.rules }; const myRules: Record = this.rules; @@ -121,10 +135,8 @@ export class Builder> { } } - // @ts-expect-error TS2322 - this.rules = otherRules; - // @ts-expect-error TS2322 - return this; + this.rules = otherRules; + return this; } public consumeToParser({ tokenVocabulary, parserConfig = {}, lexerConfig = {}}: { @@ -144,8 +156,7 @@ export class Builder> { const selfSufficientParser: Partial> = {}; // eslint-disable-next-line ts/no-unnecessary-type-assertion for (const rule of []> Object.values(this.rules)) { - // @ts-expect-error TS7053 - selfSufficientParser[rule.name] = (input: string, ...args: unknown[]) => { + selfSufficientParser[rule.name] = ((input: string, ...args: unknown[]) => { // Transform input in accordance to 19.2 input = input.replaceAll( /\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{8})/gu, @@ -179,7 +190,7 @@ export class Builder> { ${parser.errors.map(x => `${x.token.startLine}: ${x.message}`).join('\n')}`); } return result; - }; + }); } return > selfSufficientParser; } @@ -226,8 +237,7 @@ ${parser.errors.map(x => `${x.token.startLine}: ${x.message}`).join('\n')}`); }; for (const rule of Object.values(>rules)) { - // @ts-expect-error TS7053 - this[rule.name] = this.RULE(rule.name, rule.impl(implArgs)); + this[ rule.name] = this.RULE(rule.name, rule.impl(implArgs)); } this.performSelfAnalysis(); } @@ -319,99 +329,77 @@ ${parser.errors.map(x => `${x.token.startLine}: ${x.message}`).join('\n')}`); ACTION: func => this.ACTION(func), BACKTRACK: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.BACKTRACK(this[cstDef.name], { ARGS: args }); + return this.BACKTRACK( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE(this[cstDef.name], { ARGS: args }); + return this.SUBRULE( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE1: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE1(this[cstDef.name], { ARGS: args }); + return this.SUBRULE1( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE2: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE2(this[cstDef.name], { ARGS: args }); + return this.SUBRULE2( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE3: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE3(this[cstDef.name], { ARGS: args }); + return this.SUBRULE3( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE4: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE4(this[cstDef.name], { ARGS: args }); + return this.SUBRULE4( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE5: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE5(this[cstDef.name], { ARGS: args }); + return this.SUBRULE5( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE6: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE6(this[cstDef.name], { ARGS: args }); + return this.SUBRULE6( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE7: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE7(this[cstDef.name], { ARGS: args }); + return this.SUBRULE7( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE8: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE8(this[cstDef.name], { ARGS: args }); + return this.SUBRULE8( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } }, SUBRULE9: (cstDef, ...args) => { try { - // @ts-expect-error TS7053 - // eslint-disable-next-line ts/no-unsafe-argument - return this.SUBRULE9(this[cstDef.name], { ARGS: args }); + return this.SUBRULE9( this[ cstDef.name], { ARGS: args }); } catch (error: unknown) { throw error; } diff --git a/packages/core/test/grammar-builder/parserBuilder.types.ts b/packages/core/test/grammar-builder/parserBuilder.types.ts index ad52de4..4f391cb 100644 --- a/packages/core/test/grammar-builder/parserBuilder.types.ts +++ b/packages/core/test/grammar-builder/parserBuilder.types.ts @@ -1,4 +1,4 @@ -import { describe, it } from 'vitest'; +import {describe, it} from 'vitest'; import {Builder, RuleDef} from '../../lib'; const RuleA: RuleDef<'apple', 'apple'> = undefined; @@ -9,27 +9,27 @@ describe('parserBuilder', () => { describe('types', () => { it('builder constructor', () => { expectTypeOf(Builder.createBuilder( [ RuleA ])) - .toEqualTypeOf>(); + .branded.toEqualTypeOf>(); expectTypeOf(Builder.createBuilder( [ RuleB ])) - .toEqualTypeOf>(); + .branded.toEqualTypeOf>(); expectTypeOf(Builder.createBuilder( [ RuleA, RuleB ])) - .toEqualTypeOf>(); + .branded.toEqualTypeOf>(); // AddRule expectTypeOf(Builder.createBuilder( [ RuleA, RuleB ]).addRule(RuleC)) - .toEqualTypeOf>(); // Merge expectTypeOf( Builder.createBuilder( [ RuleA ]). merge(Builder.createBuilder( [ RuleB ]), []) - ).toEqualTypeOf>(); + ).branded.toEqualTypeOf>(); expectTypeOf( Builder.createBuilder( [ RuleA, RuleB ]). merge(Builder.createBuilder( [ RuleB, RuleC ]), []) - ).toEqualTypeOf>(); }); });