diff --git a/common/changes/@typespec/compiler/fix-scalar-template-param-scope_2023-04-10-16-19.json b/common/changes/@typespec/compiler/fix-scalar-template-param-scope_2023-04-10-16-19.json new file mode 100644 index 00000000000..d93de2304c1 --- /dev/null +++ b/common/changes/@typespec/compiler/fix-scalar-template-param-scope_2023-04-10-16-19.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@typespec/compiler", + "comment": "Fix: `scalar` template parameter name conflict with each other", + "type": "none" + } + ], + "packageName": "@typespec/compiler" +} diff --git a/packages/compiler/core/binder.ts b/packages/compiler/core/binder.ts index ee60f4dd55e..e51b4a1345d 100644 --- a/packages/compiler/core/binder.ts +++ b/packages/compiler/core/binder.ts @@ -572,6 +572,7 @@ export function createBinder(program: Program): Binder { function hasScope(node: Node): node is ScopeNode { switch (node.kind) { case SyntaxKind.ModelStatement: + case SyntaxKind.ScalarStatement: case SyntaxKind.AliasStatement: case SyntaxKind.TypeSpecScript: case SyntaxKind.InterfaceStatement: diff --git a/packages/compiler/test/checker/scalar.test.ts b/packages/compiler/test/checker/scalar.test.ts index f9a08750539..facacdca37a 100644 --- a/packages/compiler/test/checker/scalar.test.ts +++ b/packages/compiler/test/checker/scalar.test.ts @@ -48,6 +48,20 @@ describe("compiler: scalars", () => { strictEqual(A.name, "A"); }); + // https://github.com/microsoft/typespec/issues/1764 + it("template parameter are scoped to the scalar", async () => { + const { A, B } = await runner.compile(` + @test @doc(T) scalar A; + @test @doc(T) scalar B; + + alias AIns = A<"">; + alias BIns = B<"">; + `); + + strictEqual(A.kind, "Scalar" as const); + strictEqual(B.kind, "Scalar" as const); + }); + describe("custom scalars and default values", () => { it("allows custom numeric scalar to have a default value", async () => { const { S, M } = await runner.compile(`