Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Don't report `non-literal-string-template` diagnostic when interpolating an invalid reference
15 changes: 9 additions & 6 deletions packages/compiler/src/core/helpers/string-template-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createDiagnosticCollector } from "../diagnostics.js";
import { createDiagnostic } from "../messages.js";
import { isErrorType } from "../type-utils.js";
import type { Diagnostic, StringTemplate } from "../types.js";

export function isStringTemplateSerializable(
Expand Down Expand Up @@ -35,12 +36,14 @@ export function explainStringTemplateNotSerializable(
}
// eslint-disable-next-line no-fallthrough
default:
diagnostics.add(
createDiagnostic({
code: "non-literal-string-template",
target: span,
}),
);
if (!isErrorType(span.type)) {
diagnostics.add(
createDiagnostic({
code: "non-literal-string-template",
target: span,
}),
);
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/compiler/test/checker/values/string-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ describe("string templates", () => {
"Value interpolated in this string template cannot be converted to a string. Only literal types can be automatically interpolated.",
});
});

it("only emit invalid-ref error when interpolating an invalid reference, not non-literal-string-template", async () => {
const diagnostics = await diagnoseValue(`string("Some \${bad}")`);
expectDiagnostics(diagnostics, {
code: "invalid-ref",
message: "Unknown identifier bad",
});
});
});

describe("validate literal are assignable", () => {
Expand Down
Loading