@typespec/emitter-framework 0.6.0-dev.1 → 0.6.0-dev.4
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/dist/src/typescript/components/arrow-function.d.ts +17 -0
- package/dist/src/typescript/components/arrow-function.d.ts.map +1 -0
- package/dist/src/typescript/components/arrow-function.js +31 -0
- package/dist/src/typescript/components/arrow-function.js.map +1 -0
- package/dist/src/typescript/components/function-declaration.d.ts +5 -0
- package/dist/src/typescript/components/function-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/function-declaration.js +5 -1
- package/dist/src/typescript/components/function-declaration.js.map +1 -1
- package/dist/src/typescript/components/function-expression.d.ts +17 -0
- package/dist/src/typescript/components/function-expression.d.ts.map +1 -0
- package/dist/src/typescript/components/function-expression.js +31 -0
- package/dist/src/typescript/components/function-expression.js.map +1 -0
- package/dist/src/typescript/components/function-type.d.ts +17 -0
- package/dist/src/typescript/components/function-type.d.ts.map +1 -0
- package/dist/src/typescript/components/function-type.js +31 -0
- package/dist/src/typescript/components/function-type.js.map +1 -0
- package/dist/src/typescript/components/index.d.ts +6 -0
- package/dist/src/typescript/components/index.d.ts.map +1 -1
- package/dist/src/typescript/components/index.js +6 -0
- package/dist/src/typescript/components/index.js.map +1 -1
- package/dist/src/typescript/components/interface-member.d.ts +1 -2
- package/dist/src/typescript/components/interface-member.d.ts.map +1 -1
- package/dist/src/typescript/components/interface-member.js +3 -19
- package/dist/src/typescript/components/interface-member.js.map +1 -1
- package/dist/src/typescript/components/interface-method.d.ts +15 -0
- package/dist/src/typescript/components/interface-method.d.ts.map +1 -0
- package/dist/src/typescript/components/interface-method.js +34 -0
- package/dist/src/typescript/components/interface-method.js.map +1 -0
- package/dist/src/typescript/components/type-alias-declaration.d.ts +6 -2
- package/dist/src/typescript/components/type-alias-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/type-alias-declaration.js +7 -2
- package/dist/src/typescript/components/type-alias-declaration.js.map +1 -1
- package/dist/src/typescript/components/type-declaration.d.ts.map +1 -1
- package/dist/src/typescript/components/type-declaration.js +4 -0
- package/dist/src/typescript/components/type-declaration.js.map +1 -1
- package/dist/src/typescript/components/type-expression.d.ts +6 -0
- package/dist/src/typescript/components/type-expression.d.ts.map +1 -1
- package/dist/src/typescript/components/type-expression.js +6 -1
- package/dist/src/typescript/components/type-expression.js.map +1 -1
- package/dist/src/typescript/components/value-expression.d.ts +19 -0
- package/dist/src/typescript/components/value-expression.d.ts.map +1 -0
- package/dist/src/typescript/components/value-expression.js +72 -0
- package/dist/src/typescript/components/value-expression.js.map +1 -0
- package/dist/test/typescript/components/arrow-function.test.d.ts +2 -0
- package/dist/test/typescript/components/arrow-function.test.d.ts.map +1 -0
- package/dist/test/typescript/components/function-expression.test.d.ts +2 -0
- package/dist/test/typescript/components/function-expression.test.d.ts.map +1 -0
- package/dist/test/typescript/components/function-type.test.d.ts +2 -0
- package/dist/test/typescript/components/function-type.test.d.ts.map +1 -0
- package/dist/test/typescript/components/interface-method.test.d.ts +2 -0
- package/dist/test/typescript/components/interface-method.test.d.ts.map +1 -0
- package/dist/test/typescript/components/value-expression.test.d.ts +2 -0
- package/dist/test/typescript/components/value-expression.test.d.ts.map +1 -0
- package/dist/test/typescript/test-host.d.ts +5 -0
- package/dist/test/typescript/test-host.d.ts.map +1 -1
- package/dist/test/utils.d.ts +3 -2
- package/dist/test/utils.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/typescript/components/arrow-function.tsx +39 -0
- package/src/typescript/components/function-declaration.tsx +5 -2
- package/src/typescript/components/function-expression.tsx +41 -0
- package/src/typescript/components/function-type.tsx +38 -0
- package/src/typescript/components/index.ts +6 -0
- package/src/typescript/components/interface-member.tsx +2 -19
- package/src/typescript/components/interface-method.tsx +50 -0
- package/src/typescript/components/type-alias-declaration.tsx +10 -4
- package/src/typescript/components/type-declaration.tsx +2 -0
- package/src/typescript/components/type-expression.tsx +11 -2
- package/src/typescript/components/value-expression.tsx +56 -0
- package/test/typescript/components/arrow-function.test.tsx +85 -0
- package/test/typescript/components/function-expression.test.tsx +85 -0
- package/test/typescript/components/function-type.test.tsx +80 -0
- package/test/typescript/components/interface-declaration.test.tsx +6 -6
- package/test/typescript/components/interface-method.test.tsx +69 -0
- package/test/typescript/components/type-alias-declaration.test.tsx +20 -2
- package/test/typescript/components/value-expression.test.tsx +236 -0
- package/test/typescript/test-host.ts +8 -0
- package/test/utils.ts +8 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Children } from "@alloy-js/core";
|
|
2
|
+
import { Value } from "@typespec/compiler";
|
|
3
|
+
/**
|
|
4
|
+
* Properties for the {@link ValueExpression} component.
|
|
5
|
+
*/
|
|
6
|
+
interface ValueExpressionProps {
|
|
7
|
+
/**
|
|
8
|
+
* The TypeSpec value to be converted to a JavaScript expression.
|
|
9
|
+
*/
|
|
10
|
+
value: Value;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Generates a JavaScript value expression from a TypeSpec value.
|
|
14
|
+
* @param props properties for the value expression
|
|
15
|
+
* @returns {@link Children} representing the JavaScript value expression
|
|
16
|
+
*/
|
|
17
|
+
export declare function ValueExpression(props: Readonly<ValueExpressionProps>): Children;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=value-expression.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value-expression.d.ts","sourceRoot":"","sources":["../../../../src/typescript/components/value-expression.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAkB,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3D;;GAEG;AACH,UAAU,oBAAoB;IAC5B;;OAEG;IACH,KAAK,EAAE,KAAK,CAAC;CACd;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,QAAQ,CAAC,oBAAoB,CAAC,GAAG,QAAQ,CAoC/E"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { memo as _$memo } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
3
|
+
import * as ts from "@alloy-js/typescript";
|
|
4
|
+
import { compilerAssert } from "@typespec/compiler";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Properties for the {@link ValueExpression} component.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generates a JavaScript value expression from a TypeSpec value.
|
|
12
|
+
* @param props properties for the value expression
|
|
13
|
+
* @returns {@link Children} representing the JavaScript value expression
|
|
14
|
+
*/
|
|
15
|
+
export function ValueExpression(props) {
|
|
16
|
+
switch (props.value.valueKind) {
|
|
17
|
+
case "StringValue":
|
|
18
|
+
case "BooleanValue":
|
|
19
|
+
case "NullValue":
|
|
20
|
+
return _$createComponent(ts.ValueExpression, {
|
|
21
|
+
get jsValue() {
|
|
22
|
+
return props.value.value;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
case "NumericValue":
|
|
26
|
+
if (props.value.value.asNumber()) {
|
|
27
|
+
return _$createComponent(ts.ValueExpression, {
|
|
28
|
+
get jsValue() {
|
|
29
|
+
return props.value.value.asNumber();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
compilerAssert(props.value.value.isInteger, "BigInt value must be an integer", props.value);
|
|
34
|
+
return _$createComponent(ts.ValueExpression, {
|
|
35
|
+
get jsValue() {
|
|
36
|
+
return props.value.value.asBigInt();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
case "ArrayValue":
|
|
40
|
+
return _$createComponent(ts.ArrayExpression, {
|
|
41
|
+
get jsValue() {
|
|
42
|
+
return props.value.values.map(v => _$createComponent(ValueExpression, {
|
|
43
|
+
value: v
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
case "ScalarValue":
|
|
48
|
+
compilerAssert(props.value.value.name === "fromISO", `Unsupported scalar constructor ${props.value.value.name}`, props.value);
|
|
49
|
+
return _$createComponent(ValueExpression, {
|
|
50
|
+
get value() {
|
|
51
|
+
return props.value.value.args[0];
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
case "ObjectValue":
|
|
55
|
+
const jsProperties = {};
|
|
56
|
+
for (const [key, value] of props.value.properties) {
|
|
57
|
+
jsProperties[key] = ValueExpression({
|
|
58
|
+
value: value.value
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return _$createComponent(ts.ObjectExpression, {
|
|
62
|
+
jsValue: jsProperties
|
|
63
|
+
});
|
|
64
|
+
case "EnumValue":
|
|
65
|
+
return _$createComponent(ts.ValueExpression, {
|
|
66
|
+
get jsValue() {
|
|
67
|
+
return props.value.value.value ?? props.value.value.name;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=value-expression.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value-expression.js","names":["ts","compilerAssert","ValueExpression","props","value","valueKind","_$createComponent","jsValue","asNumber","isInteger","asBigInt","ArrayExpression","values","map","v","name","args","jsProperties","key","properties","ObjectExpression"],"sources":["../../../../src/typescript/components/value-expression.tsx"],"sourcesContent":[null],"mappings":";;AACA,OAAO,KAAKA,EAAE,MAAM,sBAAsB;AAC1C,SAASC,cAAc,QAAe,oBAAoB;;AAE1D;AACA;AACA;;AAQA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAACC,KAAqC,EAAY;EAC/E,QAAQA,KAAK,CAACC,KAAK,CAACC,SAAS;IAC3B,KAAK,aAAa;IAClB,KAAK,cAAc;IACnB,KAAK,WAAW;MACd,OAAAC,iBAAA,CAAQN,EAAE,CAACE,eAAe;QAAA,IAACK,OAAOA,CAAA;UAAA,OAAEJ,KAAK,CAACC,KAAK,CAACA,KAAK;QAAA;MAAA;IACvD,KAAK,cAAc;MACjB,IAAID,KAAK,CAACC,KAAK,CAACA,KAAK,CAACI,QAAQ,CAAC,CAAC,EAAE;QAChC,OAAAF,iBAAA,CAAQN,EAAE,CAACE,eAAe;UAAA,IAACK,OAAOA,CAAA;YAAA,OAAEJ,KAAK,CAACC,KAAK,CAACA,KAAK,CAACI,QAAQ,CAAC,CAAC;UAAA;QAAA;MAClE;MACAP,cAAc,CAACE,KAAK,CAACC,KAAK,CAACA,KAAK,CAACK,SAAS,EAAE,iCAAiC,EAAEN,KAAK,CAACC,KAAK,CAAC;MAC3F,OAAAE,iBAAA,CAAQN,EAAE,CAACE,eAAe;QAAA,IAACK,OAAOA,CAAA;UAAA,OAAEJ,KAAK,CAACC,KAAK,CAACA,KAAK,CAACM,QAAQ,CAAC,CAAC;QAAA;MAAA;IAClE,KAAK,YAAY;MACf,OAAAJ,iBAAA,CACGN,EAAE,CAACW,eAAe;QAAA,IACjBJ,OAAOA,CAAA;UAAA,OAAEJ,KAAK,CAACC,KAAK,CAACQ,MAAM,CAACC,GAAG,CAAEC,CAAC,IAAAR,iBAAA,CAC/BJ,eAAe;YAACE,KAAK,EAAEU;UAAC,EAC1B,CAAC;QAAA;MAAA;IAGR,KAAK,aAAa;MAChBb,cAAc,CACZE,KAAK,CAACC,KAAK,CAACA,KAAK,CAACW,IAAI,KAAK,SAAS,EACpC,kCAAkCZ,KAAK,CAACC,KAAK,CAACA,KAAK,CAACW,IAAI,EAAE,EAC1DZ,KAAK,CAACC,KACR,CAAC;MACD,OAAAE,iBAAA,CAAQJ,eAAe;QAAA,IAACE,KAAKA,CAAA;UAAA,OAAED,KAAK,CAACC,KAAK,CAACA,KAAK,CAACY,IAAI,CAAC,CAAC,CAAC;QAAA;MAAA;IAC1D,KAAK,aAAa;MAChB,MAAMC,YAAsC,GAAG,CAAC,CAAC;MACjD,KAAK,MAAM,CAACC,GAAG,EAAEd,KAAK,CAAC,IAAID,KAAK,CAACC,KAAK,CAACe,UAAU,EAAE;QACjDF,YAAY,CAACC,GAAG,CAAC,GAAGhB,eAAe,CAAC;UAAEE,KAAK,EAAEA,KAAK,CAACA;QAAM,CAAC,CAAC;MAC7D;MACA,OAAAE,iBAAA,CAAQN,EAAE,CAACoB,gBAAgB;QAACb,OAAO,EAAEU;MAAY;IACnD,KAAK,WAAW;MACd,OAAAX,iBAAA,CAAQN,EAAE,CAACE,eAAe;QAAA,IAACK,OAAOA,CAAA;UAAA,OAAEJ,KAAK,CAACC,KAAK,CAACA,KAAK,CAACA,KAAK,IAAID,KAAK,CAACC,KAAK,CAACA,KAAK,CAACW,IAAI;QAAA;MAAA;EACzF;AACF","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arrow-function.test.d.ts","sourceRoot":"","sources":["../../../../test/typescript/components/arrow-function.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-expression.test.d.ts","sourceRoot":"","sources":["../../../../test/typescript/components/function-expression.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-type.test.d.ts","sourceRoot":"","sources":["../../../../test/typescript/components/function-type.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface-method.test.d.ts","sourceRoot":"","sources":["../../../../test/typescript/components/interface-method.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value-expression.test.d.ts","sourceRoot":"","sources":["../../../../test/typescript/components/value-expression.test.tsx"],"names":[],"mappings":""}
|
|
@@ -8,4 +8,9 @@ export declare function createEmitterFrameworkTestRunner(options?: {
|
|
|
8
8
|
export declare function getProgram(code: string, options?: {
|
|
9
9
|
libraries: "Http"[];
|
|
10
10
|
}): Promise<Program>;
|
|
11
|
+
/**
|
|
12
|
+
* Initializes an empty program in the compiler.
|
|
13
|
+
* This is useful when you want to initialize the default TypeKits without any code.
|
|
14
|
+
*/
|
|
15
|
+
export declare function initEmptyProgram(): Promise<void>;
|
|
11
16
|
//# sourceMappingURL=test-host.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-host.d.ts","sourceRoot":"","sources":["../../../test/typescript/test-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAQ7C,wBAAsB,yBAAyB,CAC7C,OAAO,GAAE;IAAE,SAAS,EAAE,MAAM,EAAE,CAAA;CAAsB,0DASrD;AAED,wBAAsB,gCAAgC,CAAC,OAAO,GAAE;IAAE,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;CAAO,iEAK7F;AAED,wBAAsB,UAAU,CAC9B,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE;IAAE,SAAS,EAAE,MAAM,EAAE,CAAA;CAAsB,GACnD,OAAO,CAAC,OAAO,CAAC,CAUlB"}
|
|
1
|
+
{"version":3,"file":"test-host.d.ts","sourceRoot":"","sources":["../../../test/typescript/test-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAQ7C,wBAAsB,yBAAyB,CAC7C,OAAO,GAAE;IAAE,SAAS,EAAE,MAAM,EAAE,CAAA;CAAsB,0DASrD;AAED,wBAAsB,gCAAgC,CAAC,OAAO,GAAE;IAAE,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;CAAO,iEAK7F;AAED,wBAAsB,UAAU,CAC9B,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE;IAAE,SAAS,EAAE,MAAM,EAAE,CAAA;CAAsB,GACnD,OAAO,CAAC,OAAO,CAAC,CAUlB;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAEtD"}
|
package/dist/test/utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Children } from "@alloy-js/core";
|
|
1
|
+
import { Children, OutputDirectory } from "@alloy-js/core";
|
|
2
2
|
import { Program } from "@typespec/compiler";
|
|
3
|
-
export declare function getEmitOutput(tspCode: string, cb: (program: Program) => Children): Promise<string | (
|
|
3
|
+
export declare function getEmitOutput(tspCode: string, cb: (program: Program) => Children): Promise<string | (OutputDirectory | import("@alloy-js/core").OutputFile)[]>;
|
|
4
|
+
export declare function assertFileContents(res: OutputDirectory, contents: string): void;
|
|
4
5
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/test/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../test/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAU,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../test/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAU,MAAM,gBAAgB,CAAC;AAGnE,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAI7C,wBAAsB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,QAAQ,+EAOtF;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,QAIxE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/emitter-framework",
|
|
3
|
-
"version": "0.6.0-dev.
|
|
3
|
+
"version": "0.6.0-dev.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -19,16 +19,16 @@
|
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"description": "",
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@alloy-js/core": "^0.
|
|
23
|
-
"@alloy-js/typescript": "^0.
|
|
22
|
+
"@alloy-js/core": "^0.11.0",
|
|
23
|
+
"@alloy-js/typescript": "^0.11.0",
|
|
24
24
|
"@typespec/compiler": "^1.0.0-rc.0",
|
|
25
25
|
"@typespec/http": "^1.0.0-rc.0",
|
|
26
26
|
"@typespec/rest": "^0.68.0 || >=0.69.0-dev <0.69.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@alloy-js/babel-preset": "^0.2.0",
|
|
30
|
-
"@alloy-js/core": "^0.
|
|
31
|
-
"@alloy-js/typescript": "^0.
|
|
30
|
+
"@alloy-js/core": "^0.11.0",
|
|
31
|
+
"@alloy-js/typescript": "^0.11.0",
|
|
32
32
|
"@babel/cli": "^7.24.8",
|
|
33
33
|
"@babel/core": "^7.26.10",
|
|
34
34
|
"@rollup/plugin-babel": "^6.0.4",
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { splitProps } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { Operation } from "@typespec/compiler";
|
|
4
|
+
import { buildParameterDescriptors, getReturnType } from "../utils/operation.js";
|
|
5
|
+
import { TypeExpression } from "./type-expression.jsx";
|
|
6
|
+
|
|
7
|
+
export interface ArrowFunctionProps extends ts.ArrowFunctionProps {
|
|
8
|
+
type?: Operation;
|
|
9
|
+
/**
|
|
10
|
+
* Where the parameters from passed to the `parameters` prop should be placed
|
|
11
|
+
* relative the ones created from the T`ypeSpec operation.
|
|
12
|
+
*/
|
|
13
|
+
parametersMode?: "prepend" | "append" | "replace";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A TypeScript arrow function. Pass the `type` prop to create the arrow
|
|
18
|
+
* function by converting from a TypeSpec Operation. Any other props provided
|
|
19
|
+
* will take precedence.
|
|
20
|
+
*/
|
|
21
|
+
export function ArrowFunction(props: Readonly<ArrowFunctionProps>) {
|
|
22
|
+
const [efProps, updateProps, forwardProps] = splitProps(
|
|
23
|
+
props,
|
|
24
|
+
["type"],
|
|
25
|
+
["returnType", "parameters"],
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
if (!efProps.type) {
|
|
29
|
+
return <ts.ArrowFunction {...forwardProps} {...updateProps} />;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const returnType = props.returnType ?? <TypeExpression type={getReturnType(efProps.type)} />;
|
|
33
|
+
const allParameters = buildParameterDescriptors(efProps.type.parameters, {
|
|
34
|
+
params: props.parameters,
|
|
35
|
+
mode: props.parametersMode,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return <ts.ArrowFunction {...forwardProps} returnType={returnType} parameters={allParameters} />;
|
|
39
|
+
}
|
|
@@ -15,10 +15,13 @@ export type FunctionDeclarationProps =
|
|
|
15
15
|
| FunctionDeclarationPropsWithType
|
|
16
16
|
| ts.FunctionDeclarationProps;
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* A TypeScript function declaration. Pass the `type` prop to create the
|
|
20
|
+
* function declaration by converting from a TypeSpec Operation. Any other props
|
|
21
|
+
* provided will take precedence.
|
|
22
|
+
*/
|
|
18
23
|
export function FunctionDeclaration(props: FunctionDeclarationProps) {
|
|
19
24
|
if (!isTypedFunctionDeclarationProps(props)) {
|
|
20
|
-
if (!props.name) {
|
|
21
|
-
}
|
|
22
25
|
return <ts.FunctionDeclaration {...props} />;
|
|
23
26
|
}
|
|
24
27
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { splitProps } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { Operation } from "@typespec/compiler";
|
|
4
|
+
import { buildParameterDescriptors, getReturnType } from "../utils/operation.js";
|
|
5
|
+
import { TypeExpression } from "./type-expression.jsx";
|
|
6
|
+
|
|
7
|
+
export interface FunctionExpressionProps extends ts.FunctionExpressionProps {
|
|
8
|
+
type?: Operation;
|
|
9
|
+
/**
|
|
10
|
+
* Where the parameters from passed to the `parameters` prop should be placed
|
|
11
|
+
* relative the ones created from the TypeSpec operation.
|
|
12
|
+
*/
|
|
13
|
+
parametersMode?: "prepend" | "append" | "replace";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A TypeScript function expression. Pass the `type` prop to create the
|
|
18
|
+
* function expression by converting from a TypeSpec Operation. Any other props
|
|
19
|
+
* provided will take precedence.
|
|
20
|
+
*/
|
|
21
|
+
export function FunctionExpression(props: Readonly<FunctionExpressionProps>) {
|
|
22
|
+
const [efProps, updateProps, forwardProps] = splitProps(
|
|
23
|
+
props,
|
|
24
|
+
["type"],
|
|
25
|
+
["returnType", "parameters"],
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
if (!efProps.type) {
|
|
29
|
+
return <ts.FunctionExpression {...forwardProps} {...updateProps} />;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const returnType = props.returnType ?? <TypeExpression type={getReturnType(efProps.type)} />;
|
|
33
|
+
const allParameters = buildParameterDescriptors(efProps.type.parameters, {
|
|
34
|
+
params: props.parameters,
|
|
35
|
+
mode: props.parametersMode,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<ts.FunctionExpression {...forwardProps} returnType={returnType} parameters={allParameters} />
|
|
40
|
+
);
|
|
41
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { splitProps } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { Operation } from "@typespec/compiler";
|
|
4
|
+
import { buildParameterDescriptors, getReturnType } from "../utils/operation.js";
|
|
5
|
+
import { TypeExpression } from "./type-expression.jsx";
|
|
6
|
+
|
|
7
|
+
export interface FunctionTypeProps extends ts.FunctionTypeProps {
|
|
8
|
+
type?: Operation;
|
|
9
|
+
/**
|
|
10
|
+
* Where the parameters from passed to the `parameters` prop should be placed
|
|
11
|
+
* relative the ones created from the TypeSpec operation.
|
|
12
|
+
*/
|
|
13
|
+
parametersMode?: "prepend" | "append" | "replace";
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A TypeScript function type. Pass the `type` prop to create the function type
|
|
17
|
+
* by converting from a TypeSpec Operation. Any other props provided will take
|
|
18
|
+
* precedence.
|
|
19
|
+
*/
|
|
20
|
+
export function FunctionType(props: Readonly<FunctionTypeProps>) {
|
|
21
|
+
const [efProps, updateProps, forwardProps] = splitProps(
|
|
22
|
+
props,
|
|
23
|
+
["type"],
|
|
24
|
+
["returnType", "parameters"],
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
if (!efProps.type) {
|
|
28
|
+
return <ts.FunctionType {...forwardProps} {...updateProps} />;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const returnType = props.returnType ?? <TypeExpression type={getReturnType(efProps.type)} />;
|
|
32
|
+
const allParameters = buildParameterDescriptors(efProps.type.parameters, {
|
|
33
|
+
params: props.parameters,
|
|
34
|
+
mode: props.parametersMode,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return <ts.FunctionType {...forwardProps} returnType={returnType} parameters={allParameters} />;
|
|
38
|
+
}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
+
export * from "./array-expression.jsx";
|
|
2
|
+
export * from "./arrow-function.jsx";
|
|
1
3
|
export * from "./class-method.js";
|
|
2
4
|
export * from "./function-declaration.js";
|
|
5
|
+
export * from "./function-expression.js";
|
|
6
|
+
export * from "./function-type.js";
|
|
3
7
|
export * from "./interface-declaration.js";
|
|
4
8
|
export * from "./interface-member.js";
|
|
9
|
+
export * from "./interface-method.js";
|
|
5
10
|
export * from "./static-serializers.js";
|
|
6
11
|
export * from "./type-declaration.js";
|
|
7
12
|
export * from "./type-expression.js";
|
|
8
13
|
export * from "./type-transform.js";
|
|
9
14
|
export * from "./union-declaration.js";
|
|
10
15
|
export * from "./union-expression.js";
|
|
16
|
+
export * from "./value-expression.js";
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import * as ay from "@alloy-js/core";
|
|
2
1
|
import * as ts from "@alloy-js/typescript";
|
|
3
2
|
import { isNeverType, ModelProperty, Operation } from "@typespec/compiler";
|
|
4
3
|
import { $ } from "@typespec/compiler/experimental/typekit";
|
|
5
4
|
import { getHttpPart } from "@typespec/http";
|
|
6
|
-
import {
|
|
5
|
+
import { InterfaceMethod } from "./interface-method.jsx";
|
|
7
6
|
import { TypeExpression } from "./type-expression.js";
|
|
8
7
|
|
|
9
8
|
export interface InterfaceMemberProps {
|
|
@@ -36,22 +35,6 @@ export function InterfaceMember(props: InterfaceMemberProps) {
|
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
if ($.operation.is(props.type)) {
|
|
39
|
-
|
|
40
|
-
const params = (
|
|
41
|
-
<ay.Scope>
|
|
42
|
-
<FunctionDeclaration.Parameters type={props.type.parameters} />
|
|
43
|
-
</ay.Scope>
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
return (
|
|
47
|
-
<ts.InterfaceMember
|
|
48
|
-
name={name}
|
|
49
|
-
type={
|
|
50
|
-
<>
|
|
51
|
-
({params}) => {returnType}
|
|
52
|
-
</>
|
|
53
|
-
}
|
|
54
|
-
/>
|
|
55
|
-
);
|
|
38
|
+
return <InterfaceMethod type={props.type} />;
|
|
56
39
|
}
|
|
57
40
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { splitProps } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { Operation } from "@typespec/compiler";
|
|
4
|
+
import { buildParameterDescriptors, getReturnType } from "../utils/operation.js";
|
|
5
|
+
import { TypeExpression } from "./type-expression.jsx";
|
|
6
|
+
|
|
7
|
+
export interface InterfaceMethodPropsWithType extends Omit<ts.InterfaceMethodProps, "name"> {
|
|
8
|
+
type: Operation;
|
|
9
|
+
name?: string;
|
|
10
|
+
parametersMode?: "prepend" | "append" | "replace";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type InterfaceMethodProps = InterfaceMethodPropsWithType | ts.InterfaceMethodProps;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A TypeScript interface method. Pass the `type` prop to create the
|
|
17
|
+
* method by converting from a TypeSpec Operation. Any other props
|
|
18
|
+
* provided will take precedence.
|
|
19
|
+
*/
|
|
20
|
+
export function InterfaceMethod(props: Readonly<InterfaceMethodProps>) {
|
|
21
|
+
const isTypeSpecTyped = "type" in props;
|
|
22
|
+
if (!isTypeSpecTyped) {
|
|
23
|
+
return <ts.InterfaceMethod {...props} />;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const [efProps, updateProps, forwardProps] = splitProps(
|
|
27
|
+
props,
|
|
28
|
+
["type"],
|
|
29
|
+
["returnType", "parameters"],
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const name = props.name
|
|
33
|
+
? props.name
|
|
34
|
+
: ts.useTSNamePolicy().getName(efProps.type.name, "function");
|
|
35
|
+
const returnType = props.returnType ?? <TypeExpression type={getReturnType(efProps.type)} />;
|
|
36
|
+
const allParameters = buildParameterDescriptors(efProps.type.parameters, {
|
|
37
|
+
params: props.parameters,
|
|
38
|
+
mode: props.parametersMode,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<ts.InterfaceMethod
|
|
43
|
+
{...forwardProps}
|
|
44
|
+
name={name}
|
|
45
|
+
returnType={returnType}
|
|
46
|
+
parameters={allParameters}
|
|
47
|
+
{...updateProps}
|
|
48
|
+
/>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -1,23 +1,29 @@
|
|
|
1
1
|
import { refkey as getRefkey } from "@alloy-js/core";
|
|
2
2
|
import * as ts from "@alloy-js/typescript";
|
|
3
|
-
import {
|
|
3
|
+
import { Type } from "@typespec/compiler";
|
|
4
4
|
import { $ } from "@typespec/compiler/experimental/typekit";
|
|
5
5
|
import { reportDiagnostic } from "../../lib.js";
|
|
6
6
|
import { TypeExpression } from "./type-expression.jsx";
|
|
7
7
|
|
|
8
8
|
export interface TypedAliasDeclarationProps extends Omit<ts.TypeDeclarationProps, "name"> {
|
|
9
|
-
type:
|
|
9
|
+
type: Type;
|
|
10
10
|
name?: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export type TypeAliasDeclarationProps = TypedAliasDeclarationProps | ts.TypeDeclarationProps;
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Create a TypeScript type alias declaration. Pass the `type` prop to emit the
|
|
17
|
+
* type alias as the provided TypeSpec type.
|
|
18
|
+
*/
|
|
15
19
|
export function TypeAliasDeclaration(props: TypeAliasDeclarationProps) {
|
|
16
20
|
if (!isTypedAliasDeclarationProps(props)) {
|
|
17
21
|
return <ts.TypeDeclaration {...props}>{props.children}</ts.TypeDeclaration>;
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
const originalName =
|
|
24
|
+
const originalName =
|
|
25
|
+
props.name ??
|
|
26
|
+
("name" in props.type && typeof props.type.name === "string" ? props.type.name : "");
|
|
21
27
|
|
|
22
28
|
if (!originalName || originalName === "") {
|
|
23
29
|
reportDiagnostic($.program, { code: "type-declaration-missing-name", target: props.type });
|
|
@@ -26,7 +32,7 @@ export function TypeAliasDeclaration(props: TypeAliasDeclarationProps) {
|
|
|
26
32
|
const name = ts.useTSNamePolicy().getName(originalName, "type");
|
|
27
33
|
return (
|
|
28
34
|
<ts.TypeDeclaration {...props} name={name} refkey={props.refkey ?? getRefkey(props.type)}>
|
|
29
|
-
<TypeExpression type={props.type} />
|
|
35
|
+
<TypeExpression type={props.type} noReference />
|
|
30
36
|
{props.children}
|
|
31
37
|
</ts.TypeDeclaration>
|
|
32
38
|
);
|
|
@@ -27,5 +27,7 @@ export function TypeDeclaration(props: TypeDeclarationProps) {
|
|
|
27
27
|
return <EnumDeclaration type={type} {...restProps} />;
|
|
28
28
|
case "Scalar":
|
|
29
29
|
return <TypeAliasDeclaration type={type} {...restProps} />;
|
|
30
|
+
case "Operation":
|
|
31
|
+
return <TypeAliasDeclaration type={type} {...restProps} />;
|
|
30
32
|
}
|
|
31
33
|
}
|
|
@@ -5,17 +5,25 @@ import { $ } from "@typespec/compiler/experimental/typekit";
|
|
|
5
5
|
import "@typespec/http/experimental/typekit";
|
|
6
6
|
import { reportTypescriptDiagnostic } from "../../typescript/lib.js";
|
|
7
7
|
import { ArrayExpression } from "./array-expression.js";
|
|
8
|
+
import { FunctionType } from "./function-type.js";
|
|
8
9
|
import { InterfaceExpression } from "./interface-declaration.js";
|
|
9
10
|
import { RecordExpression } from "./record-expression.js";
|
|
10
11
|
import { UnionExpression } from "./union-expression.js";
|
|
11
12
|
|
|
12
13
|
export interface TypeExpressionProps {
|
|
13
14
|
type: Type;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Whether to disallow references. Setting this will force the type to be
|
|
18
|
+
* emitted inline, even if it is a declaration that would otherwise be
|
|
19
|
+
* referenced.
|
|
20
|
+
*/
|
|
21
|
+
noReference?: boolean;
|
|
14
22
|
}
|
|
15
23
|
|
|
16
24
|
export function TypeExpression(props: TypeExpressionProps) {
|
|
17
25
|
const type = $.httpPart.unpack(props.type);
|
|
18
|
-
if (isDeclaration(type)) {
|
|
26
|
+
if (!props.noReference && isDeclaration(type)) {
|
|
19
27
|
// todo: probably need abstraction around deciding what's a declaration in the output
|
|
20
28
|
// (it may not correspond to things which are declarations in TypeSpec?)
|
|
21
29
|
return <Reference refkey={refkey(type)} />;
|
|
@@ -64,7 +72,8 @@ export function TypeExpression(props: TypeExpressionProps) {
|
|
|
64
72
|
}
|
|
65
73
|
|
|
66
74
|
return <InterfaceExpression type={type} />;
|
|
67
|
-
|
|
75
|
+
case "Operation":
|
|
76
|
+
return <FunctionType type={type} />;
|
|
68
77
|
default:
|
|
69
78
|
reportTypescriptDiagnostic($.program, { code: "typescript-unsupported-type", target: type });
|
|
70
79
|
return "any";
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Children } from "@alloy-js/core";
|
|
2
|
+
import * as ts from "@alloy-js/typescript";
|
|
3
|
+
import { compilerAssert, Value } from "@typespec/compiler";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Properties for the {@link ValueExpression} component.
|
|
7
|
+
*/
|
|
8
|
+
interface ValueExpressionProps {
|
|
9
|
+
/**
|
|
10
|
+
* The TypeSpec value to be converted to a JavaScript expression.
|
|
11
|
+
*/
|
|
12
|
+
value: Value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generates a JavaScript value expression from a TypeSpec value.
|
|
17
|
+
* @param props properties for the value expression
|
|
18
|
+
* @returns {@link Children} representing the JavaScript value expression
|
|
19
|
+
*/
|
|
20
|
+
export function ValueExpression(props: Readonly<ValueExpressionProps>): Children {
|
|
21
|
+
switch (props.value.valueKind) {
|
|
22
|
+
case "StringValue":
|
|
23
|
+
case "BooleanValue":
|
|
24
|
+
case "NullValue":
|
|
25
|
+
return <ts.ValueExpression jsValue={props.value.value} />;
|
|
26
|
+
case "NumericValue":
|
|
27
|
+
if (props.value.value.asNumber()) {
|
|
28
|
+
return <ts.ValueExpression jsValue={props.value.value.asNumber()} />;
|
|
29
|
+
}
|
|
30
|
+
compilerAssert(props.value.value.isInteger, "BigInt value must be an integer", props.value);
|
|
31
|
+
return <ts.ValueExpression jsValue={props.value.value.asBigInt()} />;
|
|
32
|
+
case "ArrayValue":
|
|
33
|
+
return (
|
|
34
|
+
<ts.ArrayExpression
|
|
35
|
+
jsValue={props.value.values.map((v) => (
|
|
36
|
+
<ValueExpression value={v} />
|
|
37
|
+
))}
|
|
38
|
+
/>
|
|
39
|
+
);
|
|
40
|
+
case "ScalarValue":
|
|
41
|
+
compilerAssert(
|
|
42
|
+
props.value.value.name === "fromISO",
|
|
43
|
+
`Unsupported scalar constructor ${props.value.value.name}`,
|
|
44
|
+
props.value,
|
|
45
|
+
);
|
|
46
|
+
return <ValueExpression value={props.value.value.args[0]} />;
|
|
47
|
+
case "ObjectValue":
|
|
48
|
+
const jsProperties: Record<string, Children> = {};
|
|
49
|
+
for (const [key, value] of props.value.properties) {
|
|
50
|
+
jsProperties[key] = ValueExpression({ value: value.value });
|
|
51
|
+
}
|
|
52
|
+
return <ts.ObjectExpression jsValue={jsProperties} />;
|
|
53
|
+
case "EnumValue":
|
|
54
|
+
return <ts.ValueExpression jsValue={props.value.value.value ?? props.value.value.name} />;
|
|
55
|
+
}
|
|
56
|
+
}
|