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

Do not carry over friendly name with model is or op is #3793

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: breaking
packages:
- "@typespec/compiler"
---

Do not carry over `@friendlyName` with `model is` or `op is`

```tsp
@friendlyName("Abc{T}", T)
model Foo<T> {}
model Bar is Foo<string>;
// This can be changed to
model Abcstring is Foo<string>;
```
25 changes: 25 additions & 0 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,19 @@ import {
import { createDiagnostic, reportDiagnostic } from "../core/messages.js";
import { Program, ProjectedProgram } from "../core/program.js";
import {
AugmentDecoratorStatementNode,
DecoratorContext,
DecoratorExpressionNode,
Enum,
EnumMember,
Interface,
Model,
ModelProperty,
Namespace,
Node,
Operation,
Scalar,
SyntaxKind,
Type,
Union,
} from "../core/types.js";
Expand Down Expand Up @@ -1019,6 +1023,27 @@ export const $friendlyName: FriendlyNameDecorator = (
friendlyName: string,
sourceObject: Type | undefined
) => {
// workaround for current lack of functionality in compiler
// https://github.com/microsoft/typespec/issues/2717
if (target.kind === "Model" || target.kind === "Operation") {
if ((context.decoratorTarget as Node).kind === SyntaxKind.AugmentDecoratorStatement) {
if (
ignoreDiagnostics(
context.program.checker.resolveTypeReference(
(context.decoratorTarget as AugmentDecoratorStatementNode).targetType
)
)?.node !== target.node
) {
return;
}
}
if ((context.decoratorTarget as Node).kind === SyntaxKind.DecoratorExpression) {
if ((context.decoratorTarget as DecoratorExpressionNode).parent !== target.node) {
return;
}
}
}

// If an object was passed in, use it to format the friendly name
if (sourceObject) {
friendlyName = replaceTemplatedStringFromProperties(friendlyName, sourceObject);
Expand Down
17 changes: 13 additions & 4 deletions packages/compiler/test/decorators/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe("compiler: built-in decorators", () => {

describe("@friendlyName", () => {
it("applies @friendlyName on model", async () => {
const { A, B, C } = await runner.compile(`
const { A, B } = await runner.compile(`
@test
@friendlyName("MyNameIsA")
model A { }
Expand All @@ -363,13 +363,22 @@ describe("compiler: built-in decorators", () => {
model Templated<T> {
prop: T;
}
`);
strictEqual(getFriendlyName(runner.program, A), "MyNameIsA");
strictEqual(getFriendlyName(runner.program, B), "BModel");
});

it(" @friendlyName doesn't carry over to derived models", async () => {
const { A, B } = await runner.compile(`
@test
model C is Templated<B>{};
@friendlyName("MyNameIsA")
model A<T> { t: T; }

@test
model B is A<string> { }
`);
strictEqual(getFriendlyName(runner.program, A), "MyNameIsA");
strictEqual(getFriendlyName(runner.program, B), "BModel");
strictEqual(getFriendlyName(runner.program, C), "TemplatedB");
strictEqual(getFriendlyName(runner.program, B), undefined);
});
});

Expand Down
Loading