@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.
@@ -1087,10 +1087,69 @@ function detectLang(file) {
1087
1087
  async function parseSymbols2(opts) {
1088
1088
  const { file, content, lang } = opts;
1089
1089
  try {
1090
- return await syncGoParse(file, content, lang);
1090
+ const parsed = await syncGoParse(file, content, lang);
1091
+ if (parsed.symbols.length > 0) {
1092
+ return parsed;
1093
+ }
1094
+ return fallbackParse(file, content, lang);
1091
1095
  } catch {
1092
- return { file, lang, symbols: [], mtimeMs: Date.now() };
1096
+ return fallbackParse(file, content, lang);
1097
+ }
1098
+ }
1099
+ function fallbackParse(filePath, content, lang) {
1100
+ if (!/^\s*package\s+[A-Za-z_]\w*/m.test(content) || hasUnbalancedDelimiters(content)) {
1101
+ return { file: filePath, lang, symbols: [], mtimeMs: Date.now() };
1102
+ }
1103
+ const symbols = [];
1104
+ const packageName = content.match(/^\s*package\s+([A-Za-z_]\w*)/m)?.[1] ?? "";
1105
+ const lines = content.split(/\r?\n/);
1106
+ for (const [idx, line] of lines.entries()) {
1107
+ const trimmed = line.trimStart();
1108
+ const col = line.length - trimmed.length + 1;
1109
+ const fn = /^func\s+(?:\([^)]*\)\s*)?([A-Za-z_]\w*)\s*\(/.exec(trimmed);
1110
+ if (fn?.[1]) {
1111
+ 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] });
1112
+ continue;
1113
+ }
1114
+ const typeDecl = /^type\s+([A-Za-z_]\w*)\b/.exec(trimmed);
1115
+ if (typeDecl?.[1]) {
1116
+ addFallbackSymbol(symbols, { filePath, lang, kind: "type", name: typeDecl[1], line: idx + 1, col, signature: trimmed, scope: packageName });
1117
+ continue;
1118
+ }
1119
+ const valueDecl = /^(const|var)\s+([A-Za-z_]\w*)\b/.exec(trimmed);
1120
+ if (valueDecl?.[1] && valueDecl[2]) {
1121
+ addFallbackSymbol(symbols, { filePath, lang, kind: valueDecl[1], name: valueDecl[2], line: idx + 1, col, signature: trimmed, scope: packageName });
1122
+ }
1123
+ }
1124
+ return { file: filePath, lang, symbols, mtimeMs: Date.now() };
1125
+ }
1126
+ function addFallbackSymbol(symbols, opts) {
1127
+ symbols.push({
1128
+ id: 0,
1129
+ lang: opts.lang,
1130
+ kind: opts.kind,
1131
+ name: opts.name,
1132
+ file: opts.filePath,
1133
+ line: opts.line,
1134
+ col: opts.col,
1135
+ signature: opts.signature,
1136
+ docComment: "",
1137
+ scope: opts.scope,
1138
+ text: `${opts.name} ${opts.signature}`.trim()
1139
+ });
1140
+ }
1141
+ function hasUnbalancedDelimiters(content) {
1142
+ const pairs = { "(": ")", "[": "]", "{": "}" };
1143
+ const closers = new Set(Object.values(pairs));
1144
+ const stack = [];
1145
+ for (const ch of content) {
1146
+ if (pairs[ch]) {
1147
+ stack.push(pairs[ch]);
1148
+ } else if (closers.has(ch) && stack.pop() !== ch) {
1149
+ return true;
1150
+ }
1093
1151
  }
1152
+ return stack.length > 0;
1094
1153
  }
1095
1154
  var GO_PARSE_SCRIPT = `
1096
1155
  package main