@wrongstack/tools 0.281.1 → 0.282.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.
@@ -1130,10 +1130,69 @@ function detectLang(file) {
1130
1130
  async function parseSymbols2(opts) {
1131
1131
  const { file, content, lang } = opts;
1132
1132
  try {
1133
- return await syncGoParse(file, content, lang);
1133
+ const parsed = await syncGoParse(file, content, lang);
1134
+ if (parsed.symbols.length > 0) {
1135
+ return parsed;
1136
+ }
1137
+ return fallbackParse(file, content, lang);
1134
1138
  } catch {
1135
- return { file, lang, symbols: [], mtimeMs: Date.now() };
1139
+ return fallbackParse(file, content, lang);
1140
+ }
1141
+ }
1142
+ function fallbackParse(filePath, content, lang) {
1143
+ if (!/^\s*package\s+[A-Za-z_]\w*/m.test(content) || hasUnbalancedDelimiters(content)) {
1144
+ return { file: filePath, lang, symbols: [], mtimeMs: Date.now() };
1145
+ }
1146
+ const symbols = [];
1147
+ const packageName = content.match(/^\s*package\s+([A-Za-z_]\w*)/m)?.[1] ?? "";
1148
+ const lines = content.split(/\r?\n/);
1149
+ for (const [idx, line] of lines.entries()) {
1150
+ const trimmed = line.trimStart();
1151
+ const col = line.length - trimmed.length + 1;
1152
+ const fn = /^func\s+(?:\([^)]*\)\s*)?([A-Za-z_]\w*)\s*\(/.exec(trimmed);
1153
+ if (fn?.[1]) {
1154
+ addFallbackSymbol(symbols, { filePath, lang, kind: trimmed.startsWith("func (") ? "method" : "function", name: fn[1], line: idx + 1, col, signature: trimmed, scope: packageName ? `${packageName}.${fn[1]}` : fn[1] });
1155
+ continue;
1156
+ }
1157
+ const typeDecl = /^type\s+([A-Za-z_]\w*)\b/.exec(trimmed);
1158
+ if (typeDecl?.[1]) {
1159
+ addFallbackSymbol(symbols, { filePath, lang, kind: "type", name: typeDecl[1], line: idx + 1, col, signature: trimmed, scope: packageName });
1160
+ continue;
1161
+ }
1162
+ const valueDecl = /^(const|var)\s+([A-Za-z_]\w*)\b/.exec(trimmed);
1163
+ if (valueDecl?.[1] && valueDecl[2]) {
1164
+ addFallbackSymbol(symbols, { filePath, lang, kind: valueDecl[1], name: valueDecl[2], line: idx + 1, col, signature: trimmed, scope: packageName });
1165
+ }
1166
+ }
1167
+ return { file: filePath, lang, symbols, mtimeMs: Date.now() };
1168
+ }
1169
+ function addFallbackSymbol(symbols, opts) {
1170
+ symbols.push({
1171
+ id: 0,
1172
+ lang: opts.lang,
1173
+ kind: opts.kind,
1174
+ name: opts.name,
1175
+ file: opts.filePath,
1176
+ line: opts.line,
1177
+ col: opts.col,
1178
+ signature: opts.signature,
1179
+ docComment: "",
1180
+ scope: opts.scope,
1181
+ text: `${opts.name} ${opts.signature}`.trim()
1182
+ });
1183
+ }
1184
+ function hasUnbalancedDelimiters(content) {
1185
+ const pairs = { "(": ")", "[": "]", "{": "}" };
1186
+ const closers = new Set(Object.values(pairs));
1187
+ const stack = [];
1188
+ for (const ch of content) {
1189
+ if (pairs[ch]) {
1190
+ stack.push(pairs[ch]);
1191
+ } else if (closers.has(ch) && stack.pop() !== ch) {
1192
+ return true;
1193
+ }
1136
1194
  }
1195
+ return stack.length > 0;
1137
1196
  }
1138
1197
  var GO_PARSE_SCRIPT = `
1139
1198
  package main