hotspots-core 2.2.0

Core library for finding the riskiest code by combining complexity with git change history (Local Risk Score) across TypeScript, JavaScript, Go, Python, Rust, Java, C#, and C
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
//! ECMAScript (TypeScript/JavaScript) parser and CFG builder implementation

use super::cfg_builder::CfgBuilder;
use super::parser::{LanguageParser, ParsedModule};
use crate::ast::FunctionNode;
use crate::cfg::Cfg;
use anyhow::Result;
use regex::Regex;
use swc_common::{sync::Lrc, SourceMap};
use swc_ecma_ast::Module;

/// ECMAScript parser using SWC
///
/// Parses TypeScript and JavaScript files using the SWC compiler infrastructure.
pub struct ECMAScriptParser {
    source_map: Lrc<SourceMap>,
}

impl ECMAScriptParser {
    /// Create a new ECMAScript parser with the given source map
    pub fn new(source_map: Lrc<SourceMap>) -> Self {
        ECMAScriptParser { source_map }
    }
}

impl LanguageParser for ECMAScriptParser {
    fn parse(&self, source: &str, filename: &str) -> Result<Box<dyn ParsedModule>> {
        let module = crate::parser::parse_source(source, &self.source_map, filename)?;

        Ok(Box::new(ECMAScriptModule {
            module,
            source_map: self.source_map.clone(),
        }))
    }
}

/// Parsed ECMAScript module
struct ECMAScriptModule {
    module: Module,
    source_map: Lrc<SourceMap>,
}

impl ParsedModule for ECMAScriptModule {
    fn discover_functions(&self, file_index: usize, source: &str) -> Vec<FunctionNode> {
        crate::discover::discover_functions(&self.module, file_index, source, &self.source_map)
    }
}

/// ECMAScript CFG builder
///
/// Builds control flow graphs from ECMAScript (TypeScript/JavaScript) function bodies.
pub struct ECMAScriptCfgBuilder;

impl CfgBuilder for ECMAScriptCfgBuilder {
    fn build(&self, function: &FunctionNode) -> Cfg {
        // Delegate to the existing cfg::builder::build_cfg function
        // which already knows how to build CFGs from ECMAScript functions
        crate::cfg::builder::build_cfg(function)
    }
}

/// Extracted script block from a Vue SFC
struct ScriptBlock {
    /// The raw content between `<script ...>` and `</script>`
    content: String,
    /// 1-indexed line number of the first line of `content` in the original file
    start_line: u32,
    /// Whether `lang="ts"` (or `lang='ts'`) was detected on the script tag
    is_typescript: bool,
}

/// Extract the first `<script>` or `<script setup>` block from a Vue SFC source.
///
/// Returns `None` if no script block is found.
fn extract_script_block(source: &str) -> Option<ScriptBlock> {
    let open_re = Regex::new(r"(?i)<script(\s[^>]*)?>").unwrap();
    let close_tag = "</script>";

    let open_m = open_re.find(source)?;
    let attrs = open_re
        .captures(source)
        .and_then(|c| c.get(1))
        .map(|m| m.as_str())
        .unwrap_or("");

    let is_typescript = {
        let lang_re = Regex::new(r#"lang\s*=\s*['"]ts['"]"#).unwrap();
        lang_re.is_match(attrs)
    };

    let content_start = open_m.end();
    let close_pos = source[content_start..].find(close_tag)?;
    let content = source[content_start..content_start + close_pos].to_string();

    // Count lines before content_start to get the 1-indexed line of the first
    // content line (the newline after the opening tag puts content on the next line).
    let start_line = source[..content_start]
        .chars()
        .filter(|&c| c == '\n')
        .count() as u32
        + 1;

    Some(ScriptBlock {
        content,
        start_line,
        is_typescript,
    })
}

/// Vue SFC parser — extracts the `<script>` block and delegates to ECMAScriptParser.
pub struct VueParser {
    inner: ECMAScriptParser,
}

impl VueParser {
    pub fn new(source_map: Lrc<SourceMap>) -> Self {
        VueParser {
            inner: ECMAScriptParser::new(source_map),
        }
    }
}

impl LanguageParser for VueParser {
    fn parse(&self, source: &str, filename: &str) -> Result<Box<dyn ParsedModule>> {
        let block = extract_script_block(source)
            .ok_or_else(|| anyhow::anyhow!("No <script> block found in Vue SFC: {}", filename))?;

        // Pick a synthetic filename so SWC uses the right syntax
        let synthetic = if block.is_typescript {
            "__vue_script__.ts"
        } else {
            "__vue_script__.js"
        };

        let inner_module = self.inner.parse(&block.content, synthetic)?;

        Ok(Box::new(VueParsedModule {
            inner: inner_module,
            line_offset: block.start_line.saturating_sub(1),
        }))
    }
}

/// Wraps an ECMAScript ParsedModule and shifts all span line numbers by `line_offset`.
struct VueParsedModule {
    inner: Box<dyn ParsedModule>,
    /// Lines to add to each reported start_line / end_line
    line_offset: u32,
}

impl ParsedModule for VueParsedModule {
    fn discover_functions(&self, file_index: usize, source: &str) -> Vec<FunctionNode> {
        let mut functions = self.inner.discover_functions(file_index, source);
        let offset = self.line_offset;
        for f in &mut functions {
            f.span.start_line += offset;
            f.span.end_line += offset;
        }
        functions
    }
}

/// Extracted frontmatter block from an Astro component
struct FrontmatterBlock {
    /// The raw content between the opening and closing `---` fences
    content: String,
    /// 1-indexed line number of the first line of `content` in the original file
    start_line: u32,
}

/// Extract the `---`/`---` frontmatter fence from an Astro component source.
///
/// The fence must open on the file's first line. Returns `None` if the file
/// has no frontmatter block — a valid Astro file with no component script.
fn extract_frontmatter_block(source: &str) -> Option<FrontmatterBlock> {
    let open_re = Regex::new(r"^---[ \t]*\r?\n").unwrap();
    let open_m = open_re.find(source)?;
    let content_start = open_m.end();

    let close_re = Regex::new(r"(?m)^---[ \t]*\r?$").unwrap();
    let close_m = close_re.find(&source[content_start..])?;
    let content = source[content_start..content_start + close_m.start()].to_string();

    // Count lines before content_start to get the 1-indexed line of the first
    // content line (the newline after the opening fence puts content on the next line).
    let start_line = source[..content_start]
        .chars()
        .filter(|&c| c == '\n')
        .count() as u32
        + 1;

    Some(FrontmatterBlock {
        content,
        start_line,
    })
}

/// Astro component parser — extracts the `---` frontmatter fence and
/// delegates to ECMAScriptParser. Astro frontmatter is always TypeScript
/// (no `lang` attribute to detect, unlike Vue's `<script>` tag).
pub struct AstroParser {
    inner: ECMAScriptParser,
}

impl AstroParser {
    pub fn new(source_map: Lrc<SourceMap>) -> Self {
        AstroParser {
            inner: ECMAScriptParser::new(source_map),
        }
    }
}

impl LanguageParser for AstroParser {
    fn parse(&self, source: &str, _filename: &str) -> Result<Box<dyn ParsedModule>> {
        let Some(block) = extract_frontmatter_block(source) else {
            // No frontmatter fence — a valid Astro file with no component
            // script logic (pure markup). Nothing to analyze.
            return Ok(Box::new(EmptyParsedModule));
        };

        let inner_module = self
            .inner
            .parse(&block.content, "__astro_frontmatter__.ts")?;

        Ok(Box::new(AstroParsedModule {
            inner: inner_module,
            line_offset: block.start_line.saturating_sub(1),
        }))
    }
}

/// A `ParsedModule` with no functions, used when an Astro file has no
/// frontmatter block to analyze.
struct EmptyParsedModule;

impl ParsedModule for EmptyParsedModule {
    fn discover_functions(&self, _file_index: usize, _source: &str) -> Vec<FunctionNode> {
        Vec::new()
    }
}

/// Wraps an ECMAScript ParsedModule and shifts all span line numbers by `line_offset`.
struct AstroParsedModule {
    inner: Box<dyn ParsedModule>,
    /// Lines to add to each reported start_line / end_line
    line_offset: u32,
}

impl ParsedModule for AstroParsedModule {
    fn discover_functions(&self, file_index: usize, source: &str) -> Vec<FunctionNode> {
        let mut functions = self.inner.discover_functions(file_index, source);
        let offset = self.line_offset;
        for f in &mut functions {
            f.span.start_line += offset;
            f.span.end_line += offset;
        }
        functions
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ecmascript_parser_simple_function() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = ECMAScriptParser::new(source_map);

        let source = "function foo() { return 42; }";
        let module = parser.parse(source, "test.ts").unwrap();
        let functions = module.discover_functions(0, source);

        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, Some("foo".to_string()));
    }

    #[test]
    fn test_ecmascript_parser_multiple_functions() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = ECMAScriptParser::new(source_map);

        let source = r#"
            function foo() { return 1; }
            function bar() { return 2; }
            const baz = () => 3;
        "#;
        let module = parser.parse(source, "test.ts").unwrap();
        let functions = module.discover_functions(0, source);

        assert_eq!(functions.len(), 3);
    }

    #[test]
    fn test_ecmascript_parser_typescript() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = ECMAScriptParser::new(source_map);

        let source = r#"
            function typed(x: number): string {
                return x.toString();
            }
        "#;
        let module = parser.parse(source, "test.ts").unwrap();
        let functions = module.discover_functions(0, source);

        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, Some("typed".to_string()));
    }

    #[test]
    fn test_ecmascript_parser_jsx() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = ECMAScriptParser::new(source_map);

        let source = r#"
            function Component() {
                return <div>Hello</div>;
            }
        "#;
        let module = parser.parse(source, "test.tsx").unwrap();
        let functions = module.discover_functions(0, source);

        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, Some("Component".to_string()));
    }

    #[test]
    fn test_ecmascript_parser_class_methods() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = ECMAScriptParser::new(source_map);

        let source = r#"
            class MyClass {
                method1() { return 1; }
                method2() { return 2; }
            }
        "#;
        let module = parser.parse(source, "test.ts").unwrap();
        let functions = module.discover_functions(0, source);

        assert_eq!(functions.len(), 2);
    }

    #[test]
    fn test_ecmascript_parser_parse_error() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = ECMAScriptParser::new(source_map);

        let source = "function foo() { return }}}"; // Invalid syntax
        let result = parser.parse(source, "test.ts");

        assert!(result.is_err());
    }

    #[test]
    fn test_ecmascript_parser_empty_file() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = ECMAScriptParser::new(source_map);

        let source = "";
        let module = parser.parse(source, "test.ts").unwrap();
        let functions = module.discover_functions(0, source);

        assert_eq!(functions.len(), 0);
    }

    #[test]
    fn test_ecmascript_parser_deterministic() {
        let source = r#"
            function zzz() { return 3; }
            function aaa() { return 1; }
            function mmm() { return 2; }
        "#;

        // Parse twice with fresh source maps
        let source_map1: Lrc<SourceMap> = Default::default();
        let parser1 = ECMAScriptParser::new(source_map1);
        let module1 = parser1.parse(source, "test.ts").unwrap();
        let functions1 = module1.discover_functions(0, source);

        let source_map2: Lrc<SourceMap> = Default::default();
        let parser2 = ECMAScriptParser::new(source_map2);
        let module2 = parser2.parse(source, "test.ts").unwrap();
        let functions2 = module2.discover_functions(0, source);

        // Functions should be discovered in the same order (deterministic)
        assert_eq!(functions1.len(), functions2.len());
        assert_eq!(functions1.len(), 3);

        // Check that names are in the same order
        for (f1, f2) in functions1.iter().zip(functions2.iter()) {
            assert_eq!(f1.name, f2.name);
            assert_eq!(f1.span.start_line, f2.span.start_line);
        }

        // Verify functions are sorted by source position, not alphabetically
        assert_eq!(functions1[0].name, Some("zzz".to_string()));
        assert_eq!(functions1[1].name, Some("aaa".to_string()));
        assert_eq!(functions1[2].name, Some("mmm".to_string()));
    }

    #[test]
    fn test_extract_frontmatter_block_simple() {
        let source = "---\nconst x = 1;\n---\n<div>hi</div>\n";
        let block = extract_frontmatter_block(source).expect("frontmatter block found");
        assert_eq!(block.content, "const x = 1;\n");
        assert_eq!(block.start_line, 2);
    }

    #[test]
    fn test_extract_frontmatter_block_missing() {
        // No opening fence at all — pure markup, valid Astro file.
        let source = "<div>no frontmatter here</div>\n";
        assert!(extract_frontmatter_block(source).is_none());
    }

    #[test]
    fn test_extract_frontmatter_block_requires_fence_at_start() {
        // A `---` later in the file (e.g. inside markup/a comment) must not
        // be mistaken for an opening fence.
        let source = "<div>\n---\nnot frontmatter\n---\n</div>\n";
        assert!(extract_frontmatter_block(source).is_none());
    }

    #[test]
    fn test_astro_parser_simple_component() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = AstroParser::new(source_map);

        let source = "---\nfunction greet() { return 1; }\n---\n<div>{greet()}</div>\n";
        let module = parser.parse(source, "test.astro").unwrap();
        let functions = module.discover_functions(0, source);

        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, Some("greet".to_string()));
        // Function is on line 2 of the original file, inside the frontmatter.
        assert_eq!(functions[0].span.start_line, 2);
    }

    #[test]
    fn test_astro_parser_no_frontmatter_returns_no_functions() {
        let source_map: Lrc<SourceMap> = Default::default();
        let parser = AstroParser::new(source_map);

        let source = "<div>static markup only</div>\n";
        let module = parser.parse(source, "test.astro").unwrap();
        let functions = module.discover_functions(0, source);

        assert_eq!(functions.len(), 0);
    }
}