77import { Project , Type , TypeFormatFlags } from 'ts-morph' ;
88import * as path from 'path' ;
99import {
10+ getResultNameFromMethod ,
1011 getTypeInNamespaceOrThrow ,
1112 type MappingInterface ,
1213 type SpecType ,
1314} from './utils.ts' ;
1415
1516const rootDir = path . resolve ( import . meta. dirname , '..' ) ;
1617
18+ const MAIN_SPEC_PREFIX = 'Bidi' ;
19+
1720const specs : SpecType [ ] = [
1821 {
1922 inputFile : './main.ts' ,
2023 commandType : 'CommandData' ,
21- modulePrefix : 'Bidi' ,
24+ modulePrefix : MAIN_SPEC_PREFIX ,
2225 } ,
2326 {
2427 inputFile : './permissions.ts' ,
@@ -76,7 +79,17 @@ for (const spec of specs) {
7679 TypeFormatFlags . None ,
7780 ) ;
7881
79- let expectedResultTypeName = paramsTypeString . replace ( 'Params' , 'Result' ) ;
82+ let prefix = spec . modulePrefix ;
83+ let expectedResultTypeName = paramsTypeString . replace (
84+ 'Parameters' ,
85+ 'Result' ,
86+ ) ;
87+
88+ // We need to infer from methods
89+ // TODO: See if this is needed as a fallback always
90+ if ( paramsTypeString . includes ( 'Extensible' ) ) {
91+ expectedResultTypeName = getResultNameFromMethod ( methodString ) ;
92+ }
8093
8194 try {
8295 // Usually we get something like `BrowsingContext.GetTreeResult`
@@ -86,6 +99,8 @@ for (const spec of specs) {
8699 // Maybe it was not inside an Namespace try on the module scope
87100 apiIndexFile . getTypeAliasOrThrow ( expectedResultTypeName ) ;
88101 } catch {
102+ // The EmptyResult is only available on the main spec
103+ prefix = MAIN_SPEC_PREFIX ;
89104 // Default to EmptyResult
90105 expectedResultTypeName = `EmptyResult` ;
91106 }
@@ -94,7 +109,7 @@ for (const spec of specs) {
94109 commandMappingEntries . push ( {
95110 method : methodString ,
96111 params : `${ spec . modulePrefix } .${ paramsTypeString } ` ,
97- resultType : `${ spec . modulePrefix } .${ expectedResultTypeName } ` ,
112+ resultType : `${ prefix } .${ expectedResultTypeName } ` ,
98113 } ) ;
99114 }
100115}
0 commit comments