Skip to content

Commit 4c06173

Browse files
committed
we don't need to store a string as a key anymore, we can directly use the constructor from the Slang object
1 parent a58e7b9 commit 4c06173

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

src/slang-utils/create-nonterminal-variant-creator.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,49 @@
22
import { extractVariant } from './extract-variant.js';
33

44
import type { ParserOptions } from 'prettier';
5-
import type { AstNode, StrictPolymorphicNode } from '../slang-nodes/types.d.ts';
5+
import type {
6+
AstNode,
7+
StrictAstNode,
8+
StrictPolymorphicNode
9+
} from '../slang-nodes/types.d.ts';
610
import type { CollectedMetadata, SlangAstNode } from '../types.d.ts';
711

8-
type Constructor<T = StrictPolymorphicNode> = new (...args: any) => T;
12+
type Constructor<T = StrictAstNode> = new (...args: any) => T;
913
type ConstructorsFromInstances<U> = U extends any ? Constructor<U> : never;
14+
type TypeofFromInstances<U> = U extends any
15+
? { prototype: unknown; name: string }
16+
: never;
1017

1118
export function createNonterminalVariantCreator<
1219
T extends StrictPolymorphicNode,
1320
U extends Extract<SlangAstNode, { variant: unknown }>
1421
>(
15-
constructors: [{ name: string }, ConstructorsFromInstances<T['variant']>][],
22+
constructors: [
23+
TypeofFromInstances<U>,
24+
ConstructorsFromInstances<T['variant']>
25+
][],
1626
constructorsWithVariants?: [
17-
{ name: string },
27+
TypeofFromInstances<Extract<SlangAstNode, { variant: unknown }>>,
1828
ConstructorsFromInstances<StrictPolymorphicNode>
1929
][]
2030
) {
21-
const variantConstructors = new Map(
22-
constructors.map(([key, constructor]) => [key.name, constructor])
23-
);
31+
const variantConstructors = new Map(constructors);
2432

2533
if (constructorsWithVariants === undefined) {
2634
return (
2735
variant: U['variant'],
2836
collected: CollectedMetadata,
2937
options?: ParserOptions<AstNode>
3038
): T['variant'] => {
31-
const constructor = variantConstructors.get(variant.constructor.name);
39+
const constructor = variantConstructors.get(variant.constructor);
3240
if (constructor !== undefined)
3341
return new constructor(variant, collected, options);
3442

3543
throw new Error(`Unexpected variant: ${JSON.stringify(variant)}`);
3644
};
3745
}
3846

39-
const variantWithVariantsConstructors = new Map(
40-
constructorsWithVariants.map(([key, constructor]) => [
41-
key.name,
42-
constructor
43-
])
44-
);
47+
const variantWithVariantsConstructors = new Map(constructorsWithVariants);
4548

4649
return (
4750
variant: U['variant'] | StrictPolymorphicNode,
@@ -50,11 +53,11 @@ export function createNonterminalVariantCreator<
5053
): T['variant'] => {
5154
let constructor:
5255
| ConstructorsFromInstances<T['variant'] | StrictPolymorphicNode>
53-
| undefined = variantConstructors.get(variant.constructor.name);
56+
| undefined = variantConstructors.get(variant.constructor);
5457
if (constructor !== undefined)
5558
return new constructor(variant, collected, options);
5659

57-
constructor = variantWithVariantsConstructors.get(variant.constructor.name);
60+
constructor = variantWithVariantsConstructors.get(variant.constructor);
5861
if (constructor !== undefined)
5962
return extractVariant(new constructor(variant, collected, options));
6063

0 commit comments

Comments
 (0)