microsoft / microsoft/typespec

[Bug]: Mutators are hard to use to mutate nested types in the subgraph

Open
#7,003 0 comments 0 reactions 1 assignee Claimed by @timotheeguerin View on GitHub
compiler:core design:needed triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Describe the bug

In my scenario, I want to replace all enums with the equivalent union. So I implemented a mutator as follows:

```ts
export const EnumToUnion: unsafe_Mutator = {
name: "removeEnum",
Enum: {
filter: (type) => {
return unsafe_MutatorFlow.MutateAndRecur;
},
replace(en) {
return $.union.create({
variants: [...en.members.values()].map((enumMember) => {
return $.unionVariant.create({
type: $.literal.create(enumMember.value ?? enumMember.name),
});
}),
});
},
},
};
```

It finds enums and replaces them, but this replacement is somewhat pointless because the path to the enums are not cloned with the appropriate references updated. I have to add mutators for every type for this to work.

I would have expected the original mutator to work given that the documentation says the default MutatorFlow is MutateAndRecur. If the current behavior is what we want (which is nice in that it doesn't clone a TON of stuff if a leaf type is mutated), we need some way to traverse this subgraph, and ideally it works with the current pattern used all over emitter-framework (which just uses properties to unpack the types). Perhaps a realm-aware proxy is in order.

### Reproduction

.

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/Microsoft/typespec/discussions).
- [x] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

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.