Skip to content

Commit 6c52528

Browse files
fix: generation of mapping (GoogleChromeLabs#251)
1 parent d625fd2 commit 6c52528

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

tools/generateCommandMap.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
import {Project, Type, TypeFormatFlags} from 'ts-morph';
88
import * as path from 'path';
99
import {
10+
getResultNameFromMethod,
1011
getTypeInNamespaceOrThrow,
1112
type MappingInterface,
1213
type SpecType,
1314
} from './utils.ts';
1415

1516
const rootDir = path.resolve(import.meta.dirname, '..');
1617

18+
const MAIN_SPEC_PREFIX = 'Bidi';
19+
1720
const 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
}

tools/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ export function getTypeInNamespaceOrThrow(
5252

5353
throw new Error('Not found');
5454
}
55+
56+
export function getResultNameFromMethod(method: string) {
57+
const type = method
58+
.split('.')
59+
.map(s => s.charAt(0).toUpperCase() + s.slice(1))
60+
.join('.');
61+
return `${type}Result`;
62+
}

0 commit comments

Comments
 (0)