Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach generic type parameters to the declaring type #2619

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading