Skip to content

Commit

Permalink
Fix: Error out when using properties in array model (#2873)
Browse files Browse the repository at this point in the history
fix #2758

---------

Co-authored-by: Mark Cowlishaw <markcowl@microsoft.com>
  • Loading branch information
timotheeguerin and markcowl authored Feb 5, 2024
1 parent cc2723a commit fd4fdfb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-colts-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typespec/compiler": patch
---

Fix: Error out when using properties in array model
9 changes: 9 additions & 0 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,15 @@ export function createChecker(program: Program): Checker {
if (indexer === undefined) {
return;
}
if (indexer.key.name === "integer") {
reportCheckerDiagnostics([
createDiagnostic({
code: "no-array-properties",
target: diagnosticTarget,
}),
]);
return;
}

const [valid, diagnostics] = isTypeAssignableTo(
property.type,
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ const diagnostics = {
array: "Cannot intersect an array model.",
},
},
"no-array-properties": {
severity: "error",
messages: {
default: "Array models cannot have any properties.",
},
},
"intersect-duplicate-property": {
severity: "error",
messages: {
Expand Down
32 changes: 32 additions & 0 deletions packages/compiler/test/checker/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,38 @@ describe("compiler: models", () => {
strictEqual(A.indexer.value.kind, "Union");
});

it("model is array cannot have properties", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
@test model A is string[] {
prop: string;
}
`
);
const diagnostics = await testHost.diagnose("main.tsp");
expectDiagnostics(diagnostics, {
code: "no-array-properties",
message: "Array models cannot have any properties.",
});
});

it("model extends array cannot have properties", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
@test model A extends Array<string> {
prop: string;
}
`
);
const diagnostics = await testHost.diagnose("main.tsp");
expectDiagnostics(diagnostics, {
code: "no-array-properties",
message: "Array models cannot have any properties.",
});
});

it("doesn't allow duplicate properties", async () => {
testHost.addTypeSpecFile(
"main.tsp",
Expand Down

0 comments on commit fd4fdfb

Please sign in to comment.