22import { extractVariant } from './extract-variant.js' ;
33
44import 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' ;
610import 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 ;
913type ConstructorsFromInstances < U > = U extends any ? Constructor < U > : never ;
14+ type TypeofFromInstances < U > = U extends any
15+ ? { prototype : unknown ; name : string }
16+ : never ;
1017
1118export 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