@alloy-js/typescript 0.10.0 → 0.11.0
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/CHANGELOG.md +15 -0
- package/dist/src/builtins/node.js +1 -2
- package/dist/src/components/ArrayExpression.js +2 -5
- package/dist/src/components/ArrowFunction.d.ts +40 -0
- package/dist/src/components/ArrowFunction.d.ts.map +1 -0
- package/dist/src/components/ArrowFunction.js +67 -0
- package/dist/src/components/BarrelFile.js +1 -2
- package/dist/src/components/BlockScope.js +2 -4
- package/dist/src/components/CallSignature.js +2 -4
- package/dist/src/components/ClassDeclaration.js +2 -6
- package/dist/src/components/CommaList.js +2 -5
- package/dist/src/components/Declaration.js +3 -5
- package/dist/src/components/EnumDeclaration.js +2 -5
- package/dist/src/components/EnumMember.js +2 -4
- package/dist/src/components/ExportStatement.js +1 -2
- package/dist/src/components/FunctionBase.d.ts +60 -0
- package/dist/src/components/FunctionBase.d.ts.map +1 -0
- package/dist/src/components/FunctionBase.js +207 -0
- package/dist/src/components/FunctionCallExpression.js +2 -5
- package/dist/src/components/FunctionDeclaration.d.ts +3 -18
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
- package/dist/src/components/FunctionDeclaration.js +9 -157
- package/dist/src/components/FunctionExpression.d.ts +39 -0
- package/dist/src/components/FunctionExpression.d.ts.map +1 -0
- package/dist/src/components/FunctionExpression.js +66 -0
- package/dist/src/components/FunctionType.d.ts +39 -0
- package/dist/src/components/FunctionType.d.ts.map +1 -0
- package/dist/src/components/FunctionType.js +73 -0
- package/dist/src/components/IfStatement.js +2 -4
- package/dist/src/components/ImportStatement.js +1 -2
- package/dist/src/components/Interface.d.ts.map +1 -1
- package/dist/src/components/Interface.js +32 -11
- package/dist/src/components/InterfaceMethod.d.ts +43 -0
- package/dist/src/components/InterfaceMethod.d.ts.map +1 -0
- package/dist/src/components/InterfaceMethod.js +87 -0
- package/dist/src/components/JSDoc.js +1 -2
- package/dist/src/components/JSDocComment.js +2 -4
- package/dist/src/components/JSDocExample.js +2 -5
- package/dist/src/components/JSDocParam.js +2 -5
- package/dist/src/components/MemberChainExpression.js +2 -4
- package/dist/src/components/MemberDeclaration.d.ts +30 -0
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -0
- package/dist/src/components/MemberDeclaration.js +29 -0
- package/dist/src/components/MemberExpression.js +2 -5
- package/dist/src/components/ObjectExpression.js +2 -5
- package/dist/src/components/PackageDirectory.js +2 -5
- package/dist/src/components/PackageJson.js +1 -2
- package/dist/src/components/PropertyName.js +1 -2
- package/dist/src/components/Reference.js +1 -2
- package/dist/src/components/SingleLineCommentBlock.js +2 -4
- package/dist/src/components/SourceFile.js +2 -4
- package/dist/src/components/SwitchStatement.js +2 -5
- package/dist/src/components/TsConfigJson.js +1 -2
- package/dist/src/components/TypeDeclaration.js +2 -6
- package/dist/src/components/ValueExpression.js +1 -2
- package/dist/src/components/VarDeclaration.js +2 -5
- package/dist/src/components/index.d.ts +6 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +6 -1
- package/dist/src/components/stc/index.d.ts +7 -0
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +8 -2
- package/dist/src/context/private-scope.js +1 -2
- package/dist/src/create-package.js +1 -2
- package/dist/src/index.js +1 -2
- package/dist/src/name-conflict-resolver.js +1 -2
- package/dist/src/name-policy.js +1 -2
- package/dist/src/parameter-descriptor.d.ts +27 -17
- package/dist/src/parameter-descriptor.d.ts.map +1 -1
- package/dist/src/source-directory-data.js +1 -2
- package/dist/src/symbols/index.js +1 -2
- package/dist/src/symbols/reference.js +2 -3
- package/dist/src/symbols/scopes.js +1 -2
- package/dist/src/symbols/ts-member-scope.js +1 -2
- package/dist/src/symbols/ts-module-scope.js +1 -2
- package/dist/src/symbols/ts-output-symbol.js +1 -2
- package/dist/src/symbols/ts-package-scope.js +1 -2
- package/dist/src/utils.js +1 -2
- package/dist/test/array.test.js +105 -0
- package/dist/test/arrow-function.test.d.ts +2 -0
- package/dist/test/arrow-function.test.d.ts.map +1 -0
- package/dist/test/arrow-function.test.js +194 -0
- package/dist/test/barrel.test.js +53 -0
- package/dist/test/basic.test.js +91 -0
- package/dist/test/block-scope.test.js +30 -0
- package/dist/test/class.test.js +438 -0
- package/dist/test/enum-jsdoc.test.js +88 -0
- package/dist/test/enum.test.js +247 -0
- package/dist/test/externals.test.js +145 -0
- package/dist/test/function-call-expression.test.js +87 -0
- package/dist/test/function-declaration-jsdoc.test.js +91 -0
- package/dist/test/function-declaration.test.js +228 -0
- package/dist/test/function-expression.test.d.ts +2 -0
- package/dist/test/function-expression.test.d.ts.map +1 -0
- package/dist/test/function-expression.test.js +194 -0
- package/dist/test/function-type.test.d.ts +2 -0
- package/dist/test/function-type.test.d.ts.map +1 -0
- package/dist/test/function-type.test.js +122 -0
- package/dist/test/if-statement.test.js +25 -0
- package/dist/test/imports.test.js +434 -0
- package/dist/test/interface-jsdoc.test.js +178 -0
- package/dist/test/interface.test.d.ts.map +1 -1
- package/dist/test/interface.test.js +490 -0
- package/dist/test/jscomment.test.js +70 -0
- package/dist/test/jsdoc.test.js +134 -0
- package/dist/test/jsdocparam.test.js +164 -0
- package/dist/test/member-chain.test.js +141 -0
- package/dist/test/member-expression.test.js +359 -0
- package/dist/test/name-conflict.test.js +108 -0
- package/dist/test/name-policy.test.js +122 -0
- package/dist/test/package-external.test.js +196 -0
- package/dist/test/package-json.test.js +93 -0
- package/dist/test/package.test.js +136 -0
- package/dist/test/reference.test.js +81 -0
- package/dist/test/source-file.test.js +36 -0
- package/dist/test/switch-statement.test.js +61 -0
- package/dist/test/utils.js +45 -0
- package/dist/test/value-expression.test.js +30 -0
- package/dist/test/values.test.js +351 -0
- package/dist/test/var-declaration-jsdoc.test.js +50 -0
- package/dist/test/var-declaration.test.js +63 -0
- package/dist/test/vitest.d.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +12 -17
- package/src/components/ArrowFunction.tsx +87 -0
- package/src/components/Declaration.tsx +1 -1
- package/src/components/FunctionBase.tsx +271 -0
- package/src/components/FunctionDeclaration.tsx +13 -195
- package/src/components/FunctionExpression.tsx +86 -0
- package/src/components/FunctionType.tsx +88 -0
- package/src/components/Interface.tsx +33 -5
- package/src/components/InterfaceMethod.tsx +96 -0
- package/src/components/MemberDeclaration.tsx +70 -0
- package/src/components/index.ts +10 -0
- package/src/components/stc/index.ts +7 -0
- package/src/parameter-descriptor.ts +29 -18
- package/src/symbols/reference.ts +4 -1
- package/temp/api.json +2605 -680
- package/test/arrow-function.test.tsx +188 -0
- package/test/class.test.tsx +1 -2
- package/test/function-call-expression.test.tsx +1 -2
- package/test/function-declaration.test.tsx +1 -2
- package/test/function-expression.test.tsx +195 -0
- package/test/function-type.test.tsx +118 -0
- package/test/interface.test.tsx +256 -6
- package/tsdoc-metadata.json +1 -1
- package/babel.config.cjs +0 -4
- package/dist/src/builtins/index.js.map +0 -1
- package/dist/src/builtins/node.js.map +0 -1
- package/dist/src/components/ArrayExpression.js.map +0 -1
- package/dist/src/components/BarrelFile.js.map +0 -1
- package/dist/src/components/BlockScope.js.map +0 -1
- package/dist/src/components/CallSignature.js.map +0 -1
- package/dist/src/components/ClassDeclaration.js.map +0 -1
- package/dist/src/components/CommaList.js.map +0 -1
- package/dist/src/components/Declaration.js.map +0 -1
- package/dist/src/components/EnumDeclaration.js.map +0 -1
- package/dist/src/components/EnumMember.js.map +0 -1
- package/dist/src/components/ExportStatement.js.map +0 -1
- package/dist/src/components/FunctionCallExpression.js.map +0 -1
- package/dist/src/components/FunctionDeclaration.js.map +0 -1
- package/dist/src/components/IfStatement.js.map +0 -1
- package/dist/src/components/ImportStatement.js.map +0 -1
- package/dist/src/components/Interface.js.map +0 -1
- package/dist/src/components/JSDoc.js.map +0 -1
- package/dist/src/components/JSDocComment.js.map +0 -1
- package/dist/src/components/JSDocExample.js.map +0 -1
- package/dist/src/components/JSDocParam.js.map +0 -1
- package/dist/src/components/MemberChainExpression.js.map +0 -1
- package/dist/src/components/MemberExpression.js.map +0 -1
- package/dist/src/components/ObjectExpression.js.map +0 -1
- package/dist/src/components/PackageDirectory.js.map +0 -1
- package/dist/src/components/PackageJson.js.map +0 -1
- package/dist/src/components/PropertyName.js.map +0 -1
- package/dist/src/components/Reference.js.map +0 -1
- package/dist/src/components/SingleLineCommentBlock.js.map +0 -1
- package/dist/src/components/SourceFile.js.map +0 -1
- package/dist/src/components/SwitchStatement.js.map +0 -1
- package/dist/src/components/TsConfigJson.js.map +0 -1
- package/dist/src/components/TypeDeclaration.js.map +0 -1
- package/dist/src/components/ValueExpression.js.map +0 -1
- package/dist/src/components/VarDeclaration.js.map +0 -1
- package/dist/src/components/index.js.map +0 -1
- package/dist/src/components/stc/index.js.map +0 -1
- package/dist/src/context/index.js.map +0 -1
- package/dist/src/context/private-scope.js.map +0 -1
- package/dist/src/create-package.js.map +0 -1
- package/dist/src/index.js.map +0 -1
- package/dist/src/name-conflict-resolver.js.map +0 -1
- package/dist/src/name-policy.js.map +0 -1
- package/dist/src/parameter-descriptor.js.map +0 -1
- package/dist/src/source-directory-data.js.map +0 -1
- package/dist/src/symbols/index.js.map +0 -1
- package/dist/src/symbols/reference.js.map +0 -1
- package/dist/src/symbols/scopes.js.map +0 -1
- package/dist/src/symbols/ts-member-scope.js.map +0 -1
- package/dist/src/symbols/ts-module-scope.js.map +0 -1
- package/dist/src/symbols/ts-output-symbol.js.map +0 -1
- package/dist/src/symbols/ts-package-scope.js.map +0 -1
- package/dist/src/utils.js.map +0 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { Declaration, Output, refkey, render } from "@alloy-js/core";
|
|
3
|
+
import "@alloy-js/core/testing";
|
|
4
|
+
import { it } from "vitest";
|
|
5
|
+
import * as ts from "../src/components/index.js";
|
|
6
|
+
import { Reference } from "../src/components/Reference.js";
|
|
7
|
+
import { assertFileContents } from "./utils.js";
|
|
8
|
+
it("works with back references", () => {
|
|
9
|
+
const res = render(_$createComponent(Output, {
|
|
10
|
+
get children() {
|
|
11
|
+
return [_$createComponent(ts.SourceFile, {
|
|
12
|
+
path: "test1.ts",
|
|
13
|
+
get children() {
|
|
14
|
+
return _$createComponent(Declaration, {
|
|
15
|
+
name: "foo",
|
|
16
|
+
get refkey() {
|
|
17
|
+
return refkey("foo");
|
|
18
|
+
},
|
|
19
|
+
children: "const foo = 1;"
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}), _$createComponent(ts.SourceFile, {
|
|
23
|
+
path: "test2.ts",
|
|
24
|
+
get children() {
|
|
25
|
+
return ["const v = ", _$createComponent(Reference, {
|
|
26
|
+
get refkey() {
|
|
27
|
+
return refkey("foo");
|
|
28
|
+
}
|
|
29
|
+
}), ";"];
|
|
30
|
+
}
|
|
31
|
+
})];
|
|
32
|
+
}
|
|
33
|
+
}));
|
|
34
|
+
assertFileContents(res, {
|
|
35
|
+
"test1.ts": `
|
|
36
|
+
const foo = 1;
|
|
37
|
+
`,
|
|
38
|
+
"test2.ts": `
|
|
39
|
+
import { foo } from "./test1.js";
|
|
40
|
+
|
|
41
|
+
const v = foo;
|
|
42
|
+
`
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
it("works with forward references", () => {
|
|
46
|
+
const res = render(_$createComponent(Output, {
|
|
47
|
+
get children() {
|
|
48
|
+
return [_$createComponent(ts.SourceFile, {
|
|
49
|
+
path: "test2.ts",
|
|
50
|
+
get children() {
|
|
51
|
+
return ["const v = ", _$createComponent(Reference, {
|
|
52
|
+
get refkey() {
|
|
53
|
+
return refkey("foo");
|
|
54
|
+
}
|
|
55
|
+
}), ";"];
|
|
56
|
+
}
|
|
57
|
+
}), _$createComponent(ts.SourceFile, {
|
|
58
|
+
path: "test1.ts",
|
|
59
|
+
get children() {
|
|
60
|
+
return _$createComponent(Declaration, {
|
|
61
|
+
name: "foo",
|
|
62
|
+
get refkey() {
|
|
63
|
+
return refkey("foo");
|
|
64
|
+
},
|
|
65
|
+
children: "const foo = 1;"
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
})];
|
|
69
|
+
}
|
|
70
|
+
}));
|
|
71
|
+
assertFileContents(res, {
|
|
72
|
+
"test1.ts": `
|
|
73
|
+
const foo = 1;
|
|
74
|
+
`,
|
|
75
|
+
"test2.ts": `
|
|
76
|
+
import { foo } from "./test1.js";
|
|
77
|
+
|
|
78
|
+
const v = foo;
|
|
79
|
+
`
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { Output, render } from "@alloy-js/core";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
4
|
+
import { expect, it } from "vitest";
|
|
5
|
+
import { SourceFile } from "../src/components/SourceFile.js";
|
|
6
|
+
it("Includes header", () => {
|
|
7
|
+
const tree = render(_$createComponent(Output, {
|
|
8
|
+
get children() {
|
|
9
|
+
return _$createComponent(SourceFile, {
|
|
10
|
+
path: "hi.ts",
|
|
11
|
+
headerComment: "This is a header",
|
|
12
|
+
children: "hello!"
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}));
|
|
16
|
+
expect(tree.contents[0].contents).toEqual(d`
|
|
17
|
+
// This is a header
|
|
18
|
+
|
|
19
|
+
hello!`);
|
|
20
|
+
});
|
|
21
|
+
it("Line wraps a header", () => {
|
|
22
|
+
const tree = render(_$createComponent(Output, {
|
|
23
|
+
get children() {
|
|
24
|
+
return _$createComponent(SourceFile, {
|
|
25
|
+
path: "hi.ts",
|
|
26
|
+
headerComment: "This is a very long header that is very long and should be wrapped. Meaning, it should be split into multiple lines.",
|
|
27
|
+
children: "hello!"
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}));
|
|
31
|
+
expect(tree.contents[0].contents).toEqual(d`
|
|
32
|
+
// This is a very long header that is very long and should be wrapped. Meaning,
|
|
33
|
+
// it should be split into multiple lines.
|
|
34
|
+
|
|
35
|
+
hello!`);
|
|
36
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { List } from "@alloy-js/core";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
4
|
+
import { expect, it } from "vitest";
|
|
5
|
+
import { CaseClause, SwitchStatement } from "../src/components/SwitchStatement.js";
|
|
6
|
+
import { toSourceText } from "./utils.js";
|
|
7
|
+
it("handles various cases", () => {
|
|
8
|
+
const text = toSourceText(_$createComponent(SwitchStatement, {
|
|
9
|
+
expression: "x",
|
|
10
|
+
get children() {
|
|
11
|
+
return _$createComponent(List, {
|
|
12
|
+
hardline: true,
|
|
13
|
+
get children() {
|
|
14
|
+
return [_$createComponent(CaseClause, {
|
|
15
|
+
expression: '"test"',
|
|
16
|
+
children: "// expression"
|
|
17
|
+
}), _$createComponent(CaseClause, {
|
|
18
|
+
jsValue: "test",
|
|
19
|
+
children: "// jsValue"
|
|
20
|
+
}), _$createComponent(CaseClause, {
|
|
21
|
+
expression: "'break'",
|
|
22
|
+
"break": true,
|
|
23
|
+
children: "// break"
|
|
24
|
+
}), _$createComponent(CaseClause, {
|
|
25
|
+
expression: "'block'",
|
|
26
|
+
block: true,
|
|
27
|
+
children: "// block"
|
|
28
|
+
}), _$createComponent(CaseClause, {
|
|
29
|
+
expression: "'breakblock'",
|
|
30
|
+
"break": true,
|
|
31
|
+
block: true,
|
|
32
|
+
children: "// block"
|
|
33
|
+
}), _$createComponent(CaseClause, {
|
|
34
|
+
"default": true,
|
|
35
|
+
children: "// default"
|
|
36
|
+
})];
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}));
|
|
41
|
+
expect(text).toBe(d`
|
|
42
|
+
switch (x) {
|
|
43
|
+
case "test":
|
|
44
|
+
// expression
|
|
45
|
+
case "test":
|
|
46
|
+
// jsValue
|
|
47
|
+
case 'break':
|
|
48
|
+
// break
|
|
49
|
+
break;
|
|
50
|
+
case 'block': {
|
|
51
|
+
// block
|
|
52
|
+
}
|
|
53
|
+
case 'breakblock': {
|
|
54
|
+
// block
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
default :
|
|
58
|
+
// default
|
|
59
|
+
}
|
|
60
|
+
`);
|
|
61
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { Output, render } from "@alloy-js/core";
|
|
3
|
+
import { dedent } from "@alloy-js/core/testing";
|
|
4
|
+
import { expect } from "vitest";
|
|
5
|
+
import * as ts from "../src/index.js";
|
|
6
|
+
export function toSourceText(c, options) {
|
|
7
|
+
const res = render(_$createComponent(Output, {
|
|
8
|
+
get children() {
|
|
9
|
+
return _$createComponent(ts.SourceFile, {
|
|
10
|
+
path: "test.ts",
|
|
11
|
+
children: c
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}), options);
|
|
15
|
+
return res.contents[0].contents;
|
|
16
|
+
}
|
|
17
|
+
export function findFile(res, path) {
|
|
18
|
+
const result = findFileWorker(res, path);
|
|
19
|
+
if (!result) {
|
|
20
|
+
throw new Error("Expected to find file " + path);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
function findFileWorker(res, path) {
|
|
24
|
+
for (const item of res.contents) {
|
|
25
|
+
if (item.kind === "file") {
|
|
26
|
+
if (item.path === path) {
|
|
27
|
+
return item;
|
|
28
|
+
}
|
|
29
|
+
continue;
|
|
30
|
+
} else {
|
|
31
|
+
const found = findFileWorker(item, path);
|
|
32
|
+
if (found) {
|
|
33
|
+
return found;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function assertFileContents(res, expectedFiles) {
|
|
41
|
+
for (const [path, contents] of Object.entries(expectedFiles)) {
|
|
42
|
+
const file = findFile(res, path);
|
|
43
|
+
expect(file.contents).toBe(dedent(contents));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import { ValueExpression } from "../src/index.js";
|
|
5
|
+
import { toSourceText } from "./utils.js";
|
|
6
|
+
it.each([[undefined, "undefined"], [null, "null"], [true, "true"], [false, "false"], [42, "42"], [42n, "42n"], ["abc", `"abc"`], ["a\nb\rc\\", `"a\\nb\\rc\\\\"`], [[1, 2, 3], d`
|
|
7
|
+
[1, 2, 3]
|
|
8
|
+
`], [{
|
|
9
|
+
a: 1,
|
|
10
|
+
b: 2,
|
|
11
|
+
c: 3
|
|
12
|
+
}, d`
|
|
13
|
+
{
|
|
14
|
+
a: 1,
|
|
15
|
+
b: 2,
|
|
16
|
+
c: 3,
|
|
17
|
+
}
|
|
18
|
+
`], [{
|
|
19
|
+
"mangled-name": 1,
|
|
20
|
+
"@pagination": 2
|
|
21
|
+
}, d`
|
|
22
|
+
{
|
|
23
|
+
"mangled-name": 1,
|
|
24
|
+
"@pagination": 2,
|
|
25
|
+
}
|
|
26
|
+
`]])("works - %o => %s", (jsValue, expectedSource) => {
|
|
27
|
+
expect(toSourceText(_$createComponent(ValueExpression, {
|
|
28
|
+
jsValue: jsValue
|
|
29
|
+
}))).toBe(expectedSource);
|
|
30
|
+
});
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { mapJoin, Output, printTree, reactive, refkey, render, renderTree, StatementList } from "@alloy-js/core";
|
|
3
|
+
import "@alloy-js/core/testing";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { d } from "@alloy-js/core/testing";
|
|
6
|
+
import * as ts from "../src/index.js";
|
|
7
|
+
import { assertFileContents } from "./utils.js";
|
|
8
|
+
it("renders an object", () => {
|
|
9
|
+
expect(_$createComponent(ts.ObjectExpression, {})).toRenderTo("{}");
|
|
10
|
+
});
|
|
11
|
+
it("renders an object with properties", () => {
|
|
12
|
+
expect(_$createComponent(ts.ObjectExpression, {
|
|
13
|
+
get children() {
|
|
14
|
+
return [_$createComponent(ts.ObjectProperty, {
|
|
15
|
+
name: "foo",
|
|
16
|
+
value: "1"
|
|
17
|
+
}), ",", _$createIntrinsic("hbr", {}), _$createComponent(ts.ObjectProperty, {
|
|
18
|
+
name: "bar",
|
|
19
|
+
value: "2"
|
|
20
|
+
})];
|
|
21
|
+
}
|
|
22
|
+
})).toRenderTo(`
|
|
23
|
+
{
|
|
24
|
+
foo: 1,
|
|
25
|
+
bar: 2
|
|
26
|
+
}
|
|
27
|
+
`);
|
|
28
|
+
});
|
|
29
|
+
it("renders an object with properties, mapped", () => {
|
|
30
|
+
const propValues = new Map([["foo", "1"], ["bar", "2"]]);
|
|
31
|
+
const props = mapJoin(() => propValues, (name, value) => {
|
|
32
|
+
return _$createComponent(ts.ObjectProperty, {
|
|
33
|
+
name: name,
|
|
34
|
+
value: value
|
|
35
|
+
});
|
|
36
|
+
}, {
|
|
37
|
+
joiner: ",\n"
|
|
38
|
+
});
|
|
39
|
+
expect(_$createComponent(ts.ObjectExpression, {
|
|
40
|
+
children: props
|
|
41
|
+
})).toRenderTo(`
|
|
42
|
+
{
|
|
43
|
+
foo: 1,
|
|
44
|
+
bar: 2
|
|
45
|
+
}
|
|
46
|
+
`);
|
|
47
|
+
});
|
|
48
|
+
it("is reactive", () => {
|
|
49
|
+
const jsValue = reactive(new Map());
|
|
50
|
+
const tree = renderTree(_$createComponent(ts.ObjectExpression, {
|
|
51
|
+
jsValue: jsValue
|
|
52
|
+
}));
|
|
53
|
+
expect(printTree(tree)).toEqual(`{}`);
|
|
54
|
+
jsValue.set("hi", 1);
|
|
55
|
+
expect(printTree(tree)).toEqual(d`
|
|
56
|
+
{
|
|
57
|
+
hi: 1,
|
|
58
|
+
}
|
|
59
|
+
`);
|
|
60
|
+
jsValue.set("bye", 2);
|
|
61
|
+
expect(printTree(tree)).toEqual(d`
|
|
62
|
+
{
|
|
63
|
+
hi: 1,
|
|
64
|
+
bye: 2,
|
|
65
|
+
}
|
|
66
|
+
`);
|
|
67
|
+
});
|
|
68
|
+
it("renders objects with arrays", () => {
|
|
69
|
+
const jsValue = {
|
|
70
|
+
a: [1, 2]
|
|
71
|
+
};
|
|
72
|
+
expect(_$createComponent(ts.ObjectExpression, {
|
|
73
|
+
jsValue: jsValue
|
|
74
|
+
})).toRenderTo(`
|
|
75
|
+
{
|
|
76
|
+
a: [1, 2],
|
|
77
|
+
}
|
|
78
|
+
`);
|
|
79
|
+
});
|
|
80
|
+
it("renders complex objects", () => {
|
|
81
|
+
const jsValue = {
|
|
82
|
+
a: 1,
|
|
83
|
+
b: "hello",
|
|
84
|
+
c: true,
|
|
85
|
+
d: {
|
|
86
|
+
prop: [1, 2, 3]
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
expect(_$createComponent(ts.ObjectExpression, {
|
|
90
|
+
jsValue: jsValue
|
|
91
|
+
})).toRenderTo(`
|
|
92
|
+
{
|
|
93
|
+
a: 1,
|
|
94
|
+
b: "hello",
|
|
95
|
+
c: true,
|
|
96
|
+
d: {
|
|
97
|
+
prop: [1, 2, 3],
|
|
98
|
+
},
|
|
99
|
+
}
|
|
100
|
+
`);
|
|
101
|
+
});
|
|
102
|
+
it("renders falsy values", () => {
|
|
103
|
+
const jsValue = {
|
|
104
|
+
a: 0,
|
|
105
|
+
b: "",
|
|
106
|
+
c: false,
|
|
107
|
+
d: null,
|
|
108
|
+
e: undefined
|
|
109
|
+
};
|
|
110
|
+
expect(_$createComponent(ts.ObjectExpression, {
|
|
111
|
+
jsValue: jsValue
|
|
112
|
+
})).toRenderTo(`
|
|
113
|
+
{
|
|
114
|
+
a: 0,
|
|
115
|
+
b: "",
|
|
116
|
+
c: false,
|
|
117
|
+
d: null,
|
|
118
|
+
e: undefined,
|
|
119
|
+
}
|
|
120
|
+
`);
|
|
121
|
+
});
|
|
122
|
+
it("allows embedding things with functions", () => {
|
|
123
|
+
function Foo() {
|
|
124
|
+
return ["a b"];
|
|
125
|
+
}
|
|
126
|
+
const jsValue = {
|
|
127
|
+
a: () => "hello",
|
|
128
|
+
b: () => _$createComponent(Foo, {})
|
|
129
|
+
};
|
|
130
|
+
expect(_$createComponent(ts.ObjectExpression, {
|
|
131
|
+
jsValue: jsValue
|
|
132
|
+
})).toRenderTo(`
|
|
133
|
+
{
|
|
134
|
+
a: hello,
|
|
135
|
+
b: a b,
|
|
136
|
+
}
|
|
137
|
+
`);
|
|
138
|
+
});
|
|
139
|
+
describe("symbols", () => {
|
|
140
|
+
it("can reference members", () => {
|
|
141
|
+
const innerRefkey = refkey();
|
|
142
|
+
const outerRefkey = refkey();
|
|
143
|
+
const decl = _$createComponent(Output, {
|
|
144
|
+
get children() {
|
|
145
|
+
return _$createComponent(ts.SourceFile, {
|
|
146
|
+
path: "foo.ts",
|
|
147
|
+
get children() {
|
|
148
|
+
return _$createComponent(StatementList, {
|
|
149
|
+
get children() {
|
|
150
|
+
return [_$createComponent(ts.VarDeclaration, {
|
|
151
|
+
name: "refme",
|
|
152
|
+
refkey: outerRefkey,
|
|
153
|
+
get children() {
|
|
154
|
+
return _$createComponent(ts.ObjectExpression, {
|
|
155
|
+
get children() {
|
|
156
|
+
return [_$createComponent(ts.ObjectProperty, {
|
|
157
|
+
name: "foo",
|
|
158
|
+
refkey: innerRefkey,
|
|
159
|
+
jsValue: "hello"
|
|
160
|
+
}), ","];
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}), innerRefkey];
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
expect(decl).toRenderTo(`
|
|
172
|
+
const refme = {
|
|
173
|
+
foo: "hello",
|
|
174
|
+
};
|
|
175
|
+
refme.foo;
|
|
176
|
+
`);
|
|
177
|
+
});
|
|
178
|
+
it("can reference nested members", () => {
|
|
179
|
+
const varRefkey = refkey();
|
|
180
|
+
const fooRefkey = refkey();
|
|
181
|
+
const barRefkey = refkey();
|
|
182
|
+
const decl = _$createComponent(Output, {
|
|
183
|
+
get children() {
|
|
184
|
+
return _$createComponent(ts.SourceFile, {
|
|
185
|
+
path: "foo.ts",
|
|
186
|
+
get children() {
|
|
187
|
+
return _$createComponent(StatementList, {
|
|
188
|
+
get children() {
|
|
189
|
+
return [_$createComponent(ts.VarDeclaration, {
|
|
190
|
+
name: "refme",
|
|
191
|
+
refkey: varRefkey,
|
|
192
|
+
get children() {
|
|
193
|
+
return _$createComponent(ts.ObjectExpression, {
|
|
194
|
+
get children() {
|
|
195
|
+
return _$createComponent(ts.ObjectProperty, {
|
|
196
|
+
name: "foo",
|
|
197
|
+
refkey: fooRefkey,
|
|
198
|
+
get children() {
|
|
199
|
+
return _$createComponent(ts.ObjectExpression, {
|
|
200
|
+
get children() {
|
|
201
|
+
return _$createComponent(ts.ObjectProperty, {
|
|
202
|
+
name: "bar",
|
|
203
|
+
refkey: barRefkey,
|
|
204
|
+
children: "\"hello\","
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}), barRefkey];
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
expect(decl).toRenderTo(`
|
|
221
|
+
const refme = {
|
|
222
|
+
foo: {
|
|
223
|
+
bar: "hello",
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
refme.foo.bar;
|
|
227
|
+
`);
|
|
228
|
+
});
|
|
229
|
+
it("can reference nested members in other source files", () => {
|
|
230
|
+
const varRefkey = refkey();
|
|
231
|
+
const fooRefkey = refkey();
|
|
232
|
+
const barRefkey = refkey();
|
|
233
|
+
const decl = _$createComponent(Output, {
|
|
234
|
+
get children() {
|
|
235
|
+
return [_$createComponent(ts.SourceFile, {
|
|
236
|
+
path: "foo.ts",
|
|
237
|
+
get children() {
|
|
238
|
+
return _$createComponent(ts.VarDeclaration, {
|
|
239
|
+
"export": true,
|
|
240
|
+
name: "refme",
|
|
241
|
+
refkey: varRefkey,
|
|
242
|
+
get children() {
|
|
243
|
+
return _$createComponent(ts.ObjectExpression, {
|
|
244
|
+
get children() {
|
|
245
|
+
return _$createComponent(ts.ObjectProperty, {
|
|
246
|
+
name: "foo",
|
|
247
|
+
refkey: fooRefkey,
|
|
248
|
+
get children() {
|
|
249
|
+
return _$createComponent(ts.ObjectExpression, {
|
|
250
|
+
get children() {
|
|
251
|
+
return _$createComponent(ts.ObjectProperty, {
|
|
252
|
+
name: "bar",
|
|
253
|
+
refkey: barRefkey,
|
|
254
|
+
children: "\"hello\","
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}), _$createComponent(ts.SourceFile, {
|
|
266
|
+
path: "bar.ts",
|
|
267
|
+
get children() {
|
|
268
|
+
return ["console.log(", barRefkey, ");"];
|
|
269
|
+
}
|
|
270
|
+
})];
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
expect(decl).toRenderTo(`
|
|
274
|
+
export const refme = {
|
|
275
|
+
foo: {
|
|
276
|
+
bar: "hello",
|
|
277
|
+
}
|
|
278
|
+
}import { refme } from "./foo.js";
|
|
279
|
+
|
|
280
|
+
console.log(refme.foo.bar);
|
|
281
|
+
`);
|
|
282
|
+
});
|
|
283
|
+
it("can reference nested members in other packages", () => {
|
|
284
|
+
const varRefkey = refkey();
|
|
285
|
+
const fooRefkey = refkey();
|
|
286
|
+
const barRefkey = refkey();
|
|
287
|
+
const decl = _$createComponent(Output, {
|
|
288
|
+
get children() {
|
|
289
|
+
return [_$createComponent(ts.PackageDirectory, {
|
|
290
|
+
name: "SourcePackage",
|
|
291
|
+
path: "sp",
|
|
292
|
+
version: "1.0.0",
|
|
293
|
+
get children() {
|
|
294
|
+
return _$createComponent(ts.SourceFile, {
|
|
295
|
+
"export": ".",
|
|
296
|
+
path: "foo.ts",
|
|
297
|
+
get children() {
|
|
298
|
+
return _$createComponent(ts.VarDeclaration, {
|
|
299
|
+
"export": true,
|
|
300
|
+
name: "refme",
|
|
301
|
+
refkey: varRefkey,
|
|
302
|
+
get children() {
|
|
303
|
+
return _$createComponent(ts.ObjectExpression, {
|
|
304
|
+
get children() {
|
|
305
|
+
return _$createComponent(ts.ObjectProperty, {
|
|
306
|
+
name: "foo",
|
|
307
|
+
refkey: fooRefkey,
|
|
308
|
+
get children() {
|
|
309
|
+
return _$createComponent(ts.ObjectExpression, {
|
|
310
|
+
get children() {
|
|
311
|
+
return _$createComponent(ts.ObjectProperty, {
|
|
312
|
+
name: "bar",
|
|
313
|
+
refkey: barRefkey,
|
|
314
|
+
children: "\"hello\";"
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}), _$createComponent(ts.PackageDirectory, {
|
|
328
|
+
name: "DepPackage",
|
|
329
|
+
path: "dp",
|
|
330
|
+
version: "1.0.0",
|
|
331
|
+
get children() {
|
|
332
|
+
return _$createComponent(ts.SourceFile, {
|
|
333
|
+
path: "bar.ts",
|
|
334
|
+
get children() {
|
|
335
|
+
return ["console.log(", barRefkey, ");"];
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
})];
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
const res = render(decl);
|
|
343
|
+
assertFileContents(res, {
|
|
344
|
+
"dp/bar.ts": `
|
|
345
|
+
import { refme } from "SourcePackage";
|
|
346
|
+
|
|
347
|
+
console.log(refme.foo.bar);
|
|
348
|
+
`
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import * as ts from "../src/index.js";
|
|
5
|
+
import { toSourceText } from "./utils.js";
|
|
6
|
+
it("renders a variable declaration with documentation", () => {
|
|
7
|
+
const res = toSourceText([_$createComponent(ts.VarDeclaration, {
|
|
8
|
+
name: "myVar",
|
|
9
|
+
"const": true,
|
|
10
|
+
doc: "This is a variable documentation",
|
|
11
|
+
children: "123"
|
|
12
|
+
}), ";"]);
|
|
13
|
+
expect(res).toEqual(d`
|
|
14
|
+
/**
|
|
15
|
+
* This is a variable documentation
|
|
16
|
+
**/
|
|
17
|
+
const myVar = 123;
|
|
18
|
+
`);
|
|
19
|
+
});
|
|
20
|
+
it("renders a let declaration with documentation", () => {
|
|
21
|
+
const res = toSourceText([_$createComponent(ts.VarDeclaration, {
|
|
22
|
+
"let": true,
|
|
23
|
+
name: "counter",
|
|
24
|
+
doc: "A counter variable that can be changed",
|
|
25
|
+
type: "number",
|
|
26
|
+
children: "0"
|
|
27
|
+
}), ";"]);
|
|
28
|
+
expect(res).toEqual(d`
|
|
29
|
+
/**
|
|
30
|
+
* A counter variable that can be changed
|
|
31
|
+
**/
|
|
32
|
+
let counter: number = 0;
|
|
33
|
+
`);
|
|
34
|
+
});
|
|
35
|
+
it("renders an exported variable declaration with documentation", () => {
|
|
36
|
+
const res = toSourceText([_$createComponent(ts.VarDeclaration, {
|
|
37
|
+
"export": true,
|
|
38
|
+
name: "API_URL",
|
|
39
|
+
"const": true,
|
|
40
|
+
doc: "The base URL for API calls",
|
|
41
|
+
type: "string",
|
|
42
|
+
children: "\"https://api.example.com\""
|
|
43
|
+
}), ";"]);
|
|
44
|
+
expect(res).toEqual(d`
|
|
45
|
+
/**
|
|
46
|
+
* The base URL for API calls
|
|
47
|
+
**/
|
|
48
|
+
export const API_URL: string = "https://api.example.com";
|
|
49
|
+
`);
|
|
50
|
+
});
|