@alloy-js/csharp 0.23.0-dev.10 → 0.23.0-dev.11

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 (54) hide show
  1. package/dist/dev/src/components/method/method.test.js +64 -0
  2. package/dist/dev/src/components/method/method.test.js.map +1 -1
  3. package/dist/dev/src/identifier-utils.js +45 -0
  4. package/dist/dev/src/identifier-utils.js.map +1 -0
  5. package/dist/dev/src/index.js +2 -0
  6. package/dist/dev/src/index.js.map +1 -1
  7. package/dist/dev/src/keywords.js +39 -0
  8. package/dist/dev/src/keywords.js.map +1 -0
  9. package/dist/dev/src/name-policy.js +29 -6
  10. package/dist/dev/src/name-policy.js.map +1 -1
  11. package/dist/dev/src/name-policy.test.js +167 -0
  12. package/dist/dev/src/name-policy.test.js.map +1 -0
  13. package/dist/src/components/method/method.test.js +48 -0
  14. package/dist/src/components/method/method.test.js.map +1 -1
  15. package/dist/src/identifier-utils.d.ts +22 -0
  16. package/dist/src/identifier-utils.d.ts.map +1 -0
  17. package/dist/src/identifier-utils.js +45 -0
  18. package/dist/src/identifier-utils.js.map +1 -0
  19. package/dist/src/index.d.ts +2 -0
  20. package/dist/src/index.d.ts.map +1 -1
  21. package/dist/src/index.js +2 -0
  22. package/dist/src/index.js.map +1 -1
  23. package/dist/src/keywords.d.ts +32 -0
  24. package/dist/src/keywords.d.ts.map +1 -0
  25. package/dist/src/keywords.js +39 -0
  26. package/dist/src/keywords.js.map +1 -0
  27. package/dist/src/name-policy.d.ts +7 -0
  28. package/dist/src/name-policy.d.ts.map +1 -1
  29. package/dist/src/name-policy.js +29 -6
  30. package/dist/src/name-policy.js.map +1 -1
  31. package/dist/src/name-policy.test.d.ts +2 -0
  32. package/dist/src/name-policy.test.d.ts.map +1 -0
  33. package/dist/src/name-policy.test.js +167 -0
  34. package/dist/src/name-policy.test.js.map +1 -0
  35. package/dist/tsconfig.tsbuildinfo +1 -1
  36. package/docs/api/contexts/csharp-context.md +21 -0
  37. package/docs/api/contexts/index.md +3 -0
  38. package/docs/api/functions/createCSharpNamePolicy.md +4 -0
  39. package/docs/api/functions/index.md +5 -1
  40. package/docs/api/functions/isCSharpContextualKeyword.md +20 -0
  41. package/docs/api/functions/isCSharpKeyword.md +22 -0
  42. package/docs/api/functions/isValidCSharpIdentifier.md +20 -0
  43. package/docs/api/functions/sanitizeCSharpIdentifier.md +24 -0
  44. package/docs/api/index.md +3 -2
  45. package/docs/api/variables/csharpKeywords.md +11 -0
  46. package/docs/api/variables/index.md +1 -0
  47. package/package.json +3 -3
  48. package/src/components/method/method.test.tsx +36 -0
  49. package/src/identifier-utils.ts +45 -0
  50. package/src/index.ts +2 -0
  51. package/src/keywords.ts +162 -0
  52. package/src/name-policy.test.ts +210 -0
  53. package/src/name-policy.ts +30 -6
  54. package/temp/api.json +237 -1
@@ -1,5 +1,7 @@
1
1
  import * as core from "@alloy-js/core";
2
2
  import * as changecase from "change-case";
3
+ import { sanitizeCSharpIdentifier } from "./identifier-utils.js";
4
+ import { isCSharpKeyword } from "./keywords.js";
3
5
 
4
6
  // the context in which the name policy should be applied
5
7
  export type CSharpElements =
@@ -20,10 +22,27 @@ export type CSharpElements =
20
22
  | "type-parameter"
21
23
  | "namespace";
22
24
 
23
- // creates the C# naming policy
25
+ /**
26
+ * Prefixes the name with `@` if it is a C# keyword.
27
+ * This is the idiomatic C# way to use reserved words as identifiers.
28
+ * The check is case-sensitive, matching C# language semantics.
29
+ */
30
+ function escapeIfKeyword(name: string): string {
31
+ return isCSharpKeyword(name) ? `@${name}` : name;
32
+ }
33
+
34
+ /**
35
+ * Creates the C# naming policy with case conversion and keyword escaping.
36
+ *
37
+ * After applying the appropriate case conversion for each element kind,
38
+ * the resulting name is checked against C# reserved and contextual keywords.
39
+ * If it matches (case-sensitively), the name is prefixed with `@`.
40
+ */
24
41
  export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
25
42
  return core.createNamePolicy((name, element) => {
43
+ let result: string;
26
44
  switch (element) {
45
+ case "namespace":
27
46
  case "class":
28
47
  case "struct":
29
48
  case "enum":
@@ -34,15 +53,20 @@ export function createCSharpNamePolicy(): core.NamePolicy<CSharpElements> {
34
53
  case "class-method":
35
54
  case "type-parameter":
36
55
  case "class-property":
37
- case "namespace":
38
- return changecase.pascalCase(name);
56
+ result = changecase.pascalCase(name);
57
+ break;
39
58
  case "constant":
40
- return changecase.constantCase(name);
59
+ result = changecase.constantCase(name);
60
+ break;
41
61
  case "class-member-private":
42
- return `_${changecase.camelCase(name)}`;
62
+ result = `_${changecase.camelCase(name)}`;
63
+ break;
43
64
  default:
44
- return changecase.camelCase(name);
65
+ result = changecase.camelCase(name);
66
+ break;
45
67
  }
68
+
69
+ return escapeIfKeyword(sanitizeCSharpIdentifier(result));
46
70
  });
47
71
  }
48
72
 
package/temp/api.json CHANGED
@@ -2560,7 +2560,7 @@
2560
2560
  {
2561
2561
  "kind": "Function",
2562
2562
  "canonicalReference": "@alloy-js/csharp!createCSharpNamePolicy:function(1)",
2563
- "docComment": "",
2563
+ "docComment": "/**\n * Creates the C# naming policy with case conversion and keyword escaping.\n *\n * After applying the appropriate case conversion for each element kind,\n * the resulting name is checked against C# reserved and contextual keywords.\n * If it matches (case-sensitively), the name is prefixed with `@`.\n */\n",
2564
2564
  "excerptTokens": [
2565
2565
  {
2566
2566
  "kind": "Content",
@@ -3624,6 +3624,34 @@
3624
3624
  },
3625
3625
  "implementsTokenRanges": []
3626
3626
  },
3627
+ {
3628
+ "kind": "Variable",
3629
+ "canonicalReference": "@alloy-js/csharp!csharpContextualKeywords:var",
3630
+ "docComment": "/**\n * C# contextual keywords that are reserved in certain contexts.\n * While not always reserved, treating them as keywords in generated code\n * avoids subtle context-dependent issues.\n *\n * @see\n *\n * https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/#contextual-keywords\n */\n",
3631
+ "excerptTokens": [
3632
+ {
3633
+ "kind": "Content",
3634
+ "text": "csharpContextualKeywords: "
3635
+ },
3636
+ {
3637
+ "kind": "Reference",
3638
+ "text": "ReadonlySet",
3639
+ "canonicalReference": "!ReadonlySet:interface"
3640
+ },
3641
+ {
3642
+ "kind": "Content",
3643
+ "text": "<string>"
3644
+ }
3645
+ ],
3646
+ "fileUrlPath": "src/keywords.ts",
3647
+ "isReadonly": true,
3648
+ "releaseTag": "Public",
3649
+ "name": "csharpContextualKeywords",
3650
+ "variableTypeTokenRange": {
3651
+ "startIndex": 1,
3652
+ "endIndex": 3
3653
+ }
3654
+ },
3627
3655
  {
3628
3656
  "kind": "TypeAlias",
3629
3657
  "canonicalReference": "@alloy-js/csharp!CSharpElements:type",
@@ -3731,6 +3759,34 @@
3731
3759
  "endIndex": 8
3732
3760
  }
3733
3761
  },
3762
+ {
3763
+ "kind": "Variable",
3764
+ "canonicalReference": "@alloy-js/csharp!csharpKeywords:var",
3765
+ "docComment": "/**\n * C# reserved keywords that cannot be used as identifiers without `@` prefix.\n * These are case-sensitive in C#.\n *\n * @see\n *\n * https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/\n */\n",
3766
+ "excerptTokens": [
3767
+ {
3768
+ "kind": "Content",
3769
+ "text": "csharpKeywords: "
3770
+ },
3771
+ {
3772
+ "kind": "Reference",
3773
+ "text": "ReadonlySet",
3774
+ "canonicalReference": "!ReadonlySet:interface"
3775
+ },
3776
+ {
3777
+ "kind": "Content",
3778
+ "text": "<string>"
3779
+ }
3780
+ ],
3781
+ "fileUrlPath": "src/keywords.ts",
3782
+ "isReadonly": true,
3783
+ "releaseTag": "Public",
3784
+ "name": "csharpKeywords",
3785
+ "variableTypeTokenRange": {
3786
+ "startIndex": 1,
3787
+ "endIndex": 3
3788
+ }
3789
+ },
3734
3790
  {
3735
3791
  "kind": "Class",
3736
3792
  "canonicalReference": "@alloy-js/csharp!CSharpLexicalScope:class",
@@ -10556,6 +10612,141 @@
10556
10612
  ],
10557
10613
  "extendsTokenRanges": []
10558
10614
  },
10615
+ {
10616
+ "kind": "Function",
10617
+ "canonicalReference": "@alloy-js/csharp!isCSharpContextualKeyword:function(1)",
10618
+ "docComment": "/**\n * Returns true if the given name is a C# contextual keyword.\n * Contextual keywords are only reserved in specific language contexts\n * and are generally valid as identifiers.\n */\n",
10619
+ "excerptTokens": [
10620
+ {
10621
+ "kind": "Content",
10622
+ "text": "export declare function isCSharpContextualKeyword(name: "
10623
+ },
10624
+ {
10625
+ "kind": "Content",
10626
+ "text": "string"
10627
+ },
10628
+ {
10629
+ "kind": "Content",
10630
+ "text": "): "
10631
+ },
10632
+ {
10633
+ "kind": "Content",
10634
+ "text": "boolean"
10635
+ },
10636
+ {
10637
+ "kind": "Content",
10638
+ "text": ";"
10639
+ }
10640
+ ],
10641
+ "fileUrlPath": "src/keywords.ts",
10642
+ "returnTypeTokenRange": {
10643
+ "startIndex": 3,
10644
+ "endIndex": 4
10645
+ },
10646
+ "releaseTag": "Public",
10647
+ "overloadIndex": 1,
10648
+ "parameters": [
10649
+ {
10650
+ "parameterName": "name",
10651
+ "parameterTypeTokenRange": {
10652
+ "startIndex": 1,
10653
+ "endIndex": 2
10654
+ },
10655
+ "isOptional": false
10656
+ }
10657
+ ],
10658
+ "name": "isCSharpContextualKeyword"
10659
+ },
10660
+ {
10661
+ "kind": "Function",
10662
+ "canonicalReference": "@alloy-js/csharp!isCSharpKeyword:function(1)",
10663
+ "docComment": "/**\n * Returns true if the given name is a C# reserved keyword.\n * The check is case-sensitive, matching C# language semantics.\n *\n * Note: this only checks reserved keywords, not contextual keywords.\n * Contextual keywords are only reserved in specific language contexts\n * and are valid identifiers elsewhere (e.g., `value` is valid as a parameter name).\n * Use {@link isCSharpContextualKeyword} to check contextual keywords separately.\n */\n",
10664
+ "excerptTokens": [
10665
+ {
10666
+ "kind": "Content",
10667
+ "text": "export declare function isCSharpKeyword(name: "
10668
+ },
10669
+ {
10670
+ "kind": "Content",
10671
+ "text": "string"
10672
+ },
10673
+ {
10674
+ "kind": "Content",
10675
+ "text": "): "
10676
+ },
10677
+ {
10678
+ "kind": "Content",
10679
+ "text": "boolean"
10680
+ },
10681
+ {
10682
+ "kind": "Content",
10683
+ "text": ";"
10684
+ }
10685
+ ],
10686
+ "fileUrlPath": "src/keywords.ts",
10687
+ "returnTypeTokenRange": {
10688
+ "startIndex": 3,
10689
+ "endIndex": 4
10690
+ },
10691
+ "releaseTag": "Public",
10692
+ "overloadIndex": 1,
10693
+ "parameters": [
10694
+ {
10695
+ "parameterName": "name",
10696
+ "parameterTypeTokenRange": {
10697
+ "startIndex": 1,
10698
+ "endIndex": 2
10699
+ },
10700
+ "isOptional": false
10701
+ }
10702
+ ],
10703
+ "name": "isCSharpKeyword"
10704
+ },
10705
+ {
10706
+ "kind": "Function",
10707
+ "canonicalReference": "@alloy-js/csharp!isValidCSharpIdentifier:function(1)",
10708
+ "docComment": "/**\n * Checks whether the provided name is a valid C# identifier (without `@` prefix).\n * Does not account for keyword conflicts — use {@link isCSharpKeyword} for that.\n *\n * @param name - The name to validate.\n *\n * @returns true if the name matches C# identifier rules (letter or underscore start, word chars after).\n */\n",
10709
+ "excerptTokens": [
10710
+ {
10711
+ "kind": "Content",
10712
+ "text": "export declare function isValidCSharpIdentifier(name: "
10713
+ },
10714
+ {
10715
+ "kind": "Content",
10716
+ "text": "string"
10717
+ },
10718
+ {
10719
+ "kind": "Content",
10720
+ "text": "): "
10721
+ },
10722
+ {
10723
+ "kind": "Content",
10724
+ "text": "boolean"
10725
+ },
10726
+ {
10727
+ "kind": "Content",
10728
+ "text": ";"
10729
+ }
10730
+ ],
10731
+ "fileUrlPath": "src/identifier-utils.ts",
10732
+ "returnTypeTokenRange": {
10733
+ "startIndex": 3,
10734
+ "endIndex": 4
10735
+ },
10736
+ "releaseTag": "Public",
10737
+ "overloadIndex": 1,
10738
+ "parameters": [
10739
+ {
10740
+ "parameterName": "name",
10741
+ "parameterTypeTokenRange": {
10742
+ "startIndex": 1,
10743
+ "endIndex": 2
10744
+ },
10745
+ "isOptional": false
10746
+ }
10747
+ ],
10748
+ "name": "isValidCSharpIdentifier"
10749
+ },
10559
10750
  {
10560
10751
  "kind": "Interface",
10561
10752
  "canonicalReference": "@alloy-js/csharp!LeixcalScopePropsWithScopeInfo:interface",
@@ -15613,6 +15804,51 @@
15613
15804
  "endIndex": 9
15614
15805
  }
15615
15806
  },
15807
+ {
15808
+ "kind": "Function",
15809
+ "canonicalReference": "@alloy-js/csharp!sanitizeCSharpIdentifier:function(1)",
15810
+ "docComment": "/**\n * Transforms an arbitrary string into a valid C# identifier by replacing\n * invalid characters. The result may still be a C# keyword — callers\n * should combine with keyword escaping if needed.\n *\n * - If the first character is not a letter or underscore, a `_` prefix is added.\n * - Subsequent non-word characters are replaced with `_`.\n * - Empty strings become `_`.\n *\n * @param name - The string to sanitize.\n *\n * @returns A string that satisfies C# identifier character rules.\n */\n",
15811
+ "excerptTokens": [
15812
+ {
15813
+ "kind": "Content",
15814
+ "text": "export declare function sanitizeCSharpIdentifier(name: "
15815
+ },
15816
+ {
15817
+ "kind": "Content",
15818
+ "text": "string"
15819
+ },
15820
+ {
15821
+ "kind": "Content",
15822
+ "text": "): "
15823
+ },
15824
+ {
15825
+ "kind": "Content",
15826
+ "text": "string"
15827
+ },
15828
+ {
15829
+ "kind": "Content",
15830
+ "text": ";"
15831
+ }
15832
+ ],
15833
+ "fileUrlPath": "src/identifier-utils.ts",
15834
+ "returnTypeTokenRange": {
15835
+ "startIndex": 3,
15836
+ "endIndex": 4
15837
+ },
15838
+ "releaseTag": "Public",
15839
+ "overloadIndex": 1,
15840
+ "parameters": [
15841
+ {
15842
+ "parameterName": "name",
15843
+ "parameterTypeTokenRange": {
15844
+ "startIndex": 1,
15845
+ "endIndex": 2
15846
+ },
15847
+ "isOptional": false
15848
+ }
15849
+ ],
15850
+ "name": "sanitizeCSharpIdentifier"
15851
+ },
15616
15852
  {
15617
15853
  "kind": "Function",
15618
15854
  "canonicalReference": "@alloy-js/csharp!SourceFile:function(1)",