Skip to content

Commit c3daaa8

Browse files
committed
using instanceof instead of Map
1 parent 0beaa72 commit c3daaa8

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { extractVariant } from './extract-variant.js';
33

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

812
type Constructor<T> = new (...args: any) => T;
913
type ConstructorsFromInstances<U> = U extends any ? Constructor<U> : never;
10-
type GenericFunction<U> = U extends any
11-
? { prototype: unknown; name: string }
12-
: never;
1314
type SlangPolymorphicNode = Extract<SlangAstNode, { variant: unknown }>;
1415

1516
type NonterminalVariantFactory<
@@ -26,16 +27,15 @@ export function createNonterminalVariantSimpleCreator<
2627
T extends StrictPolymorphicNode
2728
>(
2829
constructors: [
29-
GenericFunction<U['variant']>,
30+
SlangAstNodeConstructors,
3031
ConstructorsFromInstances<T['variant']>
3132
][]
3233
): NonterminalVariantFactory<U, T> {
33-
const variantConstructors = new Map(constructors);
34-
3534
return (variant, collected, options?) => {
36-
const constructor = variantConstructors.get(variant.constructor);
37-
if (constructor !== undefined)
38-
return new constructor(variant, collected, options);
35+
for (const [instance, constructor] of constructors) {
36+
if (variant instanceof instance)
37+
return new constructor(variant, collected, options);
38+
}
3939

4040
throw new Error(`Unexpected variant: ${JSON.stringify(variant)}`);
4141
};
@@ -46,23 +46,23 @@ export function createNonterminalVariantCreator<
4646
T extends StrictPolymorphicNode
4747
>(
4848
constructors: [
49-
GenericFunction<U['variant']>,
49+
SlangAstNodeConstructors,
5050
ConstructorsFromInstances<T['variant']>
5151
][],
5252
extractVariantConstructors: [
53-
GenericFunction<SlangPolymorphicNode>,
53+
SlangAstNodeConstructors,
5454
ConstructorsFromInstances<StrictPolymorphicNode>
5555
][]
5656
): NonterminalVariantFactory<U, T> {
5757
const simpleCreator = createNonterminalVariantSimpleCreator<U, T>(
5858
constructors
5959
);
60-
const extractVariantConstructor = new Map(extractVariantConstructors);
6160

6261
return (variant, collected, options?) => {
63-
const constructor = extractVariantConstructor.get(variant.constructor);
64-
if (constructor !== undefined)
65-
return extractVariant(new constructor(variant, collected, options));
62+
for (const [instance, constructor] of extractVariantConstructors) {
63+
if (variant instanceof instance)
64+
return extractVariant(new constructor(variant, collected, options));
65+
}
6666

6767
return simpleCreator(variant, collected, options);
6868
};

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ type KeyOfAst = keyof TypeOfAst;
3232
type ValuesOf<E> = E[keyof E];
3333

3434
type SlangAstNode = { [k in KeyOfAst]: ValuesOf<TypeOfAst[k]> }[KeyOfAst];
35+
type SlangAstNodeConstructors = { [k in KeyOfAst]: TypeOfAst[k] }[KeyOfAst];

0 commit comments

Comments
 (0)