Skip to content

Commit

Permalink
refactor: ErasedSegment::children to return an iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
gvozdvmozgu authored and benfdking committed Nov 3, 2024
1 parent ec21518 commit 71247b2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
15 changes: 7 additions & 8 deletions crates/lib-core/src/parser/segments/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,13 @@ impl ErasedSegment {
.clone()
}

pub fn children(&self, seg_types: &SyntaxSet) -> Vec<ErasedSegment> {
let mut buff = Vec::new();
for seg in self.segments() {
if seg_types.contains(seg.get_type()) {
buff.push(seg.clone());
}
}
buff
pub fn children<'set>(
&'set self,
seg_types: &'set SyntaxSet,
) -> impl Iterator<Item = &ErasedSegment> + '_ {
self.segments()
.iter()
.filter(move |seg| seg_types.contains(seg.get_type()))
}

pub fn iter_patches(&self, templated_file: &TemplatedFile) -> Vec<FixPatch> {
Expand Down
6 changes: 3 additions & 3 deletions crates/lib-core/src/parser/segments/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ impl FromClauseSegment {
);
}

for clause in &direct_table_children {
for &clause in &direct_table_children {
let tmp;

let alias = FromExpressionElementSegment(clause.clone()).eventual_alias();

let table_expr = if direct_table_children.contains(clause) {
let table_expr = if direct_table_children.contains(&clause) {
clause
} else {
tmp = clause
Expand All @@ -45,7 +45,7 @@ impl FromClauseSegment {
}

for clause in join_clauses {
let aliases = JoinClauseSegment(clause).eventual_aliases();
let aliases = JoinClauseSegment(clause.clone()).eventual_aliases();

if !aliases.is_empty() {
buff.extend(aliases);
Expand Down
2 changes: 1 addition & 1 deletion crates/lib-core/src/utils/analysis/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl<T: Default + Clone> Query<'_, T> {
selectables.extend(
segment
.children(const { &SyntaxSet::new(&[SyntaxKind::SelectStatement]) })
.into_iter()
.cloned()
.map(|selectable| Selectable {
selectable,
dialect,
Expand Down
5 changes: 2 additions & 3 deletions crates/lib-core/src/utils/analysis/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pub fn get_select_statement_info(
let select_targets =
select_clause.children(const { &SyntaxSet::new(&[SyntaxKind::SelectClauseElement]) });
let select_targets = select_targets
.iter()
.map(|it| SelectClauseElementSegment(it.clone()))
.collect_vec();

Expand Down Expand Up @@ -257,8 +256,8 @@ fn get_lambda_argument_columns(segment: &ErasedSegment, dialect: Option<&Dialect
.unwrap();

if start_bracket.raw() == "(" {
let bracketed_arguments =
child_segment.children(&SyntaxSet::single(SyntaxKind::ColumnReference));
let bracketed_arguments = child_segment
.children(const { &SyntaxSet::single(SyntaxKind::ColumnReference) });

lambda_argument_columns.extend(
bracketed_arguments
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/aliasing/al08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ FROM
None,
))
}
Entry::Vacant(entry) => _ = entry.insert(clause_element),
Entry::Vacant(entry) => _ = entry.insert(clause_element.clone()),
};
}

Expand Down
11 changes: 6 additions & 5 deletions crates/lib/src/rules/structure/st06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ from x
}

let select_clause_segment = context.segment.clone();
let select_target_elements = select_clause_segment
.children(const { &SyntaxSet::new(&[SyntaxKind::SelectClauseElement]) });
let select_target_elements: Vec<_> = select_clause_segment
.children(const { &SyntaxSet::new(&[SyntaxKind::SelectClauseElement]) })
.collect();

if select_target_elements.is_empty() {
return Vec::new();
Expand All @@ -132,7 +133,7 @@ from x
.collect();
seen_band_elements.push(Vec::new());

for segment in &select_target_elements {
for &segment in &select_target_elements {
let mut current_element_band: Option<usize> = None;

for (i, band) in enumerate(SELECT_ELEMENT_ORDER_PREFERENCE) {
Expand Down Expand Up @@ -226,10 +227,10 @@ from x
let fixes = zip(select_target_elements, ordered_select_target_elements)
.filter_map(
|(initial_select_target_element, replace_select_target_element)| {
(initial_select_target_element != replace_select_target_element).then(
(initial_select_target_element != &replace_select_target_element).then(
|| {
LintFix::replace(
initial_select_target_element,
initial_select_target_element.clone(),
vec![replace_select_target_element],
None,
)
Expand Down
5 changes: 3 additions & 2 deletions crates/lib/src/rules/structure/st09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ left join bar
let comparison_operator = subcondition[1].clone();
let first_column_reference = subcondition[0].clone();
let second_column_reference = subcondition[2].clone();
let raw_comparison_operators = comparison_operator
.children(const { &SyntaxSet::new(&[SyntaxKind::RawComparisonOperator]) });
let raw_comparison_operators: Vec<_> = comparison_operator
.children(const { &SyntaxSet::new(&[SyntaxKind::RawComparisonOperator]) })
.collect();
let first_table_seg = first_column_reference
.child(
const {
Expand Down

0 comments on commit 71247b2

Please sign in to comment.