Repository navigation
Expand file tree
/
Copy pathsource-parser.ts
More file actions
172 lines (143 loc) · 4.46 KB
/
Copy pathsource-parser.ts
File metadata and controls
172 lines (143 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { unique } from './core-utils.ts';
export function extractBlockAfterMarker(source: string, marker: string): string {
const start = source.indexOf(marker);
if (start === -1) return '';
const blockStart = source.indexOf('{', start);
if (blockStart === -1) return '';
return source.slice(blockStart, findMatchingBrace(source, blockStart) + 1);
}
export function extractNamedObjectBlock(source: string, propertyName: string): string {
const pattern = new RegExp(`${propertyName}\\s*:\\s*\\{`);
const match = pattern.exec(source);
if (!match) return '';
const blockStart = source.indexOf('{', match.index);
if (blockStart === -1) return '';
return source.slice(blockStart, findMatchingBrace(source, blockStart) + 1);
}
export function extractTypeLiteral(source: string, typeName: string): string {
const pattern = new RegExp(`export\\s+type\\s+${typeName}\\s*=\\s*\\{`);
const match = pattern.exec(source);
if (!match) return '';
const blockStart = source.indexOf('{', match.index);
if (blockStart === -1) return '';
return source.slice(blockStart, findMatchingBrace(source, blockStart) + 1);
}
export function extractLeadingSourceSnippet(source: string): string {
return source.split('\n').slice(0, 40).join('\n').trim();
}
export function parseExposedReturnKeys(source: string, apiName: string): string[] {
const pattern = new RegExp(`export\\s+function\\s+${apiName}\\s*\\([^)]*\\)\\s*\\{`);
const match = pattern.exec(source);
if (!match) return [];
const bodyStart = source.indexOf('{', match.index);
if (bodyStart === -1) return [];
const body = source.slice(bodyStart + 1, findMatchingBrace(source, bodyStart));
const returnMatch = body.match(/return\s+\{([\s\S]*?)\n\}/m);
if (!returnMatch) return [];
return parseTopLevelKeys(`{${returnMatch[1]}}`).filter(key => !key.startsWith('...'));
}
export function parseTopLevelKeys(block: string): string[] {
const keys: string[] = [];
let depth = 0;
let token = '';
let inString = false;
let stringChar = '';
let escape = false;
let inLineComment = false;
let inBlockComment = false;
for (let index = 0; index < block.length; index += 1) {
const char = block[index];
const next = block[index + 1];
if (inLineComment) {
if (char === '\n') inLineComment = false;
continue;
}
if (inBlockComment) {
if (char === '*' && next === '/') {
inBlockComment = false;
index += 1;
}
continue;
}
if (inString) {
token += char;
if (escape) {
escape = false;
} else if (char === '\\') {
escape = true;
} else if (char === stringChar) {
inString = false;
}
continue;
}
if (char === '/' && next === '/') {
inLineComment = true;
index += 1;
continue;
}
if (char === '/' && next === '*') {
inBlockComment = true;
index += 1;
continue;
}
if (char === '\'' || char === '"' || char === '`') {
inString = true;
stringChar = char;
token += char;
continue;
}
if (char === '{' || char === '[' || char === '(') {
depth += 1;
token += char;
continue;
}
if (char === '}' || char === ']' || char === ')') {
depth -= 1;
token += char;
continue;
}
if (depth === 1 && (char === ':' || char === ',' || char === '\n')) {
const normalized = token.trim();
const key = normalized.replace(/,$/, '').trim();
if (char === ':' && key) {
keys.push(key.replace(/^['"]|['"]$/g, ''));
} else if ((char === ',' || char === '\n') && key.startsWith('...')) {
keys.push(key);
}
token = '';
continue;
}
token += char;
}
return unique(keys.filter(Boolean));
}
export function findMatchingBrace(source: string, startIndex: number): number {
let depth = 0;
let inString = false;
let stringChar = '';
let escape = false;
for (let index = startIndex; index < source.length; index += 1) {
const char = source[index];
if (inString) {
if (escape) {
escape = false;
} else if (char === '\\') {
escape = true;
} else if (char === stringChar) {
inString = false;
}
continue;
}
if (char === '\'' || char === '"' || char === '`') {
inString = true;
stringChar = char;
continue;
}
if (char === '{') depth += 1;
if (char === '}') {
depth -= 1;
if (depth === 0) return index;
}
}
return source.length - 1;
}