|
| 1 | +-- Pure parser: TS diagnostic message -> structured `kind` table. |
| 2 | +-- No side effects; safe to unit-test. |
| 3 | + |
| 4 | +local M = {} |
| 5 | + |
| 6 | +-- ── helpers ──────────────────────────────────────────────────────────────── |
| 7 | + |
| 8 | +local function trim(s) |
| 9 | + return (s:gsub("^%s+", ""):gsub("%s+$", "")) |
| 10 | +end |
| 11 | + |
| 12 | +-- Split a generic argument list at top-level commas, respecting nested |
| 13 | +-- <>, [], (), {} depth. "A, B<C, D>, E" -> { "A", "B<C, D>", "E" }. |
| 14 | +local function split_generic_args(s) |
| 15 | + local parts = {} |
| 16 | + local depth = 0 |
| 17 | + local current = "" |
| 18 | + for i = 1, #s do |
| 19 | + local c = s:sub(i, i) |
| 20 | + if c == "<" or c == "[" or c == "(" or c == "{" then |
| 21 | + depth = depth + 1 |
| 22 | + current = current .. c |
| 23 | + elseif c == ">" or c == "]" or c == ")" or c == "}" then |
| 24 | + depth = depth - 1 |
| 25 | + current = current .. c |
| 26 | + elseif c == "," and depth == 0 then |
| 27 | + table.insert(parts, trim(current)) |
| 28 | + current = "" |
| 29 | + else |
| 30 | + current = current .. c |
| 31 | + end |
| 32 | + end |
| 33 | + if #trim(current) > 0 then |
| 34 | + table.insert(parts, trim(current)) |
| 35 | + end |
| 36 | + return parts |
| 37 | +end |
| 38 | + |
| 39 | +-- Split a top-level union "A | B | C<D | E>" into members, respecting depth. |
| 40 | +local function split_union(s) |
| 41 | + local parts = {} |
| 42 | + local depth = 0 |
| 43 | + local current = "" |
| 44 | + for i = 1, #s do |
| 45 | + local c = s:sub(i, i) |
| 46 | + if c == "<" or c == "[" or c == "(" or c == "{" then |
| 47 | + depth = depth + 1 |
| 48 | + current = current .. c |
| 49 | + elseif c == ">" or c == "]" or c == ")" or c == "}" then |
| 50 | + depth = depth - 1 |
| 51 | + current = current .. c |
| 52 | + elseif c == "|" and depth == 0 then |
| 53 | + local t = trim(current) |
| 54 | + if t ~= "" then |
| 55 | + table.insert(parts, t) |
| 56 | + end |
| 57 | + current = "" |
| 58 | + else |
| 59 | + current = current .. c |
| 60 | + end |
| 61 | + end |
| 62 | + local t = trim(current) |
| 63 | + if t ~= "" then |
| 64 | + table.insert(parts, t) |
| 65 | + end |
| 66 | + return parts |
| 67 | +end |
| 68 | +M.split_union = split_union |
| 69 | + |
| 70 | +-- Strip common Effect-ecosystem noise before matching the outer shape. |
| 71 | +local function clean_effect_string(s) |
| 72 | + if not s then |
| 73 | + return s |
| 74 | + end |
| 75 | + s = s:gsub("^YieldWrap<(.+)>$", "%1") |
| 76 | + s = s:gsub('import%("[^"]+"%)%.', "") |
| 77 | + return trim(s) |
| 78 | +end |
| 79 | + |
| 80 | +-- Unwrap Context.Tag<"Id", Service> -> Service. Handles both the bare |
| 81 | +-- form and the namespaced Context.Tag / Context.Reference variants. |
| 82 | +local function unwrap_context_tag(member) |
| 83 | + local service = member:match('^Context%.Tag<"[^"]-",%s*(.-)>$') |
| 84 | + or member:match('^Context%.Reference<"[^"]-",%s*(.-)>$') |
| 85 | + or member:match('^Tag<"[^"]-",%s*(.-)>$') |
| 86 | + return service or member |
| 87 | +end |
| 88 | + |
| 89 | +local function unwrap_union_services(r) |
| 90 | + if not r or r == "never" then |
| 91 | + return r |
| 92 | + end |
| 93 | + local members = split_union(r) |
| 94 | + for i, m in ipairs(members) do |
| 95 | + members[i] = unwrap_context_tag(m) |
| 96 | + end |
| 97 | + return table.concat(members, " | ") |
| 98 | +end |
| 99 | + |
| 100 | +-- Parse Effect<A, E, R> / Stream<A, E, R> / Layer<ROut, E, RIn>. Returns |
| 101 | +-- { tag, A, E, R, labels = {A,E,R|ROut,E,RIn} } or nil. |
| 102 | +local function parse_effect_type(s) |
| 103 | + if not s then |
| 104 | + return nil |
| 105 | + end |
| 106 | + s = clean_effect_string(s) |
| 107 | + |
| 108 | + local tag, inner |
| 109 | + inner = s:match("^Effect%.Effect<(.+)>$") or s:match("^Effect<(.+)>$") |
| 110 | + if inner then |
| 111 | + tag = "effect" |
| 112 | + else |
| 113 | + inner = s:match("^Stream%.Stream<(.+)>$") or s:match("^Stream<(.+)>$") |
| 114 | + if inner then |
| 115 | + tag = "stream" |
| 116 | + else |
| 117 | + inner = s:match("^Layer%.Layer<(.+)>$") or s:match("^Layer<(.+)>$") |
| 118 | + if inner then |
| 119 | + tag = "layer" |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + if not inner then |
| 125 | + return nil |
| 126 | + end |
| 127 | + |
| 128 | + local parts = split_generic_args(inner) |
| 129 | + local A, E, R = parts[1], parts[2] or "never", parts[3] or "never" |
| 130 | + if not A or A == "" then |
| 131 | + return nil |
| 132 | + end |
| 133 | + |
| 134 | + -- Context.Tag unwrapping applies to the requirements channel for Effect / |
| 135 | + -- Stream (and RIn for Layer). E is untouched — errors aren't Tag-wrapped. |
| 136 | + R = unwrap_union_services(R) |
| 137 | + |
| 138 | + if tag == "layer" then |
| 139 | + return { tag = "layer", A = A, E = E, R = R, labels = { "ROut", "E", "RIn" } } |
| 140 | + end |
| 141 | + return { tag = tag, A = A, E = E, R = R, labels = { "A", "E", "R" } } |
| 142 | +end |
| 143 | +M.parse_effect_type = parse_effect_type |
| 144 | + |
| 145 | +function M.has_scope(r) |
| 146 | + if not r then |
| 147 | + return false |
| 148 | + end |
| 149 | + for _, m in ipairs(split_union(r)) do |
| 150 | + if m == "Scope" or m == "Scope.Scope" then |
| 151 | + return true |
| 152 | + end |
| 153 | + end |
| 154 | + return false |
| 155 | +end |
| 156 | + |
| 157 | +-- Members of `a` that aren't in `b`. Both are top-level union strings. |
| 158 | +function M.union_diff(a, b) |
| 159 | + if not a or a == "never" then |
| 160 | + return {} |
| 161 | + end |
| 162 | + local b_set = {} |
| 163 | + if b and b ~= "never" then |
| 164 | + for _, part in ipairs(split_union(b)) do |
| 165 | + b_set[part] = true |
| 166 | + end |
| 167 | + end |
| 168 | + local missing = {} |
| 169 | + for _, part in ipairs(split_union(a)) do |
| 170 | + if not b_set[part] then |
| 171 | + table.insert(missing, part) |
| 172 | + end |
| 173 | + end |
| 174 | + return missing |
| 175 | +end |
| 176 | + |
| 177 | +-- ── public API ──────────────────────────────────────────────────────────── |
| 178 | + |
| 179 | +-- Default set of diagnostic sources we treat as "TypeScript". Tight, |
| 180 | +-- exact-match set — extensible via setup({ sources = { ... } }). |
| 181 | +M.default_sources = { |
| 182 | + typescript = true, |
| 183 | + ts = true, |
| 184 | + vtsls = true, |
| 185 | +} |
| 186 | + |
| 187 | +function M.is_ts_source(source, allowed) |
| 188 | + if not source then |
| 189 | + return false |
| 190 | + end |
| 191 | + local set = allowed or M.default_sources |
| 192 | + return set[source] == true |
| 193 | +end |
| 194 | + |
| 195 | +-- Parse a TS diagnostic message into a structured kind + captures. |
| 196 | +-- Returns nil if no pattern matched. |
| 197 | +function M.parse(msg, opts) |
| 198 | + opts = opts or {} |
| 199 | + |
| 200 | + -- Capture "Property 'X' is missing" before trimming, since TS often puts |
| 201 | + -- this detail on a continuation line we're about to strip. |
| 202 | + local missing_prop_full = msg:match("Property '([^']+)' is missing") |
| 203 | + |
| 204 | + -- TS often appends follow-up sentences after the core error; strip them so |
| 205 | + -- the `$`-anchored patterns below can still match. |
| 206 | + msg = msg |
| 207 | + :gsub("\n.*", "") |
| 208 | + :gsub("%.%s+Property.-$", "") |
| 209 | + :gsub(" with '[^']+': %w+%'.-$", "") |
| 210 | + :gsub(" with '[^']+'.-$", "") |
| 211 | + |
| 212 | + local got, expected = msg:match("Type '(.+)' is not assignable to type '(.+)'%.?$") |
| 213 | + if not got then |
| 214 | + got, expected = msg:match("Argument of type '(.+)' is not assignable to parameter of type '(.+)'%.?$") |
| 215 | + end |
| 216 | + |
| 217 | + if got then |
| 218 | + if opts.effect ~= false then |
| 219 | + local got_eff = parse_effect_type(got) |
| 220 | + local exp_eff = parse_effect_type(expected) |
| 221 | + if got_eff and exp_eff and got_eff.tag == exp_eff.tag then |
| 222 | + local missing_services = M.union_diff(got_eff.R, exp_eff.R) |
| 223 | + local unhandled_errors = M.union_diff(got_eff.E, exp_eff.E) |
| 224 | + local success_differs = got_eff.A ~= exp_eff.A |
| 225 | + local scope_required = M.has_scope(got_eff.R) and not M.has_scope(exp_eff.R) |
| 226 | + local diff_count = 0 |
| 227 | + if success_differs then |
| 228 | + diff_count = diff_count + 1 |
| 229 | + end |
| 230 | + if got_eff.E ~= exp_eff.E then |
| 231 | + diff_count = diff_count + 1 |
| 232 | + end |
| 233 | + if got_eff.R ~= exp_eff.R then |
| 234 | + diff_count = diff_count + 1 |
| 235 | + end |
| 236 | + return { |
| 237 | + kind = "effect_mismatch", |
| 238 | + tag = got_eff.tag, |
| 239 | + labels = got_eff.labels, |
| 240 | + got = got_eff, |
| 241 | + expected = exp_eff, |
| 242 | + missing_services = missing_services, |
| 243 | + unhandled_errors = unhandled_errors, |
| 244 | + success_differs = success_differs, |
| 245 | + scope_required = scope_required, |
| 246 | + diff_count = diff_count, |
| 247 | + } |
| 248 | + end |
| 249 | + end |
| 250 | + |
| 251 | + local missing_prop = msg:match("Property '([^']+)' is missing") or missing_prop_full |
| 252 | + return { kind = "type_mismatch", got = got, expected = expected, missing = missing_prop } |
| 253 | + end |
| 254 | + |
| 255 | + local prop, in_type, req_type = msg:match("Property '(.-)' is missing in type '(.-)' but required in type '(.-)'") |
| 256 | + if prop then |
| 257 | + return { kind = "missing_property", prop = prop, in_type = in_type, required = req_type } |
| 258 | + end |
| 259 | + |
| 260 | + local mp, on_type = msg:match("Property '(.-)' does not exist on type '(.-)'") |
| 261 | + if mp then |
| 262 | + return { kind = "unknown_property", prop = mp, on_type = on_type } |
| 263 | + end |
| 264 | + |
| 265 | + local name = msg:match("Cannot find name '(.-)'") |
| 266 | + if name then |
| 267 | + return { kind = "undefined", name = name } |
| 268 | + end |
| 269 | + |
| 270 | + local module_path = msg:match("Cannot find module '(.-)' or its corresponding type declarations") |
| 271 | + or msg:match("Cannot find module '(.-)'") |
| 272 | + if module_path then |
| 273 | + return { kind = "module_not_found", path = module_path } |
| 274 | + end |
| 275 | + |
| 276 | + local no_mod, no_mem = msg:match("Module '\"(.-)\"' has no exported member '(.-)'") |
| 277 | + if not no_mod then |
| 278 | + no_mod, no_mem = msg:match("Module '(.-)' has no exported member '(.-)'") |
| 279 | + end |
| 280 | + if no_mod then |
| 281 | + return { kind = "export_not_found", module = no_mod, member = no_mem } |
| 282 | + end |
| 283 | + |
| 284 | + local implicit = msg:match("Parameter '(.-)' implicitly has an 'any' type") |
| 285 | + if implicit then |
| 286 | + return { kind = "implicit_any", name = implicit } |
| 287 | + end |
| 288 | + |
| 289 | + local used_before = msg:match("Variable '(.-)' is used before being assigned") |
| 290 | + if used_before then |
| 291 | + return { kind = "used_before_assigned", name = used_before } |
| 292 | + end |
| 293 | + |
| 294 | + local nullish = msg:match("Object is possibly '(.-)'") |
| 295 | + if nullish then |
| 296 | + return { kind = "nullish", value = nullish } |
| 297 | + end |
| 298 | + |
| 299 | + local exp_args, got_args = msg:match("Expected (%d+) arguments?, but got (%d+)") |
| 300 | + if exp_args then |
| 301 | + return { kind = "arg_count", expected = exp_args, got = got_args } |
| 302 | + end |
| 303 | + |
| 304 | + local const_name = msg:match("Cannot assign to '(.-)' because it is a constant") |
| 305 | + if const_name then |
| 306 | + return { kind = "const_assign", name = const_name } |
| 307 | + end |
| 308 | + |
| 309 | + if msg:match("has no call signatures") then |
| 310 | + local t = msg:match("Type '(.-)' has no call signatures") |
| 311 | + return { kind = "not_callable", type = t } |
| 312 | + end |
| 313 | + |
| 314 | + local dep_sig, dep_name = msg:match("The signature '(.-)' of '(.-)' is deprecated") |
| 315 | + if dep_sig then |
| 316 | + return { kind = "deprecated", name = dep_name, signature = dep_sig } |
| 317 | + end |
| 318 | + local deprecated_name = msg:match("'(.-)' is deprecated") |
| 319 | + if deprecated_name then |
| 320 | + return { kind = "deprecated", name = deprecated_name } |
| 321 | + end |
| 322 | + |
| 323 | + -- User-provided extra parsers (setup opts.extra_patterns). |
| 324 | + if opts.extra_patterns then |
| 325 | + for _, parser in ipairs(opts.extra_patterns) do |
| 326 | + local result = parser(msg) |
| 327 | + if result then |
| 328 | + return result |
| 329 | + end |
| 330 | + end |
| 331 | + end |
| 332 | + |
| 333 | + return nil |
| 334 | +end |
| 335 | + |
| 336 | +return M |
0 commit comments