diff --git a/common/changes/@typespec/compiler/fix-unsupported-default-location_2023-04-11-17-15.json b/common/changes/@typespec/compiler/fix-unsupported-default-location_2023-04-11-17-15.json new file mode 100644 index 00000000000..67a64cb01dc --- /dev/null +++ b/common/changes/@typespec/compiler/fix-unsupported-default-location_2023-04-11-17-15.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@typespec/compiler", + "comment": "**Fix** `unsupported-default` diagnostic showing at the wrong location", + "type": "none" + } + ], + "packageName": "@typespec/compiler" +} \ No newline at end of file diff --git a/packages/compiler/core/checker.ts b/packages/compiler/core/checker.ts index ce5a0f5e944..68b095b3e32 100644 --- a/packages/compiler/core/checker.ts +++ b/packages/compiler/core/checker.ts @@ -2978,8 +2978,8 @@ export function createChecker(program: Program): Checker { reportCheckerDiagnostic( createDiagnostic({ code: "unsupported-default", - format: { type: type.kind }, - target: defaultType, + format: { type: defaultType.kind }, + target: defaultNode, }) ); return errorType; diff --git a/packages/compiler/test/checker/model.test.ts b/packages/compiler/test/checker/model.test.ts index 376d17f5deb..0f9eda35b52 100644 --- a/packages/compiler/test/checker/model.test.ts +++ b/packages/compiler/test/checker/model.test.ts @@ -6,6 +6,7 @@ import { createTestHost, expectDiagnosticEmpty, expectDiagnostics, + extractCursor, TestHost, } from "../../testing/index.js"; @@ -149,6 +150,21 @@ describe("compiler: models", () => { } }); + it(`emit diagnostic when using non value type as default value`, async () => { + const { source, pos } = extractCursor(` + model Foo { + prop?: string = ┆D; + } + `); + testHost.addTypeSpecFile("main.tsp", source); + const diagnostics = await testHost.diagnose("main.tsp"); + expectDiagnostics(diagnostics, { + code: "unsupported-default", + message: "Default must be have a value type but has type 'TemplateParameter'.", + pos, + }); + }); + it(`doesn't emit unsupported-default diagnostic when type is an error`, async () => { testHost.addTypeSpecFile( "main.tsp",