@aiready/core 0.24.18 → 0.24.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-3NEZ5M7Y.mjs +937 -0
- package/dist/chunk-5IVDH26E.mjs +142 -88
- package/dist/chunk-BE52N7T2.mjs +892 -0
- package/dist/chunk-GQMKSUA4.mjs +381 -336
- package/dist/chunk-LRM26BOB.mjs +381 -336
- package/dist/chunk-MOM3IXCA.mjs +362 -0
- package/dist/chunk-OT6FOHL4.mjs +144 -88
- package/dist/chunk-OVWWYI75.mjs +307 -0
- package/dist/chunk-U3IY2CFC.mjs +18 -22
- package/dist/chunk-WPFXQH5F.mjs +307 -0
- package/dist/chunk-WUDUSXUE.mjs +366 -0
- package/dist/chunk-YQATXOKD.mjs +36 -0
- package/dist/chunk-YVPVNRFQ.mjs +937 -0
- package/dist/client/index.js +5 -1
- package/dist/client/index.mjs +2 -2
- package/dist/csharp-parser-JE5MWHQS.mjs +9 -0
- package/dist/csharp-parser-XW7WHE77.mjs +5 -9
- package/dist/go-parser-KTG4CGF5.mjs +5 -9
- package/dist/go-parser-T7PR6WJI.mjs +9 -0
- package/dist/index-CcP12wb-.d.mts +996 -693
- package/dist/index-CcP12wb-.d.ts +996 -693
- package/dist/index-DGbarGnr.d.mts +996 -693
- package/dist/index-DGbarGnr.d.ts +996 -693
- package/dist/index-slasaNzr.d.mts +1612 -0
- package/dist/index-slasaNzr.d.ts +1612 -0
- package/dist/index.d.mts +32 -1
- package/dist/index.d.ts +32 -1
- package/dist/index.js +61 -7
- package/dist/index.mjs +58 -12
- package/dist/java-parser-EOKMGQ6B.mjs +5 -9
- package/dist/java-parser-WGOXKULP.mjs +9 -0
- package/dist/python-parser-7NCR7VCQ.mjs +8 -0
- package/dist/python-parser-BCI7JVLF.mjs +4 -8
- package/dist/typescript-parser-3KYGSSY5.mjs +3 -0
- package/dist/typescript-parser-4BA4VYAF.mjs +3 -7
- package/dist/typescript-parser-FOUPHVFI.mjs +3 -0
- package/dist/typescript-parser-G3TSNR7O.mjs +7 -0
- package/dist/typescript-parser-RMNCTHRD.mjs +3 -7
- package/dist/typescript-parser-WALISXF4.mjs +7 -0
- package/package.json +14 -11
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ParseError
|
|
3
|
+
} from "./chunk-YQATXOKD.mjs";
|
|
4
|
+
|
|
5
|
+
// src/parsers/typescript-parser.ts
|
|
6
|
+
import { parse } from "@typescript-eslint/typescript-estree";
|
|
7
|
+
var TypeScriptParser = class {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.language = "typescript" /* TypeScript */;
|
|
10
|
+
this.extensions = [".ts", ".tsx", ".js", ".jsx"];
|
|
11
|
+
}
|
|
12
|
+
async initialize() {
|
|
13
|
+
}
|
|
14
|
+
canHandle(filePath) {
|
|
15
|
+
return this.extensions.some((ext) => filePath.endsWith(ext));
|
|
16
|
+
}
|
|
17
|
+
async getAST(code, filePath) {
|
|
18
|
+
try {
|
|
19
|
+
return parse(code, {
|
|
20
|
+
filePath,
|
|
21
|
+
loc: true,
|
|
22
|
+
range: true,
|
|
23
|
+
tokens: true,
|
|
24
|
+
comment: true,
|
|
25
|
+
jsx: filePath.endsWith("x")
|
|
26
|
+
});
|
|
27
|
+
} catch (error) {
|
|
28
|
+
const err = error;
|
|
29
|
+
throw new ParseError(err.message || "Unknown error", filePath, {
|
|
30
|
+
line: err.lineNumber || 1,
|
|
31
|
+
column: err.column || 0
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
parse(code, filePath) {
|
|
36
|
+
try {
|
|
37
|
+
const ast = parse(code, {
|
|
38
|
+
filePath,
|
|
39
|
+
loc: true,
|
|
40
|
+
range: true,
|
|
41
|
+
tokens: true,
|
|
42
|
+
comment: true,
|
|
43
|
+
jsx: filePath.endsWith("x")
|
|
44
|
+
});
|
|
45
|
+
const imports = this.extractImports(ast);
|
|
46
|
+
const exports = this.extractExports(ast, code, filePath);
|
|
47
|
+
return {
|
|
48
|
+
exports,
|
|
49
|
+
imports,
|
|
50
|
+
language: this.language
|
|
51
|
+
};
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new ParseError(error.message, filePath, {
|
|
54
|
+
line: error.lineNumber || 1,
|
|
55
|
+
column: error.column || 0
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
getNamingConventions() {
|
|
60
|
+
return {
|
|
61
|
+
variablePattern: /^[a-z][a-zA-Z0-9]*$/,
|
|
62
|
+
functionPattern: /^[a-z][a-zA-Z0-9]*$/,
|
|
63
|
+
classPattern: /^[A-Z][a-zA-Z0-9]*$/,
|
|
64
|
+
constantPattern: /^[A-Z][A-Z0-9_]*$/,
|
|
65
|
+
typePattern: /^[A-Z][a-zA-Z0-9]*$/,
|
|
66
|
+
interfacePattern: /^I?[A-Z][a-zA-Z0-9]*$/
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
analyzeMetadata(node, code) {
|
|
70
|
+
if (!code) return {};
|
|
71
|
+
return {
|
|
72
|
+
isPure: this.isLikelyPure(node),
|
|
73
|
+
hasSideEffects: !this.isLikelyPure(node)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
extractImports(ast) {
|
|
77
|
+
const imports = [];
|
|
78
|
+
for (const node of ast.body) {
|
|
79
|
+
if (node.type === "ImportDeclaration") {
|
|
80
|
+
const specifiers = [];
|
|
81
|
+
let isTypeOnly = false;
|
|
82
|
+
if (node.importKind === "type") {
|
|
83
|
+
isTypeOnly = true;
|
|
84
|
+
}
|
|
85
|
+
for (const spec of node.specifiers) {
|
|
86
|
+
if (spec.type === "ImportSpecifier") {
|
|
87
|
+
const imported = spec.imported;
|
|
88
|
+
const name = imported.type === "Identifier" ? imported.name : imported.value;
|
|
89
|
+
specifiers.push(name);
|
|
90
|
+
} else if (spec.type === "ImportDefaultSpecifier") {
|
|
91
|
+
specifiers.push("default");
|
|
92
|
+
} else if (spec.type === "ImportNamespaceSpecifier") {
|
|
93
|
+
specifiers.push("*");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
imports.push({
|
|
97
|
+
source: node.source.value,
|
|
98
|
+
specifiers,
|
|
99
|
+
isTypeOnly,
|
|
100
|
+
loc: node.loc ? {
|
|
101
|
+
start: {
|
|
102
|
+
line: node.loc.start.line,
|
|
103
|
+
column: node.loc.start.column
|
|
104
|
+
},
|
|
105
|
+
end: { line: node.loc.end.line, column: node.loc.end.column }
|
|
106
|
+
} : void 0
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return imports;
|
|
111
|
+
}
|
|
112
|
+
extractExports(ast, code, filePath) {
|
|
113
|
+
const exports = [];
|
|
114
|
+
for (const node of ast.body) {
|
|
115
|
+
if (node.type === "ExportNamedDeclaration") {
|
|
116
|
+
if (node.declaration) {
|
|
117
|
+
const declaration = node.declaration;
|
|
118
|
+
if ((declaration.type === "FunctionDeclaration" || declaration.type === "TSDeclareFunction") && declaration.id) {
|
|
119
|
+
exports.push(
|
|
120
|
+
this.createExport(
|
|
121
|
+
declaration.id.name,
|
|
122
|
+
"function",
|
|
123
|
+
node,
|
|
124
|
+
// Pass the outer ExportNamedDeclaration
|
|
125
|
+
code,
|
|
126
|
+
filePath
|
|
127
|
+
)
|
|
128
|
+
);
|
|
129
|
+
} else if (declaration.type === "ClassDeclaration" && declaration.id) {
|
|
130
|
+
exports.push(
|
|
131
|
+
this.createExport(
|
|
132
|
+
declaration.id.name,
|
|
133
|
+
"class",
|
|
134
|
+
node,
|
|
135
|
+
// Pass the outer ExportNamedDeclaration
|
|
136
|
+
code,
|
|
137
|
+
filePath
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
} else if (declaration.type === "TSTypeAliasDeclaration") {
|
|
141
|
+
exports.push(
|
|
142
|
+
this.createExport(
|
|
143
|
+
declaration.id.name,
|
|
144
|
+
"type",
|
|
145
|
+
node,
|
|
146
|
+
// Pass the outer ExportNamedDeclaration
|
|
147
|
+
code,
|
|
148
|
+
filePath
|
|
149
|
+
)
|
|
150
|
+
);
|
|
151
|
+
} else if (declaration.type === "TSInterfaceDeclaration") {
|
|
152
|
+
exports.push(
|
|
153
|
+
this.createExport(
|
|
154
|
+
declaration.id.name,
|
|
155
|
+
"interface",
|
|
156
|
+
node,
|
|
157
|
+
// Pass the outer ExportNamedDeclaration
|
|
158
|
+
code,
|
|
159
|
+
filePath
|
|
160
|
+
)
|
|
161
|
+
);
|
|
162
|
+
} else if (declaration.type === "VariableDeclaration") {
|
|
163
|
+
for (const decl of declaration.declarations) {
|
|
164
|
+
if (decl.id.type === "Identifier") {
|
|
165
|
+
exports.push(
|
|
166
|
+
this.createExport(
|
|
167
|
+
decl.id.name,
|
|
168
|
+
"const",
|
|
169
|
+
node,
|
|
170
|
+
code,
|
|
171
|
+
filePath,
|
|
172
|
+
decl.init
|
|
173
|
+
)
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
} else if (node.type === "ExportDefaultDeclaration") {
|
|
180
|
+
exports.push(
|
|
181
|
+
this.createExport("default", "default", node, code, filePath)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return exports;
|
|
186
|
+
}
|
|
187
|
+
createExport(name, type, node, code, filePath, initializer) {
|
|
188
|
+
const documentation = this.extractDocumentation(node, code);
|
|
189
|
+
let methodCount;
|
|
190
|
+
let propertyCount;
|
|
191
|
+
let parameters;
|
|
192
|
+
let isPrimitive = false;
|
|
193
|
+
let isTyped = false;
|
|
194
|
+
if (initializer) {
|
|
195
|
+
if (initializer.type === "Literal" || initializer.type === "TemplateLiteral" && initializer.expressions.length === 0) {
|
|
196
|
+
isPrimitive = true;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
let structNode = node.type === "ExportNamedDeclaration" && node.declaration ? node.declaration : node.type === "ExportDefaultDeclaration" ? node.declaration : node;
|
|
200
|
+
if (!structNode) structNode = node;
|
|
201
|
+
if (filePath.endsWith(".ts") || filePath.endsWith(".tsx")) {
|
|
202
|
+
if (structNode.type === "TSTypeAliasDeclaration" || structNode.type === "TSInterfaceDeclaration" || structNode.type === "TSEnumDeclaration") {
|
|
203
|
+
isTyped = true;
|
|
204
|
+
} else if (structNode.type === "FunctionDeclaration" || structNode.type === "TSDeclareFunction") {
|
|
205
|
+
const func = structNode;
|
|
206
|
+
const hasReturnType = !!func.returnType;
|
|
207
|
+
const allParamsTyped = func.params.length === 0 || func.params.every((p) => !!p.typeAnnotation);
|
|
208
|
+
isTyped = hasReturnType && allParamsTyped;
|
|
209
|
+
} else if (structNode.type === "VariableDeclaration") {
|
|
210
|
+
const variable = structNode;
|
|
211
|
+
isTyped = variable.declarations.every(
|
|
212
|
+
(d) => !!d.id.typeAnnotation || !!d.init
|
|
213
|
+
);
|
|
214
|
+
} else if (structNode.type === "ClassDeclaration") {
|
|
215
|
+
isTyped = true;
|
|
216
|
+
}
|
|
217
|
+
} else if (filePath.endsWith(".js") || filePath.endsWith(".jsx")) {
|
|
218
|
+
isTyped = false;
|
|
219
|
+
}
|
|
220
|
+
if (structNode.type === "ClassDeclaration" || structNode.type === "TSInterfaceDeclaration") {
|
|
221
|
+
const body = structNode.type === "ClassDeclaration" ? structNode.body.body : structNode.body.body;
|
|
222
|
+
methodCount = body.filter(
|
|
223
|
+
(m) => m.type === "MethodDefinition" || m.type === "TSMethodSignature"
|
|
224
|
+
).length;
|
|
225
|
+
propertyCount = body.filter(
|
|
226
|
+
(m) => m.type === "PropertyDefinition" || m.type === "TSPropertySignature"
|
|
227
|
+
).length;
|
|
228
|
+
if (structNode.type === "ClassDeclaration") {
|
|
229
|
+
const constructor = body.find(
|
|
230
|
+
(m) => m.type === "MethodDefinition" && m.kind === "constructor"
|
|
231
|
+
);
|
|
232
|
+
if (constructor && constructor.value && constructor.value.params) {
|
|
233
|
+
parameters = constructor.value.params.map((p) => {
|
|
234
|
+
if (p.type === "Identifier") return p.name;
|
|
235
|
+
if (p.type === "TSParameterProperty" && p.parameter.type === "Identifier") {
|
|
236
|
+
return p.parameter.name;
|
|
237
|
+
}
|
|
238
|
+
return void 0;
|
|
239
|
+
}).filter((p) => !!p);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (!parameters && (structNode.type === "FunctionDeclaration" || structNode.type === "TSDeclareFunction" || structNode.type === "MethodDefinition")) {
|
|
244
|
+
const funcNode = structNode.type === "MethodDefinition" ? structNode.value : structNode;
|
|
245
|
+
if (funcNode && funcNode.params) {
|
|
246
|
+
parameters = funcNode.params.map((p) => {
|
|
247
|
+
if (p.type === "Identifier") return p.name;
|
|
248
|
+
return void 0;
|
|
249
|
+
}).filter((p) => !!p);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
name,
|
|
254
|
+
type,
|
|
255
|
+
isPrimitive,
|
|
256
|
+
loc: node.loc ? {
|
|
257
|
+
start: { line: node.loc.start.line, column: node.loc.start.column },
|
|
258
|
+
end: { line: node.loc.end.line, column: node.loc.end.column }
|
|
259
|
+
} : void 0,
|
|
260
|
+
documentation,
|
|
261
|
+
methodCount,
|
|
262
|
+
propertyCount,
|
|
263
|
+
parameters,
|
|
264
|
+
isPure: this.isLikelyPure(node),
|
|
265
|
+
hasSideEffects: !this.isLikelyPure(node),
|
|
266
|
+
isTyped
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
extractDocumentation(node, code) {
|
|
270
|
+
if (node.range) {
|
|
271
|
+
const start = node.range[0];
|
|
272
|
+
const precedingCode = code.substring(0, start);
|
|
273
|
+
const jsdocMatch = precedingCode.match(/\/\*\*([\s\S]*?)\*\/\s*$/);
|
|
274
|
+
if (jsdocMatch) {
|
|
275
|
+
return {
|
|
276
|
+
content: jsdocMatch[1].trim(),
|
|
277
|
+
type: "jsdoc"
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return void 0;
|
|
282
|
+
}
|
|
283
|
+
isLikelyPure(node) {
|
|
284
|
+
const sn = node.type === "ExportNamedDeclaration" && node.declaration ? node.declaration : node.type === "ExportDefaultDeclaration" ? node.declaration : node;
|
|
285
|
+
if (!sn) return false;
|
|
286
|
+
if (sn.type === "VariableDeclaration" && sn.kind === "const") return true;
|
|
287
|
+
if (sn.type === "FunctionDeclaration" || sn.type === "TSDeclareFunction" || sn.type === "MethodDefinition") {
|
|
288
|
+
const body = sn.type === "MethodDefinition" ? sn.value.body : sn.body;
|
|
289
|
+
if (body && body.type === "BlockStatement") {
|
|
290
|
+
const bodyContent = JSON.stringify(
|
|
291
|
+
body,
|
|
292
|
+
(_, v) => typeof v === "bigint" ? v.toString() : v
|
|
293
|
+
);
|
|
294
|
+
if (bodyContent.includes('"name":"console"') || bodyContent.includes('"name":"process"') || bodyContent.includes('"name":"fs"') || bodyContent.includes('"name":"fetch"') || bodyContent.includes('"name":"axios"')) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
export {
|
|
306
|
+
TypeScriptParser
|
|
307
|
+
};
|
package/dist/chunk-U3IY2CFC.mjs
CHANGED
|
@@ -1,36 +1,32 @@
|
|
|
1
1
|
// src/types/language.ts
|
|
2
2
|
var Language = /* @__PURE__ */ ((Language2) => {
|
|
3
|
-
Language2[
|
|
4
|
-
Language2[
|
|
5
|
-
Language2[
|
|
6
|
-
Language2[
|
|
7
|
-
Language2[
|
|
8
|
-
Language2[
|
|
9
|
-
Language2[
|
|
3
|
+
Language2['TypeScript'] = 'typescript';
|
|
4
|
+
Language2['JavaScript'] = 'javascript';
|
|
5
|
+
Language2['Python'] = 'python';
|
|
6
|
+
Language2['Java'] = 'java';
|
|
7
|
+
Language2['Go'] = 'go';
|
|
8
|
+
Language2['Rust'] = 'rust';
|
|
9
|
+
Language2['CSharp'] = 'csharp';
|
|
10
10
|
return Language2;
|
|
11
11
|
})(Language || {});
|
|
12
12
|
var LANGUAGE_EXTENSIONS = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
'.ts': 'typescript' /* TypeScript */,
|
|
14
|
+
'.tsx': 'typescript' /* TypeScript */,
|
|
15
|
+
'.js': 'javascript' /* JavaScript */,
|
|
16
|
+
'.jsx': 'javascript' /* JavaScript */,
|
|
17
|
+
'.py': 'python' /* Python */,
|
|
18
|
+
'.java': 'java' /* Java */,
|
|
19
|
+
'.go': 'go' /* Go */,
|
|
20
|
+
'.rs': 'rust' /* Rust */,
|
|
21
|
+
'.cs': 'csharp' /* CSharp */,
|
|
22
22
|
};
|
|
23
23
|
var ParseError = class extends Error {
|
|
24
24
|
constructor(message, filePath, loc) {
|
|
25
25
|
super(message);
|
|
26
26
|
this.filePath = filePath;
|
|
27
27
|
this.loc = loc;
|
|
28
|
-
this.name =
|
|
28
|
+
this.name = 'ParseError';
|
|
29
29
|
}
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
export {
|
|
33
|
-
Language,
|
|
34
|
-
LANGUAGE_EXTENSIONS,
|
|
35
|
-
ParseError
|
|
36
|
-
};
|
|
32
|
+
export { Language, LANGUAGE_EXTENSIONS, ParseError };
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ParseError
|
|
3
|
+
} from "./chunk-YQATXOKD.mjs";
|
|
4
|
+
|
|
5
|
+
// src/parsers/typescript-parser.ts
|
|
6
|
+
import { parse } from "@typescript-eslint/typescript-estree";
|
|
7
|
+
var TypeScriptParser = class {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.language = "typescript" /* TypeScript */;
|
|
10
|
+
this.extensions = [".ts", ".tsx", ".js", ".jsx"];
|
|
11
|
+
}
|
|
12
|
+
async initialize() {
|
|
13
|
+
}
|
|
14
|
+
canHandle(filePath) {
|
|
15
|
+
return this.extensions.some((ext) => filePath.endsWith(ext));
|
|
16
|
+
}
|
|
17
|
+
async getAST(code, filePath) {
|
|
18
|
+
try {
|
|
19
|
+
return parse(code, {
|
|
20
|
+
filePath,
|
|
21
|
+
loc: true,
|
|
22
|
+
range: true,
|
|
23
|
+
tokens: true,
|
|
24
|
+
comment: true,
|
|
25
|
+
jsx: filePath.endsWith("x")
|
|
26
|
+
});
|
|
27
|
+
} catch (error) {
|
|
28
|
+
const err = error;
|
|
29
|
+
throw new ParseError(err.message || "Unknown error", filePath, {
|
|
30
|
+
line: err.lineNumber || 1,
|
|
31
|
+
column: err.column || 0
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
parse(code, filePath) {
|
|
36
|
+
try {
|
|
37
|
+
const ast = parse(code, {
|
|
38
|
+
filePath,
|
|
39
|
+
loc: true,
|
|
40
|
+
range: true,
|
|
41
|
+
tokens: true,
|
|
42
|
+
comment: true,
|
|
43
|
+
jsx: filePath.endsWith("x")
|
|
44
|
+
});
|
|
45
|
+
const imports = this.extractImports(ast);
|
|
46
|
+
const exports = this.extractExports(ast, code, filePath);
|
|
47
|
+
return {
|
|
48
|
+
exports,
|
|
49
|
+
imports,
|
|
50
|
+
language: this.language
|
|
51
|
+
};
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new ParseError(error.message, filePath, {
|
|
54
|
+
line: error.lineNumber || 1,
|
|
55
|
+
column: error.column || 0
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
getNamingConventions() {
|
|
60
|
+
return {
|
|
61
|
+
variablePattern: /^[a-z][a-zA-Z0-9]*$/,
|
|
62
|
+
functionPattern: /^[a-z][a-zA-Z0-9]*$/,
|
|
63
|
+
classPattern: /^[A-Z][a-zA-Z0-9]*$/,
|
|
64
|
+
constantPattern: /^[A-Z][A-Z0-9_]*$/,
|
|
65
|
+
typePattern: /^[A-Z][a-zA-Z0-9]*$/,
|
|
66
|
+
interfacePattern: /^I?[A-Z][a-zA-Z0-9]*$/
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
analyzeMetadata(node, code) {
|
|
70
|
+
if (!code) return {};
|
|
71
|
+
return {
|
|
72
|
+
isPure: this.isLikelyPure(node),
|
|
73
|
+
hasSideEffects: !this.isLikelyPure(node)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
extractImports(ast) {
|
|
77
|
+
const imports = [];
|
|
78
|
+
for (const node of ast.body) {
|
|
79
|
+
if (node.type === "ImportDeclaration") {
|
|
80
|
+
const specifiers = [];
|
|
81
|
+
let isTypeOnly = false;
|
|
82
|
+
if (node.importKind === "type") {
|
|
83
|
+
isTypeOnly = true;
|
|
84
|
+
}
|
|
85
|
+
for (const spec of node.specifiers) {
|
|
86
|
+
if (spec.type === "ImportSpecifier") {
|
|
87
|
+
const imported = spec.imported;
|
|
88
|
+
const name = imported.type === "Identifier" ? imported.name : imported.value;
|
|
89
|
+
specifiers.push(name);
|
|
90
|
+
} else if (spec.type === "ImportDefaultSpecifier") {
|
|
91
|
+
specifiers.push("default");
|
|
92
|
+
} else if (spec.type === "ImportNamespaceSpecifier") {
|
|
93
|
+
specifiers.push("*");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
imports.push({
|
|
97
|
+
source: node.source.value,
|
|
98
|
+
specifiers,
|
|
99
|
+
isTypeOnly,
|
|
100
|
+
loc: node.loc ? {
|
|
101
|
+
start: {
|
|
102
|
+
line: node.loc.start.line,
|
|
103
|
+
column: node.loc.start.column
|
|
104
|
+
},
|
|
105
|
+
end: { line: node.loc.end.line, column: node.loc.end.column }
|
|
106
|
+
} : void 0
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return imports;
|
|
111
|
+
}
|
|
112
|
+
extractExports(ast, code, filePath) {
|
|
113
|
+
const exports = [];
|
|
114
|
+
for (const node of ast.body) {
|
|
115
|
+
if (node.type === "ExportNamedDeclaration") {
|
|
116
|
+
if (node.declaration) {
|
|
117
|
+
const declaration = node.declaration;
|
|
118
|
+
if ((declaration.type === "FunctionDeclaration" || declaration.type === "TSDeclareFunction") && declaration.id) {
|
|
119
|
+
exports.push(
|
|
120
|
+
this.createExport(
|
|
121
|
+
declaration.id.name,
|
|
122
|
+
"function",
|
|
123
|
+
node,
|
|
124
|
+
// Pass the outer ExportNamedDeclaration
|
|
125
|
+
code,
|
|
126
|
+
filePath
|
|
127
|
+
)
|
|
128
|
+
);
|
|
129
|
+
} else if (declaration.type === "ClassDeclaration" && declaration.id) {
|
|
130
|
+
exports.push(
|
|
131
|
+
this.createExport(
|
|
132
|
+
declaration.id.name,
|
|
133
|
+
"class",
|
|
134
|
+
node,
|
|
135
|
+
// Pass the outer ExportNamedDeclaration
|
|
136
|
+
code,
|
|
137
|
+
filePath
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
} else if (declaration.type === "TSTypeAliasDeclaration") {
|
|
141
|
+
exports.push(
|
|
142
|
+
this.createExport(
|
|
143
|
+
declaration.id.name,
|
|
144
|
+
"type",
|
|
145
|
+
node,
|
|
146
|
+
// Pass the outer ExportNamedDeclaration
|
|
147
|
+
code,
|
|
148
|
+
filePath
|
|
149
|
+
)
|
|
150
|
+
);
|
|
151
|
+
} else if (declaration.type === "TSInterfaceDeclaration") {
|
|
152
|
+
exports.push(
|
|
153
|
+
this.createExport(
|
|
154
|
+
declaration.id.name,
|
|
155
|
+
"interface",
|
|
156
|
+
node,
|
|
157
|
+
// Pass the outer ExportNamedDeclaration
|
|
158
|
+
code,
|
|
159
|
+
filePath
|
|
160
|
+
)
|
|
161
|
+
);
|
|
162
|
+
} else if (declaration.type === "VariableDeclaration") {
|
|
163
|
+
for (const decl of declaration.declarations) {
|
|
164
|
+
if (decl.id.type === "Identifier") {
|
|
165
|
+
exports.push(
|
|
166
|
+
this.createExport(
|
|
167
|
+
decl.id.name,
|
|
168
|
+
"const",
|
|
169
|
+
node,
|
|
170
|
+
code,
|
|
171
|
+
filePath,
|
|
172
|
+
decl.init
|
|
173
|
+
)
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
} else if (node.type === "ExportDefaultDeclaration") {
|
|
180
|
+
exports.push(
|
|
181
|
+
this.createExport("default", "default", node, code, filePath)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return exports;
|
|
186
|
+
}
|
|
187
|
+
createExport(name, type, node, code, filePath, initializer) {
|
|
188
|
+
const documentation = this.extractDocumentation(node, code);
|
|
189
|
+
let methodCount;
|
|
190
|
+
let propertyCount;
|
|
191
|
+
let parameters;
|
|
192
|
+
let isPrimitive = false;
|
|
193
|
+
let isTyped = false;
|
|
194
|
+
if (initializer) {
|
|
195
|
+
if (initializer.type === "Literal" || initializer.type === "TemplateLiteral" && initializer.expressions.length === 0) {
|
|
196
|
+
isPrimitive = true;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
let structNode = node.type === "ExportNamedDeclaration" && node.declaration ? node.declaration : node.type === "ExportDefaultDeclaration" ? node.declaration : node;
|
|
200
|
+
if (!structNode) structNode = node;
|
|
201
|
+
if (filePath.endsWith(".ts") || filePath.endsWith(".tsx")) {
|
|
202
|
+
if (structNode.type === "TSTypeAliasDeclaration" || structNode.type === "TSInterfaceDeclaration" || structNode.type === "TSEnumDeclaration") {
|
|
203
|
+
isTyped = true;
|
|
204
|
+
} else if (structNode.type === "FunctionDeclaration" || structNode.type === "TSDeclareFunction") {
|
|
205
|
+
const func = structNode;
|
|
206
|
+
const hasReturnType = !!func.returnType;
|
|
207
|
+
const allParamsTyped = func.params.length === 0 || func.params.every((p) => !!p.typeAnnotation);
|
|
208
|
+
isTyped = hasReturnType && allParamsTyped;
|
|
209
|
+
} else if (structNode.type === "VariableDeclaration") {
|
|
210
|
+
const variable = structNode;
|
|
211
|
+
isTyped = variable.declarations.every(
|
|
212
|
+
(d) => !!d.id.typeAnnotation || !!d.init
|
|
213
|
+
);
|
|
214
|
+
} else if (structNode.type === "ClassDeclaration") {
|
|
215
|
+
isTyped = true;
|
|
216
|
+
}
|
|
217
|
+
} else if (filePath.endsWith(".js") || filePath.endsWith(".jsx")) {
|
|
218
|
+
isTyped = false;
|
|
219
|
+
}
|
|
220
|
+
if (structNode.type === "ClassDeclaration" || structNode.type === "TSInterfaceDeclaration") {
|
|
221
|
+
const body = structNode.type === "ClassDeclaration" ? structNode.body.body : structNode.body.body;
|
|
222
|
+
methodCount = body.filter(
|
|
223
|
+
(m) => m.type === "MethodDefinition" || m.type === "TSMethodSignature"
|
|
224
|
+
).length;
|
|
225
|
+
propertyCount = body.filter(
|
|
226
|
+
(m) => m.type === "PropertyDefinition" || m.type === "TSPropertySignature"
|
|
227
|
+
).length;
|
|
228
|
+
if (structNode.type === "ClassDeclaration") {
|
|
229
|
+
const constructor = body.find(
|
|
230
|
+
(m) => m.type === "MethodDefinition" && m.kind === "constructor"
|
|
231
|
+
);
|
|
232
|
+
if (constructor && constructor.value && constructor.value.params) {
|
|
233
|
+
parameters = constructor.value.params.map((p) => {
|
|
234
|
+
if (p.type === "Identifier") return p.name;
|
|
235
|
+
if (p.type === "TSParameterProperty" && p.parameter.type === "Identifier") {
|
|
236
|
+
return p.parameter.name;
|
|
237
|
+
}
|
|
238
|
+
return void 0;
|
|
239
|
+
}).filter((p) => !!p);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (!parameters && (structNode.type === "FunctionDeclaration" || structNode.type === "TSDeclareFunction" || structNode.type === "MethodDefinition")) {
|
|
244
|
+
const funcNode = structNode.type === "MethodDefinition" ? structNode.value : structNode;
|
|
245
|
+
if (funcNode && funcNode.params) {
|
|
246
|
+
parameters = funcNode.params.map((p) => {
|
|
247
|
+
if (p.type === "Identifier") return p.name;
|
|
248
|
+
return void 0;
|
|
249
|
+
}).filter((p) => !!p);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
name,
|
|
254
|
+
type,
|
|
255
|
+
isPrimitive,
|
|
256
|
+
loc: node.loc ? {
|
|
257
|
+
start: { line: node.loc.start.line, column: node.loc.start.column },
|
|
258
|
+
end: { line: node.loc.end.line, column: node.loc.end.column }
|
|
259
|
+
} : void 0,
|
|
260
|
+
documentation,
|
|
261
|
+
methodCount,
|
|
262
|
+
propertyCount,
|
|
263
|
+
parameters,
|
|
264
|
+
isPure: this.isLikelyPure(node),
|
|
265
|
+
hasSideEffects: !this.isLikelyPure(node),
|
|
266
|
+
isTyped
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
extractDocumentation(node, code) {
|
|
270
|
+
if (node.range) {
|
|
271
|
+
const start = node.range[0];
|
|
272
|
+
const precedingCode = code.substring(0, start);
|
|
273
|
+
const jsdocMatch = precedingCode.match(/\/\*\*([\s\S]*?)\*\/\s*$/);
|
|
274
|
+
if (jsdocMatch) {
|
|
275
|
+
return {
|
|
276
|
+
content: jsdocMatch[1].trim(),
|
|
277
|
+
type: "jsdoc"
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return void 0;
|
|
282
|
+
}
|
|
283
|
+
isLikelyPure(node) {
|
|
284
|
+
const sn = node.type === "ExportNamedDeclaration" && node.declaration ? node.declaration : node.type === "ExportDefaultDeclaration" ? node.declaration : node;
|
|
285
|
+
if (!sn) return false;
|
|
286
|
+
if (sn.type === "VariableDeclaration" && sn.kind === "const") return true;
|
|
287
|
+
if (sn.type === "FunctionDeclaration" || sn.type === "TSDeclareFunction" || sn.type === "MethodDefinition") {
|
|
288
|
+
const body = sn.type === "MethodDefinition" ? sn.value.body : sn.body;
|
|
289
|
+
if (body && body.type === "BlockStatement") {
|
|
290
|
+
const bodyContent = JSON.stringify(
|
|
291
|
+
body,
|
|
292
|
+
(_, v) => typeof v === "bigint" ? v.toString() : v
|
|
293
|
+
);
|
|
294
|
+
if (bodyContent.includes('"name":"console"') || bodyContent.includes('"name":"process"') || bodyContent.includes('"name":"fs"') || bodyContent.includes('"name":"global"') || bodyContent.includes('"name":"window"') || bodyContent.includes('"name":"fetch"') || bodyContent.includes('"name":"axios"')) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
export {
|
|
306
|
+
TypeScriptParser
|
|
307
|
+
};
|