@xyd-js/sources 0.0.0-build
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 +9 -0
- package/ISSUES.md +10 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/TODO.md +9 -0
- package/__fixtures__/-1.typescript/package.json +5 -0
- package/__fixtures__/-1.typescript/src/index.ts +0 -0
- package/__fixtures__/-1.typescript/src/settings.ts +592 -0
- package/__fixtures__/-1.typescript/tsconfig.json +8 -0
- package/__fixtures__/-2.react.basic/1.flat-interface.output.json +114 -0
- package/__fixtures__/-2.react.basic/2.file-connect-interface.output.json +60 -0
- package/__fixtures__/-2.react.basic/2a.file-connect-interface-advanced.output.json +92 -0
- package/__fixtures__/-2.react.basic/3.props-as-variants.output.json +166 -0
- package/__fixtures__/-2.react.basic/4.outside-interface.output.json +60 -0
- package/__fixtures__/-2.react.basic/5.inline-props.output.json +60 -0
- package/__fixtures__/-2.react.basic/5.outside-inline-props.output.json +60 -0
- package/__fixtures__/-2.react.basic/5a.inline-props+outside.output.json +60 -0
- package/__fixtures__/-2.react.basic/5b.inline-props+interfaces.output.json +92 -0
- package/__fixtures__/-2.react.basic/package.json +4 -0
- package/__fixtures__/-2.react.basic/src/1.flat-interface.tsx +41 -0
- package/__fixtures__/-2.react.basic/src/2.file-connect-interface.tsx +28 -0
- package/__fixtures__/-2.react.basic/src/2a.file-connect-interace+advanced.tsx +54 -0
- package/__fixtures__/-2.react.basic/src/3.props-as-variants.tsx +67 -0
- package/__fixtures__/-2.react.basic/src/4.outside-interface.tsx +13 -0
- package/__fixtures__/-2.react.basic/src/4.outside-interface2.ts +15 -0
- package/__fixtures__/-2.react.basic/src/5.inline-props.tsx +19 -0
- package/__fixtures__/-2.react.basic/src/5a.inline-props+outside.tsx +19 -0
- package/__fixtures__/-2.react.basic/src/5b.inline-props+interfaces.tsx +62 -0
- package/__fixtures__/-2.react.basic/src/index.ts +0 -0
- package/__fixtures__/-2.react.basic/tsconfig.json +8 -0
- package/__tests__/sourcesToUniform.test.ts +167 -0
- package/__tests__/testResolvePropertySymbol.ts +838 -0
- package/__tests__/types.ts +12 -0
- package/__tests__/utils.ts +108 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +237 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +6 -0
- package/dist/react.d.ts +6 -0
- package/dist/react.js +212 -0
- package/dist/react.js.map +1 -0
- package/dist/ts.cjs +1794 -0
- package/dist/ts.cjs.map +1 -0
- package/dist/ts.d.cts +14 -0
- package/dist/ts.d.ts +14 -0
- package/dist/ts.js +1760 -0
- package/dist/ts.js.map +1 -0
- package/package.json +41 -0
- package/packages/react/index.ts +1 -0
- package/packages/react/uniformToReactUniform.ts +275 -0
- package/packages/ts/SignatureText.ts +233 -0
- package/packages/ts/TypeDocTransformer.ts +1519 -0
- package/packages/ts/__fixtures__/packages/package-a/package.json +4 -0
- package/packages/ts/__fixtures__/packages/package-a/src/index.ts +56 -0
- package/packages/ts/__fixtures__/packages/package-a/tsconfig.json +23 -0
- package/packages/ts/__fixtures__/packages/package-b/package.json +7 -0
- package/packages/ts/__fixtures__/packages/package-b/src/billing.ts +193 -0
- package/packages/ts/__fixtures__/packages/package-b/src/index.ts +8 -0
- package/packages/ts/__fixtures__/packages/package-b/tsconfig.json +20 -0
- package/packages/ts/__fixtures__/packages2/package-a/package.json +4 -0
- package/packages/ts/__fixtures__/packages2/package-a/src/index.ts +496 -0
- package/packages/ts/__fixtures__/packages2/package-a/tsconfig.json +6 -0
- package/packages/ts/__fixtures__/packages3/package-a/package.json +4 -0
- package/packages/ts/__fixtures__/packages3/package-a/src/index.ts +488 -0
- package/packages/ts/__fixtures__/packages3/package-a/tsconfig.json +6 -0
- package/packages/ts/__fixtures__/packages3/project.json +171 -0
- package/packages/ts/__fixtures__/react/react-a/package.json +5 -0
- package/packages/ts/__fixtures__/react/react-a/src/TestAbc.tsx +90 -0
- package/packages/ts/__fixtures__/react/react-a/src/TestBasic.tsx +27 -0
- package/packages/ts/__fixtures__/react/react-a/src/TestNamedParameters.tsx +27 -0
- package/packages/ts/__fixtures__/react/react-a/src/TestNamedParameters2.tsx +26 -0
- package/packages/ts/__fixtures__/react/react-a/src/TestUnion.tsx +32 -0
- package/packages/ts/__fixtures__/react/react-a/src/index.ts +1 -0
- package/packages/ts/__fixtures__/react/react-a/tsconfig.json +8 -0
- package/packages/ts/__fixtures__/references-output-project.json +344 -0
- package/packages/ts/__fixtures__/references-output-react.json +68 -0
- package/packages/ts/__fixtures__/references-output.json +129 -0
- package/packages/ts/__tests__/sourcesToUniform.test.ts +106 -0
- package/packages/ts/context.ts +0 -0
- package/packages/ts/converterts/ts-class.ts +0 -0
- package/packages/ts/converterts/ts-enum.ts +0 -0
- package/packages/ts/converterts/ts-function.ts +0 -0
- package/packages/ts/converterts/ts-interface.ts +0 -0
- package/packages/ts/converterts/ts-type.ts +0 -0
- package/packages/ts/index.ts +129 -0
- package/packages/ts/ts-core.ts +0 -0
- package/packages/ts/uniformToMiniUniform.ts +486 -0
- package/src/index.ts +0 -0
- package/test-cmd/index.ts +62 -0
- package/tsconfig.json +38 -0
- package/tsup.config.ts +39 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
|
|
4
|
+
// TODO: in the future more lightweight solution ??? - currently it's fine cuz it uses battle-tested TypeScript API
|
|
5
|
+
|
|
6
|
+
const printer = ts.createPrinter({removeComments: true});
|
|
7
|
+
|
|
8
|
+
export class SignatureTextLoader {
|
|
9
|
+
protected sourceFile: ts.SourceFile;
|
|
10
|
+
|
|
11
|
+
constructor(sourcePath: string) {
|
|
12
|
+
const source = fs.readFileSync(sourcePath, 'utf-8');
|
|
13
|
+
|
|
14
|
+
this.sourceFile = ts.createSourceFile(
|
|
15
|
+
sourcePath,
|
|
16
|
+
source,
|
|
17
|
+
ts.ScriptTarget.Latest,
|
|
18
|
+
true
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class MultiSignatureLoader {
|
|
24
|
+
private loaders: Map<string, SignatureTextLoader>
|
|
25
|
+
|
|
26
|
+
constructor() {
|
|
27
|
+
this.loaders = new Map<string, SignatureTextLoader>()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected load(path: string) {
|
|
31
|
+
if (this.loaders.has(path)) {
|
|
32
|
+
return this.loaders.get(path)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const loader = new SignatureTextLoader(path)
|
|
36
|
+
this.loaders.set(path, loader)
|
|
37
|
+
|
|
38
|
+
return loader
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get the signature text of a function, class, interface, enum, or type alias at a specific line.
|
|
44
|
+
*
|
|
45
|
+
* @param sign - instance of SignatureText
|
|
46
|
+
* @param targetLine - the line number of the signature in source code
|
|
47
|
+
*
|
|
48
|
+
* @returns code friendly signature text
|
|
49
|
+
*/
|
|
50
|
+
export function signatureTextByLine(
|
|
51
|
+
sign: SignatureTextLoader,
|
|
52
|
+
targetLine: number,
|
|
53
|
+
options?: any // TODO: fix any
|
|
54
|
+
) {
|
|
55
|
+
return signatureText.call(sign, targetLine, options)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get the source code of a function, class, interface, enum, or type alias at a specific line.
|
|
60
|
+
*
|
|
61
|
+
* @param sign - instance of SignatureText
|
|
62
|
+
* @param targetLine - the line number of the signature in source code
|
|
63
|
+
*
|
|
64
|
+
* @returns source code of the signature
|
|
65
|
+
*/
|
|
66
|
+
export function signatureSourceCodeByLine(
|
|
67
|
+
sign: SignatureTextLoader,
|
|
68
|
+
targetLine: number
|
|
69
|
+
) {
|
|
70
|
+
return signatureSourceCode.call(sign, targetLine)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function signatureText(
|
|
74
|
+
this: SignatureTextLoader,
|
|
75
|
+
targetLine: number,
|
|
76
|
+
options?: any // TODO: fix any
|
|
77
|
+
) {
|
|
78
|
+
const sourceFile = this.sourceFile;
|
|
79
|
+
const signatureNode = findSignatureNode.call(
|
|
80
|
+
this,
|
|
81
|
+
sourceFile,
|
|
82
|
+
[targetLine]
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
if (!signatureNode) {
|
|
86
|
+
console.error("(signatureText): `signatureNode` is empty, something went wrong");
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const printableSignatureNode = nodeToPrintableSignatureNode(signatureNode, options?.typeOnly);
|
|
91
|
+
if (!printableSignatureNode) {
|
|
92
|
+
console.error("(signatureText): cannot convert `signatureNode` to `printableSignatureNode`");
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return printer.printNode(ts.EmitHint.Unspecified, printableSignatureNode, sourceFile).trim()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// TODO: this function is probably not optimized well (recursion when not needed)
|
|
100
|
+
function findSignatureNode(
|
|
101
|
+
this: SignatureTextLoader,
|
|
102
|
+
node: ts.Node,
|
|
103
|
+
targetLines: number[]
|
|
104
|
+
) {
|
|
105
|
+
let isSourceFile = false
|
|
106
|
+
if (node === node.getSourceFile()) {
|
|
107
|
+
isSourceFile = true
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!isSourceFile && isNodeAtLine(node, targetLines, this.sourceFile)) {
|
|
111
|
+
return node
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let signatureNode: ts.Node | undefined;
|
|
115
|
+
|
|
116
|
+
ts.forEachChild(node, (n) => {
|
|
117
|
+
if (signatureNode) {
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
signatureNode = findSignatureNode.call(this, n, targetLines)
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return signatureNode
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
function signatureSourceCode(
|
|
128
|
+
this: SignatureTextLoader,
|
|
129
|
+
targetLine: number
|
|
130
|
+
) {
|
|
131
|
+
const sourceFile = this.sourceFile;
|
|
132
|
+
const signatureNode = findSignatureNode.call(
|
|
133
|
+
this,
|
|
134
|
+
sourceFile,
|
|
135
|
+
[targetLine]
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
if (!signatureNode) {
|
|
139
|
+
console.error("(signatureSourceCode): `signatureNode` is empty, something went wrong");
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Get the start and end positions of the node
|
|
144
|
+
const start = signatureNode.getStart(sourceFile);
|
|
145
|
+
const end = signatureNode.getEnd();
|
|
146
|
+
|
|
147
|
+
// Get the source text between start and end
|
|
148
|
+
return sourceFile.text.substring(start, end).trim();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function nodeToPrintableSignatureNode(node: ts.Node, typeOnly: boolean = false): ts.Node | undefined {
|
|
152
|
+
if (typeOnly) {
|
|
153
|
+
if ("type" in node) {
|
|
154
|
+
return node.type as ts.Node
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let resp: ts.Node | undefined;
|
|
161
|
+
|
|
162
|
+
if (ts.isFunctionDeclaration(node)) {
|
|
163
|
+
resp = ts.factory.updateFunctionDeclaration(
|
|
164
|
+
node,
|
|
165
|
+
node.modifiers,
|
|
166
|
+
node.asteriskToken,
|
|
167
|
+
node.name,
|
|
168
|
+
node.typeParameters,
|
|
169
|
+
node.parameters,
|
|
170
|
+
node.type,
|
|
171
|
+
undefined
|
|
172
|
+
);
|
|
173
|
+
} else if (ts.isClassDeclaration(node)) {
|
|
174
|
+
resp = ts.factory.updateClassDeclaration(
|
|
175
|
+
node,
|
|
176
|
+
node.modifiers,
|
|
177
|
+
node.name,
|
|
178
|
+
node.typeParameters,
|
|
179
|
+
node.heritageClauses,
|
|
180
|
+
[]
|
|
181
|
+
);
|
|
182
|
+
} else if (ts.isInterfaceDeclaration(node)) {
|
|
183
|
+
resp = ts.factory.updateInterfaceDeclaration(
|
|
184
|
+
node,
|
|
185
|
+
node.modifiers,
|
|
186
|
+
node.name,
|
|
187
|
+
node.typeParameters,
|
|
188
|
+
node.heritageClauses,
|
|
189
|
+
[]
|
|
190
|
+
);
|
|
191
|
+
} else if (ts.isEnumDeclaration(node)) {
|
|
192
|
+
resp = ts.factory.updateEnumDeclaration(
|
|
193
|
+
node,
|
|
194
|
+
node.modifiers,
|
|
195
|
+
node.name,
|
|
196
|
+
[]
|
|
197
|
+
);
|
|
198
|
+
} else if (ts.isTypeAliasDeclaration(node)) {
|
|
199
|
+
resp = ts.factory.updateTypeAliasDeclaration(
|
|
200
|
+
node,
|
|
201
|
+
node.modifiers,
|
|
202
|
+
node.name,
|
|
203
|
+
node.typeParameters,
|
|
204
|
+
node.type
|
|
205
|
+
);
|
|
206
|
+
} else if (ts.isPropertySignature(node)) {
|
|
207
|
+
resp = ts.factory.updatePropertySignature(
|
|
208
|
+
node,
|
|
209
|
+
node.modifiers,
|
|
210
|
+
node.name,
|
|
211
|
+
node.questionToken,
|
|
212
|
+
node.type
|
|
213
|
+
);
|
|
214
|
+
} else {
|
|
215
|
+
resp = node
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!resp) {
|
|
219
|
+
console.error("(nodeToPrintableSignatureNode): resp is empty, something went wrong");
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return resp
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function isNodeAtLine(node: ts.Node, lines: number[], sf: ts.SourceFile): boolean {
|
|
227
|
+
const {line: startLine} = sf.getLineAndCharacterOfPosition(node.getStart());
|
|
228
|
+
|
|
229
|
+
return lines.includes(startLine + 1); // lines are 0-based internally
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|