tokless 0.4.1 → 0.5.1

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.
@@ -0,0 +1,451 @@
1
+ 'use strict';
2
+
3
+ const { preprocessBraceLines } = require('../../core/lexer');
4
+ const {
5
+ DEFAULT_CONTROL_KEYWORDS,
6
+ findDeclarationRange,
7
+ buildCompactSignature,
8
+ } = require('../../core/block-anchor');
9
+
10
+ const ID = 'javascript';
11
+ const EXTENSIONS = ['.js', '.jsx', '.mjs', '.cjs', '.ts', '.tsx'];
12
+ const TS_EXTS = new Set(['.ts', '.tsx']);
13
+
14
+ function hasFunctionRhs(analyzed, startIdx, headerEndIdx) {
15
+ let text = '';
16
+ for (let i = startIdx; i <= headerEndIdx; i++) {
17
+ text += ' ' + analyzed[i].clean;
18
+ }
19
+ return /=>/.test(text) || /=\s*(?:async\s+)?function\b/.test(text);
20
+ }
21
+
22
+ function findIifeRange(analyzed, startIdx) {
23
+ const startInfo = analyzed[startIdx];
24
+ const trimmed = startInfo.clean.trim();
25
+ // 陷阱: 仅匹配带显式前缀的顶层 IIFE/UMD(`(function`、`!function`、`(() =>`),避免把普通顶层函数误判为包装器
26
+ const isIifeHeader =
27
+ /^(?:;\s*)?\(\s*(?:async\s+)?function\b/.test(trimmed) ||
28
+ /^(?:;\s*)?(?:[!+~-]\s*|void\s+)(?:async\s+)?function\b/.test(trimmed) ||
29
+ /^(?:;\s*)?\(\s*(?:async\s+)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>\s*\{?/.test(trimmed);
30
+ if (!isIifeHeader) return null;
31
+
32
+ let headerEndIdx = -1;
33
+ for (let j = startIdx; j < analyzed.length && j <= startIdx + 2; j++) {
34
+ if (analyzed[j].braceEnd > startInfo.braceStart) {
35
+ headerEndIdx = j;
36
+ break;
37
+ }
38
+ }
39
+ if (headerEndIdx === -1) return null;
40
+
41
+ for (let j = headerEndIdx; j < analyzed.length; j++) {
42
+ if (analyzed[j].braceEnd <= startInfo.braceStart) {
43
+ if (j <= headerEndIdx) return null;
44
+ // 陷阱: UMD 头部适配器在闭合 `}` 后紧跟 `, function () {` 工厂块换行开启时,需继续追踪工厂体至末尾
45
+ const tail = (analyzed[j].clean + ' ' + (j + 1 < analyzed.length ? analyzed[j + 1].clean : '')).trim();
46
+ if (/,\s*\(?\s*(?:async\s+)?(?:function\b|(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>)/.test(tail)) {
47
+ if (j + 1 < analyzed.length && analyzed[j + 1].braceEnd > startInfo.braceStart) {
48
+ j++;
49
+ continue;
50
+ }
51
+ }
52
+ return {
53
+ headerEndIdx,
54
+ endIdx: j,
55
+ bodyDepth: startInfo.braceStart + 1,
56
+ };
57
+ }
58
+ }
59
+ return null;
60
+ }
61
+
62
+ function parseTestCallBlock(analyzed, startIdx) {
63
+ const startClean = analyzed[startIdx].clean;
64
+ const trimmed = startClean.trim();
65
+ const m = trimmed.match(
66
+ /^(?:await\s+)?(describe|test|it)((?:\.(?:only|skip|concurrent|serial|sequential))*)\s*\(/
67
+ );
68
+ if (!m) return null;
69
+
70
+ const baseFn = m[1];
71
+ const callee = baseFn + (m[2] || '');
72
+ const openParenCol = startClean.indexOf('(', startClean.indexOf(baseFn));
73
+ if (openParenCol === -1) return null;
74
+
75
+ let relParen = 0;
76
+ let relBracket = 0;
77
+ let relBrace = 0;
78
+ let firstCommaLine = -1;
79
+ let firstCommaCol = -1;
80
+ let paramCleanSinceSep = '';
81
+ let bodyOpenLine = -1;
82
+ let bodyDepth = -1;
83
+ let callEndLine = -1;
84
+
85
+ for (let j = startIdx; j < analyzed.length; j++) {
86
+ const cleanLine = analyzed[j].clean;
87
+ const kStart = j === startIdx ? openParenCol : 0;
88
+ let curBraceDepth = analyzed[j].braceStart;
89
+ if (j === startIdx) {
90
+ for (let p = 0; p < kStart; p++) {
91
+ if (cleanLine[p] === '{') curBraceDepth++;
92
+ else if (cleanLine[p] === '}') curBraceDepth = Math.max(0, curBraceDepth - 1);
93
+ }
94
+ }
95
+
96
+ for (let k = kStart; k < cleanLine.length; k++) {
97
+ const ch = cleanLine[k];
98
+ if (ch === '{') curBraceDepth++;
99
+ else if (ch === '}') curBraceDepth = Math.max(0, curBraceDepth - 1);
100
+
101
+ if (j === startIdx && k === openParenCol) {
102
+ relParen = 1;
103
+ continue;
104
+ }
105
+
106
+ if (ch === '(') {
107
+ relParen++;
108
+ if (relParen === 2 && relBracket === 0 && relBrace === 0) {
109
+ paramCleanSinceSep += '(';
110
+ }
111
+ } else if (ch === ')') {
112
+ relParen--;
113
+ if (relParen === 0) {
114
+ callEndLine = j;
115
+ break;
116
+ }
117
+ if (relParen === 1 && relBracket === 0 && relBrace === 0) {
118
+ paramCleanSinceSep += ')';
119
+ }
120
+ } else if (ch === '[') {
121
+ relBracket++;
122
+ } else if (ch === ']') {
123
+ relBracket = Math.max(0, relBracket - 1);
124
+ } else if (ch === '{') {
125
+ if (relParen === 1 && relBracket === 0 && relBrace === 0 && bodyOpenLine === -1) {
126
+ const prev = paramCleanSinceSep.trim();
127
+ if (/=>\s*$/.test(prev) || /^(?:async\s+)?function\b/.test(prev)) {
128
+ bodyOpenLine = j;
129
+ bodyDepth = curBraceDepth;
130
+ }
131
+ }
132
+ relBrace++;
133
+ } else if (ch === '}') {
134
+ relBrace = Math.max(0, relBrace - 1);
135
+ } else if (ch === ',' && relParen === 1 && relBracket === 0 && relBrace === 0) {
136
+ if (firstCommaLine === -1) {
137
+ firstCommaLine = j;
138
+ firstCommaCol = k;
139
+ }
140
+ paramCleanSinceSep = '';
141
+ } else if (relParen === 1 && relBracket === 0 && relBrace === 0) {
142
+ paramCleanSinceSep += ch;
143
+ }
144
+ }
145
+
146
+ if (callEndLine !== -1) break;
147
+ }
148
+
149
+ if (callEndLine === -1 || bodyOpenLine === -1 || firstCommaLine === -1) {
150
+ return null;
151
+ }
152
+
153
+ const rawArgChunks = [];
154
+ for (let j = startIdx; j <= firstCommaLine; j++) {
155
+ const rawLine = analyzed[j].raw;
156
+ const from = j === startIdx ? openParenCol + 1 : 0;
157
+ const to = j === firstCommaLine ? firstCommaCol : rawLine.length;
158
+ rawArgChunks.push(rawLine.slice(from, to));
159
+ }
160
+ const firstArgText = rawArgChunks.join(' ').replace(/\s+/g, ' ').trim();
161
+ if (!firstArgText) return null;
162
+
163
+ let label = firstArgText;
164
+ if (
165
+ (label.startsWith("'") && label.endsWith("'") && label.length >= 2) ||
166
+ (label.startsWith('"') && label.endsWith('"') && label.length >= 2) ||
167
+ (label.startsWith('`') && label.endsWith('`') && label.length >= 2)
168
+ ) {
169
+ label = label.slice(1, -1);
170
+ }
171
+
172
+ let displayArg = firstArgText;
173
+ if (displayArg.length > 110) {
174
+ const q = displayArg[0];
175
+ if (q === "'" || q === '"' || q === '`') {
176
+ displayArg = displayArg.slice(0, 106) + '...' + q;
177
+ } else {
178
+ displayArg = displayArg.slice(0, 107) + '...';
179
+ }
180
+ }
181
+
182
+ return {
183
+ kind: baseFn === 'describe' ? 'suite' : 'test',
184
+ name: label,
185
+ signature: `${callee}(${displayArg})`,
186
+ headerEndIdx: bodyOpenLine,
187
+ endIdx: callEndLine,
188
+ bodyDepth,
189
+ };
190
+ }
191
+
192
+ function parse(lines, ext = '.js', options = {}) {
193
+ if (ext && typeof ext === 'object') {
194
+ options = ext;
195
+ ext = '.js';
196
+ }
197
+ const normalizedExt = String(ext || '.js').toLowerCase();
198
+ const isTs = TS_EXTS.has(normalizedExt);
199
+ const lineOffset = options.lineOffset || 0;
200
+
201
+ const analyzed = preprocessBraceLines(lines, 'js');
202
+ const items = [];
203
+ const classStack = [];
204
+ const suiteStack = [];
205
+ let activeIifeEnd = -1;
206
+ let activeIifeBodyDepth = -1;
207
+
208
+ for (let i = 0; i < analyzed.length; i++) {
209
+ const info = analyzed[i];
210
+ const trimmed = info.clean.trim();
211
+ if (!trimmed) continue;
212
+
213
+ while (classStack.length > 0 && i > classStack[classStack.length - 1].endIdx) {
214
+ classStack.pop();
215
+ }
216
+ while (suiteStack.length > 0 && i > suiteStack[suiteStack.length - 1].endIdx) {
217
+ suiteStack.pop();
218
+ }
219
+ if (activeIifeEnd !== -1 && i > activeIifeEnd) {
220
+ activeIifeEnd = -1;
221
+ activeIifeBodyDepth = -1;
222
+ }
223
+
224
+ if (
225
+ info.braceStart === 0 &&
226
+ activeIifeEnd === -1 &&
227
+ classStack.length === 0 &&
228
+ suiteStack.length === 0
229
+ ) {
230
+ const iife = findIifeRange(analyzed, i);
231
+ if (iife) {
232
+ activeIifeEnd = iife.endIdx;
233
+ activeIifeBodyDepth = iife.bodyDepth;
234
+ i = iife.headerEndIdx;
235
+ continue;
236
+ }
237
+ }
238
+
239
+ const topScopeDepth =
240
+ activeIifeEnd !== -1 && i <= activeIifeEnd ? activeIifeBodyDepth : 0;
241
+ const currentSuite = suiteStack.length > 0 ? suiteStack[suiteStack.length - 1] : null;
242
+ const activeScopeDepth = currentSuite !== null ? currentSuite.bodyDepth : topScopeDepth;
243
+ const currentClass = classStack.length > 0 ? classStack[classStack.length - 1] : null;
244
+ const inClassBody =
245
+ currentClass !== null &&
246
+ i <= currentClass.endIdx &&
247
+ info.braceStart === currentClass.bodyDepth;
248
+
249
+ if (info.braceStart === activeScopeDepth && !inClassBody) {
250
+ const testBlock = parseTestCallBlock(analyzed, i);
251
+ if (testBlock) {
252
+ items.push({
253
+ kind: testBlock.kind,
254
+ name: testBlock.name,
255
+ signature: testBlock.signature,
256
+ startLine: i + 1 + lineOffset,
257
+ endLine: testBlock.endIdx + 1 + lineOffset,
258
+ depth: suiteStack.length,
259
+ });
260
+ if (testBlock.kind === 'suite' && testBlock.headerEndIdx < testBlock.endIdx) {
261
+ suiteStack.push({
262
+ endIdx: testBlock.endIdx,
263
+ bodyDepth: testBlock.bodyDepth,
264
+ });
265
+ i = testBlock.headerEndIdx;
266
+ } else {
267
+ i = testBlock.endIdx;
268
+ }
269
+ continue;
270
+ }
271
+
272
+ let kind = null;
273
+ let name = '';
274
+ let m;
275
+
276
+ if (
277
+ (m = trimmed.match(
278
+ /^(?:export\s+(?:default\s+)?)?(?:declare\s+)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)/
279
+ )) ||
280
+ (m = trimmed.match(/^(?:module\.)?exports\s*=\s*(?:abstract\s+)?class\s*([A-Za-z_$][\w$]*)?/))
281
+ ) {
282
+ kind = 'class';
283
+ name = m[1] || 'default';
284
+ } else if (
285
+ isTs &&
286
+ (m = trimmed.match(
287
+ /^(?:export\s+(?:default\s+)?)?(?:declare\s+)?interface\s+([A-Za-z_$][\w$]*)/
288
+ ))
289
+ ) {
290
+ kind = 'interface';
291
+ name = m[1];
292
+ } else if (
293
+ isTs &&
294
+ (m = trimmed.match(
295
+ /^(?:export\s+(?:default\s+)?)?(?:declare\s+)?type\s+([A-Za-z_$][\w$]*)\b/
296
+ ))
297
+ ) {
298
+ kind = 'type';
299
+ name = m[1];
300
+ } else if (
301
+ isTs &&
302
+ (m = trimmed.match(
303
+ /^(?:export\s+(?:default\s+)?)?(?:declare\s+)?(?:const\s+)?enum\s+([A-Za-z_$][\w$]*)/
304
+ ))
305
+ ) {
306
+ kind = 'enum';
307
+ name = m[1];
308
+ } else if (
309
+ isTs &&
310
+ (m = trimmed.match(
311
+ /^(?:export\s+(?:default\s+)?)?(?:declare\s+)?(?:namespace|module)\s+([A-Za-z_$][\w$]*)/
312
+ ))
313
+ ) {
314
+ kind = 'namespace';
315
+ name = m[1];
316
+ } else if (
317
+ (m = trimmed.match(
318
+ /^(?:export\s+(?:default\s+)?)?(?:declare\s+)?(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)?\s*(?:<[\s\S]*>)?\s*\(/
319
+ )) ||
320
+ (m = trimmed.match(
321
+ /^(?:export\s+(?:default\s+)?)?(?:declare\s+)?(?:async\s+)?function\s*\*?\s+([A-Za-z_$][\w$]*)\b/
322
+ ))
323
+ ) {
324
+ kind = 'function';
325
+ name = m[1] || 'default';
326
+ } else if (
327
+ (m = trimmed.match(/^(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\b/))
328
+ ) {
329
+ const rangeProbe = findDeclarationRange(analyzed, i, 'js');
330
+ if (hasFunctionRhs(analyzed, i, rangeProbe.headerEndIdx)) {
331
+ kind = 'function';
332
+ name = m[1];
333
+ } else if (/=\s*(?:abstract\s+)?class\b/.test(trimmed)) {
334
+ kind = 'class';
335
+ name = m[1];
336
+ }
337
+ } else if ((m = trimmed.match(/^(?:module\.)?exports\.([A-Za-z_$][\w$]*)\s*=/))) {
338
+ const rangeProbe = findDeclarationRange(analyzed, i, 'js');
339
+ if (hasFunctionRhs(analyzed, i, rangeProbe.headerEndIdx)) {
340
+ kind = 'function';
341
+ name = m[1];
342
+ }
343
+ } else if (/^module\.exports\s*=\s*\{/.test(trimmed)) {
344
+ kind = 'export';
345
+ name = 'module.exports';
346
+ } else if (/^export\s+default\s+(?:async\s+)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>/.test(trimmed)) {
347
+ kind = 'function';
348
+ name = 'default';
349
+ } else if (/^export\s+default\s+(?:async\s+)?function\b/.test(trimmed)) {
350
+ kind = 'function';
351
+ name = 'default';
352
+ }
353
+
354
+ if (kind) {
355
+ const range = findDeclarationRange(analyzed, i, 'js');
356
+ let signature = buildCompactSignature(analyzed, i, range.headerEndIdx, range.hasBlock);
357
+ if (kind === 'export' && name === 'module.exports') {
358
+ const exportNames = [];
359
+ for (let k = i; k <= range.endIdx; k++) {
360
+ const lineClean = analyzed[k].clean
361
+ .replace(/^module\.exports\s*=\s*\{?/, '')
362
+ .replace(/\}?;?\s*$/, '');
363
+ for (const token of lineClean.split(',')) {
364
+ const keyMatch = token.trim().match(/^([A-Za-z_$][\w$]*)/);
365
+ if (keyMatch) exportNames.push(keyMatch[1]);
366
+ }
367
+ }
368
+ if (exportNames.length > 0) {
369
+ const joined = exportNames.join(', ');
370
+ signature =
371
+ joined.length > 100
372
+ ? `module.exports = { ${joined.slice(0, 97)}... }`
373
+ : `module.exports = { ${joined} }`;
374
+ }
375
+ }
376
+
377
+ items.push({
378
+ kind,
379
+ name,
380
+ signature,
381
+ startLine: i + 1 + lineOffset,
382
+ endLine: range.endIdx + 1 + lineOffset,
383
+ depth: suiteStack.length,
384
+ });
385
+
386
+ if (kind === 'class' && range.hasBlock) {
387
+ classStack.push({
388
+ endIdx: range.endIdx,
389
+ bodyDepth: info.braceStart + 1,
390
+ });
391
+ i = range.headerEndIdx;
392
+ } else {
393
+ i = range.endIdx;
394
+ }
395
+ continue;
396
+ }
397
+ }
398
+
399
+ if (inClassBody) {
400
+ let methodMatch = trimmed.match(
401
+ /^(?:@[A-Za-z_$][\w$.]*(?:\([^)]*\))?\s+)*(?:(?:public|private|protected|static|async|readonly|override|abstract|accessor|get|set)\s+)*\*?\s*(#?[A-Za-z_$][\w$]*|\[[^\]]+\])\s*\??\s*(?:<[^>]*>)?\s*\(/
402
+ );
403
+ let isArrowField = false;
404
+ if (!methodMatch) {
405
+ const fieldMatch = trimmed.match(
406
+ /^(?:@[A-Za-z_$][\w$.]*(?:\([^)]*\))?\s+)*(?:(?:public|private|protected|static|readonly|override)\s+)*(#?[A-Za-z_$][\w$]*)\s*(?:!\s*)?(?::[^=]+)?=(?!=)/
407
+ );
408
+ if (fieldMatch) {
409
+ const rangeProbe = findDeclarationRange(analyzed, i, 'js');
410
+ if (hasFunctionRhs(analyzed, i, rangeProbe.headerEndIdx)) {
411
+ methodMatch = fieldMatch;
412
+ isArrowField = true;
413
+ }
414
+ }
415
+ }
416
+
417
+ if (methodMatch) {
418
+ const methodName = methodMatch[1];
419
+ if (!DEFAULT_CONTROL_KEYWORDS.has(methodName)) {
420
+ const range = findDeclarationRange(analyzed, i, 'js');
421
+ if (range.hasBlock || isArrowField || (isTs && trimmed.endsWith(';'))) {
422
+ const signature = buildCompactSignature(
423
+ analyzed,
424
+ i,
425
+ range.headerEndIdx,
426
+ range.hasBlock
427
+ );
428
+ items.push({
429
+ kind: 'method',
430
+ name: methodName,
431
+ signature,
432
+ startLine: i + 1 + lineOffset,
433
+ endLine: range.endIdx + 1 + lineOffset,
434
+ depth: suiteStack.length + classStack.length,
435
+ });
436
+ i = range.endIdx;
437
+ continue;
438
+ }
439
+ }
440
+ }
441
+ }
442
+ }
443
+
444
+ return items;
445
+ }
446
+
447
+ module.exports = {
448
+ id: ID,
449
+ extensions: EXTENSIONS,
450
+ parse,
451
+ };
@@ -0,0 +1,167 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ preprocessBraceLines,
5
+ findMatchingForward,
6
+ } = require('../../core/lexer');
7
+ const {
8
+ stripLeadingAttributesAndAnnotations,
9
+ matchContainerHeader,
10
+ scanBraceLanguage,
11
+ } = require('../../core/block-anchor');
12
+
13
+ const ID = 'kotlin';
14
+ const EXTENSIONS = ['.kt', '.kts'];
15
+ const KOTLIN_CONTAINERS = new Set(['class', 'interface', 'object', 'enum']);
16
+
17
+ function extractKotlinFunName(coreHeader) {
18
+ const funMatch = coreHeader.match(
19
+ /^(?:(?:public|private|protected|internal|open|abstract|override|final|suspend|inline|infix|operator|tailrec|external|actual|expect)\s+)*fun\b\s*([\s\S]*)$/
20
+ );
21
+ if (!funMatch) return null;
22
+ let rest = funMatch[1].trimStart();
23
+ if (rest.startsWith('<')) {
24
+ let angle = 0;
25
+ for (let i = 0; i < rest.length; i++) {
26
+ if (rest[i] === '<') angle++;
27
+ else if (rest[i] === '>') {
28
+ angle--;
29
+ if (angle === 0) {
30
+ rest = rest.slice(i + 1).trimStart();
31
+ break;
32
+ }
33
+ }
34
+ }
35
+ }
36
+ const parenIdx = rest.indexOf('(');
37
+ const head = (parenIdx !== -1 ? rest.slice(0, parenIdx) : rest.split(/[=:]/)[0]).trim();
38
+ if (!head) return null;
39
+ const extMatch = head.match(/([A-Za-z_][\w.]*(?:<[^>]*>)?\??\.[A-Za-z_`][\w`]*|[A-Za-z_`][\w`]*)$/);
40
+ if (!extMatch) return null;
41
+ return extMatch[1].replace(/\s+/g, '');
42
+ }
43
+
44
+ function parse(lines, ext = '.kt', options = {}) {
45
+ if (ext && typeof ext === 'object') {
46
+ options = ext;
47
+ ext = '.kt';
48
+ }
49
+ const lineOffset = options.lineOffset || 0;
50
+ const analyzed = preprocessBraceLines(lines, 'kotlin');
51
+
52
+ return scanBraceLanguage(analyzed, {
53
+ lexer: 'kotlin',
54
+ containers: KOTLIN_CONTAINERS,
55
+ javaStyleEnum: true,
56
+ lineOffset,
57
+ mapCallableKind(callable, scopeStack) {
58
+ if (scopeStack.length === 0 && callable.kind === 'function') {
59
+ return 'fun';
60
+ }
61
+ return callable.kind;
62
+ },
63
+ onStatementLine({
64
+ analyzed: rows,
65
+ lineIdx,
66
+ scopeStack,
67
+ items,
68
+ lineOffset: offset,
69
+ findDeclarationRange,
70
+ buildCompactSignature,
71
+ }) {
72
+ const trimmed = rows[lineIdx].clean.trim();
73
+ if (!trimmed) return null;
74
+ const core = stripLeadingAttributesAndAnnotations(trimmed);
75
+ if (!core) return null;
76
+
77
+ const container = matchContainerHeader(core, KOTLIN_CONTAINERS);
78
+ if (container) {
79
+ const range = findDeclarationRange(rows, lineIdx, 'kotlin');
80
+ if (!range.hasBlock) {
81
+ const signature = buildCompactSignature(rows, lineIdx, range.headerEndIdx, false);
82
+ items.push({
83
+ kind: container.kind,
84
+ name: container.name,
85
+ signature,
86
+ startLine: lineIdx + 1 + offset,
87
+ endLine: range.endIdx + 1 + offset,
88
+ depth: scopeStack.length,
89
+ });
90
+ return { jumpToLine: range.endIdx };
91
+ }
92
+ return null;
93
+ }
94
+
95
+ if (/\bfun\b/.test(core)) {
96
+ const range = findDeclarationRange(rows, lineIdx, 'kotlin');
97
+ const headerText = stripLeadingAttributesAndAnnotations(
98
+ rows
99
+ .slice(lineIdx, range.headerEndIdx + 1)
100
+ .map((r) => r.clean)
101
+ .join(' ')
102
+ );
103
+ const funName = extractKotlinFunName(headerText);
104
+ if (!funName) return null;
105
+
106
+ // 陷阱: Kotlin 单表达式函数 `fun f() = run { ... }` 右值带尾随 Lambda 花括号,以及扩展函数 `Type.foo()` 统一在此精确定名
107
+ let isExprBodyOrAbstract = !range.hasBlock;
108
+ if (range.hasBlock) {
109
+ const firstOpenParen = headerText.indexOf('(');
110
+ if (firstOpenParen !== -1) {
111
+ let p = 0;
112
+ let closeIdx = -1;
113
+ for (let k = firstOpenParen; k < headerText.length; k++) {
114
+ if (headerText[k] === '(') p++;
115
+ else if (headerText[k] === ')') {
116
+ p--;
117
+ if (p === 0) {
118
+ closeIdx = k;
119
+ break;
120
+ }
121
+ }
122
+ }
123
+ if (closeIdx !== -1) {
124
+ const afterParams = headerText.slice(closeIdx + 1).split('{')[0];
125
+ if (afterParams.includes('=')) {
126
+ isExprBodyOrAbstract = true;
127
+ }
128
+ }
129
+ }
130
+ }
131
+
132
+ if (isExprBodyOrAbstract || funName.includes('.')) {
133
+ let endIdx = range.endIdx;
134
+ if (range.hasBlock && isExprBodyOrAbstract) {
135
+ const braceLineClean = rows[range.headerEndIdx].clean;
136
+ const braceCol = braceLineClean.indexOf('{');
137
+ if (braceCol !== -1) {
138
+ const closeBrace = findMatchingForward(rows, range.headerEndIdx, braceCol, '{', '}');
139
+ if (closeBrace) endIdx = closeBrace.line;
140
+ }
141
+ }
142
+ let sig = buildCompactSignature(rows, lineIdx, range.headerEndIdx, range.hasBlock);
143
+ if (isExprBodyOrAbstract && sig.includes('=')) {
144
+ sig = sig.replace(/\s*=\s*[^=]*$/, '').trim() || sig;
145
+ }
146
+ items.push({
147
+ kind: scopeStack.length > 0 ? 'method' : 'fun',
148
+ name: funName,
149
+ signature: sig,
150
+ startLine: lineIdx + 1 + offset,
151
+ endLine: endIdx + 1 + offset,
152
+ depth: scopeStack.length,
153
+ });
154
+ return { jumpToLine: endIdx };
155
+ }
156
+ }
157
+
158
+ return null;
159
+ },
160
+ });
161
+ }
162
+
163
+ module.exports = {
164
+ id: ID,
165
+ extensions: EXTENSIONS,
166
+ parse,
167
+ };
@@ -0,0 +1,79 @@
1
+ 'use strict';
2
+
3
+ const ID = 'markdown';
4
+ const EXTENSIONS = ['.md', '.mdx'];
5
+
6
+ function parse(lines, ext = '.md', options = {}) {
7
+ if (ext && typeof ext === 'object') {
8
+ options = ext;
9
+ ext = '.md';
10
+ }
11
+ const lineOffset = options.lineOffset || 0;
12
+ const headings = [];
13
+ let inFence = false;
14
+ let fenceChar = '';
15
+ let fenceLen = 0;
16
+
17
+ for (let i = 0; i < lines.length; i++) {
18
+ const line = lines[i];
19
+ const trimmed = line.trim();
20
+
21
+ const fenceMatch = trimmed.match(/^(`{3,}|~{3,})/);
22
+ if (fenceMatch) {
23
+ const marker = fenceMatch[1][0];
24
+ const len = fenceMatch[1].length;
25
+ if (!inFence) {
26
+ inFence = true;
27
+ fenceChar = marker;
28
+ fenceLen = len;
29
+ } else if (marker === fenceChar && len >= fenceLen && /^(`+|~+)\s*$/.test(trimmed)) {
30
+ inFence = false;
31
+ fenceChar = '';
32
+ fenceLen = 0;
33
+ }
34
+ continue;
35
+ }
36
+ if (inFence) continue;
37
+
38
+ const hMatch = line.match(/^(#{1,6})\s+(.+?)\s*#*$/);
39
+ if (hMatch) {
40
+ headings.push({
41
+ level: hMatch[1].length,
42
+ signature: `${hMatch[1]} ${hMatch[2].trim()}`,
43
+ name: hMatch[2].trim(),
44
+ startIdx: i,
45
+ });
46
+ }
47
+ }
48
+
49
+ if (headings.length === 0) return [];
50
+ const minLevel = Math.min(...headings.map((h) => h.level));
51
+
52
+ return headings.map((h, idx) => {
53
+ let endIdx = lines.length - 1;
54
+ for (let next = idx + 1; next < headings.length; next++) {
55
+ if (headings[next].level <= h.level) {
56
+ endIdx = headings[next].startIdx - 1;
57
+ break;
58
+ }
59
+ }
60
+ while (endIdx > h.startIdx && !lines[endIdx].trim()) {
61
+ endIdx--;
62
+ }
63
+ return {
64
+ kind: 'heading',
65
+ name: h.name,
66
+ signature: h.signature,
67
+ startLine: h.startIdx + 1 + lineOffset,
68
+ endLine: endIdx + 1 + lineOffset,
69
+ depth: h.level - minLevel,
70
+ level: h.level,
71
+ };
72
+ });
73
+ }
74
+
75
+ module.exports = {
76
+ id: ID,
77
+ extensions: EXTENSIONS,
78
+ parse,
79
+ };