@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,201 @@
|
|
|
1
|
+
import { Props, refkey } from "@alloy-js/core";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
FunctionDeclaration,
|
|
6
|
+
ParameterDescriptor,
|
|
7
|
+
VarDeclaration,
|
|
8
|
+
} from "../src/index.js";
|
|
9
|
+
import { toSourceText } from "./utils.js";
|
|
10
|
+
|
|
11
|
+
it("works", () => {
|
|
12
|
+
expect(toSourceText(<FunctionDeclaration name="foo" />)).toBe(d`
|
|
13
|
+
function foo() {
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
`);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("can be exported", () => {
|
|
20
|
+
expect(toSourceText(<FunctionDeclaration export name="foo" />)).toBe(d`
|
|
21
|
+
export function foo() {
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
`);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("can be a default export", () => {
|
|
28
|
+
expect(toSourceText(<FunctionDeclaration export default name="foo" />))
|
|
29
|
+
.toBe(d`
|
|
30
|
+
export default function foo() {
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
`);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("can be a default export", () => {
|
|
37
|
+
expect(toSourceText(<FunctionDeclaration export default name="foo" />))
|
|
38
|
+
.toBe(d`
|
|
39
|
+
export default function foo() {
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
`);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("can be an async function", () => {
|
|
46
|
+
expect(toSourceText(<FunctionDeclaration async export name="foo" />)).toBe(d`
|
|
47
|
+
export async function foo() {
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
`);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("can be an async function with returnType", () => {
|
|
54
|
+
expect(
|
|
55
|
+
toSourceText(
|
|
56
|
+
<FunctionDeclaration async export name="foo" returnType="Foo"/>,
|
|
57
|
+
),
|
|
58
|
+
).toBe(d`
|
|
59
|
+
export async function foo(): Promise<Foo> {
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
`);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("can be an async function with returnType element", () => {
|
|
66
|
+
function Foo(_props?: Props) {
|
|
67
|
+
return <>Foo</>;
|
|
68
|
+
}
|
|
69
|
+
expect(
|
|
70
|
+
toSourceText(
|
|
71
|
+
<FunctionDeclaration async export name="foo" returnType={<Foo />}/>,
|
|
72
|
+
),
|
|
73
|
+
).toBe(d`
|
|
74
|
+
export async function foo(): Promise<Foo> {
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
`);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("supports parameters by element", () => {
|
|
81
|
+
const decl =
|
|
82
|
+
<FunctionDeclaration name="foo">
|
|
83
|
+
return a + b;
|
|
84
|
+
<FunctionDeclaration.Parameters>a, b</FunctionDeclaration.Parameters>
|
|
85
|
+
</FunctionDeclaration>;
|
|
86
|
+
|
|
87
|
+
// there is an extra line break here because the return statement ends in a
|
|
88
|
+
// line break, which is not significant but we don't know that at transform
|
|
89
|
+
// time. Ideally the renderer would take care of this - I think it's the same
|
|
90
|
+
// problem as handling removal of linebreaks when the only contents of a line
|
|
91
|
+
// are something undefined, e.g. how
|
|
92
|
+
//
|
|
93
|
+
// <>a
|
|
94
|
+
// {undefined}
|
|
95
|
+
// b</>
|
|
96
|
+
//
|
|
97
|
+
// should render to
|
|
98
|
+
//
|
|
99
|
+
// a
|
|
100
|
+
// b
|
|
101
|
+
//
|
|
102
|
+
// so in principle this can be fixed later.
|
|
103
|
+
expect(toSourceText(decl)).toBe(d`
|
|
104
|
+
function foo(a, b) {
|
|
105
|
+
return a + b;
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
`);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("symbols", () => {
|
|
112
|
+
it("creates a nested scope", () => {
|
|
113
|
+
const innerRefkey = refkey();
|
|
114
|
+
const outerRefkey = refkey();
|
|
115
|
+
const decl =
|
|
116
|
+
<>
|
|
117
|
+
<FunctionDeclaration name="foo">
|
|
118
|
+
{innerRefkey}
|
|
119
|
+
<VarDeclaration name="refme" refkey={innerRefkey}>1</VarDeclaration>
|
|
120
|
+
</FunctionDeclaration>;
|
|
121
|
+
<VarDeclaration name="refme" refkey={outerRefkey}>2</VarDeclaration>
|
|
122
|
+
{outerRefkey}
|
|
123
|
+
</>;
|
|
124
|
+
expect(toSourceText(decl)).toBe(d`
|
|
125
|
+
function foo() {
|
|
126
|
+
refme
|
|
127
|
+
const refme = 1;
|
|
128
|
+
};
|
|
129
|
+
const refme = 2;
|
|
130
|
+
refme
|
|
131
|
+
`);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("throws an error when trying to access a symbol in a nested function scope", () => {
|
|
135
|
+
const innerRefkey = refkey();
|
|
136
|
+
const decl =
|
|
137
|
+
<>
|
|
138
|
+
<FunctionDeclaration name="foo">
|
|
139
|
+
<VarDeclaration name="refme" refkey={innerRefkey}>1</VarDeclaration>
|
|
140
|
+
</FunctionDeclaration>;
|
|
141
|
+
{innerRefkey}
|
|
142
|
+
</>;
|
|
143
|
+
expect(() => toSourceText(decl)).toThrow(/Cannot reference a symbol/);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("creates symbols for parameters", () => {
|
|
147
|
+
const rk = refkey();
|
|
148
|
+
|
|
149
|
+
const decl =
|
|
150
|
+
<>
|
|
151
|
+
<FunctionDeclaration name="foo" parameters={{sym: { type: "any", refkey: rk }}}>
|
|
152
|
+
<FunctionDeclaration name="bar">
|
|
153
|
+
{rk}
|
|
154
|
+
</FunctionDeclaration>
|
|
155
|
+
</FunctionDeclaration>
|
|
156
|
+
</>;
|
|
157
|
+
|
|
158
|
+
expect(toSourceText(decl)).toBe(d`
|
|
159
|
+
function foo(sym: any) {
|
|
160
|
+
function bar() {
|
|
161
|
+
sym
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
`);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("creates symbols for parameters and addresses conflicts", () => {
|
|
168
|
+
const decl =
|
|
169
|
+
<>
|
|
170
|
+
<FunctionDeclaration name="foo" parameters={{conflict: "any"}}>
|
|
171
|
+
<VarDeclaration name="conflict">1</VarDeclaration>
|
|
172
|
+
</FunctionDeclaration>
|
|
173
|
+
</>;
|
|
174
|
+
|
|
175
|
+
expect(toSourceText(decl)).toBe(d`
|
|
176
|
+
function foo(conflict: any) {
|
|
177
|
+
const conflict_2 = 1;
|
|
178
|
+
}
|
|
179
|
+
`);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("create optional parameters", () => {
|
|
183
|
+
const paramDesc: ParameterDescriptor = {
|
|
184
|
+
refkey: refkey(),
|
|
185
|
+
type: "any",
|
|
186
|
+
optional: true,
|
|
187
|
+
};
|
|
188
|
+
const decl =
|
|
189
|
+
<>
|
|
190
|
+
<FunctionDeclaration name="foo" parameters={{foo: paramDesc}}>
|
|
191
|
+
console.log(foo);
|
|
192
|
+
</FunctionDeclaration>
|
|
193
|
+
</>;
|
|
194
|
+
|
|
195
|
+
expect(toSourceText(decl)).toBe(d`
|
|
196
|
+
function foo(foo?: any) {
|
|
197
|
+
console.log(foo);
|
|
198
|
+
}
|
|
199
|
+
`);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { Output, SourceDirectory, 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 { tsNameConflictResolver } from "../src/name-conflict-resolver.js";
|
|
7
|
+
import { assertFileContents } from "./utils.js";
|
|
8
|
+
|
|
9
|
+
it("works with default imports", () => {
|
|
10
|
+
const res = render(
|
|
11
|
+
<Output>
|
|
12
|
+
<ts.SourceFile path="test1.ts">
|
|
13
|
+
<ts.FunctionDeclaration export default name="asdf" refkey={refkey("test")} />
|
|
14
|
+
</ts.SourceFile>
|
|
15
|
+
|
|
16
|
+
<ts.SourceFile path="test2.ts">
|
|
17
|
+
const v = <Reference refkey={refkey("test")} />;
|
|
18
|
+
</ts.SourceFile>
|
|
19
|
+
</Output>,
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
assertFileContents(res, {
|
|
23
|
+
"test1.ts": `
|
|
24
|
+
export default function asdf() {
|
|
25
|
+
|
|
26
|
+
}
|
|
27
|
+
`,
|
|
28
|
+
"test2.ts": `
|
|
29
|
+
import asdf from "./test1.js";
|
|
30
|
+
|
|
31
|
+
const v = asdf;
|
|
32
|
+
`,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("works with named imports", () => {
|
|
37
|
+
const res = render(
|
|
38
|
+
<Output>
|
|
39
|
+
<ts.SourceFile path="test1.ts">
|
|
40
|
+
<ts.FunctionDeclaration export name="test" />
|
|
41
|
+
</ts.SourceFile>
|
|
42
|
+
|
|
43
|
+
<ts.SourceFile path="test2.ts">
|
|
44
|
+
const v = <Reference refkey={refkey("test")} />;
|
|
45
|
+
</ts.SourceFile>
|
|
46
|
+
</Output>,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
assertFileContents(res, {
|
|
50
|
+
"test1.ts": `
|
|
51
|
+
export function test() {
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
`,
|
|
55
|
+
"test2.ts": `
|
|
56
|
+
import { test } from "./test1.js";
|
|
57
|
+
|
|
58
|
+
const v = test;
|
|
59
|
+
`,
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("works with default and named imports", () => {
|
|
64
|
+
const res = render(
|
|
65
|
+
<Output>
|
|
66
|
+
<ts.SourceFile path="test1.ts">
|
|
67
|
+
<ts.FunctionDeclaration export default name="test1" />
|
|
68
|
+
<ts.FunctionDeclaration export name="test2" />
|
|
69
|
+
</ts.SourceFile>
|
|
70
|
+
|
|
71
|
+
<ts.SourceFile path="test2.ts">
|
|
72
|
+
const v1 = <Reference refkey={refkey("test1")} />;
|
|
73
|
+
const v2 = <Reference refkey={refkey("test2")} />;
|
|
74
|
+
</ts.SourceFile>
|
|
75
|
+
</Output>,
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
assertFileContents(res, {
|
|
79
|
+
"test1.ts": `
|
|
80
|
+
export default function test1() {
|
|
81
|
+
|
|
82
|
+
}
|
|
83
|
+
export function test2() {
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
`,
|
|
87
|
+
"test2.ts": `
|
|
88
|
+
import test1, { test2 } from "./test1.js";
|
|
89
|
+
|
|
90
|
+
const v1 = test1;
|
|
91
|
+
const v2 = test2;
|
|
92
|
+
`,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("works with default and named imports and name conflicts", () => {
|
|
97
|
+
const res = render(
|
|
98
|
+
<Output nameConflictResolver={tsNameConflictResolver}>
|
|
99
|
+
<ts.SourceFile path="test1.ts">
|
|
100
|
+
<ts.FunctionDeclaration export default name="test1" />
|
|
101
|
+
<ts.FunctionDeclaration export name="test2" />
|
|
102
|
+
</ts.SourceFile>
|
|
103
|
+
|
|
104
|
+
<ts.SourceFile path="test2.ts">
|
|
105
|
+
<ts.FunctionDeclaration export default name="test1" refkey={refkey("test3")} />
|
|
106
|
+
<ts.FunctionDeclaration export name="test2" refkey={refkey("test4")} />
|
|
107
|
+
</ts.SourceFile>
|
|
108
|
+
|
|
109
|
+
<ts.SourceFile path="test3.ts">
|
|
110
|
+
const v1 = <Reference refkey={refkey("test1")} />;
|
|
111
|
+
const v1_1 = <Reference refkey={refkey("test2")}/>;
|
|
112
|
+
const v2 = <Reference refkey={refkey("test3")} />;
|
|
113
|
+
const v3 = <Reference refkey={refkey("test3")}/>;
|
|
114
|
+
const v4 = <Reference refkey={refkey("test4")} />;
|
|
115
|
+
</ts.SourceFile>
|
|
116
|
+
</Output>,
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
assertFileContents(res, {
|
|
120
|
+
"test1.ts": `
|
|
121
|
+
export default function test1() {
|
|
122
|
+
|
|
123
|
+
}
|
|
124
|
+
export function test2() {
|
|
125
|
+
|
|
126
|
+
}
|
|
127
|
+
`,
|
|
128
|
+
"test2.ts": `
|
|
129
|
+
export default function test1() {
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
export function test2() {
|
|
133
|
+
|
|
134
|
+
}
|
|
135
|
+
`,
|
|
136
|
+
"test3.ts": `
|
|
137
|
+
import test1_1, { test2 as test2_1 } from "./test1.js";
|
|
138
|
+
import test1_2, { test2 as test2_2 } from "./test2.js";
|
|
139
|
+
|
|
140
|
+
const v1 = test1_1;
|
|
141
|
+
const v1_1 = test2_1;
|
|
142
|
+
const v2 = test1_2;
|
|
143
|
+
const v3 = test1_2;
|
|
144
|
+
const v4 = test2_2;
|
|
145
|
+
`,
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("works with imports from different directories", () => {
|
|
150
|
+
const res = render(
|
|
151
|
+
<Output>
|
|
152
|
+
<SourceDirectory path="src">
|
|
153
|
+
<ts.SourceFile path="test1.ts">
|
|
154
|
+
<ts.FunctionDeclaration export name="test" />
|
|
155
|
+
const v = <Reference refkey={refkey("test2")} />
|
|
156
|
+
</ts.SourceFile>
|
|
157
|
+
</SourceDirectory>
|
|
158
|
+
|
|
159
|
+
<ts.SourceFile path="test2.ts">
|
|
160
|
+
const v = <Reference refkey={refkey("test")} />;
|
|
161
|
+
<ts.FunctionDeclaration export name="test2" />
|
|
162
|
+
</ts.SourceFile>
|
|
163
|
+
</Output>,
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
assertFileContents(res, {
|
|
167
|
+
"src/test1.ts": `
|
|
168
|
+
import { test2 } from "../test2.js";
|
|
169
|
+
|
|
170
|
+
export function test() {
|
|
171
|
+
|
|
172
|
+
}
|
|
173
|
+
const v = test2
|
|
174
|
+
`,
|
|
175
|
+
"test2.ts": `
|
|
176
|
+
import { test } from "./src/test1.js";
|
|
177
|
+
|
|
178
|
+
const v = test;
|
|
179
|
+
export function test2() {
|
|
180
|
+
|
|
181
|
+
}
|
|
182
|
+
`,
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("handles conflicts with local declarations", () => {
|
|
187
|
+
const res = render(
|
|
188
|
+
<Output nameConflictResolver={tsNameConflictResolver}>
|
|
189
|
+
<SourceDirectory path="src">
|
|
190
|
+
<ts.SourceFile path="test1.ts">
|
|
191
|
+
<ts.FunctionDeclaration export name="test" />
|
|
192
|
+
const v = <Reference refkey={refkey("test2")} />
|
|
193
|
+
</ts.SourceFile>
|
|
194
|
+
</SourceDirectory>
|
|
195
|
+
|
|
196
|
+
<ts.SourceFile path="test2.ts">
|
|
197
|
+
const v = <Reference refkey={refkey("test")} />;
|
|
198
|
+
<ts.FunctionDeclaration export name="test" />
|
|
199
|
+
</ts.SourceFile>
|
|
200
|
+
</Output>,
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
assertFileContents(res, {
|
|
204
|
+
"test2.ts": `
|
|
205
|
+
import { test as test_1 } from "./src/test1.js";
|
|
206
|
+
|
|
207
|
+
const v = test_1;
|
|
208
|
+
export function test() {
|
|
209
|
+
|
|
210
|
+
}
|
|
211
|
+
`,
|
|
212
|
+
});
|
|
213
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { NamePolicyContext, refkey } from "@alloy-js/core";
|
|
2
|
+
import "@alloy-js/core/testing";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
4
|
+
import { expect, it } from "vitest";
|
|
5
|
+
import * as ts from "../src/components/index.js";
|
|
6
|
+
import { Reference } from "../src/components/Reference.js";
|
|
7
|
+
import { createTSNamePolicy } from "../src/name-policy.js";
|
|
8
|
+
import { toSourceText } from "./utils.js";
|
|
9
|
+
|
|
10
|
+
it("declares interfaces", () => {
|
|
11
|
+
const res = toSourceText(<ts.InterfaceDeclaration name="Foo" />);
|
|
12
|
+
expect(res).toEqual(d`
|
|
13
|
+
interface Foo {
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
`);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("accepts export and default", () => {
|
|
20
|
+
const res = toSourceText(
|
|
21
|
+
<ts.InterfaceDeclaration name="Foo" export default />,
|
|
22
|
+
);
|
|
23
|
+
expect(res).toEqual(d`
|
|
24
|
+
export default interface Foo {
|
|
25
|
+
|
|
26
|
+
}
|
|
27
|
+
`);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("creates extends", () => {
|
|
31
|
+
const res = toSourceText(
|
|
32
|
+
<ts.InterfaceDeclaration name="Foo" extends="string" />,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
expect(res).toEqual(d`
|
|
36
|
+
interface Foo extends string {
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
`);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("can create members", () => {
|
|
43
|
+
const res = toSourceText(
|
|
44
|
+
<ts.InterfaceDeclaration name="Foo">
|
|
45
|
+
<ts.InterfaceMember name="member" type="string" />;
|
|
46
|
+
<ts.InterfaceMember name="circular" type={<Reference refkey={refkey("Foo")} />} />;
|
|
47
|
+
<ts.InterfaceMember indexer="str: string" type="number" />;
|
|
48
|
+
</ts.InterfaceDeclaration>,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(res).toEqual(d`
|
|
52
|
+
interface Foo {
|
|
53
|
+
member: string;
|
|
54
|
+
circular: Foo;
|
|
55
|
+
[str: string]: number;
|
|
56
|
+
}
|
|
57
|
+
`);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("can create optional members", () => {
|
|
61
|
+
const res = toSourceText(
|
|
62
|
+
<ts.InterfaceDeclaration name="Foo">
|
|
63
|
+
<ts.InterfaceMember name="member" type="string" />;
|
|
64
|
+
<ts.InterfaceMember optional name="circular" type={<Reference refkey={refkey("Foo")} />} />;
|
|
65
|
+
<ts.InterfaceMember indexer="str: string" type="number" />;
|
|
66
|
+
</ts.InterfaceDeclaration>,
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(res).toEqual(d`
|
|
70
|
+
interface Foo {
|
|
71
|
+
member: string;
|
|
72
|
+
circular?: Foo;
|
|
73
|
+
[str: string]: number;
|
|
74
|
+
}
|
|
75
|
+
`);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("can create readonly members", () => {
|
|
79
|
+
const res = toSourceText(
|
|
80
|
+
<ts.InterfaceDeclaration name="Foo">
|
|
81
|
+
<ts.InterfaceMember readonly name="member" type="string" />;
|
|
82
|
+
<ts.InterfaceMember name="circular" type={<Reference refkey={refkey("Foo")} />} />;
|
|
83
|
+
<ts.InterfaceMember indexer="str: string" type="number" />;
|
|
84
|
+
</ts.InterfaceDeclaration>,
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect(res).toEqual(d`
|
|
88
|
+
interface Foo {
|
|
89
|
+
readonly member: string;
|
|
90
|
+
circular: Foo;
|
|
91
|
+
[str: string]: number;
|
|
92
|
+
}
|
|
93
|
+
`);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("has interface expressions", () => {
|
|
97
|
+
const res = toSourceText(
|
|
98
|
+
<ts.InterfaceExpression>
|
|
99
|
+
<ts.InterfaceMember name="member" type="string" />;
|
|
100
|
+
</ts.InterfaceExpression>,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
expect(res).toEqual(d`
|
|
104
|
+
{
|
|
105
|
+
member: string;
|
|
106
|
+
}
|
|
107
|
+
`);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("supports the naming policy", () => {
|
|
111
|
+
const policy = createTSNamePolicy();
|
|
112
|
+
const res = toSourceText(
|
|
113
|
+
<NamePolicyContext.Provider value={policy}>
|
|
114
|
+
<ts.InterfaceDeclaration name="interface_name">
|
|
115
|
+
<ts.InterfaceMember name="member_property" type="string" />;
|
|
116
|
+
</ts.InterfaceDeclaration>
|
|
117
|
+
</NamePolicyContext.Provider>,
|
|
118
|
+
);
|
|
119
|
+
expect(res).toEqual(d`
|
|
120
|
+
interface InterfaceName {
|
|
121
|
+
memberProperty: string;
|
|
122
|
+
}
|
|
123
|
+
`);
|
|
124
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Output, refkey, render } from "@alloy-js/core";
|
|
2
|
+
import "@alloy-js/core/testing";
|
|
3
|
+
import { d } from "@alloy-js/core/testing";
|
|
4
|
+
import { expect, it } from "vitest";
|
|
5
|
+
import * as ts from "../src/components/index.js";
|
|
6
|
+
import { createTSNamePolicy } from "../src/name-policy.js";
|
|
7
|
+
|
|
8
|
+
it("applies to functions and variables", () => {
|
|
9
|
+
const ref1 = refkey({});
|
|
10
|
+
const ref2 = refkey({});
|
|
11
|
+
|
|
12
|
+
const namePolicy = createTSNamePolicy();
|
|
13
|
+
const res = render(
|
|
14
|
+
<Output namePolicy={namePolicy}>
|
|
15
|
+
<ts.SourceFile path="test.ts">
|
|
16
|
+
<ts.FunctionDeclaration name="foo-bar" refkey={ref1} />
|
|
17
|
+
<ts.VarDeclaration name="one-two" refkey={ref2}>
|
|
18
|
+
"hello"
|
|
19
|
+
</ts.VarDeclaration>
|
|
20
|
+
<ts.Reference refkey={ref1} />;
|
|
21
|
+
<ts.Reference refkey={ref2} />;
|
|
22
|
+
</ts.SourceFile>
|
|
23
|
+
</Output>,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
expect(res.contents[0].contents).toEqual(d`
|
|
27
|
+
function fooBar() {
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
const oneTwo = "hello";
|
|
31
|
+
fooBar;
|
|
32
|
+
oneTwo;
|
|
33
|
+
`);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("keeps _ and $ prefix", () => {
|
|
37
|
+
const ref1 = refkey({});
|
|
38
|
+
const ref2 = refkey({});
|
|
39
|
+
|
|
40
|
+
const namePolicy = createTSNamePolicy();
|
|
41
|
+
const res = render(
|
|
42
|
+
<Output namePolicy={namePolicy}>
|
|
43
|
+
<ts.SourceFile path="test.ts">
|
|
44
|
+
<ts.FunctionDeclaration name="_foo-bar" refkey={ref1} />
|
|
45
|
+
<ts.VarDeclaration name="$one-two" refkey={ref2}>
|
|
46
|
+
"hello"
|
|
47
|
+
</ts.VarDeclaration>
|
|
48
|
+
<ts.Reference refkey={ref1} />;
|
|
49
|
+
<ts.Reference refkey={ref2} />;
|
|
50
|
+
</ts.SourceFile>
|
|
51
|
+
</Output>,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(res.contents[0].contents).toEqual(d`
|
|
55
|
+
function _fooBar() {
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
const $oneTwo = "hello";
|
|
59
|
+
_fooBar;
|
|
60
|
+
$oneTwo;
|
|
61
|
+
`);
|
|
62
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Output, refkey, render } from "@alloy-js/core";
|
|
2
|
+
import { d } from "@alloy-js/core/testing";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import * as ts from "../src/index.js";
|
|
5
|
+
import { findFile } from "./utils.js";
|
|
6
|
+
|
|
7
|
+
it("imports external packages", () => {
|
|
8
|
+
const res = render(
|
|
9
|
+
<Output>
|
|
10
|
+
<ts.PackageDirectory name="greeting-lib" path="greeting-lib" version="1.0.0">
|
|
11
|
+
<ts.SourceFile path="greetings.ts">
|
|
12
|
+
<ts.FunctionDeclaration name="getGreeting">
|
|
13
|
+
return "Hello world!";
|
|
14
|
+
</ts.FunctionDeclaration>
|
|
15
|
+
</ts.SourceFile>
|
|
16
|
+
|
|
17
|
+
<ts.SourceFile path="logGreetings.ts">
|
|
18
|
+
<ts.FunctionDeclaration export name="printGreeting">
|
|
19
|
+
console.log("Hello world!");
|
|
20
|
+
</ts.FunctionDeclaration>
|
|
21
|
+
</ts.SourceFile>
|
|
22
|
+
|
|
23
|
+
<ts.BarrelFile export="." />
|
|
24
|
+
</ts.PackageDirectory>
|
|
25
|
+
|
|
26
|
+
<ts.PackageDirectory name="consumer" path="consumer" version="1.0.0">
|
|
27
|
+
<ts.SourceFile export="." path="ref.ts">
|
|
28
|
+
<ts.Reference refkey={refkey("getGreeting")} />();
|
|
29
|
+
</ts.SourceFile>
|
|
30
|
+
</ts.PackageDirectory>
|
|
31
|
+
</Output>,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const consumerPkgJson = findFile(res, "consumer/package.json")!;
|
|
35
|
+
const greetingPkgJson = findFile(res, "greeting-lib/package.json")!;
|
|
36
|
+
const refFile = findFile(res, "consumer/ref.ts")!;
|
|
37
|
+
|
|
38
|
+
expect(consumerPkgJson.contents).toEqual(d`
|
|
39
|
+
{
|
|
40
|
+
"name": "consumer",
|
|
41
|
+
"version": "1.0.0",
|
|
42
|
+
"type": "module",
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"greeting-lib": "1.0.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"typescript": "^5.5.2"
|
|
48
|
+
},
|
|
49
|
+
"exports": {
|
|
50
|
+
".": "./dist/ref.js"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
`);
|
|
54
|
+
|
|
55
|
+
expect(greetingPkgJson.contents).toEqual(d`
|
|
56
|
+
{
|
|
57
|
+
"name": "greeting-lib",
|
|
58
|
+
"version": "1.0.0",
|
|
59
|
+
"type": "module",
|
|
60
|
+
"dependencies": {},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"typescript": "^5.5.2"
|
|
63
|
+
},
|
|
64
|
+
"exports": {
|
|
65
|
+
".": "./dist/index.js"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
`);
|
|
69
|
+
|
|
70
|
+
expect(refFile.contents).toEqual(d`
|
|
71
|
+
import { getGreeting } from "greeting-lib";
|
|
72
|
+
|
|
73
|
+
getGreeting();
|
|
74
|
+
`);
|
|
75
|
+
});
|