Fix captures (#4421)

* Fix rowcondition and import captures

* Only check extra blocks if not yet seen
This commit is contained in:
JT 2022-02-11 07:37:10 -05:00 committed by GitHub
parent e16d6ae00c
commit 886ed5ab2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 3 deletions

View File

@ -3944,8 +3944,16 @@ pub fn discover_captures_in_expr(
}
Expr::RowCondition(block_id) | Expr::Subexpression(block_id) => {
let block = working_set.get_block(*block_id);
let result = discover_captures_in_block(working_set, block, seen, seen_blocks);
output.extend(&result);
let results = {
let mut seen = vec![];
discover_captures_in_block(working_set, block, &mut seen, seen_blocks)
};
seen_blocks.insert(*block_id, results.clone());
for var_id in results.into_iter() {
if !seen.contains(&var_id) {
output.push(var_id)
}
}
}
Expr::Table(headers, values) => {
for header in headers {
@ -4066,6 +4074,17 @@ pub fn parse(
let captures = discover_captures_in_block(working_set, &output, &mut seen, &mut seen_blocks);
output.captures = captures;
// Also check other blocks that might have been imported
for (block_idx, block) in working_set.delta.blocks.iter().enumerate() {
let block_id = block_idx + working_set.permanent_state.num_blocks();
if !seen_blocks.contains_key(&block_id) {
let captures =
discover_captures_in_block(working_set, block, &mut seen, &mut seen_blocks);
seen_blocks.insert(block_id, captures);
}
}
for (block_id, captures) in seen_blocks.into_iter() {
// In theory, we should only be updating captures where we have new information
// the only place where this is possible would be blocks that are newly created

View File

@ -607,7 +607,7 @@ pub struct StateDelta {
pub(crate) file_contents: Vec<(Vec<u8>, usize, usize)>,
vars: Vec<Type>, // indexed by VarId
decls: Vec<Box<dyn Command>>, // indexed by DeclId
blocks: Vec<Block>, // indexed by BlockId
pub blocks: Vec<Block>, // indexed by BlockId
overlays: Vec<Overlay>, // indexed by OverlayId
pub scope: Vec<ScopeFrame>,
#[cfg(feature = "plugin")]

View File

@ -296,3 +296,11 @@ def 'say-hi' [] {
"HelloHello",
)
}
#[test]
fn capture_row_condition() -> TestResult {
run_test(
r#"let name = "foo"; [foo] | where $'($name)' =~ $it | str collect"#,
"foo",
)
}