@alloy-js/csharp 0.21.0-dev.18 → 0.21.0-dev.19

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.
Files changed (29) hide show
  1. package/dist/src/components/access-expression/access-expression.d.ts +5 -0
  2. package/dist/src/components/access-expression/access-expression.d.ts.map +1 -1
  3. package/dist/src/components/access-expression/access-expression.js.map +1 -1
  4. package/dist/src/components/access-expression/part-descriptors.d.ts +8 -0
  5. package/dist/src/components/access-expression/part-descriptors.d.ts.map +1 -1
  6. package/dist/src/components/access-expression/part-descriptors.js +22 -2
  7. package/dist/src/components/access-expression/part-descriptors.js.map +1 -1
  8. package/dist/src/components/attributes/attributes.d.ts +2 -6
  9. package/dist/src/components/attributes/attributes.d.ts.map +1 -1
  10. package/dist/src/components/attributes/attributes.js +14 -7
  11. package/dist/src/components/attributes/attributes.js.map +1 -1
  12. package/dist/src/components/attributes/attributes.test.js +51 -13
  13. package/dist/src/components/attributes/attributes.test.js.map +1 -1
  14. package/dist/src/contexts/reference-context.d.ts +12 -0
  15. package/dist/src/contexts/reference-context.d.ts.map +1 -0
  16. package/dist/src/contexts/reference-context.js +15 -0
  17. package/dist/src/contexts/reference-context.js.map +1 -0
  18. package/dist/src/symbols/reference.d.ts.map +1 -1
  19. package/dist/src/symbols/reference.js +4 -1
  20. package/dist/src/symbols/reference.js.map +1 -1
  21. package/dist/tsconfig.tsbuildinfo +1 -1
  22. package/package.json +1 -1
  23. package/src/components/access-expression/access-expression.tsx +6 -0
  24. package/src/components/access-expression/part-descriptors.ts +26 -2
  25. package/src/components/attributes/attributes.test.tsx +39 -7
  26. package/src/components/attributes/attributes.tsx +12 -18
  27. package/src/contexts/reference-context.ts +18 -0
  28. package/src/symbols/reference.tsx +9 -1
  29. package/temp/api.json +29 -74
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/csharp",
3
- "version": "0.21.0-dev.18",
3
+ "version": "0.21.0-dev.19",
4
4
  "description": "Alloy components for CSharp language.",
5
5
  "exports": {
6
6
  ".": {
@@ -122,6 +122,12 @@ export interface AccessExpressionPartProps {
122
122
  * you provide a symbol or refkey and the symbol's nullable flag is set.
123
123
  */
124
124
  nullable?: boolean;
125
+
126
+ /**
127
+ * Wether this part is an attribute reference (used in an attribute context).
128
+ * In that case the "Attribute" suffix is trimmed from the name if present.
129
+ */
130
+ attribute?: boolean;
125
131
  }
126
132
 
127
133
  AccessExpression.Part = function (props: AccessExpressionPartProps) {
@@ -128,9 +128,12 @@ function createPartDescriptorFromProps(
128
128
  } else if (first && partProps.refkey) {
129
129
  return partProps.refkey;
130
130
  } else if (partProps.id !== undefined) {
131
- return partProps.id;
131
+ return normalizeIfAttribute(partProps.id, partProps.attribute);
132
132
  } else if (symbolSource.value) {
133
- return escapeId(symbolSource.value.name);
133
+ return normalizeIfAttribute(
134
+ escapeId(symbolSource.value.name),
135
+ partProps.attribute,
136
+ );
134
137
  } else {
135
138
  return "<unresolved symbol>";
136
139
  }
@@ -173,3 +176,24 @@ function createPartDescriptorFromProps(
173
176
  function escapeId(id: string) {
174
177
  return id.replace(/"/g, '\\"');
175
178
  }
179
+
180
+ function normalizeIfAttribute(id: string, isAttribute?: boolean) {
181
+ if (isAttribute) {
182
+ return normalizeAttributeName(id);
183
+ }
184
+ return id;
185
+ }
186
+
187
+ /**
188
+ * Normalize attribute name by removing the "Attribute" suffix if present.
189
+ * @example
190
+ * ```ts
191
+ * normalizeAttributeName("TestAttribute") // returns "Test"
192
+ * ```
193
+ */
194
+ export function normalizeAttributeName(name: string) {
195
+ if (name !== undefined && name.endsWith("Attribute")) {
196
+ return name.substring(0, name.length - "Attribute".length);
197
+ }
198
+ return name;
199
+ }
@@ -1,5 +1,8 @@
1
- import { namekey } from "@alloy-js/core";
2
- import { expect, it } from "vitest";
1
+ import { ClassDeclaration } from "#components/class/declaration.jsx";
2
+ import { createLibrary } from "#createLibrary";
3
+ import { TestNamespace } from "#test/utils.jsx";
4
+ import { List, namekey } from "@alloy-js/core";
5
+ import { describe, expect, it } from "vitest";
3
6
  import { Attribute, AttributeList } from "./attributes.jsx";
4
7
 
5
8
  it("define attribute", () => {
@@ -8,13 +11,42 @@ it("define attribute", () => {
8
11
  `);
9
12
  });
10
13
 
11
- it("define attribute whose name ending with 'Attribute'", () => {
12
- expect(<Attribute name="TestAttribute" />).toRenderTo(`
14
+ describe("Attribute suffix trimming", () => {
15
+ it("define attribute whose name ending with 'Attribute'", () => {
16
+ expect(<Attribute name="TestAttribute" />).toRenderTo(`
13
17
  [Test]
14
- `);
15
- expect(<Attribute name={namekey("TestAttribute")} />).toRenderTo(`
18
+ `);
19
+ });
20
+
21
+ it("when using namekey", () => {
22
+ const key = namekey("TestAttribute");
23
+ expect(
24
+ <TestNamespace>
25
+ <List>
26
+ <Attribute name={key} />
27
+ <ClassDeclaration name={key} />
28
+ </List>
29
+ </TestNamespace>,
30
+ ).toRenderTo(`
16
31
  [Test]
17
- `);
32
+ class TestAttribute;
33
+ `);
34
+ });
35
+
36
+ it("when referenced from library", () => {
37
+ const TestLib = createLibrary("TestLib", {
38
+ TestAttribute: { kind: "method", methodKind: "ordinary" },
39
+ });
40
+ expect(
41
+ <TestNamespace>
42
+ <Attribute name={TestLib.TestAttribute} />
43
+ </TestNamespace>,
44
+ ).toRenderTo(`
45
+ using TestLib;
46
+
47
+ [Test]
48
+ `);
49
+ });
18
50
  });
19
51
 
20
52
  it("define attribute with single arg", () => {
@@ -1,16 +1,13 @@
1
+ import { normalizeAttributeName } from "#components/access-expression/part-descriptors.js";
1
2
  import {
2
3
  Children,
3
4
  findKeyedChildren,
4
5
  For,
5
6
  Indent,
6
- Refkey,
7
+ Refkeyable,
7
8
  taggedComponent,
8
9
  } from "@alloy-js/core";
9
-
10
- export interface AttributeItem {
11
- name: string;
12
- args?: string[];
13
- }
10
+ import { ReferenceContext } from "../../contexts/reference-context.js";
14
11
 
15
12
  export type AttributesProp = Array<string | AttributeProps | Children>;
16
13
 
@@ -55,7 +52,7 @@ function renderAttribute(attr: string | AttributeProps | Children): Children {
55
52
 
56
53
  export interface AttributeProps {
57
54
  /** Attribute name */
58
- name: string | Refkey;
55
+ name: string | Refkeyable;
59
56
 
60
57
  /** Argument */
61
58
  args?: Children[];
@@ -82,7 +79,8 @@ export const Attribute = taggedComponent(
82
79
  (props: AttributeProps) => {
83
80
  return (
84
81
  <group>
85
- [{normalizeAttributeName(props.name)}
82
+ [
83
+ <AttributeName name={props.name} />
86
84
  {props.args && props.args.length > 0 && (
87
85
  <>
88
86
  (
@@ -100,14 +98,10 @@ export const Attribute = taggedComponent(
100
98
  },
101
99
  );
102
100
 
103
- function normalizeAttributeName(name: string | Refkey) {
104
- const nameStr =
105
- typeof name === "string" ? name
106
- : typeof name === "object" && "name" in name ? name.name
107
- : undefined;
108
-
109
- if (nameStr !== undefined && nameStr.endsWith("Attribute")) {
110
- return nameStr.substring(0, nameStr.length - "Attribute".length);
111
- }
112
- return name;
101
+ function AttributeName(props: Pick<AttributeProps, "name">) {
102
+ return typeof props.name === "string" ?
103
+ normalizeAttributeName(props.name)
104
+ : <ReferenceContext.Provider value={"attribute"}>
105
+ {props.name}
106
+ </ReferenceContext.Provider>;
113
107
  }
@@ -0,0 +1,18 @@
1
+ import { ComponentContext, createContext, useContext } from "@alloy-js/core";
2
+
3
+ /** What kind of reference.
4
+ * - 'standard' Default reference nothing special.
5
+ * - 'attribute' means we are referencing an attribute and so the name should be trimmed of "Attribute" suffix.
6
+ */
7
+
8
+ export type ReferenceContext = "standard" | "attribute";
9
+
10
+ /**
11
+ * Provides scopes for instance and static private members.
12
+ */
13
+ export const ReferenceContext: ComponentContext<ReferenceContext> =
14
+ createContext<ReferenceContext>();
15
+
16
+ export function useReferenceContext(): "standard" | "attribute" {
17
+ return useContext(ReferenceContext) ?? "standard";
18
+ }
@@ -7,6 +7,7 @@ import {
7
7
  resolve,
8
8
  unresolvedRefkey,
9
9
  } from "@alloy-js/core";
10
+ import { useReferenceContext } from "../contexts/reference-context.js";
10
11
  import { CSharpScope } from "../scopes/csharp.js";
11
12
  import { CSharpNamespaceScope } from "../scopes/namespace.js";
12
13
  import { useSourceFileScope } from "../scopes/source-file.js";
@@ -56,12 +57,19 @@ export function ref(
56
57
  }
57
58
 
58
59
  const parts = [];
60
+ const referenceContext = useReferenceContext();
59
61
 
60
62
  for (const nsScope of pathDown) {
61
63
  parts.push(<AccessExpression.Part symbol={nsScope.ownerSymbol!} />);
62
64
  }
63
65
 
64
- parts.push(<AccessExpression.Part symbol={lexicalDeclaration} />);
66
+ parts.push(
67
+ <AccessExpression.Part
68
+ symbol={lexicalDeclaration}
69
+ attribute={referenceContext === "attribute"}
70
+ />,
71
+ );
72
+
65
73
  for (const member of memberPath) {
66
74
  parts.push(<AccessExpression.Part symbol={member} />);
67
75
  }
package/temp/api.json CHANGED
@@ -995,6 +995,33 @@
995
995
  "endIndex": 4
996
996
  }
997
997
  },
998
+ {
999
+ "kind": "PropertySignature",
1000
+ "canonicalReference": "@alloy-js/csharp!AccessExpressionPartProps#attribute:member",
1001
+ "docComment": "/**\n * Wether this part is an attribute reference (used in an attribute context).\n * In that case the \"Attribute\" suffix is trimmed from the name if present.\n */\n",
1002
+ "excerptTokens": [
1003
+ {
1004
+ "kind": "Content",
1005
+ "text": "attribute?: "
1006
+ },
1007
+ {
1008
+ "kind": "Content",
1009
+ "text": "boolean"
1010
+ },
1011
+ {
1012
+ "kind": "Content",
1013
+ "text": ";"
1014
+ }
1015
+ ],
1016
+ "isReadonly": false,
1017
+ "isOptional": true,
1018
+ "releaseTag": "Public",
1019
+ "name": "attribute",
1020
+ "propertyTypeTokenRange": {
1021
+ "startIndex": 1,
1022
+ "endIndex": 2
1023
+ }
1024
+ },
998
1025
  {
999
1026
  "kind": "PropertySignature",
1000
1027
  "canonicalReference": "@alloy-js/csharp!AccessExpressionPartProps#children:member",
@@ -1577,78 +1604,6 @@
1577
1604
  "endIndex": 14
1578
1605
  }
1579
1606
  },
1580
- {
1581
- "kind": "Interface",
1582
- "canonicalReference": "@alloy-js/csharp!AttributeItem:interface",
1583
- "docComment": "",
1584
- "excerptTokens": [
1585
- {
1586
- "kind": "Content",
1587
- "text": "export interface AttributeItem "
1588
- }
1589
- ],
1590
- "fileUrlPath": "src/components/attributes/attributes.tsx",
1591
- "releaseTag": "Public",
1592
- "name": "AttributeItem",
1593
- "preserveMemberOrder": false,
1594
- "members": [
1595
- {
1596
- "kind": "PropertySignature",
1597
- "canonicalReference": "@alloy-js/csharp!AttributeItem#args:member",
1598
- "docComment": "",
1599
- "excerptTokens": [
1600
- {
1601
- "kind": "Content",
1602
- "text": "args?: "
1603
- },
1604
- {
1605
- "kind": "Content",
1606
- "text": "string[]"
1607
- },
1608
- {
1609
- "kind": "Content",
1610
- "text": ";"
1611
- }
1612
- ],
1613
- "isReadonly": false,
1614
- "isOptional": true,
1615
- "releaseTag": "Public",
1616
- "name": "args",
1617
- "propertyTypeTokenRange": {
1618
- "startIndex": 1,
1619
- "endIndex": 2
1620
- }
1621
- },
1622
- {
1623
- "kind": "PropertySignature",
1624
- "canonicalReference": "@alloy-js/csharp!AttributeItem#name:member",
1625
- "docComment": "",
1626
- "excerptTokens": [
1627
- {
1628
- "kind": "Content",
1629
- "text": "name: "
1630
- },
1631
- {
1632
- "kind": "Content",
1633
- "text": "string"
1634
- },
1635
- {
1636
- "kind": "Content",
1637
- "text": ";"
1638
- }
1639
- ],
1640
- "isReadonly": false,
1641
- "isOptional": false,
1642
- "releaseTag": "Public",
1643
- "name": "name",
1644
- "propertyTypeTokenRange": {
1645
- "startIndex": 1,
1646
- "endIndex": 2
1647
- }
1648
- }
1649
- ],
1650
- "extendsTokenRanges": []
1651
- },
1652
1607
  {
1653
1608
  "kind": "Function",
1654
1609
  "canonicalReference": "@alloy-js/csharp!AttributeList:function(1)",
@@ -1863,8 +1818,8 @@
1863
1818
  },
1864
1819
  {
1865
1820
  "kind": "Reference",
1866
- "text": "Refkey",
1867
- "canonicalReference": "@alloy-js/core!Refkey:type"
1821
+ "text": "Refkeyable",
1822
+ "canonicalReference": "@alloy-js/core!Refkeyable:type"
1868
1823
  },
1869
1824
  {
1870
1825
  "kind": "Content",