@alloy-js/typescript 0.22.0-dev.0 → 0.22.0-dev.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/typescript",
3
- "version": "0.22.0-dev.0",
3
+ "version": "0.22.0-dev.1",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -22,7 +22,7 @@
22
22
  "author": "brian.terlson@microsoft.com",
23
23
  "license": "MIT",
24
24
  "dependencies": {
25
- "@alloy-js/core": "~0.21.0 || >= 0.22.0-dev.0",
25
+ "@alloy-js/core": "~0.21.0 || >= 0.22.0-dev.1",
26
26
  "change-case": "^5.4.4",
27
27
  "pathe": "^2.0.3"
28
28
  },
@@ -1,4 +1,10 @@
1
- import { Namekey, OutputScopeOptions } from "@alloy-js/core";
1
+ import {
2
+ createComponent,
3
+ Namekey,
4
+ onCleanup,
5
+ OutputScopeOptions,
6
+ toRef,
7
+ } from "@alloy-js/core";
2
8
  import { useLexicalScope, useMemberOwner } from "../utils.js";
3
9
  import { useTSScope } from "./scopes.js";
4
10
  import { TSLexicalScope } from "./ts-lexical-scope.js";
@@ -24,7 +30,7 @@ export function createTypeAndValueSymbol(
24
30
 
25
31
  export function createTypeSymbol(
26
32
  name: string | Namekey,
27
- options: CreateTsSymbolOptions,
33
+ options: CreateTsSymbolOptions = {},
28
34
  ) {
29
35
  const scope = useLexicalScope();
30
36
  if (scope && !scope.types) {
@@ -125,3 +131,46 @@ export function createMemberScope(
125
131
  const parent = useTSScope();
126
132
  return new TSMemberScope(name, parent, ownerSymbol, options);
127
133
  }
134
+
135
+ export function decl(namekey: Namekey, options?: CreateTsSymbolOptions) {
136
+ return createComponent(() => {
137
+ const symbol = createValueSymbol(namekey, options);
138
+
139
+ onCleanup(() => {
140
+ symbol.delete();
141
+ });
142
+
143
+ return toRef(symbol, "name");
144
+ }, {});
145
+ }
146
+
147
+ export function declType(namekey: Namekey, options?: CreateTsSymbolOptions) {
148
+ return createComponent(() => {
149
+ const symbol = createTypeSymbol(namekey, options);
150
+
151
+ onCleanup(() => {
152
+ symbol.delete();
153
+ });
154
+
155
+ return toRef(symbol, "name");
156
+ }, {});
157
+ }
158
+
159
+ export function declMember(
160
+ namekey: Namekey,
161
+ locationOptions: { jsPrivate?: boolean; static?: boolean } = {},
162
+ options?: CreateTsSymbolOptions,
163
+ ) {
164
+ // Create the symbol inside a component so that we only get these side
165
+ // effects when this function is rendered (and not from something like
166
+ // children()/childrenArray() invoking the memo) and we get cleanup logic.
167
+ return createComponent(() => {
168
+ const symbol = createMemberSymbol(namekey, locationOptions, options);
169
+
170
+ onCleanup(() => {
171
+ symbol.delete();
172
+ });
173
+
174
+ return toRef(symbol, "name");
175
+ }, {});
176
+ }
package/temp/api.json CHANGED
@@ -3572,7 +3572,7 @@
3572
3572
  },
3573
3573
  {
3574
3574
  "kind": "Content",
3575
- "text": ", options: "
3575
+ "text": ", options?: "
3576
3576
  },
3577
3577
  {
3578
3578
  "kind": "Reference",
@@ -3615,7 +3615,7 @@
3615
3615
  "startIndex": 4,
3616
3616
  "endIndex": 5
3617
3617
  },
3618
- "isOptional": false
3618
+ "isOptional": true
3619
3619
  }
3620
3620
  ],
3621
3621
  "name": "createTypeSymbol"
@@ -3688,6 +3688,78 @@
3688
3688
  ],
3689
3689
  "name": "createValueSymbol"
3690
3690
  },
3691
+ {
3692
+ "kind": "Function",
3693
+ "canonicalReference": "@alloy-js/typescript!decl:function(1)",
3694
+ "docComment": "",
3695
+ "excerptTokens": [
3696
+ {
3697
+ "kind": "Content",
3698
+ "text": "export declare function decl(namekey: "
3699
+ },
3700
+ {
3701
+ "kind": "Reference",
3702
+ "text": "Namekey",
3703
+ "canonicalReference": "@alloy-js/core!Namekey:interface"
3704
+ },
3705
+ {
3706
+ "kind": "Content",
3707
+ "text": ", options?: "
3708
+ },
3709
+ {
3710
+ "kind": "Reference",
3711
+ "text": "CreateTsSymbolOptions",
3712
+ "canonicalReference": "@alloy-js/typescript!CreateTsSymbolOptions:interface"
3713
+ },
3714
+ {
3715
+ "kind": "Content",
3716
+ "text": "): "
3717
+ },
3718
+ {
3719
+ "kind": "Content",
3720
+ "text": "import(\"@alloy-js/core\")."
3721
+ },
3722
+ {
3723
+ "kind": "Reference",
3724
+ "text": "ComponentCreator",
3725
+ "canonicalReference": "@alloy-js/core!ComponentCreator:interface"
3726
+ },
3727
+ {
3728
+ "kind": "Content",
3729
+ "text": "<{}>"
3730
+ },
3731
+ {
3732
+ "kind": "Content",
3733
+ "text": ";"
3734
+ }
3735
+ ],
3736
+ "fileUrlPath": "src/symbols/index.ts",
3737
+ "returnTypeTokenRange": {
3738
+ "startIndex": 5,
3739
+ "endIndex": 8
3740
+ },
3741
+ "releaseTag": "Public",
3742
+ "overloadIndex": 1,
3743
+ "parameters": [
3744
+ {
3745
+ "parameterName": "namekey",
3746
+ "parameterTypeTokenRange": {
3747
+ "startIndex": 1,
3748
+ "endIndex": 2
3749
+ },
3750
+ "isOptional": false
3751
+ },
3752
+ {
3753
+ "parameterName": "options",
3754
+ "parameterTypeTokenRange": {
3755
+ "startIndex": 3,
3756
+ "endIndex": 4
3757
+ },
3758
+ "isOptional": true
3759
+ }
3760
+ ],
3761
+ "name": "decl"
3762
+ },
3691
3763
  {
3692
3764
  "kind": "Function",
3693
3765
  "canonicalReference": "@alloy-js/typescript!Declaration:function(1)",
@@ -4151,6 +4223,166 @@
4151
4223
  }
4152
4224
  ]
4153
4225
  },
4226
+ {
4227
+ "kind": "Function",
4228
+ "canonicalReference": "@alloy-js/typescript!declMember:function(1)",
4229
+ "docComment": "",
4230
+ "excerptTokens": [
4231
+ {
4232
+ "kind": "Content",
4233
+ "text": "export declare function declMember(namekey: "
4234
+ },
4235
+ {
4236
+ "kind": "Reference",
4237
+ "text": "Namekey",
4238
+ "canonicalReference": "@alloy-js/core!Namekey:interface"
4239
+ },
4240
+ {
4241
+ "kind": "Content",
4242
+ "text": ", locationOptions?: "
4243
+ },
4244
+ {
4245
+ "kind": "Content",
4246
+ "text": "{\n jsPrivate?: boolean;\n static?: boolean;\n}"
4247
+ },
4248
+ {
4249
+ "kind": "Content",
4250
+ "text": ", options?: "
4251
+ },
4252
+ {
4253
+ "kind": "Reference",
4254
+ "text": "CreateTsSymbolOptions",
4255
+ "canonicalReference": "@alloy-js/typescript!CreateTsSymbolOptions:interface"
4256
+ },
4257
+ {
4258
+ "kind": "Content",
4259
+ "text": "): "
4260
+ },
4261
+ {
4262
+ "kind": "Content",
4263
+ "text": "import(\"@alloy-js/core\")."
4264
+ },
4265
+ {
4266
+ "kind": "Reference",
4267
+ "text": "ComponentCreator",
4268
+ "canonicalReference": "@alloy-js/core!ComponentCreator:interface"
4269
+ },
4270
+ {
4271
+ "kind": "Content",
4272
+ "text": "<{}>"
4273
+ },
4274
+ {
4275
+ "kind": "Content",
4276
+ "text": ";"
4277
+ }
4278
+ ],
4279
+ "fileUrlPath": "src/symbols/index.ts",
4280
+ "returnTypeTokenRange": {
4281
+ "startIndex": 7,
4282
+ "endIndex": 10
4283
+ },
4284
+ "releaseTag": "Public",
4285
+ "overloadIndex": 1,
4286
+ "parameters": [
4287
+ {
4288
+ "parameterName": "namekey",
4289
+ "parameterTypeTokenRange": {
4290
+ "startIndex": 1,
4291
+ "endIndex": 2
4292
+ },
4293
+ "isOptional": false
4294
+ },
4295
+ {
4296
+ "parameterName": "locationOptions",
4297
+ "parameterTypeTokenRange": {
4298
+ "startIndex": 3,
4299
+ "endIndex": 4
4300
+ },
4301
+ "isOptional": true
4302
+ },
4303
+ {
4304
+ "parameterName": "options",
4305
+ "parameterTypeTokenRange": {
4306
+ "startIndex": 5,
4307
+ "endIndex": 6
4308
+ },
4309
+ "isOptional": true
4310
+ }
4311
+ ],
4312
+ "name": "declMember"
4313
+ },
4314
+ {
4315
+ "kind": "Function",
4316
+ "canonicalReference": "@alloy-js/typescript!declType:function(1)",
4317
+ "docComment": "",
4318
+ "excerptTokens": [
4319
+ {
4320
+ "kind": "Content",
4321
+ "text": "export declare function declType(namekey: "
4322
+ },
4323
+ {
4324
+ "kind": "Reference",
4325
+ "text": "Namekey",
4326
+ "canonicalReference": "@alloy-js/core!Namekey:interface"
4327
+ },
4328
+ {
4329
+ "kind": "Content",
4330
+ "text": ", options?: "
4331
+ },
4332
+ {
4333
+ "kind": "Reference",
4334
+ "text": "CreateTsSymbolOptions",
4335
+ "canonicalReference": "@alloy-js/typescript!CreateTsSymbolOptions:interface"
4336
+ },
4337
+ {
4338
+ "kind": "Content",
4339
+ "text": "): "
4340
+ },
4341
+ {
4342
+ "kind": "Content",
4343
+ "text": "import(\"@alloy-js/core\")."
4344
+ },
4345
+ {
4346
+ "kind": "Reference",
4347
+ "text": "ComponentCreator",
4348
+ "canonicalReference": "@alloy-js/core!ComponentCreator:interface"
4349
+ },
4350
+ {
4351
+ "kind": "Content",
4352
+ "text": "<{}>"
4353
+ },
4354
+ {
4355
+ "kind": "Content",
4356
+ "text": ";"
4357
+ }
4358
+ ],
4359
+ "fileUrlPath": "src/symbols/index.ts",
4360
+ "returnTypeTokenRange": {
4361
+ "startIndex": 5,
4362
+ "endIndex": 8
4363
+ },
4364
+ "releaseTag": "Public",
4365
+ "overloadIndex": 1,
4366
+ "parameters": [
4367
+ {
4368
+ "parameterName": "namekey",
4369
+ "parameterTypeTokenRange": {
4370
+ "startIndex": 1,
4371
+ "endIndex": 2
4372
+ },
4373
+ "isOptional": false
4374
+ },
4375
+ {
4376
+ "parameterName": "options",
4377
+ "parameterTypeTokenRange": {
4378
+ "startIndex": 3,
4379
+ "endIndex": 4
4380
+ },
4381
+ "isOptional": true
4382
+ }
4383
+ ],
4384
+ "name": "declType"
4385
+ },
4154
4386
  {
4155
4387
  "kind": "Function",
4156
4388
  "canonicalReference": "@alloy-js/typescript!ElseClause:function(1)",
@@ -0,0 +1,129 @@
1
+ import { code, namekey, Output } from "@alloy-js/core";
2
+ import "@alloy-js/core/testing";
3
+ import { d } from "@alloy-js/core/testing";
4
+ import { expect, it } from "vitest";
5
+ import * as ts from "../src/index.js";
6
+ import { decl, declMember, declType } from "../src/symbols/index.js";
7
+
8
+ it("declares variables", () => {
9
+ const hi = namekey("hi");
10
+
11
+ expect(
12
+ <Output>
13
+ <ts.SourceFile path="test1.ts">
14
+ {code`
15
+ const ${decl(hi)} = 12;
16
+ console.log(${hi});
17
+ `}
18
+ </ts.SourceFile>
19
+ <ts.SourceFile path="test2.ts">{hi};</ts.SourceFile>
20
+ </Output>,
21
+ ).toRenderTo({
22
+ "test1.ts": d`
23
+ const hi = 12;
24
+ console.log(hi);
25
+ `,
26
+ "test2.ts": d`
27
+ import { hi } from "./test1.js";
28
+
29
+ hi;
30
+ `,
31
+ });
32
+ });
33
+
34
+ it("declares types", () => {
35
+ const hi = namekey("hi");
36
+
37
+ expect(
38
+ <Output>
39
+ <ts.SourceFile path="test1.ts">
40
+ {code`
41
+ interface ${declType(hi)} {
42
+ value: number;
43
+ }
44
+ `}
45
+ </ts.SourceFile>
46
+ <ts.SourceFile path="test2.ts">
47
+ <ts.VarDeclaration name="test" type={hi} />
48
+ </ts.SourceFile>
49
+ </Output>,
50
+ ).toRenderTo({
51
+ "test1.ts": d`
52
+ interface hi {
53
+ value: number;
54
+ }
55
+ `,
56
+ "test2.ts": d`
57
+ import type { hi } from "./test1.js";
58
+
59
+ const test: hi
60
+ `,
61
+ });
62
+ });
63
+
64
+ it("declares variables", () => {
65
+ const hi = namekey("hi");
66
+
67
+ expect(
68
+ <Output>
69
+ <ts.SourceFile path="test1.ts">
70
+ {code`
71
+ interface ${declType(hi)} {
72
+ value: number;
73
+ }
74
+ `}
75
+ </ts.SourceFile>
76
+ <ts.SourceFile path="test2.ts">
77
+ <ts.VarDeclaration name="test" type={hi} />
78
+ </ts.SourceFile>
79
+ </Output>,
80
+ ).toRenderTo({
81
+ "test1.ts": d`
82
+ interface hi {
83
+ value: number;
84
+ }
85
+ `,
86
+ "test2.ts": d`
87
+ import type { hi } from "./test1.js";
88
+
89
+ const test: hi
90
+ `,
91
+ });
92
+ });
93
+
94
+ it("declares members", () => {
95
+ const publicMember = namekey("publicMember");
96
+ const privateMember = namekey("privateMember");
97
+ const staticMethod = namekey("staticMethod");
98
+ expect(
99
+ <Output>
100
+ <ts.SourceFile path="test1.ts">
101
+ <ts.ClassDeclaration name="Foo">
102
+ {code`
103
+ public ${declMember(publicMember)} = 12;
104
+ #${declMember(privateMember, { jsPrivate: true })} = 34;
105
+ constructor() {
106
+ ${publicMember} = 24;
107
+ ${privateMember} = 10;
108
+ }
109
+ `}
110
+ <hbr />
111
+ static ${declMember(staticMethod, { static: true })}() {"{}"};
112
+ </ts.ClassDeclaration>
113
+ <hbr />
114
+ {staticMethod}();
115
+ </ts.SourceFile>
116
+ </Output>,
117
+ ).toRenderTo(d`
118
+ class Foo {
119
+ public publicMember = 12;
120
+ #privateMember = 34;
121
+ constructor() {
122
+ this.publicMember = 24;
123
+ this.#privateMember = 10;
124
+ }
125
+ static $staticMethod() {};
126
+ }
127
+ Foo.staticMethod();
128
+ `);
129
+ });