semver-analyzer-ts 0.0.4

TypeScript/JavaScript support for the semver-analyzer
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! cloneElement prop injection detection.
//!
//! Detects the `Children.map(children, child => cloneElement(child, { props }))` pattern
//! in React component source code. This pattern is used to inject props into children,
//! creating a structural coupling between parent and child components.
//!
//! The detection extracts the property names from cloneElement's second argument,
//! which can then be matched against family members' declared props to infer
//! parent-child relationships.

use crate::sd_types::CloneElementInjection;
use oxc_allocator::Allocator;
use oxc_ast::ast::*;
use oxc_parser::Parser;
use oxc_span::SourceType;

/// Detect `cloneElement` prop injections in a component's source code.
///
/// Finds patterns like:
/// - `Children.map(children, child => cloneElement(child, { rowid }))`
/// - `React.Children.map(children, (child) => cloneElement(child, { rowid: value }))`
/// - `Children.forEach(children, child => { ... cloneElement(child, { prop }) })`
///
/// Returns a list of injections, one per cloneElement call found.
pub fn detect_clone_element_injections(source: &str) -> Vec<CloneElementInjection> {
    let allocator = Allocator::default();
    let source_type = SourceType::tsx();
    let parsed = Parser::new(&allocator, source, source_type).parse();

    let mut injections = Vec::new();
    for stmt in &parsed.program.body {
        find_clone_element_in_statement(stmt, &mut injections);
    }

    injections
}

// ── Statement walking ──────────────────────────────────────────────────

fn find_clone_element_in_statement<'a>(
    stmt: &'a Statement<'a>,
    injections: &mut Vec<CloneElementInjection>,
) {
    match stmt {
        Statement::FunctionDeclaration(f) => {
            if let Some(body) = &f.body {
                for inner in &body.statements {
                    find_clone_element_in_statement(inner, injections);
                }
            }
        }
        Statement::ReturnStatement(ret) => {
            if let Some(expr) = &ret.argument {
                find_clone_element_in_expression(expr, injections);
            }
        }
        Statement::VariableDeclaration(decl) => {
            for declarator in &decl.declarations {
                if let Some(init) = &declarator.init {
                    find_clone_element_in_expression(init, injections);
                }
            }
        }
        Statement::ExpressionStatement(expr_stmt) => {
            find_clone_element_in_expression(&expr_stmt.expression, injections);
        }
        Statement::ExportNamedDeclaration(export) => {
            if let Some(decl) = &export.declaration {
                find_clone_element_in_declaration(decl, injections);
            }
        }
        Statement::ExportDefaultDeclaration(export) => {
            if let Some(expr) = export.declaration.as_expression() {
                find_clone_element_in_expression(expr, injections);
            }
        }
        Statement::BlockStatement(block) => {
            for inner in &block.body {
                find_clone_element_in_statement(inner, injections);
            }
        }
        Statement::IfStatement(if_stmt) => {
            find_clone_element_in_statement(&if_stmt.consequent, injections);
            if let Some(alt) = &if_stmt.alternate {
                find_clone_element_in_statement(alt, injections);
            }
        }
        Statement::ClassDeclaration(class) => {
            for element in &class.body.body {
                match element {
                    ClassElement::MethodDefinition(method) => {
                        if let Some(body) = &method.value.body {
                            for stmt in &body.statements {
                                find_clone_element_in_statement(stmt, injections);
                            }
                        }
                    }
                    ClassElement::PropertyDefinition(prop) => {
                        if let Some(init) = &prop.value {
                            find_clone_element_in_expression(init, injections);
                        }
                    }
                    _ => {}
                }
            }
        }
        _ => {}
    }
}

fn find_clone_element_in_declaration<'a>(
    decl: &'a Declaration<'a>,
    injections: &mut Vec<CloneElementInjection>,
) {
    match decl {
        Declaration::FunctionDeclaration(f) => {
            if let Some(body) = &f.body {
                for stmt in &body.statements {
                    find_clone_element_in_statement(stmt, injections);
                }
            }
        }
        Declaration::VariableDeclaration(var_decl) => {
            for declarator in &var_decl.declarations {
                if let Some(init) = &declarator.init {
                    find_clone_element_in_expression(init, injections);
                }
            }
        }
        Declaration::ClassDeclaration(class) => {
            for element in &class.body.body {
                match element {
                    ClassElement::MethodDefinition(method) => {
                        if let Some(body) = &method.value.body {
                            for stmt in &body.statements {
                                find_clone_element_in_statement(stmt, injections);
                            }
                        }
                    }
                    ClassElement::PropertyDefinition(prop) => {
                        if let Some(init) = &prop.value {
                            find_clone_element_in_expression(init, injections);
                        }
                    }
                    _ => {}
                }
            }
        }
        _ => {}
    }
}

// ── Expression walking ─────────────────────────────────────────────────

fn find_clone_element_in_expression<'a>(
    expr: &'a Expression<'a>,
    injections: &mut Vec<CloneElementInjection>,
) {
    match expr {
        Expression::CallExpression(call) => {
            // Check if this is a cloneElement call
            if let Some(injection) = try_extract_clone_element(call) {
                injections.push(injection);
            }
            // Also recurse into arguments (Children.map callback contains cloneElement)
            for arg in &call.arguments {
                if let Some(expr) = arg.as_expression() {
                    find_clone_element_in_expression(expr, injections);
                }
            }
        }
        Expression::ArrowFunctionExpression(arrow) => {
            for stmt in &arrow.body.statements {
                find_clone_element_in_statement(stmt, injections);
            }
        }
        Expression::FunctionExpression(func) => {
            if let Some(body) = &func.body {
                for stmt in &body.statements {
                    find_clone_element_in_statement(stmt, injections);
                }
            }
        }
        Expression::ParenthesizedExpression(paren) => {
            find_clone_element_in_expression(&paren.expression, injections);
        }
        Expression::ConditionalExpression(cond) => {
            find_clone_element_in_expression(&cond.consequent, injections);
            find_clone_element_in_expression(&cond.alternate, injections);
        }
        Expression::LogicalExpression(logical) => {
            find_clone_element_in_expression(&logical.left, injections);
            find_clone_element_in_expression(&logical.right, injections);
        }
        Expression::JSXElement(el) => {
            // Recurse into JSX children and attributes
            for child in &el.children {
                find_clone_element_in_jsx_child(child, injections);
            }
            for attr in &el.opening_element.attributes {
                if let JSXAttributeItem::Attribute(attr) = attr {
                    if let Some(JSXAttributeValue::ExpressionContainer(container)) = &attr.value {
                        if let Some(expr) = container.expression.as_expression() {
                            find_clone_element_in_expression(expr, injections);
                        }
                    }
                }
            }
        }
        _ => {}
    }
}

fn find_clone_element_in_jsx_child<'a>(
    child: &'a JSXChild<'a>,
    injections: &mut Vec<CloneElementInjection>,
) {
    match child {
        JSXChild::ExpressionContainer(container) => {
            if let Some(expr) = container.expression.as_expression() {
                find_clone_element_in_expression(expr, injections);
            }
        }
        JSXChild::Element(el) => {
            for child in &el.children {
                find_clone_element_in_jsx_child(child, injections);
            }
            for attr in &el.opening_element.attributes {
                if let JSXAttributeItem::Attribute(attr) = attr {
                    if let Some(JSXAttributeValue::ExpressionContainer(container)) = &attr.value {
                        if let Some(expr) = container.expression.as_expression() {
                            find_clone_element_in_expression(expr, injections);
                        }
                    }
                }
            }
        }
        _ => {}
    }
}

// ── cloneElement extraction ────────────────────────────────────────────

/// Check if a call expression is `cloneElement(child, { ... })` and extract
/// the injected prop names from the second argument.
///
/// Public so it can be called from the main AST walk in `extract_source_info`
/// without requiring a separate full parse.
pub fn try_extract_clone_element_from_call(call: &CallExpression) -> Option<CloneElementInjection> {
    try_extract_clone_element(call)
}

fn try_extract_clone_element(call: &CallExpression) -> Option<CloneElementInjection> {
    // Match `cloneElement(...)` or `React.cloneElement(...)`
    let is_clone_element = match &call.callee {
        Expression::Identifier(id) => id.name == "cloneElement",
        Expression::StaticMemberExpression(member) => {
            member.property.name == "cloneElement"
                && matches!(&member.object, Expression::Identifier(id) if id.name == "React")
        }
        _ => false,
    };

    if !is_clone_element {
        return None;
    }

    // Need at least 2 arguments: cloneElement(element, props)
    if call.arguments.len() < 2 {
        return None;
    }

    // Extract prop names from the second argument (object expression)
    let props_arg = call.arguments[1].as_expression()?;
    let prop_names = extract_object_prop_names(props_arg);

    if prop_names.is_empty() {
        return None;
    }

    Some(CloneElementInjection {
        injected_props: prop_names,
    })
}

/// Extract property names from an object expression.
///
/// Handles:
/// - `{ rowid }` → ["rowid"] (shorthand)
/// - `{ rowid: ariaLabelledBy }` → ["rowid"]
/// - `{ isDisabled: true }` → ["isDisabled"]
/// - `{ ...spread }` → skipped (not a specific prop injection)
/// - `condition ? { isDisabled: true } : {}` → ["isDisabled"] (conditional)
fn extract_object_prop_names(expr: &Expression) -> Vec<String> {
    match expr {
        Expression::ObjectExpression(obj) => {
            let mut names = Vec::new();
            for prop in &obj.properties {
                match prop {
                    ObjectPropertyKind::ObjectProperty(p) => {
                        if let Some(name) = property_key_name(&p.key) {
                            // Skip standard HTML/ARIA attributes — these are
                            // generic attribute injections, not structural coupling
                            if !name.starts_with("aria-")
                                && !name.starts_with("data-")
                                && name != "className"
                                && name != "style"
                                && name != "ref"
                                && name != "key"
                            {
                                names.push(name);
                            }
                        }
                    }
                    ObjectPropertyKind::SpreadProperty(_) => {
                        // Spread props don't tell us specific prop names
                    }
                }
            }
            names
        }
        // Handle conditional: areAllGroupsDisabled ? { isDisabled: true } : {}
        Expression::ConditionalExpression(cond) => {
            let mut names = extract_object_prop_names(&cond.consequent);
            let alt_names = extract_object_prop_names(&cond.alternate);
            for name in alt_names {
                if !names.contains(&name) {
                    names.push(name);
                }
            }
            names
        }
        Expression::ParenthesizedExpression(paren) => extract_object_prop_names(&paren.expression),
        _ => Vec::new(),
    }
}

/// Get the string name from a property key.
fn property_key_name(key: &PropertyKey) -> Option<String> {
    match key {
        PropertyKey::StaticIdentifier(id) => Some(id.name.to_string()),
        PropertyKey::StringLiteral(s) => Some(s.value.to_string()),
        _ => None,
    }
}

// ── Tests ──────────────────────────────────────────────────────────────

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

    #[test]
    fn test_simple_clone_element() {
        let source = r#"
            const DataListItem = ({ children, rowid }: Props) => {
                return (
                    <li>
                        {Children.map(children, (child) =>
                            isValidElement(child) &&
                            cloneElement(child, { rowid: ariaLabelledBy })
                        )}
                    </li>
                );
            };
        "#;
        let injections = detect_clone_element_injections(source);
        assert_eq!(injections.len(), 1);
        assert_eq!(injections[0].injected_props, vec!["rowid"]);
    }

    #[test]
    fn test_clone_element_shorthand() {
        // DataListItemRow pattern: cloneElement(child, { rowid })
        let source = r#"
            const DataListItemRow = ({ children, rowid }: Props) => {
                return (
                    <div>
                        {Children.map(children, (child) =>
                            isValidElement(child) &&
                            cloneElement(child, { rowid })
                        )}
                    </div>
                );
            };
        "#;
        let injections = detect_clone_element_injections(source);
        assert_eq!(injections.len(), 1);
        assert_eq!(injections[0].injected_props, vec!["rowid"]);
    }

    #[test]
    fn test_clone_element_conditional() {
        // ToggleGroup pattern: conditional injection
        let source = r#"
            const ToggleGroup = ({ children, areAllGroupsDisabled }: Props) => {
                return (
                    <div>
                        {Children.map(children, (child) =>
                            child.type === ToggleGroupItem
                                ? cloneElement(child, areAllGroupsDisabled ? { isDisabled: true } : {})
                                : child
                        )}
                    </div>
                );
            };
        "#;
        let injections = detect_clone_element_injections(source);
        assert_eq!(injections.len(), 1);
        assert_eq!(injections[0].injected_props, vec!["isDisabled"]);
    }

    #[test]
    fn test_clone_element_multiple_props() {
        // JumpLinks pattern: multiple props injected
        let source = r#"
            const JumpLinks = ({ children }: Props) => {
                return (
                    <nav>
                        {Children.map(children, (child, i) =>
                            cloneElement(child, {
                                onClick(ev) { handleClick(ev, i); },
                                isActive: activeIndex === i,
                            })
                        )}
                    </nav>
                );
            };
        "#;
        let injections = detect_clone_element_injections(source);
        assert_eq!(injections.len(), 1);
        assert!(injections[0]
            .injected_props
            .contains(&"onClick".to_string()));
        assert!(injections[0]
            .injected_props
            .contains(&"isActive".to_string()));
    }

    #[test]
    fn test_clone_element_breadcrumb() {
        // Breadcrumb: injects showDivider
        let source = r#"
            const Breadcrumb = ({ children }: Props) => {
                return (
                    <nav>
                        <ol>
                            {Children.map(children, (child, i) =>
                                cloneElement(child, { showDivider: i > 0 })
                            )}
                        </ol>
                    </nav>
                );
            };
        "#;
        let injections = detect_clone_element_injections(source);
        assert_eq!(injections.len(), 1);
        assert_eq!(injections[0].injected_props, vec!["showDivider"]);
    }

    #[test]
    fn test_no_clone_element() {
        let source = r#"
            const Simple = ({ children }: Props) => {
                return <div>{children}</div>;
            };
        "#;
        let injections = detect_clone_element_injections(source);
        assert!(injections.is_empty());
    }

    #[test]
    fn test_clone_element_skips_aria_attrs() {
        // Tooltip pattern: injects only aria attributes — should be filtered
        let source = r#"
            const Tooltip = ({ children }: Props) => {
                return cloneElement(children, { 'aria-describedby': id });
            };
        "#;
        let injections = detect_clone_element_injections(source);
        // aria-describedby is filtered out, so no injection recorded
        assert!(injections.is_empty());
    }

    #[test]
    fn test_clone_element_react_dot_clone() {
        // React.cloneElement form
        let source = r#"
            const Parent = ({ children }: Props) => {
                return (
                    <div>
                        {Children.map(children, child =>
                            React.cloneElement(child, { isActive: true })
                        )}
                    </div>
                );
            };
        "#;
        let injections = detect_clone_element_injections(source);
        assert_eq!(injections.len(), 1);
        assert_eq!(injections[0].injected_props, vec!["isActive"]);
    }

    #[test]
    fn test_clone_element_in_class_component() {
        let source = r#"
            class DataListItem extends Component {
                render() {
                    const { children } = this.props;
                    return (
                        <li>
                            {Children.map(children, (child) =>
                                cloneElement(child, { rowid: this.props.id })
                            )}
                        </li>
                    );
                }
            }
        "#;
        let injections = detect_clone_element_injections(source);
        assert_eq!(injections.len(), 1);
        assert_eq!(injections[0].injected_props, vec!["rowid"]);
    }
}