Skip to content

Commit 8cd67ab

Browse files
committed
Always use additionaltypes for interfaces
Fixes #31 Stil uses _ in front of the additional types, this is on purpose tho, since the types aren't actually in the schemas, and could change at any time, so the user should be vary when using them.
1 parent ac04227 commit 8cd67ab

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/converter.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ const CTX_CMT_NOT_ALLOWED_IN = ['content', 'devtools'];
6868
const CTX_CMT_ALLOWED_IN = ['proxy'];
6969

7070
function pascalCase(s: string): string {
71-
return s.split('_').map(x => x.charAt(0).toUpperCase() + x.slice(1)).join('');
71+
return s
72+
.split('_')
73+
.map((x) => x.charAt(0).toUpperCase() + x.slice(1))
74+
.join('');
7275
}
7376

7477
// Formats an allowedContexts array to a readable string
@@ -495,7 +498,16 @@ export default class Converter {
495498
let properties = this.convertObjectProperties(type);
496499
// If it has no properties, just say it's some type of object
497500
if (properties.length > 0) {
498-
out += `{\n${properties.join(';\n')};\n}`;
501+
if (type.id && !root) {
502+
const typeName = `_${pascalCase(type.id!)}`;
503+
this.additionalTypes.push(
504+
`${commentFromSchema(type)}interface ${typeName} {\n${properties.join(';\n')};\n}`
505+
);
506+
// And then just reference it by name in output
507+
out += typeName;
508+
} else {
509+
out += `{\n${properties.join(';\n')};\n}`;
510+
}
499511
} else {
500512
out += 'object';
501513
}
@@ -665,11 +677,12 @@ export default class Converter {
665677
if (properties === undefined) return [];
666678
let convertedProperties = [];
667679
// For each property, just add it as a const, appending | undefined if it's optional
668-
for (let prop of Object.keys(properties)) {
680+
for (let [propName, prop] of Object.entries(properties)) {
681+
prop.id = propName;
669682
convertedProperties.push(
670-
`${commentFromSchema(properties[prop])}const ${prop}: ${this.convertType(
671-
properties[prop]
672-
)}${properties[prop].optional ? ' | undefined' : ''};`
683+
`${commentFromSchema(prop)}const ${propName}: ${this.convertType(prop)}${
684+
prop.optional ? ' | undefined' : ''
685+
};`
673686
);
674687
}
675688
return convertedProperties;
@@ -744,7 +757,7 @@ export default class Converter {
744757
if (callback) {
745758
// Remove callback from parameters as we're gonna handle it as a promise return
746759
func.parameters = func.parameters!.filter((x) => x !== callback);
747-
let parameters = this.convertParameters(callback.parameters, false, func.name);
760+
let parameters = this.convertParameters(callback.parameters, false, pascalCase(`${func.name}_return`));
748761
if (parameters.length > 1) {
749762
// Since these files are originally chrome, some things are a bit weird
750763
// Callbacks (which is what chrome uses) have no issues with returning multiple values

0 commit comments

Comments
 (0)