Skip to content

Commit

Permalink
isErrorModel check for base models (#2968)
Browse files Browse the repository at this point in the history
fix #2957
  • Loading branch information
timotheeguerin authored Mar 1, 2024
1 parent fa8b959 commit 8c7c76b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/is-error-model-2024-1-29-0-44-17.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: feature
packages:
- "@typespec/compiler"
---

Any subtype of an error(marked with `@error`) is now an error.
18 changes: 15 additions & 3 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,28 @@ const errorKey = createStateSymbol("error");

/**
* `@error` decorator marks a model as an error type.
*
* `@error` can only be specified on a model.
* Any derived models (using extends) will also be seen as error types.
*/
export function $error(context: DecoratorContext, entity: Model) {
validateDecoratorUniqueOnNode(context, entity, $error);
context.program.stateSet(errorKey).add(entity);
}

/**
* Check if the type is an error model or a descendant of an error model.
*/
export function isErrorModel(program: Program, target: Type): boolean {
return program.stateSet(errorKey).has(target);
if (target.kind !== "Model") {
return false;
}
let current: Model | undefined = target;
while (current) {
if (program.stateSet(errorKey).has(current)) {
return true;
}
current = current.baseModel;
}
return false;
}

// -- @format decorator ---------------------
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler/test/decorators/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ describe("compiler: built-in decorators", () => {
ok(isErrorModel(runner.program, A), "isError should be true");
});

it("applies @error on derived models", async () => {
const { B, C } = await runner.compile(`
@error model A { }
@test model B extends A { }
@test model C extends B { }
`);
ok(isErrorModel(runner.program, B), "isError should be true");
ok(isErrorModel(runner.program, C), "isError should be true");
});

it("emit diagnostic if error is not applied to a model", async () => {
const diagnostics = await runner.diagnose(`
@error
Expand Down

0 comments on commit 8c7c76b

Please sign in to comment.