Skip to content

Commit 6b970e4

Browse files
committed
Initial work to support StringTemplates.
1 parent c52fb20 commit 6b970e4

3 files changed

Lines changed: 88 additions & 24 deletions

File tree

tools/apiview/emitters/typespec-apiview/package-lock.json

Lines changed: 2 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/apiview/emitters/typespec-apiview/src/apiview.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ import {
1313
IdentifierNode,
1414
InterfaceStatementNode,
1515
IntersectionExpressionNode,
16-
listServices,
1716
MemberExpressionNode,
1817
ModelExpressionNode,
1918
ModelPropertyNode,
2019
ModelSpreadPropertyNode,
2120
ModelStatementNode,
2221
Namespace,
2322
navigateProgram,
24-
NoTarget,
2523
NumericLiteralNode,
2624
OperationSignatureDeclarationNode,
2725
OperationSignatureReferenceNode,
2826
OperationStatementNode,
2927
Program,
3028
ScalarStatementNode,
3129
StringLiteralNode,
30+
StringTemplateExpressionNode,
31+
StringTemplateHeadNode,
32+
StringTemplateSpanNode,
3233
SyntaxKind,
3334
TemplateArgumentNode,
3435
TemplateParameterDeclarationNode,
@@ -43,7 +44,6 @@ import { ApiViewDiagnostic, ApiViewDiagnosticLevel } from "./diagnostic.js";
4344
import { ApiViewNavigation } from "./navigation.js";
4445
import { generateId, NamespaceModel } from "./namespace-model.js";
4546
import { LIB_VERSION } from "./version.js";
46-
import { reportDiagnostic } from "./lib.js";
4747

4848
const WHITESPACE = " ";
4949

@@ -633,9 +633,45 @@ export class ApiView {
633633
this.tokenize(obj.argument);
634634
}
635635
break;
636+
case SyntaxKind.StringTemplateExpression:
637+
obj = node as StringTemplateExpressionNode;
638+
const multiLine = (obj.head.value.includes("\n") || obj.spans.some(x => x.literal.value.includes("\n")))
639+
640+
if (multiLine) {
641+
this.punctuation(`"""`);
642+
this.newline();
643+
this.indent();
644+
} else {
645+
this.punctuation(`"`);
646+
}
647+
this.tokenize(obj.head);
648+
for (const span of obj.spans) {
649+
this.tokenize(span);
650+
}
651+
if (multiLine) {
652+
this.newline();
653+
this.deindent();
654+
this.punctuation(`"""`);
655+
} else {
656+
this.punctuation(`"`);
657+
}
658+
break;
659+
case SyntaxKind.StringTemplateSpan:
660+
obj = node as StringTemplateSpanNode;
661+
this.punctuation("${", false, false);
662+
this.tokenize(obj.expression);
663+
this.punctuation("}", false, false);
664+
this.tokenize(obj.literal);
665+
break;
666+
case SyntaxKind.StringTemplateHead:
667+
case SyntaxKind.StringTemplateMiddle:
668+
case SyntaxKind.StringTemplateTail:
669+
obj = node as StringTemplateHeadNode;
670+
this.literal(obj.value);
671+
break;
636672
default:
637673
// All Projection* cases should fall in here...
638-
throw new Error(`Case "${node.kind.toString()}" not implemented`);
674+
throw new Error(`Case "${SyntaxKind[node.kind].toString()}" not implemented`);
639675
}
640676
}
641677

tools/apiview/emitters/typespec-apiview/test/apiview.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,4 +693,50 @@ describe("apiview: tests", () => {
693693
validateDefinitionIds(apiview);
694694
});
695695
});
696+
697+
describe("string templates", () => {
698+
it("templates", async () => {
699+
const input = `
700+
@TypeSpec.service( { title: "Test", version: "1" } )
701+
namespace Azure.Test {
702+
alias myconst = "foobar";
703+
model Person {
704+
simple: "Simple \${123} end";
705+
multiline: """
706+
Multi
707+
\${123}
708+
\${true}
709+
line
710+
""";
711+
ref: "Ref this alias \${myconst} end";
712+
template: Template<"custom">;
713+
}
714+
alias Template<T extends valueof string> = "Foo \${T} bar";
715+
}`;
716+
717+
const expect = `
718+
namespace Azure.Test {
719+
model Person {
720+
simple: "Simple \${123} end";
721+
multiline: """
722+
Multi
723+
\${123}
724+
\${true}
725+
line
726+
""";
727+
ref: "Ref this alias \${myconst} end";
728+
template: Template<"custom">;
729+
}
730+
731+
alias myconst = "foobar";
732+
733+
alias Template<T extends valueof string> = "Foo \${T} bar";
734+
}
735+
`;
736+
const apiview = await apiViewFor(input, {});
737+
const lines = apiViewText(apiview);
738+
compare(expect, lines, 9);
739+
validateDefinitionIds(apiview);
740+
});
741+
});
696742
});

0 commit comments

Comments
 (0)