@alloy-js/csharp 0.18.0-dev.8 → 0.18.0-dev.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,22 @@
1
- import { expect, it } from "vitest";
1
+ import { describe, expect, it } from "vitest";
2
2
  import {
3
3
  DocC,
4
4
  DocCode,
5
5
  DocComment,
6
- DocCompletionList,
7
6
  DocDescription,
8
7
  DocExample,
9
8
  DocException,
10
9
  DocInclude,
11
- DocItem,
12
10
  DocList,
13
11
  DocPara,
14
12
  DocParam,
15
13
  DocParamRef,
16
14
  DocPermission,
17
15
  DocRemarks,
18
- DocResponse,
19
16
  DocReturns,
20
17
  DocSee,
21
18
  DocSeeAlso,
22
19
  DocSummary,
23
- DocTerm,
24
20
  DocTypeParam,
25
21
  DocTypeParamRef,
26
22
  DocValue,
@@ -56,9 +52,7 @@ it("define c", () => {
56
52
  <DocC>inline code</DocC>
57
53
  </DocComment>,
58
54
  ).toRenderTo(`
59
- /// <c>
60
- /// inline code
61
- /// </c>
55
+ /// <c>inline code</c>
62
56
  `);
63
57
  });
64
58
 
@@ -89,7 +83,7 @@ it("define exception", () => {
89
83
  it("define include", () => {
90
84
  expect(
91
85
  <DocComment>
92
- <>{DocInclude({ file: "external.xml", path: "/doc/summary" })}</>
86
+ <DocInclude file="external.xml" path="/doc/summary" />
93
87
  </DocComment>,
94
88
  ).toRenderTo(`
95
89
  /// <include file="external.xml" path="/doc/summary" />
@@ -164,66 +158,20 @@ it("define permission", () => {
164
158
  `);
165
159
  });
166
160
 
167
- it("define response", () => {
168
- expect(
169
- <DocComment>
170
- <DocResponse>response info</DocResponse>
171
- </DocComment>,
172
- ).toRenderTo(`
173
- /// <response>
174
- /// response info
175
- /// </response>
176
- `);
177
- });
178
-
179
- it("define completionlist", () => {
180
- expect(
181
- <DocComment>
182
- <DocCompletionList>completion list</DocCompletionList>
183
- </DocComment>,
184
- ).toRenderTo(`
185
- /// <completionlist>
186
- /// completion list
187
- /// </completionlist>
188
- `);
189
- });
190
-
191
161
  it("define list", () => {
192
162
  expect(
193
163
  <DocComment>
194
- <DocList>list content</DocList>
164
+ <DocList type="bullet" items={["item 1", "item 2", "item 3"]} />
195
165
  </DocComment>,
196
166
  ).toRenderTo(`
197
- /// <list>
198
- /// list content
167
+ /// <list type="bullet">
168
+ /// <item><description>item 1</description></item>
169
+ /// <item><description>item 2</description></item>
170
+ /// <item><description>item 3</description></item>
199
171
  /// </list>
200
172
  `);
201
173
  });
202
174
 
203
- it("define item", () => {
204
- expect(
205
- <DocComment>
206
- <DocItem>item content</DocItem>
207
- </DocComment>,
208
- ).toRenderTo(`
209
- /// <item>
210
- /// item content
211
- /// </item>
212
- `);
213
- });
214
-
215
- it("define term", () => {
216
- expect(
217
- <DocComment>
218
- <DocTerm>term content</DocTerm>
219
- </DocComment>,
220
- ).toRenderTo(`
221
- /// <term>
222
- /// term content
223
- /// </term>
224
- `);
225
- });
226
-
227
175
  it("define description", () => {
228
176
  expect(
229
177
  <DocComment>
@@ -248,40 +196,93 @@ it("define para", () => {
248
196
  `);
249
197
  });
250
198
 
251
- it("define see", () => {
252
- expect(
253
- <DocComment>
254
- <>{DocSee({ cref: "T:MyType" })}</>
255
- </DocComment>,
256
- ).toRenderTo(`
257
- /// <see cref="T:MyType" />
258
- `);
199
+ describe("define see", () => {
200
+ it("with cref", () => {
201
+ expect(
202
+ <DocComment>
203
+ <DocSee cref="T:MyType" />
204
+ </DocComment>,
205
+ ).toRenderTo(`
206
+ /// <see cref="T:MyType" />
207
+ `);
208
+ });
209
+
210
+ it("with href", () => {
211
+ expect(
212
+ <DocComment>
213
+ <DocSee href="https://github.com" />
214
+ </DocComment>,
215
+ ).toRenderTo(`
216
+ /// <see href="https://github.com" />
217
+ `);
218
+ });
219
+ it("with href and children", () => {
220
+ expect(
221
+ <DocComment>
222
+ <DocSee href="https://github.com">GitHub</DocSee>
223
+ </DocComment>,
224
+ ).toRenderTo(`
225
+ /// <see href="https://github.com">GitHub</see>
226
+ `);
227
+ });
228
+
229
+ it("with langword", () => {
230
+ expect(
231
+ <DocComment>
232
+ <DocSee langword="keyword" />
233
+ </DocComment>,
234
+ ).toRenderTo(`
235
+ /// <see langword="keyword" />
236
+ `);
237
+ });
259
238
  });
260
239
 
261
- it("define seealso", () => {
262
- expect(
263
- <DocComment>
264
- <>{DocSeeAlso({ cref: "T:OtherType" })}</>
265
- </DocComment>,
266
- ).toRenderTo(`
267
- /// <seealso cref="T:OtherType" />
268
- `);
240
+ describe("define seealso", () => {
241
+ it("with cref", () => {
242
+ expect(
243
+ <DocComment>
244
+ <DocSeeAlso cref="T:OtherType" />
245
+ </DocComment>,
246
+ ).toRenderTo(`
247
+ /// <seealso cref="T:OtherType" />
248
+ `);
249
+ });
250
+
251
+ it("with href", () => {
252
+ expect(
253
+ <DocComment>
254
+ <DocSeeAlso href="https://github.com" />
255
+ </DocComment>,
256
+ ).toRenderTo(`
257
+ /// <seealso href="https://github.com" />
258
+ `);
259
+ });
260
+
261
+ it("with children", () => {
262
+ expect(
263
+ <DocComment>
264
+ <DocSeeAlso cref="T:OtherType">OtherType</DocSeeAlso>
265
+ </DocComment>,
266
+ ).toRenderTo(`
267
+ /// <seealso cref="T:OtherType">OtherType</seealso>
268
+ `);
269
+ });
269
270
  });
270
271
 
271
272
  it("define paramref", () => {
272
273
  expect(
273
274
  <DocComment>
274
- <>{DocParamRef({ name: "x" })}</>
275
+ <DocParamRef name="x" />
275
276
  </DocComment>,
276
277
  ).toRenderTo(`
277
- /// <paramref name="x" />
278
+ /// <paramref name="${"x"}" />
278
279
  `);
279
280
  });
280
281
 
281
282
  it("define typeparamref", () => {
282
283
  expect(
283
284
  <DocComment>
284
- <>{DocTypeParamRef({ name: "T" })}</>
285
+ <DocTypeParamRef name="T" />
285
286
  </DocComment>,
286
287
  ).toRenderTo(`
287
288
  /// <typeparamref name="T" />
@@ -1,4 +1,4 @@
1
- import { Children, code, List, Prose, Show } from "@alloy-js/core";
1
+ import { Children, code, For, Indent, List, Prose, Show } from "@alloy-js/core";
2
2
 
3
3
  export interface DocCommentProps {
4
4
  children: Children;
@@ -53,7 +53,7 @@ export function makeInlineDocCommentTag(name: string) {
53
53
 
54
54
  export const DocSummary = makeDocCommentTag("summary");
55
55
  export const DocCode = makeDocCommentTag("code");
56
- export const DocC = makeDocCommentTag("c");
56
+ export const DocC = makeInlineDocCommentTag("c");
57
57
  export const DocExample = makeDocCommentTag("example");
58
58
  export const DocException = makeDocCommentTag("exception");
59
59
 
@@ -85,29 +85,40 @@ export const DocReturns = makeDocCommentTag("returns");
85
85
  export const DocRemarks = makeDocCommentTag("remarks");
86
86
  export const DocValue = makeDocCommentTag("value");
87
87
  export const DocPermission = makeDocCommentTag("permission");
88
- export const DocResponse = makeDocCommentTag("response");
89
- export const DocCompletionList = makeDocCommentTag("completionlist");
90
- export const DocList = makeDocCommentTag("list");
91
- export const DocItem = makeDocCommentTag("item");
92
- export const DocTerm = makeDocCommentTag("term");
93
88
  export const DocDescription = makeDocCommentTag("description");
94
89
  export const DocPara = makeDocCommentTag("para");
95
90
 
96
91
  export interface DocSeeProps {
97
- cref: string;
92
+ cref?: string;
93
+ href?: string;
98
94
  langword?: string;
99
95
  children?: Children;
100
96
  }
101
- export const DocSee = (props: DocSeeProps) =>
102
- `<see cref="${props.cref}"${props.langword ? ` langword="${props.langword}"` : ""}${props.children ? `>${props.children}</see>` : " />"}`;
97
+ export const DocSee = (props: DocSeeProps) => {
98
+ const attributes = [
99
+ props.cref ? `cref="${props.cref}"` : undefined,
100
+ props.href ? `href="${props.href}"` : undefined,
101
+ props.langword ? `langword="${props.langword}"` : undefined,
102
+ ]
103
+ .filter(Boolean)
104
+ .join(" ");
105
+ return code`<see ${attributes}${props.children ? code`>${props.children}</see>` : " />"}`;
106
+ };
103
107
 
104
108
  export interface DocSeeAlsoProps {
105
- cref: string;
106
- langword?: string;
109
+ cref?: string;
110
+ href?: string;
107
111
  children?: Children;
108
112
  }
109
- export const DocSeeAlso = (props: DocSeeAlsoProps) =>
110
- `<seealso cref="${props.cref}"${props.langword ? ` langword="${props.langword}"` : ""}${props.children ? `>${props.children}</seealso>` : " />"}`;
113
+ export const DocSeeAlso = (props: DocSeeAlsoProps) => {
114
+ const attributes = [
115
+ props.cref ? `cref="${props.cref}"` : undefined,
116
+ props.href ? `href="${props.href}"` : undefined,
117
+ ]
118
+ .filter(Boolean)
119
+ .join(" ");
120
+ return code`<seealso ${attributes}${props.children ? code`>${props.children}</seealso>` : " />"}`;
121
+ };
111
122
 
112
123
  export interface DocParamRefProps {
113
124
  name: string;
@@ -120,3 +131,22 @@ export interface DocTypeParamRefProps {
120
131
  }
121
132
  export const DocTypeParamRef = (props: DocTypeParamRefProps) =>
122
133
  `<typeparamref name="${props.name}" />`;
134
+
135
+ export interface DocListProps {
136
+ type?: "bullet" | "number";
137
+ items: Children[];
138
+ }
139
+ export function DocList(props: DocListProps) {
140
+ return (
141
+ <Prose>
142
+ {`<list type="${props.type ?? "bullet"}">`}
143
+ <Indent>
144
+ <For each={props.items}>
145
+ {(item) => code`<item><description>${item}</description></item>`}
146
+ </For>
147
+ </Indent>
148
+ <hbr />
149
+ {`</list>`}
150
+ </Prose>
151
+ );
152
+ }
@@ -0,0 +1,103 @@
1
+ import { d } from "@alloy-js/core/testing";
2
+ import { describe, expect, it } from "vitest";
3
+ import { DocFromMarkdown } from "./from-markdown.jsx";
4
+
5
+ it("convert code block to <code>", () => {
6
+ expect(
7
+ <DocFromMarkdown
8
+ markdown={d`
9
+ Some markdown with code
10
+ \`\`\`csharp
11
+ var foo = "bar";
12
+ \`\`\`
13
+ `}
14
+ />,
15
+ ).toRenderTo(`
16
+ Some markdown with code
17
+ <code>
18
+ var foo = "bar";
19
+ </code>
20
+ `);
21
+ });
22
+
23
+ it("convert inline code block to <c>", () => {
24
+ expect(
25
+ <DocFromMarkdown
26
+ markdown={d`
27
+ Some markdown with \`inline\` code
28
+ `}
29
+ />,
30
+ ).toRenderTo(`
31
+ Some markdown with <c>inline</c> code
32
+ `);
33
+ });
34
+
35
+ it("convert link to <see>", () => {
36
+ expect(
37
+ <DocFromMarkdown
38
+ markdown={d`
39
+ Some markdown with [link](https://example.com)
40
+ `}
41
+ />,
42
+ ).toRenderTo(`
43
+ Some markdown with <see href="https://example.com">link</see>
44
+ `);
45
+ });
46
+
47
+ it("convert bullet list to <list>", () => {
48
+ expect(
49
+ <DocFromMarkdown
50
+ markdown={d`
51
+ - Item 1
52
+ - Item 2
53
+ - Item 3
54
+ `}
55
+ />,
56
+ ).toRenderTo(`
57
+ <list type="bullet">
58
+ <item><description>Item 1</description></item>
59
+ <item><description>Item 2</description></item>
60
+ <item><description>Item 3</description></item>
61
+ </list>
62
+ `);
63
+ });
64
+
65
+ it("convert numbered list to <list>", () => {
66
+ expect(
67
+ <DocFromMarkdown
68
+ markdown={d`
69
+ 1. Item 1
70
+ 2. Item 2
71
+ 3. Item 3
72
+ `}
73
+ />,
74
+ ).toRenderTo(`
75
+ <list type="number">
76
+ <item><description>Item 1</description></item>
77
+ <item><description>Item 2</description></item>
78
+ <item><description>Item 3</description></item>
79
+ </list>
80
+ `);
81
+ });
82
+
83
+ describe("strip unsupported elements", () => {
84
+ it("bold", () => {
85
+ expect(
86
+ <DocFromMarkdown
87
+ markdown={d`
88
+ Some markdown with **bold** text
89
+ `}
90
+ />,
91
+ ).toRenderTo(`Some markdown with bold text`);
92
+ });
93
+
94
+ it("italic", () => {
95
+ expect(
96
+ <DocFromMarkdown
97
+ markdown={d`
98
+ Some markdown with *italic* text
99
+ `}
100
+ />,
101
+ ).toRenderTo(`Some markdown with italic text`);
102
+ });
103
+ });
@@ -0,0 +1,58 @@
1
+ import { marked, Tokens, type Token } from "marked";
2
+ import { DocC, DocCode, DocList, DocSee } from "./comment.jsx";
3
+
4
+ export interface DocFromMarkdownProps {
5
+ markdown: string;
6
+ }
7
+
8
+ /** Convert markdown to a Csharp doc comment */
9
+ export function DocFromMarkdown(props: DocFromMarkdownProps) {
10
+ const tokens = marked.lexer(props.markdown);
11
+ return renderTokens(tokens);
12
+ }
13
+
14
+ function renderTokens(tokens: Token[]) {
15
+ return (
16
+ <>
17
+ {tokens.map((token, index) => (
18
+ <>
19
+ <DocFromMarkedToken token={token} />
20
+ {token.type === "paragraph" && index !== tokens.length - 1 && <br />}
21
+ </>
22
+ ))}
23
+ </>
24
+ );
25
+ }
26
+
27
+ function DocFromMarkedToken(props: { token: Token }) {
28
+ const { token } = props;
29
+ switch (token.type) {
30
+ case "paragraph":
31
+ return token.tokens ? renderTokens(token.tokens) : null;
32
+ case "code":
33
+ return <DocCode>{token.text}</DocCode>;
34
+ case "codespan":
35
+ return <DocC>{token.text}</DocC>;
36
+ case "list":
37
+ return (
38
+ <DocList
39
+ type={token.ordered ? "number" : "bullet"}
40
+ items={token.items.map((x: Tokens.ListItem) => (
41
+ <>{renderTokens(x.tokens)}</>
42
+ ))}
43
+ />
44
+ );
45
+ case "link":
46
+ return (
47
+ <DocSee href={token.href}>
48
+ {token.tokens && renderTokens(token.tokens)}
49
+ </DocSee>
50
+ );
51
+ case "strong":
52
+ case "em":
53
+ return token.text;
54
+ case "text":
55
+ default:
56
+ return token.raw;
57
+ }
58
+ }
@@ -1,6 +1,8 @@
1
1
  export * from "./ClassDeclaration.jsx";
2
2
  export * from "./ClassMethod.jsx";
3
3
  export * from "./Declaration.js";
4
+ export * from "./doc/comment.jsx";
5
+ export * from "./doc/from-markdown.jsx";
4
6
  export * from "./EnumDeclaration.jsx";
5
7
  export * from "./interface/declaration.js";
6
8
  export * from "./interface/method.js";