From 6aad4a795f7dc4f686e83453bf237b80d03295e0 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 22 Jul 2024 15:25:40 -0700 Subject: [PATCH] Add `const` template parameter to get the precise lib type (#3933) Without that there is some loss of information --- .chronus/changes/lib-const-2024-6-22-22-3-25.md | 8 ++++++++ .chronus/changes/lib-const-2024-6-22-22-3-26.md | 10 ++++++++++ packages/compiler/src/core/library.ts | 6 +++--- packages/compiler/src/core/param-message.ts | 2 +- .../http-server-javascript/src/common/reference.ts | 2 +- packages/http/src/payload.ts | 2 +- packages/http/test/multipart.test.ts | 2 +- packages/protobuf/src/transform/index.ts | 2 +- 8 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 .chronus/changes/lib-const-2024-6-22-22-3-25.md create mode 100644 .chronus/changes/lib-const-2024-6-22-22-3-26.md diff --git a/.chronus/changes/lib-const-2024-6-22-22-3-25.md b/.chronus/changes/lib-const-2024-6-22-22-3-25.md new file mode 100644 index 0000000000..c17edf07da --- /dev/null +++ b/.chronus/changes/lib-const-2024-6-22-22-3-25.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Add `const` template parameter to get the precise lib type diff --git a/.chronus/changes/lib-const-2024-6-22-22-3-26.md b/.chronus/changes/lib-const-2024-6-22-22-3-26.md new file mode 100644 index 0000000000..e6c475b297 --- /dev/null +++ b/.chronus/changes/lib-const-2024-6-22-22-3-26.md @@ -0,0 +1,10 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/http-server-javascript" + - "@typespec/http" + - "@typespec/protobuf" +--- + +Fix some diagnostic not showing the right message diff --git a/packages/compiler/src/core/library.ts b/packages/compiler/src/core/library.ts index a8025ef17a..e9041ea85a 100644 --- a/packages/compiler/src/core/library.ts +++ b/packages/compiler/src/core/library.ts @@ -63,9 +63,9 @@ function createStateKeys( * const lib = createTypeSpecLibrary(libDef); */ export function createTypeSpecLibrary< - T extends { [code: string]: DiagnosticMessages }, - E extends Record, - State extends string = never, + const T extends { [code: string]: DiagnosticMessages }, + const E extends Record, + const State extends string = never, >(lib: Readonly>): TypeSpecLibrary { let emitterOptionValidator: JSONSchemaValidator; diff --git a/packages/compiler/src/core/param-message.ts b/packages/compiler/src/core/param-message.ts index df35803a69..8e8e5365ca 100644 --- a/packages/compiler/src/core/param-message.ts +++ b/packages/compiler/src/core/param-message.ts @@ -1,6 +1,6 @@ import type { CallableMessage } from "./types.js"; -export function paramMessage( +export function paramMessage( strings: readonly string[], ...keys: T ): CallableMessage { diff --git a/packages/http-server-javascript/src/common/reference.ts b/packages/http-server-javascript/src/common/reference.ts index 7feeb3d69c..e6af69fa18 100644 --- a/packages/http-server-javascript/src/common/reference.ts +++ b/packages/http-server-javascript/src/common/reference.ts @@ -253,7 +253,7 @@ export function emitTypeReference( default: reportDiagnostic(ctx.program, { code: "unrecognized-intrinsic", - format: { name: (type satisfies never as IntrinsicType).name }, + format: { intrinsic: (type satisfies never as IntrinsicType).name }, target: position, }); return "unknown"; diff --git a/packages/http/src/payload.ts b/packages/http/src/payload.ts index fda7a5536a..1beeeeef08 100644 --- a/packages/http/src/payload.ts +++ b/packages/http/src/payload.ts @@ -345,7 +345,7 @@ function resolveMultiPartBodyFromTuple( diagnostics.add( createDiagnostic({ code: "multipart-invalid-content-type", - format: { contentType, valid: multipartContentTypesValues.join(", ") }, + format: { contentType, supportedContentTypes: multipartContentTypesValues.join(", ") }, target: type, }) ); diff --git a/packages/http/test/multipart.test.ts b/packages/http/test/multipart.test.ts index 19989fd379..735a2e8e27 100644 --- a/packages/http/test/multipart.test.ts +++ b/packages/http/test/multipart.test.ts @@ -13,7 +13,7 @@ it("emit diagnostic when using invalid content type for multipart ", async () => expectDiagnostics(diagnostics, { code: "@typespec/http/multipart-invalid-content-type", message: - "Content type 'application/json' is not a multipart content type. Supported content types are: .", + "Content type 'application/json' is not a multipart content type. Supported content types are: multipart/form-data, multipart/mixed.", }); }); diff --git a/packages/protobuf/src/transform/index.ts b/packages/protobuf/src/transform/index.ts index 8a17febf32..1d506a9ebf 100644 --- a/packages/protobuf/src/transform/index.ts +++ b/packages/protobuf/src/transform/index.ts @@ -442,7 +442,7 @@ function tspToProto(program: Program, emitterOptions: ProtobufEmitterOptions): P reportDiagnostic(program, { code: "unsupported-intrinsic", - format: { type: t.name }, + format: { name: t.name }, target: t, });