diff --git a/tools/apiview/emitters/typespec-apiview/CHANGELOG.md b/tools/apiview/emitters/typespec-apiview/CHANGELOG.md index 7f0f3c25013..589e4acafca 100644 --- a/tools/apiview/emitters/typespec-apiview/CHANGELOG.md +++ b/tools/apiview/emitters/typespec-apiview/CHANGELOG.md @@ -3,6 +3,7 @@ ## Version 0.4.9 (07-09-2024) Fix issue where "unknown" was rendered as "any". Support value syntax for objects and arrays. +Support const statements in namespaces. ## Version 0.4.8 (04-18-2024) Display suppressions in APIView. diff --git a/tools/apiview/emitters/typespec-apiview/package-lock.json b/tools/apiview/emitters/typespec-apiview/package-lock.json index 544db7bdcbb..a9acabdc9f2 100644 --- a/tools/apiview/emitters/typespec-apiview/package-lock.json +++ b/tools/apiview/emitters/typespec-apiview/package-lock.json @@ -1,12 +1,12 @@ { "name": "@azure-tools/typespec-apiview", - "version": "0.4.8", + "version": "0.4.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@azure-tools/typespec-apiview", - "version": "0.4.8", + "version": "0.4.9", "license": "MIT", "devDependencies": { "@azure-tools/typespec-azure-core": ">=0.43 <1.0", diff --git a/tools/apiview/emitters/typespec-apiview/src/apiview.ts b/tools/apiview/emitters/typespec-apiview/src/apiview.ts index 336d6f1d207..111eaa8b686 100644 --- a/tools/apiview/emitters/typespec-apiview/src/apiview.ts +++ b/tools/apiview/emitters/typespec-apiview/src/apiview.ts @@ -5,6 +5,7 @@ import { AugmentDecoratorStatementNode, BaseNode, BooleanLiteralNode, + ConstStatementNode, DecoratorExpressionNode, DirectiveExpressionNode, EnumMemberNode, @@ -446,6 +447,15 @@ export class ApiView { throw new Error(`Case "BlockComment" not implemented`); case SyntaxKind.TypeSpecScript: throw new Error(`Case "TypeSpecScript" not implemented`); + case SyntaxKind.ConstStatement: + obj = node as ConstStatementNode; + this.namespaceStack.push(obj.id.sv); + this.keyword("const", false, true); + this.tokenizeIdentifier(obj.id, "declaration"); + this.punctuation("=", true, true); + this.tokenize(obj.value); + this.namespaceStack.pop(); + break; case SyntaxKind.DecoratorExpression: obj = node as DecoratorExpressionNode; this.punctuation("@", false, false); @@ -1053,6 +1063,11 @@ export class ApiView { this.punctuation(";"); this.blankLines(1); } + for (const node of model.constants.values()) { + this.tokenize(node); + this.punctuation(";"); + this.blankLines(1); + } this.endGroup(); this.blankLines(1); this.namespaceStack.pop(); diff --git a/tools/apiview/emitters/typespec-apiview/src/namespace-model.ts b/tools/apiview/emitters/typespec-apiview/src/namespace-model.ts index 83de71b1c80..bc45a3e5865 100644 --- a/tools/apiview/emitters/typespec-apiview/src/namespace-model.ts +++ b/tools/apiview/emitters/typespec-apiview/src/namespace-model.ts @@ -32,6 +32,7 @@ import { DirectiveExpressionNode, StringLiteralNode, ObjectLiteralNode, + ConstStatementNode, } from "@typespec/compiler"; export class NamespaceModel { @@ -66,6 +67,7 @@ export class NamespaceModel { >(); aliases = new Map(); augmentDecorators = new Array(); + constants = new Array(); constructor(name: string, ns: Namespace, program: Program) { this.name = name; @@ -118,6 +120,11 @@ export class NamespaceModel { this.augmentDecorators.push(augment); } + // collect contants + for (const constant of findNodes(SyntaxKind.ConstStatement, program, ns)) { + this.constants.push(constant); + } + // sort operations and models this.operations = new Map([...this.operations].sort(caseInsensitiveSort)); this.resources = new Map([...this.resources].sort(caseInsensitiveSort)); diff --git a/tools/apiview/emitters/typespec-apiview/test/apiview.test.ts b/tools/apiview/emitters/typespec-apiview/test/apiview.test.ts index f107848f0b8..e38fc0ee244 100644 --- a/tools/apiview/emitters/typespec-apiview/test/apiview.test.ts +++ b/tools/apiview/emitters/typespec-apiview/test/apiview.test.ts @@ -932,4 +932,29 @@ describe("apiview: tests", () => { validateDefinitionIds(apiview); }); }); + + describe("constants", () => { + it("renders constants", async () => { + const input = ` + #suppress "deprecated" + @TypeSpec.service( { title: "Test", version: "1" } ) + namespace Azure.Test { + const a = 123; + const b = #{name: "abc"}; + const c = a; + } + `; + const expect = ` + namespace Azure.Test { + const a = 123; + const b = #{name: "abc"}; + const c = a; + } + `; + const apiview = await apiViewFor(input, {}); + const actual = apiViewText(apiview); + compare(expect, actual, 10); + validateDefinitionIds(apiview); + }); + }); });