Skip to content

Commit

Permalink
typescript: Fix invalid identifier names in functions and interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ewlsh committed Feb 27, 2024
1 parent 6f71161 commit 78a5a87
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ class ModuleGenerator extends FormatGenerator<string[]> {
}
}

// TODO: Check if this is necessary anywhere else...
if (isInvalid(name) && !isGlobal) {
name = `["${name}"]`
}

const inParamsDef: string[] = this.generateInParameters(inParams)

def.push(
Expand All @@ -692,7 +697,7 @@ class ModuleGenerator extends FormatGenerator<string[]> {
)})${retSep} ${returnType}`,
)

// Add overloaded methods
// TODO: Add overloaded methods
// if (overloads && tsFunction.overloads.length > 0) {
// def.push(...this.addInfoComment(`Overloads of ${name}`, indentCount))
// for (const func of tsFunction.overloads) {
Expand Down
10 changes: 10 additions & 0 deletions packages/lib/src/gir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { IntrospectedProperty, IntrospectedField } from './gir/property.js'
import { GenerationOptions } from './types.js'
import { sanitizeIdentifierName } from './gir/util.js'

export { sanitizeMemberName, isInvalid } from './gir/util.js'

export {
IntrospectedBase,
Options as IntrospectedOptions,
Expand Down Expand Up @@ -57,6 +59,14 @@ export class TypeIdentifier extends TypeExpression {
return type
}

/**
* TODO: gi.ts didn't deal with sanitizing types but probably should have to avoid
* invalid names such as "3gppProfile"
*/
sanitize() {
return new TypeIdentifier(sanitizeIdentifierName(this.namespace, this.name), this.namespace)
}

protected _resolve(namespace: IntrospectedNamespace, options: GenerationOptions): TypeIdentifier | null {
const name: string = sanitizeIdentifierName(null, this.name)
const ns_name = this.namespace
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/gir/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,14 @@ export function parseTypeExpression(modName: string, type: string): TypeExpressi
const baseType = parseTypeString(type);

if (baseType.namespace) {
return new TypeIdentifier(baseType.name, baseType.namespace);
return new TypeIdentifier(baseType.name, baseType.namespace).sanitize();
} else {
const primitiveType = resolvePrimitiveType(baseType.name);

if (primitiveType !== null) {
return primitiveType;
} else {
return new TypeIdentifier(baseType.name, modName);
return new TypeIdentifier(baseType.name, modName).sanitize();
}
}
}
Expand Down

0 comments on commit 78a5a87

Please sign in to comment.