@alloy-js/typescript 0.1.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/LICENSE.txt +7 -0
- package/api-extractor.json +4 -0
- package/babel.config.cjs +4 -0
- package/dist/src/builtins/index.d.ts +2 -0
- package/dist/src/builtins/index.d.ts.map +1 -0
- package/dist/src/builtins/index.js +1 -0
- package/dist/src/builtins/index.js.map +1 -0
- package/dist/src/builtins/node.d.ts +183 -0
- package/dist/src/builtins/node.d.ts.map +1 -0
- package/dist/src/builtins/node.js +39 -0
- package/dist/src/builtins/node.js.map +1 -0
- package/dist/src/components/ArrayExpression.d.ts +7 -0
- package/dist/src/components/ArrayExpression.d.ts.map +1 -0
- package/dist/src/components/ArrayExpression.js +37 -0
- package/dist/src/components/ArrayExpression.js.map +1 -0
- package/dist/src/components/BarrelFile.d.ts +49 -0
- package/dist/src/components/BarrelFile.d.ts.map +1 -0
- package/dist/src/components/BarrelFile.js +73 -0
- package/dist/src/components/BarrelFile.js.map +1 -0
- package/dist/src/components/Block.d.ts +6 -0
- package/dist/src/components/Block.d.ts.map +1 -0
- package/dist/src/components/Block.js +11 -0
- package/dist/src/components/Block.js.map +1 -0
- package/dist/src/components/ClassDeclaration.d.ts +64 -0
- package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
- package/dist/src/components/ClassDeclaration.js +122 -0
- package/dist/src/components/ClassDeclaration.js.map +1 -0
- package/dist/src/components/Declaration.d.ts +53 -0
- package/dist/src/components/Declaration.d.ts.map +1 -0
- package/dist/src/components/Declaration.js +60 -0
- package/dist/src/components/Declaration.js.map +1 -0
- package/dist/src/components/EnumDeclaration.d.ts +12 -0
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
- package/dist/src/components/EnumDeclaration.js +48 -0
- package/dist/src/components/EnumDeclaration.js.map +1 -0
- package/dist/src/components/EnumMember.d.ts +28 -0
- package/dist/src/components/EnumMember.d.ts.map +1 -0
- package/dist/src/components/EnumMember.js +28 -0
- package/dist/src/components/EnumMember.js.map +1 -0
- package/dist/src/components/ExportStatement.d.ts +7 -0
- package/dist/src/components/ExportStatement.d.ts.map +1 -0
- package/dist/src/components/ExportStatement.js +15 -0
- package/dist/src/components/ExportStatement.js.map +1 -0
- package/dist/src/components/FunctionCallExpression.d.ts +7 -0
- package/dist/src/components/FunctionCallExpression.d.ts.map +1 -0
- package/dist/src/components/FunctionCallExpression.js +14 -0
- package/dist/src/components/FunctionCallExpression.js.map +1 -0
- package/dist/src/components/FunctionDeclaration.d.ts +29 -0
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -0
- package/dist/src/components/FunctionDeclaration.js +91 -0
- package/dist/src/components/FunctionDeclaration.js.map +1 -0
- package/dist/src/components/ImportStatement.d.ts +11 -0
- package/dist/src/components/ImportStatement.d.ts.map +1 -0
- package/dist/src/components/ImportStatement.js +92 -0
- package/dist/src/components/ImportStatement.js.map +1 -0
- package/dist/src/components/Interface.d.ts +32 -0
- package/dist/src/components/Interface.d.ts.map +1 -0
- package/dist/src/components/Interface.js +50 -0
- package/dist/src/components/Interface.js.map +1 -0
- package/dist/src/components/ObjectExpression.d.ts +16 -0
- package/dist/src/components/ObjectExpression.d.ts.map +1 -0
- package/dist/src/components/ObjectExpression.js +101 -0
- package/dist/src/components/ObjectExpression.js.map +1 -0
- package/dist/src/components/PackageDirectory.d.ts +23 -0
- package/dist/src/components/PackageDirectory.d.ts.map +1 -0
- package/dist/src/components/PackageDirectory.js +62 -0
- package/dist/src/components/PackageDirectory.js.map +1 -0
- package/dist/src/components/PackageJson.d.ts +16 -0
- package/dist/src/components/PackageJson.d.ts.map +1 -0
- package/dist/src/components/PackageJson.js +36 -0
- package/dist/src/components/PackageJson.js.map +1 -0
- package/dist/src/components/Reference.d.ts +6 -0
- package/dist/src/components/Reference.d.ts.map +1 -0
- package/dist/src/components/Reference.js +8 -0
- package/dist/src/components/Reference.js.map +1 -0
- package/dist/src/components/SourceFile.d.ts +15 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -0
- package/dist/src/components/SourceFile.js +62 -0
- package/dist/src/components/SourceFile.js.map +1 -0
- package/dist/src/components/TsConfigJson.d.ts +5 -0
- package/dist/src/components/TsConfigJson.d.ts.map +1 -0
- package/dist/src/components/TsConfigJson.js +53 -0
- package/dist/src/components/TsConfigJson.js.map +1 -0
- package/dist/src/components/TypeDeclaration.d.ts +5 -0
- package/dist/src/components/TypeDeclaration.d.ts.map +1 -0
- package/dist/src/components/TypeDeclaration.js +14 -0
- package/dist/src/components/TypeDeclaration.js.map +1 -0
- package/dist/src/components/ValueExpression.d.ts +5 -0
- package/dist/src/components/ValueExpression.d.ts.map +1 -0
- package/dist/src/components/ValueExpression.js +32 -0
- package/dist/src/components/ValueExpression.js.map +1 -0
- package/dist/src/components/VarDeclaration.d.ts +11 -0
- package/dist/src/components/VarDeclaration.d.ts.map +1 -0
- package/dist/src/components/VarDeclaration.js +29 -0
- package/dist/src/components/VarDeclaration.js.map +1 -0
- package/dist/src/components/index.d.ts +22 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +22 -0
- package/dist/src/components/index.js.map +1 -0
- package/dist/src/components/stc/index.d.ts +102 -0
- package/dist/src/components/stc/index.d.ts.map +1 -0
- package/dist/src/components/stc/index.js +28 -0
- package/dist/src/components/stc/index.js.map +1 -0
- package/dist/src/create-package.d.ts +33 -0
- package/dist/src/create-package.d.ts.map +1 -0
- package/dist/src/create-package.js +50 -0
- package/dist/src/create-package.js.map +1 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +7 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/name-conflict-resolver.d.ts +3 -0
- package/dist/src/name-conflict-resolver.d.ts.map +1 -0
- package/dist/src/name-conflict-resolver.js +15 -0
- package/dist/src/name-conflict-resolver.js.map +1 -0
- package/dist/src/name-policy.d.ts +5 -0
- package/dist/src/name-policy.d.ts.map +1 -0
- package/dist/src/name-policy.js +23 -0
- package/dist/src/name-policy.js.map +1 -0
- package/dist/src/source-directory-data.d.ts +7 -0
- package/dist/src/source-directory-data.d.ts.map +1 -0
- package/dist/src/source-directory-data.js +13 -0
- package/dist/src/source-directory-data.js.map +1 -0
- package/dist/src/symbols/index.d.ts +7 -0
- package/dist/src/symbols/index.d.ts.map +1 -0
- package/dist/src/symbols/index.js +7 -0
- package/dist/src/symbols/index.js.map +1 -0
- package/dist/src/symbols/reference.d.ts +3 -0
- package/dist/src/symbols/reference.d.ts.map +1 -0
- package/dist/src/symbols/reference.js +75 -0
- package/dist/src/symbols/reference.js.map +1 -0
- package/dist/src/symbols/scopes.d.ts +16 -0
- package/dist/src/symbols/scopes.d.ts.map +1 -0
- package/dist/src/symbols/scopes.js +5 -0
- package/dist/src/symbols/scopes.js.map +1 -0
- package/dist/src/symbols/ts-member-scope.d.ts +9 -0
- package/dist/src/symbols/ts-member-scope.d.ts.map +1 -0
- package/dist/src/symbols/ts-member-scope.js +10 -0
- package/dist/src/symbols/ts-member-scope.js.map +1 -0
- package/dist/src/symbols/ts-module-scope.d.ts +19 -0
- package/dist/src/symbols/ts-module-scope.d.ts.map +1 -0
- package/dist/src/symbols/ts-module-scope.js +36 -0
- package/dist/src/symbols/ts-module-scope.js.map +1 -0
- package/dist/src/symbols/ts-output-symbol.d.ts +28 -0
- package/dist/src/symbols/ts-output-symbol.d.ts.map +1 -0
- package/dist/src/symbols/ts-output-symbol.js +30 -0
- package/dist/src/symbols/ts-output-symbol.js.map +1 -0
- package/dist/src/symbols/ts-package-scope.d.ts +54 -0
- package/dist/src/symbols/ts-package-scope.d.ts.map +1 -0
- package/dist/src/symbols/ts-package-scope.js +40 -0
- package/dist/src/symbols/ts-package-scope.js.map +1 -0
- package/dist/src/utils.d.ts +2 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +7 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/test/array.test.d.ts +2 -0
- package/dist/test/array.test.d.ts.map +1 -0
- package/dist/test/barrel.test.d.ts +2 -0
- package/dist/test/barrel.test.d.ts.map +1 -0
- package/dist/test/basic.test.d.ts +2 -0
- package/dist/test/basic.test.d.ts.map +1 -0
- package/dist/test/class.test.d.ts +2 -0
- package/dist/test/class.test.d.ts.map +1 -0
- package/dist/test/enum.test.d.ts +2 -0
- package/dist/test/enum.test.d.ts.map +1 -0
- package/dist/test/externals.test.d.ts +2 -0
- package/dist/test/externals.test.d.ts.map +1 -0
- package/dist/test/function-call-expression.test.d.ts +2 -0
- package/dist/test/function-call-expression.test.d.ts.map +1 -0
- package/dist/test/function-declaration.test.d.ts +2 -0
- package/dist/test/function-declaration.test.d.ts.map +1 -0
- package/dist/test/imports.test.d.ts +2 -0
- package/dist/test/imports.test.d.ts.map +1 -0
- package/dist/test/interface.test.d.ts +2 -0
- package/dist/test/interface.test.d.ts.map +1 -0
- package/dist/test/name-policy.test.d.ts +2 -0
- package/dist/test/name-policy.test.d.ts.map +1 -0
- package/dist/test/package-external.test.d.ts +2 -0
- package/dist/test/package-external.test.d.ts.map +1 -0
- package/dist/test/package.test.d.ts +2 -0
- package/dist/test/package.test.d.ts.map +1 -0
- package/dist/test/reference.test.d.ts +2 -0
- package/dist/test/reference.test.d.ts.map +1 -0
- package/dist/test/utils.d.ts +5 -0
- package/dist/test/utils.d.ts.map +1 -0
- package/dist/test/values.test.d.ts +2 -0
- package/dist/test/values.test.d.ts.map +1 -0
- package/dist/test/var-declaration.test.d.ts +2 -0
- package/dist/test/var-declaration.test.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +49 -0
- package/src/builtins/index.ts +1 -0
- package/src/builtins/node.ts +219 -0
- package/src/components/ArrayExpression.tsx +41 -0
- package/src/components/BarrelFile.tsx +96 -0
- package/src/components/Block.tsx +12 -0
- package/src/components/ClassDeclaration.tsx +145 -0
- package/src/components/Declaration.tsx +105 -0
- package/src/components/EnumDeclaration.tsx +53 -0
- package/src/components/EnumMember.tsx +52 -0
- package/src/components/ExportStatement.tsx +21 -0
- package/src/components/FunctionCallExpression.tsx +15 -0
- package/src/components/FunctionDeclaration.tsx +132 -0
- package/src/components/ImportStatement.tsx +131 -0
- package/src/components/Interface.tsx +62 -0
- package/src/components/ObjectExpression.tsx +130 -0
- package/src/components/PackageDirectory.tsx +104 -0
- package/src/components/PackageJson.tsx +69 -0
- package/src/components/Reference.tsx +12 -0
- package/src/components/SourceFile.tsx +71 -0
- package/src/components/TsConfigJson.tsx +62 -0
- package/src/components/TypeDeclaration.tsx +10 -0
- package/src/components/ValueExpression.tsx +32 -0
- package/src/components/VarDeclaration.tsx +42 -0
- package/src/components/index.ts +21 -0
- package/src/components/stc/index.ts +28 -0
- package/src/create-package.ts +108 -0
- package/src/index.ts +6 -0
- package/src/name-conflict-resolver.ts +22 -0
- package/src/name-policy.ts +39 -0
- package/src/source-directory-data.ts +25 -0
- package/src/symbols/index.ts +6 -0
- package/src/symbols/reference.ts +120 -0
- package/src/symbols/scopes.ts +28 -0
- package/src/symbols/ts-member-scope.ts +23 -0
- package/src/symbols/ts-module-scope.ts +66 -0
- package/src/symbols/ts-output-symbol.ts +63 -0
- package/src/symbols/ts-package-scope.ts +112 -0
- package/src/utils.ts +7 -0
- package/temp/api.json +9866 -0
- package/test/array.test.tsx +14 -0
- package/test/barrel.test.tsx +46 -0
- package/test/basic.test.tsx +78 -0
- package/test/class.test.tsx +181 -0
- package/test/enum.test.tsx +132 -0
- package/test/externals.test.tsx +139 -0
- package/test/function-call-expression.test.tsx +49 -0
- package/test/function-declaration.test.tsx +201 -0
- package/test/imports.test.tsx +213 -0
- package/test/interface.test.tsx +124 -0
- package/test/name-policy.test.tsx +62 -0
- package/test/package-external.test.tsx +75 -0
- package/test/package.test.tsx +68 -0
- package/test/reference.test.tsx +59 -0
- package/test/utils.tsx +59 -0
- package/test/values.test.tsx +292 -0
- package/test/var-declaration.test.tsx +45 -0
- package/test/vitest.d.ts +10 -0
- package/tsconfig.json +11 -0
- package/tsdoc-metadata.json +11 -0
- package/vitest.config.ts +18 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Output, refkey, render } from "@alloy-js/core";
|
|
2
|
+
import "@alloy-js/core/testing";
|
|
3
|
+
import { it } from "vitest";
|
|
4
|
+
import * as ts from "../src/components/index.js";
|
|
5
|
+
import { PackageDirectory } from "../src/components/PackageDirectory.js";
|
|
6
|
+
import { assertFileContents } from "./utils.js";
|
|
7
|
+
|
|
8
|
+
it("exports source files", () => {
|
|
9
|
+
const res = render(
|
|
10
|
+
<Output>
|
|
11
|
+
<PackageDirectory name="greeting-js" path="." version="1.0.0">
|
|
12
|
+
|
|
13
|
+
<ts.SourceFile path="greeting.ts">
|
|
14
|
+
<ts.FunctionDeclaration name="getGreeting">
|
|
15
|
+
return "Hello world!";
|
|
16
|
+
</ts.FunctionDeclaration>
|
|
17
|
+
</ts.SourceFile>
|
|
18
|
+
|
|
19
|
+
<ts.SourceFile export path="printing.ts">
|
|
20
|
+
<ts.VarDeclaration name="greeting">
|
|
21
|
+
{refkey("getGreeting")}()
|
|
22
|
+
</ts.VarDeclaration>
|
|
23
|
+
<ts.FunctionDeclaration export name="printGreeting">
|
|
24
|
+
console.log(greeting);
|
|
25
|
+
</ts.FunctionDeclaration>
|
|
26
|
+
</ts.SourceFile>
|
|
27
|
+
|
|
28
|
+
<ts.BarrelFile export="." />
|
|
29
|
+
|
|
30
|
+
</PackageDirectory>
|
|
31
|
+
</Output>,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
assertFileContents(res, {
|
|
35
|
+
"greeting.ts": `
|
|
36
|
+
function getGreeting() {
|
|
37
|
+
return "Hello world!";
|
|
38
|
+
}
|
|
39
|
+
`,
|
|
40
|
+
"printing.ts": `
|
|
41
|
+
import { getGreeting } from "./greeting.js";
|
|
42
|
+
|
|
43
|
+
const greeting = getGreeting();
|
|
44
|
+
export function printGreeting() {
|
|
45
|
+
console.log(greeting);
|
|
46
|
+
}
|
|
47
|
+
`,
|
|
48
|
+
"index.ts": `
|
|
49
|
+
export * from "./greeting.js";
|
|
50
|
+
export * from "./printing.js";
|
|
51
|
+
`,
|
|
52
|
+
"package.json": `
|
|
53
|
+
{
|
|
54
|
+
"name": "greeting-js",
|
|
55
|
+
"version": "1.0.0",
|
|
56
|
+
"type": "module",
|
|
57
|
+
"dependencies": {},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"typescript": "^5.5.2"
|
|
60
|
+
},
|
|
61
|
+
"exports": {
|
|
62
|
+
"./printing.js": "./dist/printing.js",
|
|
63
|
+
".": "./dist/index.js"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
`,
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Declaration, Output, refkey, render } from "@alloy-js/core";
|
|
2
|
+
import "@alloy-js/core/testing";
|
|
3
|
+
import { it } from "vitest";
|
|
4
|
+
import * as ts from "../src/components/index.js";
|
|
5
|
+
import { Reference } from "../src/components/Reference.js";
|
|
6
|
+
import { assertFileContents } from "./utils.js";
|
|
7
|
+
|
|
8
|
+
it("works with back references", () => {
|
|
9
|
+
const res = render(
|
|
10
|
+
<Output>
|
|
11
|
+
<ts.SourceFile path="test1.ts">
|
|
12
|
+
<Declaration name="foo">
|
|
13
|
+
const foo = 1;
|
|
14
|
+
</Declaration>
|
|
15
|
+
</ts.SourceFile>
|
|
16
|
+
|
|
17
|
+
<ts.SourceFile path="test2.ts">
|
|
18
|
+
const v = <Reference refkey={refkey("foo")} />;
|
|
19
|
+
</ts.SourceFile>
|
|
20
|
+
</Output>,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
assertFileContents(res, {
|
|
24
|
+
"test1.ts": `
|
|
25
|
+
const foo = 1;
|
|
26
|
+
`,
|
|
27
|
+
"test2.ts": `
|
|
28
|
+
import { foo } from "./test1.js";
|
|
29
|
+
|
|
30
|
+
const v = foo;
|
|
31
|
+
`,
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("works with forward references", () => {
|
|
36
|
+
const res = render(
|
|
37
|
+
<Output>
|
|
38
|
+
<ts.SourceFile path="test2.ts">
|
|
39
|
+
const v = <Reference refkey={refkey("foo")} />;
|
|
40
|
+
</ts.SourceFile>
|
|
41
|
+
<ts.SourceFile path="test1.ts">
|
|
42
|
+
<Declaration name="foo">
|
|
43
|
+
const foo = 1;
|
|
44
|
+
</Declaration>
|
|
45
|
+
</ts.SourceFile>
|
|
46
|
+
</Output>,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
assertFileContents(res, {
|
|
50
|
+
"test1.ts": `
|
|
51
|
+
const foo = 1;
|
|
52
|
+
`,
|
|
53
|
+
"test2.ts": `
|
|
54
|
+
import { foo } from "./test1.js";
|
|
55
|
+
|
|
56
|
+
const v = foo;
|
|
57
|
+
`,
|
|
58
|
+
});
|
|
59
|
+
});
|
package/test/utils.tsx
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Children,
|
|
3
|
+
Output,
|
|
4
|
+
OutputDirectory,
|
|
5
|
+
OutputFile,
|
|
6
|
+
render,
|
|
7
|
+
} from "@alloy-js/core";
|
|
8
|
+
import { dedent } from "@alloy-js/core/testing";
|
|
9
|
+
import { expect } from "vitest";
|
|
10
|
+
import * as ts from "../src/index.js";
|
|
11
|
+
|
|
12
|
+
export function toSourceText(c: Children): string {
|
|
13
|
+
const res = render(
|
|
14
|
+
<Output>
|
|
15
|
+
<ts.SourceFile path="test.ts">{c}</ts.SourceFile>
|
|
16
|
+
</Output>,
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
return res.contents[0].contents as string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function findFile(res: OutputDirectory, path: string): OutputFile {
|
|
23
|
+
const result = findFileWorker(res, path);
|
|
24
|
+
|
|
25
|
+
if (!result) {
|
|
26
|
+
throw new Error("Expected to find file " + path);
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
|
|
30
|
+
function findFileWorker(
|
|
31
|
+
res: OutputDirectory,
|
|
32
|
+
path: string,
|
|
33
|
+
): OutputFile | null {
|
|
34
|
+
for (const item of res.contents) {
|
|
35
|
+
if (item.kind === "file") {
|
|
36
|
+
if (item.path === path) {
|
|
37
|
+
return item;
|
|
38
|
+
}
|
|
39
|
+
continue;
|
|
40
|
+
} else {
|
|
41
|
+
const found = findFileWorker(item, path);
|
|
42
|
+
if (found) {
|
|
43
|
+
return found;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function assertFileContents(
|
|
52
|
+
res: OutputDirectory,
|
|
53
|
+
expectedFiles: Record<string, string>,
|
|
54
|
+
) {
|
|
55
|
+
for (const [path, contents] of Object.entries(expectedFiles)) {
|
|
56
|
+
const file = findFile(res, path);
|
|
57
|
+
expect(file.contents).toBe(dedent(contents));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Output,
|
|
3
|
+
mapJoin,
|
|
4
|
+
reactive,
|
|
5
|
+
refkey,
|
|
6
|
+
render,
|
|
7
|
+
renderTree,
|
|
8
|
+
} from "@alloy-js/core";
|
|
9
|
+
import "@alloy-js/core/testing";
|
|
10
|
+
import { describe, expect, it } from "vitest";
|
|
11
|
+
|
|
12
|
+
import { d, renderToString } from "@alloy-js/core/testing";
|
|
13
|
+
import * as ts from "../src/index.js";
|
|
14
|
+
import { assertFileContents } from "./utils.jsx";
|
|
15
|
+
|
|
16
|
+
it("renders an object", () => {
|
|
17
|
+
expect(<ts.ObjectExpression />).toRenderTo("{}");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("renders an object with properties", () => {
|
|
21
|
+
expect(
|
|
22
|
+
<ts.ObjectExpression>
|
|
23
|
+
<ts.ObjectProperty name="foo" value="1" />,
|
|
24
|
+
<ts.ObjectProperty name="bar" value="2" />
|
|
25
|
+
</ts.ObjectExpression>,
|
|
26
|
+
).toRenderTo(`
|
|
27
|
+
{
|
|
28
|
+
foo: 1,
|
|
29
|
+
bar: 2
|
|
30
|
+
}
|
|
31
|
+
`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("renders an object with properties, mapped", () => {
|
|
35
|
+
const propValues = new Map([
|
|
36
|
+
["foo", "1"],
|
|
37
|
+
["bar", "2"],
|
|
38
|
+
]);
|
|
39
|
+
const props = mapJoin(
|
|
40
|
+
propValues,
|
|
41
|
+
(name, value) => {
|
|
42
|
+
return <ts.ObjectProperty name={name} value={value} />;
|
|
43
|
+
},
|
|
44
|
+
{ joiner: ",\n" },
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
expect(<ts.ObjectExpression>
|
|
48
|
+
{props}
|
|
49
|
+
</ts.ObjectExpression>)
|
|
50
|
+
.toRenderTo(`
|
|
51
|
+
{
|
|
52
|
+
foo: 1,
|
|
53
|
+
bar: 2
|
|
54
|
+
}
|
|
55
|
+
`);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("is reactive", () => {
|
|
59
|
+
const jsValue = reactive(new Map());
|
|
60
|
+
jsValue.set("hi", 1);
|
|
61
|
+
|
|
62
|
+
const tree = renderTree(<ts.ObjectExpression jsValue={jsValue} />);
|
|
63
|
+
|
|
64
|
+
expect(renderToString(tree)).toEqual(d`
|
|
65
|
+
{
|
|
66
|
+
hi: 1
|
|
67
|
+
}
|
|
68
|
+
`);
|
|
69
|
+
|
|
70
|
+
jsValue.set("bye", 2);
|
|
71
|
+
|
|
72
|
+
expect(renderToString(tree)).toEqual(d`
|
|
73
|
+
{
|
|
74
|
+
hi: 1,
|
|
75
|
+
bye: 2
|
|
76
|
+
}
|
|
77
|
+
`);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("renders objects with arrays", () => {
|
|
81
|
+
const jsValue = {
|
|
82
|
+
a: [1, 2],
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
expect(<ts.ObjectExpression jsValue={jsValue} />).toRenderTo(`
|
|
86
|
+
{
|
|
87
|
+
a: [
|
|
88
|
+
1,
|
|
89
|
+
2
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
`);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("renders complex objects", () => {
|
|
96
|
+
const jsValue = {
|
|
97
|
+
a: 1,
|
|
98
|
+
b: "hello",
|
|
99
|
+
c: true,
|
|
100
|
+
d: {
|
|
101
|
+
prop: [1, 2, 3],
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
expect(<ts.ObjectExpression jsValue={jsValue} />).toRenderTo(`
|
|
106
|
+
{
|
|
107
|
+
a: 1,
|
|
108
|
+
b: "hello",
|
|
109
|
+
c: true,
|
|
110
|
+
d: {
|
|
111
|
+
prop: [
|
|
112
|
+
1,
|
|
113
|
+
2,
|
|
114
|
+
3
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
`);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("renders falsy values", () => {
|
|
122
|
+
const jsValue = {
|
|
123
|
+
a: 0,
|
|
124
|
+
b: "",
|
|
125
|
+
c: false,
|
|
126
|
+
d: null,
|
|
127
|
+
e: undefined,
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
expect(<ts.ObjectExpression jsValue={jsValue} />).toRenderTo(`
|
|
131
|
+
{
|
|
132
|
+
a: 0,
|
|
133
|
+
b: "",
|
|
134
|
+
c: false,
|
|
135
|
+
d: null,
|
|
136
|
+
e: undefined
|
|
137
|
+
}
|
|
138
|
+
`);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("allows embedding things with functions", () => {
|
|
142
|
+
function Foo() {
|
|
143
|
+
return <>
|
|
144
|
+
a
|
|
145
|
+
b
|
|
146
|
+
</>;
|
|
147
|
+
}
|
|
148
|
+
const jsValue = {
|
|
149
|
+
a: () => "hello",
|
|
150
|
+
b: () => <Foo />,
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
expect(<ts.ObjectExpression jsValue={jsValue} />).toRenderTo(`
|
|
154
|
+
{
|
|
155
|
+
a: hello,
|
|
156
|
+
b: a
|
|
157
|
+
b
|
|
158
|
+
}
|
|
159
|
+
`);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe("symbols", () => {
|
|
163
|
+
it("can reference members", () => {
|
|
164
|
+
const innerRefkey = refkey();
|
|
165
|
+
const outerRefkey = refkey();
|
|
166
|
+
const decl =
|
|
167
|
+
<Output>
|
|
168
|
+
<ts.SourceFile path="foo.ts">
|
|
169
|
+
<ts.VarDeclaration name="refme" refkey={outerRefkey}>
|
|
170
|
+
<ts.ObjectExpression>
|
|
171
|
+
<ts.ObjectProperty name="foo" refkey={innerRefkey} jsValue="hello" />
|
|
172
|
+
</ts.ObjectExpression>
|
|
173
|
+
</ts.VarDeclaration>
|
|
174
|
+
{innerRefkey}
|
|
175
|
+
</ts.SourceFile>
|
|
176
|
+
</Output>;
|
|
177
|
+
expect(decl).toRenderTo(`
|
|
178
|
+
const refme = {
|
|
179
|
+
foo: "hello"
|
|
180
|
+
};
|
|
181
|
+
refme.foo
|
|
182
|
+
`);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("can reference nested members", () => {
|
|
186
|
+
const varRefkey = refkey();
|
|
187
|
+
const fooRefkey = refkey();
|
|
188
|
+
const barRefkey = refkey();
|
|
189
|
+
const decl =
|
|
190
|
+
<Output>
|
|
191
|
+
<ts.SourceFile path="foo.ts">
|
|
192
|
+
<ts.VarDeclaration name="refme" refkey={varRefkey}>
|
|
193
|
+
<ts.ObjectExpression>
|
|
194
|
+
<ts.ObjectProperty name="foo" refkey={fooRefkey}>
|
|
195
|
+
<ts.ObjectExpression>
|
|
196
|
+
<ts.ObjectProperty name="bar" refkey={barRefkey}>
|
|
197
|
+
"hello";
|
|
198
|
+
</ts.ObjectProperty>
|
|
199
|
+
</ts.ObjectExpression>
|
|
200
|
+
</ts.ObjectProperty>
|
|
201
|
+
</ts.ObjectExpression>
|
|
202
|
+
</ts.VarDeclaration>
|
|
203
|
+
{barRefkey}
|
|
204
|
+
</ts.SourceFile>
|
|
205
|
+
</Output>;
|
|
206
|
+
expect(decl).toRenderTo(`
|
|
207
|
+
const refme = {
|
|
208
|
+
foo: {
|
|
209
|
+
bar: "hello";
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
refme.foo.bar
|
|
213
|
+
`);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("can reference nested members in other source files", () => {
|
|
217
|
+
const varRefkey = refkey();
|
|
218
|
+
const fooRefkey = refkey();
|
|
219
|
+
const barRefkey = refkey();
|
|
220
|
+
const decl =
|
|
221
|
+
<Output>
|
|
222
|
+
<ts.SourceFile path="foo.ts">
|
|
223
|
+
<ts.VarDeclaration export name="refme" refkey={varRefkey}>
|
|
224
|
+
<ts.ObjectExpression>
|
|
225
|
+
<ts.ObjectProperty name="foo" refkey={fooRefkey}>
|
|
226
|
+
<ts.ObjectExpression>
|
|
227
|
+
<ts.ObjectProperty name="bar" refkey={barRefkey}>
|
|
228
|
+
"hello";
|
|
229
|
+
</ts.ObjectProperty>
|
|
230
|
+
</ts.ObjectExpression>
|
|
231
|
+
</ts.ObjectProperty>
|
|
232
|
+
</ts.ObjectExpression>
|
|
233
|
+
</ts.VarDeclaration>
|
|
234
|
+
</ts.SourceFile>
|
|
235
|
+
|
|
236
|
+
<ts.SourceFile path="bar.ts">
|
|
237
|
+
console.log({barRefkey});
|
|
238
|
+
</ts.SourceFile>
|
|
239
|
+
|
|
240
|
+
</Output>;
|
|
241
|
+
expect(decl).toRenderTo(`
|
|
242
|
+
export const refme = {
|
|
243
|
+
foo: {
|
|
244
|
+
bar: "hello";
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
import { refme } from "./foo.js";
|
|
249
|
+
|
|
250
|
+
console.log(refme.foo.bar);
|
|
251
|
+
|
|
252
|
+
`);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("can reference nested members in other packages", () => {
|
|
256
|
+
const varRefkey = refkey();
|
|
257
|
+
const fooRefkey = refkey();
|
|
258
|
+
const barRefkey = refkey();
|
|
259
|
+
const decl =
|
|
260
|
+
<Output>
|
|
261
|
+
<ts.PackageDirectory name="SourcePackage" path="sp" version="1.0.0">
|
|
262
|
+
<ts.SourceFile export="." path="foo.ts">
|
|
263
|
+
<ts.VarDeclaration export name="refme" refkey={varRefkey}>
|
|
264
|
+
<ts.ObjectExpression>
|
|
265
|
+
<ts.ObjectProperty name="foo" refkey={fooRefkey}>
|
|
266
|
+
<ts.ObjectExpression>
|
|
267
|
+
<ts.ObjectProperty name="bar" refkey={barRefkey}>
|
|
268
|
+
"hello";
|
|
269
|
+
</ts.ObjectProperty>
|
|
270
|
+
</ts.ObjectExpression>
|
|
271
|
+
</ts.ObjectProperty>
|
|
272
|
+
</ts.ObjectExpression>
|
|
273
|
+
</ts.VarDeclaration>
|
|
274
|
+
</ts.SourceFile>
|
|
275
|
+
</ts.PackageDirectory>
|
|
276
|
+
|
|
277
|
+
<ts.PackageDirectory name="DepPackage" path="dp" version="1.0.0">
|
|
278
|
+
<ts.SourceFile path="bar.ts">
|
|
279
|
+
console.log({barRefkey});
|
|
280
|
+
</ts.SourceFile>
|
|
281
|
+
</ts.PackageDirectory>
|
|
282
|
+
</Output>;
|
|
283
|
+
const res = render(decl);
|
|
284
|
+
assertFileContents(res, {
|
|
285
|
+
"dp/bar.ts": `
|
|
286
|
+
import { refme } from "SourcePackage";
|
|
287
|
+
|
|
288
|
+
console.log(refme.foo.bar);
|
|
289
|
+
`,
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Output, refkey, render } from "@alloy-js/core";
|
|
2
|
+
import "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import * as ts from "../src/index.js";
|
|
5
|
+
import { assertFileContents } from "./utils.js";
|
|
6
|
+
|
|
7
|
+
it("works", () => {
|
|
8
|
+
expect(
|
|
9
|
+
<Output>
|
|
10
|
+
<ts.SourceFile path="test.js">
|
|
11
|
+
<ts.VarDeclaration name="hi" value="12" />
|
|
12
|
+
</ts.SourceFile>
|
|
13
|
+
</Output>,
|
|
14
|
+
).toRenderTo("const hi = 12;");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("works end-to-end", () => {
|
|
18
|
+
const TestType = refkey("TestType");
|
|
19
|
+
|
|
20
|
+
const res = render(
|
|
21
|
+
<Output>
|
|
22
|
+
<ts.SourceFile path="types.ts">
|
|
23
|
+
<ts.TypeDeclaration name="TestType" refkey={TestType}>
|
|
24
|
+
"hello" | "goodbye"
|
|
25
|
+
</ts.TypeDeclaration>
|
|
26
|
+
</ts.SourceFile>
|
|
27
|
+
<ts.SourceFile path="test.ts">
|
|
28
|
+
<ts.VarDeclaration export let name="hi" type={<ts.Reference refkey={TestType} />}>
|
|
29
|
+
"hello"
|
|
30
|
+
</ts.VarDeclaration>
|
|
31
|
+
</ts.SourceFile>
|
|
32
|
+
</Output>,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
assertFileContents(res, {
|
|
36
|
+
"types.ts": `
|
|
37
|
+
type TestType = "hello" | "goodbye";
|
|
38
|
+
`,
|
|
39
|
+
"test.ts": `
|
|
40
|
+
import { TestType } from "./types.js";
|
|
41
|
+
|
|
42
|
+
export let hi: TestType = "hello";
|
|
43
|
+
`,
|
|
44
|
+
});
|
|
45
|
+
});
|
package/test/vitest.d.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"emitDeclarationOnly": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "dist"
|
|
7
|
+
},
|
|
8
|
+
"references": [{ "path": "../core" }],
|
|
9
|
+
"include": ["src/**/*.ts", "src/**/*.tsx", "test/**/*.ts", "test/**/*.tsx"],
|
|
10
|
+
"exclude": ["node_modules", "dist"]
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
+
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
+
{
|
|
4
|
+
"tsdocVersion": "0.12",
|
|
5
|
+
"toolPackages": [
|
|
6
|
+
{
|
|
7
|
+
"packageName": "@microsoft/api-extractor",
|
|
8
|
+
"packageVersion": "7.47.7"
|
|
9
|
+
}
|
|
10
|
+
]
|
|
11
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { babel } from "@rollup/plugin-babel";
|
|
2
|
+
import { defineConfig } from "vitest/config";
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
esbuild: {
|
|
6
|
+
jsx: "preserve",
|
|
7
|
+
sourcemap: "both",
|
|
8
|
+
},
|
|
9
|
+
plugins: [
|
|
10
|
+
babel({
|
|
11
|
+
inputSourceMap: true as any,
|
|
12
|
+
sourceMaps: "both",
|
|
13
|
+
babelHelpers: "bundled",
|
|
14
|
+
extensions: [".ts", ".tsx"],
|
|
15
|
+
presets: ["@babel/preset-typescript", "@alloy-js/babel-preset"],
|
|
16
|
+
}),
|
|
17
|
+
],
|
|
18
|
+
});
|