Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TSP APIView] Support constant statements #8587

Merged
merged 2 commits into from
Jul 9, 2024
Merged
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
1 change: 1 addition & 0 deletions tools/apiview/emitters/typespec-apiview/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions tools/apiview/emitters/typespec-apiview/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tools/apiview/emitters/typespec-apiview/src/apiview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AugmentDecoratorStatementNode,
BaseNode,
BooleanLiteralNode,
ConstStatementNode,
DecoratorExpressionNode,
DirectiveExpressionNode,
EnumMemberNode,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
DirectiveExpressionNode,
StringLiteralNode,
ObjectLiteralNode,
ConstStatementNode,
} from "@typespec/compiler";

export class NamespaceModel {
Expand Down Expand Up @@ -66,6 +67,7 @@ export class NamespaceModel {
>();
aliases = new Map<string, AliasStatementNode>();
augmentDecorators = new Array<AugmentDecoratorStatementNode>();
constants = new Array<ConstStatementNode>();

constructor(name: string, ns: Namespace, program: Program) {
this.name = name;
Expand Down Expand Up @@ -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));
Expand Down
25 changes: 25 additions & 0 deletions tools/apiview/emitters/typespec-apiview/test/apiview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});