Skip to content

Commit

Permalink
Attach generic type parameters to the declaring type (#2619)
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd authored Jun 19, 2024
1 parent d08ae51 commit f3bf565
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 169 deletions.
4 changes: 2 additions & 2 deletions compiler/src/model/build-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
for (const typeParameter of declaration.getTypeParameters()) {
type.generics = (type.generics ?? []).concat({
name: modelGenerics(typeParameter),
namespace: type.name.namespace
namespace: type.name.namespace + '.' + type.name.name
})
}

Expand Down Expand Up @@ -532,7 +532,7 @@ function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | Int
for (const typeParameter of declaration.getTypeParameters()) {
type.generics = (type.generics ?? []).concat({
name: modelGenerics(typeParameter),
namespace: type.name.namespace
namespace: type.name.namespace + '.' + type.name.name
})
}

Expand Down
5 changes: 3 additions & 2 deletions compiler/src/model/json-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ export default function buildJsonSpec (): Map<string, JsonSpec> {
const files = readdirSync(jsonSpecPath)
.filter(file => file.endsWith('.json'))

const map = new Map()
const map: Map<string, JsonSpec> = new Map()
for (const file of files) {
const json = require(join(jsonSpecPath, file))
const name = Object.keys(json)[0]
map.set(name, json[name])
}

return map
// Ensure deterministic ordering
return new Map([...map.entries()].sort((a, b) => a[0].localeCompare(b[0])))
}
18 changes: 16 additions & 2 deletions compiler/src/model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,20 @@ export function modelType (node: Node): model.ValueOf {
namespace: getNameSpace(node)
}
}

if (Node.isTypeParameterDeclaration(declaration)) {
const parent = declaration.getParent()
assert(
parent,
Node.isClassDeclaration(parent) ||
Node.isInterfaceDeclaration(parent) ||
Node.isTypeAliasDeclaration(parent),
'It should be a class, interface, or type alias declaration'
)

type.type.namespace = `${type.type.namespace}.${parent.getName() as string}`
}

return type
}
}
Expand Down Expand Up @@ -477,7 +491,7 @@ export function modelTypeAlias (declaration: TypeAliasDeclaration): model.TypeAl
assert(declaration, type != null, 'Type alias without a referenced type')
const generics = declaration.getTypeParameters().map(typeParameter => ({
name: modelGenerics(typeParameter),
namespace: getNameSpace(typeParameter)
namespace: getNameSpace(typeParameter) + '.' + declaration.getName()
}))

const alias = modelType(type)
Expand Down Expand Up @@ -717,7 +731,7 @@ export function hoistTypeAnnotations (type: model.TypeDefinition, jsDocs: JSDoc[
'@codegen_names must have the number of items as the union definition'
)
} else if (tag === 'es_quirk') {
type.esQuirk = value
type.esQuirk = value.replace(/\r/g, '')
} else {
assert(jsDocs, false, `Unhandled tag: '${tag}' with value: '${value}' on type ${type.name.name}`)
}
Expand Down
Loading

0 comments on commit f3bf565

Please sign in to comment.