dimpact 0.1.0

impact analysis from diff
Documentation
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use crate::ir::reference::{RefKind, UnresolvedRef};
use crate::ir::{Symbol, SymbolId, SymbolKind, TextRange};
use crate::languages::LanguageAnalyzer;
use crate::languages::path::resolve_module_path;
use crate::languages::util::{byte_to_line, line_offsets};
use crate::ts_core::{QueryRunner, compile_queries_javascript, load_javascript_spec};

pub struct SpecJsAnalyzer {
    queries: crate::ts_core::CompiledQueries,
    runner: QueryRunner,
}

impl SpecJsAnalyzer {
    pub fn new() -> Self {
        let spec = load_javascript_spec();
        let queries = compile_queries_javascript(&spec).expect("compile js queries");
        let runner = QueryRunner::new_javascript();
        Self { queries, runner }
    }
}

impl Default for SpecJsAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

// Import line_offsets from util
// fn line_lookup(src: &str) -> Vec<usize> { ... }

impl LanguageAnalyzer for SpecJsAnalyzer {
    fn language(&self) -> &'static str {
        "javascript"
    }

    fn symbols_in_file(&self, path: &str, source: &str) -> Vec<Symbol> {
        let offs = line_offsets(source);
        let mut out = Vec::new();
        for caps in self.runner.run_captures(source, &self.queries.decl) {
            if let Some(nc) = caps.iter().find(|c| c.name == "name") {
                let name = &source[nc.start..nc.end];
                if name.is_empty() {
                    continue;
                }
                let decl_cap = caps.iter().find(|c| c.name == "decl");
                let kind = match decl_cap.map(|d| d.kind.as_str()) {
                    Some("class_declaration") => SymbolKind::Struct,
                    Some("method_definition") => SymbolKind::Method,
                    _ => SymbolKind::Function,
                };
                let (sl, el) = if let Some(dc) = decl_cap {
                    (
                        byte_to_line(&offs, dc.start),
                        byte_to_line(&offs, dc.end.saturating_sub(1))
                            .max(byte_to_line(&offs, dc.start)),
                    )
                } else {
                    (
                        byte_to_line(&offs, nc.start),
                        byte_to_line(&offs, nc.end.saturating_sub(1))
                            .max(byte_to_line(&offs, nc.start)),
                    )
                };
                out.push(Symbol {
                    id: SymbolId::new("javascript", path, &kind, name, sl),
                    name: name.to_string(),
                    kind,
                    file: path.to_string(),
                    range: TextRange {
                        start_line: sl,
                        end_line: el,
                    },
                    language: "javascript".to_string(),
                });
            }
        }
        // Fallback: CommonJS function expressions (module.exports = function..., exports.foo = function...)
        use regex::Regex;
        fn find_block_end(src: &str, start_idx: usize) -> usize {
            // naive brace matching from first '{' after start_idx
            let bytes = src.as_bytes();
            let mut i = start_idx;
            while i < bytes.len() && bytes[i] != b'{' {
                i += 1;
            }
            if i >= bytes.len() {
                return start_idx;
            }
            let mut depth = 0i32;
            while i < bytes.len() {
                let b = bytes[i];
                if b == b'{' {
                    depth += 1;
                } else if b == b'}' {
                    depth -= 1;
                    if depth == 0 {
                        return i + 1;
                    }
                }
                i += 1;
            }
            bytes.len()
        }
        let re_default = Regex::new(r#"(?m)^\s*module\.exports\s*=\s*function\s*\("#).unwrap();
        if let Some(m) = re_default.find(source) {
            let sl = byte_to_line(&offs, m.start());
            let endb = find_block_end(source, m.start());
            let el = byte_to_line(&offs, endb.saturating_sub(1)).max(sl);
            let kind = SymbolKind::Function;
            out.push(Symbol {
                id: SymbolId::new("javascript", path, &kind, "default", sl),
                name: "default".to_string(),
                kind,
                file: path.to_string(),
                range: TextRange {
                    start_line: sl,
                    end_line: el,
                },
                language: "javascript".to_string(),
            });
        }
        let re_named = Regex::new(
            r#"(?m)^\s*(?:module\.exports|exports)\.([A-Za-z_$][\w$]*)\s*=\s*function\s*\("#,
        )
        .unwrap();
        for cap in re_named.captures_iter(source) {
            let m = cap.get(0).unwrap();
            let name = cap.get(1).unwrap().as_str();
            let sl = byte_to_line(&offs, m.start());
            let endb = find_block_end(source, m.start());
            let el = byte_to_line(&offs, endb.saturating_sub(1)).max(sl);
            let kind = SymbolKind::Function;
            out.push(Symbol {
                id: SymbolId::new("javascript", path, &kind, name, sl),
                name: name.to_string(),
                kind,
                file: path.to_string(),
                range: TextRange {
                    start_line: sl,
                    end_line: el,
                },
                language: "javascript".to_string(),
            });
        }
        // Fallback: module.exports = { foo(){}, bar: () => {} }
        if let Some(m) = Regex::new(r#"(?m)^\s*module\.exports\s*=\s*\{"#)
            .unwrap()
            .find(source)
        {
            let start = m.start();
            let endb = find_block_end(source, start);
            let body = &source[start..endb];
            // method syntax foo() { }
            let re_obj_method =
                Regex::new(r#"(?m)\b([A-Za-z_$][\w$]*)\s*\(\s*[^\)]*\s*\)\s*\{"#).unwrap();
            for cap in re_obj_method.captures_iter(body) {
                let name = cap.get(1).unwrap().as_str();
                let s_abs = start + cap.get(0).unwrap().start();
                let sl = byte_to_line(&offs, s_abs);
                let el = byte_to_line(&offs, endb.saturating_sub(1)).max(sl);
                let kind = SymbolKind::Function;
                out.push(Symbol {
                    id: SymbolId::new("javascript", path, &kind, name, sl),
                    name: name.to_string(),
                    kind,
                    file: path.to_string(),
                    range: TextRange {
                        start_line: sl,
                        end_line: el,
                    },
                    language: "javascript".to_string(),
                });
            }
            // property with arrow: name: (...) => {
            let re_obj_arrow =
                Regex::new(r#"(?m)\b([A-Za-z_$][\w$]*)\s*:\s*\(?[^\)]*\)?\s*=>\s*\{"#).unwrap();
            for cap in re_obj_arrow.captures_iter(body) {
                let name = cap.get(1).unwrap().as_str();
                let s_abs = start + cap.get(0).unwrap().start();
                let sl = byte_to_line(&offs, s_abs);
                let el = byte_to_line(&offs, endb.saturating_sub(1)).max(sl);
                let kind = SymbolKind::Function;
                out.push(Symbol {
                    id: SymbolId::new("javascript", path, &kind, name, sl),
                    name: name.to_string(),
                    kind,
                    file: path.to_string(),
                    range: TextRange {
                        start_line: sl,
                        end_line: el,
                    },
                    language: "javascript".to_string(),
                });
            }
        }
        // Fallback: class field arrow methods: class A { m = () => { ... } }
        // (also tolerates modifiers: public/private/protected/readonly/static)
        let re_class = Regex::new(r#"(?m)\bclass\s+[A-Za-z_$][\w$]*\s*\{"#).unwrap();
        let re_field_arrow = Regex::new(r#"(?m)(?:\s*(?:public|private|protected|readonly|static)\s+)*\s*([A-Za-z_$][\w$]*)\s*=\s*\(?[^\)]*\)?\s*=>\s*\{"#).unwrap();
        for m in re_class.captures_iter(source) {
            let start = m.get(0).unwrap().start();
            let endb = find_block_end(source, start);
            let body = &source[start..endb];
            for cap in re_field_arrow.captures_iter(body) {
                let name = cap.get(1).unwrap().as_str();
                let s_abs = start + cap.get(0).unwrap().start();
                // find the '{' after match to compute body end
                let mut brace_from = s_abs;
                let bytes = source.as_bytes();
                while brace_from < bytes.len() && bytes[brace_from] != b'{' {
                    brace_from += 1;
                }
                let meth_end = find_block_end(source, brace_from);
                let sl = byte_to_line(&offs, s_abs);
                let el = byte_to_line(&offs, meth_end.saturating_sub(1)).max(sl);
                let kind = SymbolKind::Method;
                out.push(Symbol {
                    id: SymbolId::new("javascript", path, &kind, name, sl),
                    name: name.to_string(),
                    kind,
                    file: path.to_string(),
                    range: TextRange {
                        start_line: sl,
                        end_line: el,
                    },
                    language: "javascript".to_string(),
                });
            }
        }
        out
    }

    fn unresolved_refs(&self, path: &str, source: &str) -> Vec<UnresolvedRef> {
        let offs = line_offsets(source);
        let mut out = Vec::new();
        for caps in self.runner.run_captures(source, &self.queries.calls) {
            // prefer property_identifier if present else identifier
            let name_cap = caps.iter().find(|c| c.name == "name");
            if let Some(n) = name_cap {
                let name = source[n.start..n.end].to_string();
                if name.is_empty() {
                    continue;
                }
                // consider member call by checking if any capture kind is member_expression
                let is_method = caps.iter().any(|c| c.kind == "member_expression");
                let ln = byte_to_line(&offs, n.start);
                let qual = caps
                    .iter()
                    .find(|c| c.name == "qual")
                    .map(|q| source[q.start..q.end].to_string());
                out.push(UnresolvedRef {
                    name,
                    kind: RefKind::Call,
                    file: path.to_string(),
                    line: ln,
                    qualifier: qual.filter(|s| !s.is_empty()),
                    is_method,
                });
            }
        }
        // Fallback: optional chaining calls like obj?.m()
        use regex::Regex;
        let re_opt_member =
            Regex::new(r#"([A-Za-z_$][\w$\.]+)\?\.\s*([A-Za-z_$][\w$]*)\s*\("#).unwrap();
        for cap in re_opt_member.captures_iter(source) {
            let q = cap.get(1).unwrap().as_str().to_string();
            let name = cap.get(2).unwrap().as_str().to_string();
            let ln = byte_to_line(&offs, cap.get(0).unwrap().start());
            out.push(UnresolvedRef {
                name,
                kind: RefKind::Call,
                file: path.to_string(),
                line: ln,
                qualifier: Some(q),
                is_method: true,
            });
        }
        // Fallback: optional chaining on identifier: foo?.()
        let re_opt_ident = Regex::new(r#"\b([A-Za-z_$][\w$]*)\s*\?\.\s*\("#).unwrap();
        for cap in re_opt_ident.captures_iter(source) {
            let name = cap.get(1).unwrap().as_str().to_string();
            let ln = byte_to_line(&offs, cap.get(0).unwrap().start());
            out.push(UnresolvedRef {
                name,
                kind: RefKind::Call,
                file: path.to_string(),
                line: ln,
                qualifier: None,
                is_method: false,
            });
        }
        // Fallback: obj.func?.()
        let re_opt_member2 =
            Regex::new(r#"([A-Za-z_$][\w$\.]+)\.([A-Za-z_$][\w$]*)\s*\?\.\s*\("#).unwrap();
        for cap in re_opt_member2.captures_iter(source) {
            let q = cap.get(1).unwrap().as_str().to_string();
            let name = cap.get(2).unwrap().as_str().to_string();
            let ln = byte_to_line(&offs, cap.get(0).unwrap().start());
            out.push(UnresolvedRef {
                name,
                kind: RefKind::Call,
                file: path.to_string(),
                line: ln,
                qualifier: Some(q),
                is_method: true,
            });
        }
        out
    }

    fn imports_in_file(
        &self,
        path: &str,
        source: &str,
    ) -> std::collections::HashMap<String, String> {
        use regex::Regex;
        let mut map = std::collections::HashMap::new();
        let re_from = Regex::new(r#"(?m)^\s*import\s+(.+?)\s+from\s+['\"]([^'\"]+)['\"]"#).unwrap();
        let re_require = Regex::new(r#"(?m)require\s*\(\s*['\"]([^'\"]+)['\"]\s*\)"#).unwrap();
        let re_export_named =
            Regex::new(r#"(?m)^\s*export\s*\{([^}]+)\}\s*from\s*['\"]([^'\"]+)['\"]"#).unwrap();
        let re_export_all =
            Regex::new(r#"(?m)^\s*export\s*\*\s*from\s*['\"]([^'\"]+)['\"]"#).unwrap();
        let re_req_alias = Regex::new(r#"(?m)^\s*(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*require\s*\(\s*['\"]([^'\"]+)['\"]\s*\)"#).unwrap();
        let re_req_destruct = Regex::new(r#"(?m)^\s*(?:const|let|var)\s*\{([^}]+)\}\s*=\s*require\s*\(\s*['\"]([^'\"]+)['\"]\s*\)"#).unwrap();
        for cap in re_from.captures_iter(source) {
            let head = cap.get(1).unwrap().as_str().trim();
            let raw = cap.get(2).unwrap().as_str();
            if let Some(norm) = normalize_es_module_path(path, raw) {
                // glob prefixes for module and module/index
                map.insert(format!("__glob__{}", norm.clone()), norm.clone());
                let idx = format!("{}/index", norm);
                map.insert(format!("__glob__{}", idx.clone()), idx);
                // namespace import: * as A
                if let Some(ns) = head.strip_prefix("* as ") {
                    let alias = ns.trim();
                    map.insert(alias.to_string(), norm.clone());
                }
                // default import: A from 'mod'
                if head.starts_with(|c: char| c.is_alphabetic() || c == '_' || c == '$')
                    && !head.starts_with('{')
                    && let Some(first) = head.split(',').next()
                {
                    let alias = first.trim();
                    if !alias.is_empty() {
                        map.insert(alias.to_string(), format!("{}::default", norm));
                    }
                }
                // named imports: { a as b, c }
                if head.starts_with('{') {
                    let inner = head.trim().trim_start_matches('{').trim_end_matches('}');
                    for seg in inner.split(',') {
                        let seg = seg.trim();
                        if seg.is_empty() {
                            continue;
                        }
                        if let Some((orig, alias)) = seg.split_once(" as ") {
                            map.insert(
                                alias.trim().to_string(),
                                format!("{}::{}", norm, orig.trim()),
                            );
                        } else {
                            map.insert(seg.to_string(), format!("{}::{}", norm, seg));
                        }
                    }
                }
            }
        }
        // export { a as b, c } from 'mod'
        for cap in re_export_named.captures_iter(source) {
            let inner = cap.get(1).unwrap().as_str();
            let raw = cap.get(2).unwrap().as_str();
            if let Some(norm) = normalize_es_module_path(path, raw) {
                for seg in inner.split(',') {
                    let seg = seg.trim();
                    if seg.is_empty() {
                        continue;
                    }
                    if let Some((orig, alias)) = seg.split_once(" as ") {
                        let o = orig.trim();
                        let a = alias.trim();
                        map.insert(format!("__export__{}", a), format!("{}::{}", norm, o));
                    } else {
                        let o = seg;
                        map.insert(format!("__export__{}", o), format!("{}::{}", norm, o));
                    }
                }
                map.insert(format!("__export_glob__{}", norm.clone()), norm.clone());
                let idx = format!("{}/index", norm);
                map.insert(format!("__export_glob__{}", idx.clone()), idx);
            }
        }
        // export * from 'mod'
        for cap in re_export_all.captures_iter(source) {
            let raw = cap.get(1).unwrap().as_str();
            if let Some(norm) = normalize_es_module_path(path, raw) {
                map.insert(format!("__export_glob__{}", norm.clone()), norm.clone());
                let idx = format!("{}/index", norm);
                map.insert(format!("__export_glob__{}", idx.clone()), idx);
            }
        }
        // const X = require('mod')
        for cap in re_req_alias.captures_iter(source) {
            let alias = cap.get(1).unwrap().as_str();
            let raw = cap.get(2).unwrap().as_str();
            if let Some(norm) = normalize_es_module_path(path, raw) {
                map.insert(alias.to_string(), format!("{}::default", norm));
                map.insert(format!("__glob__{}", norm.clone()), norm.clone());
                let idx = format!("{}/index", norm);
                map.insert(format!("__glob__{}", idx.clone()), idx);
            }
        }
        // const { a, b: c } = require('mod')
        for cap in re_req_destruct.captures_iter(source) {
            let inner = cap.get(1).unwrap().as_str();
            let raw = cap.get(2).unwrap().as_str();
            if let Some(norm) = normalize_es_module_path(path, raw) {
                for seg in inner.split(',') {
                    let seg = seg.trim();
                    if seg.is_empty() {
                        continue;
                    }
                    if let Some((orig, alias)) = seg.split_once(':') {
                        map.insert(
                            alias.trim().to_string(),
                            format!("{}::{}", norm, orig.trim()),
                        );
                    } else {
                        map.insert(seg.to_string(), format!("{}::{}", norm, seg));
                    }
                }
                map.insert(format!("__glob__{}", norm.clone()), norm.clone());
                let idx = format!("{}/index", norm);
                map.insert(format!("__glob__{}", idx.clone()), idx);
            }
        }
        for cap in re_require.captures_iter(source) {
            let raw = cap.get(1).unwrap().as_str();
            if let Some(norm) = normalize_es_module_path(path, raw) {
                map.insert(format!("__glob__{}", norm.clone()), norm.clone());
                let idx = format!("{}/index", norm);
                map.insert(format!("__glob__{}", idx.clone()), idx);
            }
        }
        map
    }
}

fn normalize_es_module_path(cur_file: &str, raw: &str) -> Option<String> {
    // Supported JS/TS extensions
    let exts = [".js", ".mjs", ".cjs", ".ts", ".tsx"];
    resolve_module_path(cur_file, raw, &exts)
}