microsoft / microsoft/typespec

Consider a helper for naming template instantiations in emitters automatically.

Open
#2,862 1 comment 0 reactions 0 assignees View on GitHub
design:accepted triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

Currently if a templated type doesn't have a friendlyName, the logic for computing a good alternative name is complex enough to warrant a helper function. I came up with this kind of logic for protobuf:

```ts
function getModelName(model: Model): string {
const friendlyName = getFriendlyName(program, model);

if (friendlyName) return capitalize(friendlyName);

const templateArguments = isTemplateInstance(model) ? model.templateMapper!.args : [];

const prefix = templateArguments
.map(function getTypePrefixName(arg, idx) {
if ("name" in arg && typeof arg.name === "string" && arg.name !== "")
return capitalize(arg.name!);
else {
reportDiagnostic.once(program, {
code: "unspeakable-template-argument",
// TODO/witemple - I'd rather attach the diagnostic to the template argument, but it's the best I can do for
// now to attach it to the model itself.
target: model,
format: {
name: model.name,
},
});

return `T${idx}`;
}
})
.join("");

return prefix + capitalize(model.name);
}
```

This seems like the kind of thing where we could take an opinion on a good default and provide a helper for it.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.