Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/ripr/src/analysis/language/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ mod static_limit;
#[cfg(test)]
mod tests;
mod tests_extract;
#[cfg(test)]
mod tests_extract_tests;
pub(crate) mod tsconfig;
mod types;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fn contains_member_call_name(body_text: &str, object_name: &str, method_name: &s
///
/// - table-form test with `has_dynamic_matcher_arg = true` →
/// `typescript_table_case_unresolved`
/// Real producer: `tests_extract.rs::test_callee_is_each` accepts array-form
/// Real producer: `tests_extract.rs::call_callee_is_active_each_declaration` accepts array-form
/// `test.each(...)` / `it.each(...)` calls and stores the call source in
/// `TypeScriptTest::body_text`. When the matcher expected value is a row
/// variable, syntax-only preview evidence cannot bind the row to a concrete
Expand Down
98 changes: 59 additions & 39 deletions crates/ripr/src/analysis/language/typescript/tests_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

use super::*;

#[derive(Clone, Copy)]
enum TestDeclarationRoot {
Test,
Describe,
}

impl TestDeclarationRoot {
fn matches_identifier(self, name: &str) -> bool {
match self {
Self::Test => matches!(name, "test" | "it"),
Self::Describe => name == "describe",
}
}
}

pub(crate) fn extract_tests(file: &Path, source: &str) -> Vec<TypeScriptTest> {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source, source_type_for(file)).parse();
Expand Down Expand Up @@ -110,10 +125,9 @@ pub(crate) fn describe_body_from_statement<'a>(
let Expression::CallExpression(call) = &expr_stmt.expression else {
return None;
};
let Expression::Identifier(ident) = &call.callee else {
return None;
};
if ident.name.as_str() != "describe" {
if !call_callee_is_active_declaration(call, TestDeclarationRoot::Describe)
&& !call_callee_is_active_each_declaration(call, TestDeclarationRoot::Describe)
{
return None;
}
let name = string_argument(call.arguments.first()?)?;
Expand Down Expand Up @@ -153,31 +167,21 @@ pub(crate) fn test_name_and_assertions_from_call(
call: &oxc_ast::ast::CallExpression<'_>,
source: &str,
) -> Option<(String, Vec<TypeScriptAssertion>)> {
if test_callee_is_identifier(call) {
let name = string_argument(call.arguments.first()?)?;
let callback = call.arguments.get(1)?;
let receiver = test_callback_receiver_name(callback);
let assertions = function_body_statements_from_argument(callback)
.map(|statements| {
collect_expect_assertions_in_statements(statements, source, receiver.as_deref())
})
.unwrap_or_default();
return Some((name, assertions));
}

if test_callee_is_each(call) {
let name = string_argument(call.arguments.first()?)?;
let callback = call.arguments.get(1)?;
let receiver = test_callback_receiver_name(callback);
let assertions = function_body_statements_from_argument(callback)
.map(|statements| {
collect_expect_assertions_in_statements(statements, source, receiver.as_deref())
})
.unwrap_or_default();
return Some((name, assertions));
if !call_callee_is_active_declaration(call, TestDeclarationRoot::Test)
&& !call_callee_is_active_each_declaration(call, TestDeclarationRoot::Test)
{
return None;
}

None
let name = string_argument(call.arguments.first()?)?;
let callback = call.arguments.get(1)?;
let receiver = test_callback_receiver_name(callback);
let assertions = function_body_statements_from_argument(callback)
.map(|statements| {
collect_expect_assertions_in_statements(statements, source, receiver.as_deref())
})
.unwrap_or_default();
Some((name, assertions))
}

/// Extract the name bound to the test callback's first parameter.
Expand All @@ -198,27 +202,43 @@ fn test_callback_receiver_name(arg: &oxc_ast::ast::Argument<'_>) -> Option<Strin
super::owners::binding_identifier_name(&first.pattern).map(|name| name.to_string())
}

pub(crate) fn test_callee_is_identifier(call: &oxc_ast::ast::CallExpression<'_>) -> bool {
let Expression::Identifier(ident) = &call.callee else {
return false;
};
matches!(ident.name.as_str(), "test" | "it")
fn call_callee_is_active_declaration(
call: &oxc_ast::ast::CallExpression<'_>,
root: TestDeclarationRoot,
) -> bool {
expression_is_active_declaration(&call.callee, root)
}

pub(crate) fn test_callee_is_each(call: &oxc_ast::ast::CallExpression<'_>) -> bool {
fn call_callee_is_active_each_declaration(
call: &oxc_ast::ast::CallExpression<'_>,
root: TestDeclarationRoot,
) -> bool {
let Expression::CallExpression(each_call) = &call.callee else {
return false;
};
let Expression::StaticMemberExpression(member) = &each_call.callee else {
return false;
};
if member.property.name.as_str() != "each" {
return false;
member.property.name.as_str() == "each"
&& expression_is_active_declaration(&member.object, root)
Comment on lines +222 to +223

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve table-case limits for modified .each tests

Failure mode: when the new grammar extracts test.only.each(...) or it.concurrent.each(...) with a row-derived expected value, table_case_test in static_limit.rs still recognizes only bodies beginning with test.each( or it.each(. The test therefore receives the generic dynamic-assertion limitation but loses the more specific typescript_table_case_unresolved evidence, even though row binding remains unsupported. Fix direction: share the accepted .each grammar with table-case detection or store this fact during extraction, then add an output-level regression for a modified table test. Validation: run the focused TypeScript tests plus cargo xtask goldens check. Confidence: High, based on the direct predicate mismatch; compilation could not be run because the environment could not download oxc_allocator.

AGENTS.md reference: AGENTS.md:L262-L271

Useful? React with 👍 / 👎.

}

fn expression_is_active_declaration(
expression: &Expression<'_>,
root: TestDeclarationRoot,
) -> bool {
match expression {
Expression::Identifier(ident) => root.matches_identifier(ident.name.as_str()),
Expression::StaticMemberExpression(member) => {
is_active_declaration_modifier(member.property.name.as_str())
&& expression_is_active_declaration(&member.object, root)
}
_ => false,
}
let Expression::Identifier(ident) = &member.object else {
return false;
};
matches!(ident.name.as_str(), "test" | "it")
}

fn is_active_declaration_modifier(name: &str) -> bool {
matches!(name, "only" | "concurrent" | "sequential")
}

pub(crate) fn string_argument(arg: &oxc_ast::ast::Argument<'_>) -> Option<String> {
Expand Down
223 changes: 223 additions & 0 deletions crates/ripr/src/analysis/language/typescript/tests_extract_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
//! Focused extraction tests for active Jest/Vitest declaration forms.

use super::*;

#[test]
fn extracts_active_test_modifiers_with_assertions() {
let tests = extract_tests(
Path::new("tests/pricing.test.ts"),
r#"
test.only("focused", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
it.concurrent("parallel", () => {
expect(add(1, 2)).toBe(3);
});
test.sequential("serial", () => {
expect(normalize("x")).toBe("x");
});
"#,
);

assert_eq!(tests.len(), 3);
assert_eq!(tests[0].local_name, "focused");
assert_eq!(tests[1].local_name, "parallel");
assert_eq!(tests[2].local_name, "serial");
assert!(tests.iter().all(|test| test.assertions.len() == 1));
}

#[test]
fn recurses_active_describe_modifiers() {
let tests = extract_tests(
Path::new("tests/pricing.test.ts"),
r#"
describe.only("focused suite", () => {
test("focused case", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
});
describe.concurrent("parallel suite", () => {
it("parallel case", () => {
expect(add(1, 2)).toBe(3);
});
});
describe.sequential("serial suite", () => {
test("serial case", () => {
expect(normalize("x")).toBe("x");
});
});
"#,
);

assert_eq!(tests.len(), 3);
assert_eq!(tests[0].name, "focused suite focused case");
assert_eq!(tests[1].name, "parallel suite parallel case");
assert_eq!(tests[2].name, "serial suite serial case");
assert!(tests.iter().all(|test| test.describe_names.len() == 1));
}

#[test]
fn recurses_bare_and_modified_parameterized_suites() {
let tests = extract_tests(
Path::new("tests/pricing.test.ts"),
r#"
describe.each([[100], [150]])("amount %i", () => {
test("discount", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
});
describe.concurrent.each([[1], [2]])("parallel %i", () => {
it.only("adds", () => {
expect(add(1, 2)).toBe(3);
});
});
"#,
);

assert_eq!(tests.len(), 2);
assert_eq!(tests[0].name, "amount %i discount");
assert_eq!(tests[0].describe_names, vec!["amount %i".to_string()]);
assert_eq!(tests[1].name, "parallel %i adds");
assert_eq!(tests[1].describe_names, vec!["parallel %i".to_string()]);
assert!(tests.iter().all(|test| test.assertions.len() == 1));
}

#[test]
fn recognizes_active_modifier_chains_before_each_in_either_order() {
let tests = extract_tests(
Path::new("tests/pricing.test.ts"),
r#"
test.only.each([
[100, 90],
[150, 140],
])("discounts %#", (amount, expected) => {
expect(applyDiscount(amount, 100)).toBe(expected);
});
it.concurrent.only.each([
[1, 2, 3],
])("adds concurrently %#", (left, right, expected) => {
expect(add(left, right)).toBe(expected);
});
test.only.sequential.each([
["x", "x"],
])("normalizes sequentially %#", (value, expected) => {
expect(normalize(value)).toBe(expected);
});
"#,
);

assert_eq!(tests.len(), 3);
assert_eq!(tests[0].local_name, "discounts %#");
assert_eq!(tests[1].local_name, "adds concurrently %#");
assert_eq!(tests[2].local_name, "normalizes sequentially %#");
assert!(tests.iter().all(|test| test.assertions.len() == 1));
}

#[test]
fn keeps_repeated_and_mixed_active_modifier_chains_discoverable() {
let tests = extract_tests(
Path::new("tests/pricing.test.ts"),
r#"
test.only.only("repeated focus", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
test.concurrent.concurrent("repeated concurrent", () => {
expect(add(1, 2)).toBe(3);
});
test.concurrent.sequential("mixed execution flags", () => {
expect(add(2, 3)).toBe(5);
});
describe.sequential.concurrent("mixed suite flags", () => {
test("nested active", () => {
expect(normalize("x")).toBe("x");
});
});
"#,
);

assert_eq!(tests.len(), 4);
assert_eq!(tests[0].local_name, "repeated focus");
assert_eq!(tests[1].local_name, "repeated concurrent");
assert_eq!(tests[2].local_name, "mixed execution flags");
assert_eq!(tests[3].name, "mixed suite flags nested active");
assert!(tests.iter().all(|test| test.assertions.len() == 1));
}

#[test]
fn keeps_disabled_conditional_expected_failure_and_unknown_declarations_uncredited() {
let tests = extract_tests(
Path::new("tests/pricing.test.ts"),
r#"
test.skip("skipped", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
it.todo("todo");
describe.skip("disabled suite", () => {
test("nested", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
});
test.runIf(true)("conditional", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
test.skipIf(false)("conditional skip", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
test.fails("expected failure", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
runner.only("unknown root", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
test.retry("unknown modifier", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
test.only.skip("active then disabled", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
test.skip.only("disabled then active", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
describe.only.skip("active then disabled suite", () => {
test("nested", () => {
expect(applyDiscount(100, 100)).toBe(90);
});
});
"#,
);

assert!(tests.is_empty());
}

#[test]
fn extracted_active_test_reaches_direct_owner_relation() {
let tests = extract_tests(
Path::new("tests/pricing.test.ts"),
r#"
test.only("discount boundary", () => {
const result = applyDiscount(100, 100);
expect(result).toBe(90);
});
"#,
);
assert_eq!(tests.len(), 1);

let owner = TypeScriptOwner {
name: "applyDiscount".to_string(),
file: PathBuf::from("src/pricing.ts"),
start_line: 1,
end_line: 20,
owner_kind: OwnerKind::Function,
class_name: None,
decorated: false,
imports: Vec::new(),
};
let candidates = related_test_candidates(&owner, &tests, None, &ReExportIndex::empty(), None);

assert_eq!(candidates.len(), 1);
assert_eq!(
candidates[0].relation,
TypeScriptRelationKind::DirectOwnerCall
);
assert_eq!(candidates[0].test.name, "discount boundary");
}
Loading