From 19117bd1923f72ebe72eff0d63d811691069aa97 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Tue, 11 Apr 2023 10:14:15 -0700 Subject: [PATCH 1/2] Fix diagnostic showing in wrong location --- packages/compiler/core/checker.ts | 4 ++-- packages/compiler/test/checker/model.test.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/compiler/core/checker.ts b/packages/compiler/core/checker.ts index c5ce4176cf0..66f4019d695 100644 --- a/packages/compiler/core/checker.ts +++ b/packages/compiler/core/checker.ts @@ -2974,8 +2974,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", From e4c1005f3d3d9cef12a034fd023345853a7ade6a Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Tue, 11 Apr 2023 10:15:06 -0700 Subject: [PATCH 2/2] Changelog --- ...-unsupported-default-location_2023-04-11-17-15.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/@typespec/compiler/fix-unsupported-default-location_2023-04-11-17-15.json 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