Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
chore: code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Nov 22, 2022
1 parent 456c42e commit 91cb65f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 49 deletions.
3 changes: 2 additions & 1 deletion crates/rome_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ fn update_suppression<L: Language>(
})
}

/// Convenient type that to mark a function that is responsible to create a mutation to add a suppression comment.
/// Payload received by the function responsible to mark a suppression comment
pub struct SuppressionCommentEmitterPayload<'a, L: Language> {
/// The possible offset found in the [TextRange] of the emitted diagnostic
pub token_offset: TokenAtOffset<SyntaxToken<L>>,
Expand All @@ -696,6 +696,7 @@ pub struct SuppressionCommentEmitterPayload<'a, L: Language> {
pub diagnostic_text_range: &'a TextRange,
}

/// Convenient type that to mark a function that is responsible to create a mutation to add a suppression comment.
type SuppressionCommentEmitter<L> = fn(SuppressionCommentEmitterPayload<L>);

type SignalHandler<'a, L, Break> = &'a mut dyn FnMut(&dyn AnalyzerSignal<L>) -> ControlFlow<Break>;
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_analyze/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ pub trait Rule: RuleMeta + Sized {
}

/// Create a code action that allows to suppress the rule. The function
/// has to return the node to which the suppression comment needs to be applied.
/// returns the node to which the suppression comment is applied.
fn suppress(
ctx: &RuleContext<Self>,
text_range: &TextRange,
Expand Down
13 changes: 10 additions & 3 deletions crates/rome_analyze/src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where

fn actions(&self) -> AnalyzerActionIter<L> {
if let Some(action) = (self.action)() {
AnalyzerActionIter::new(vec![action])
AnalyzerActionIter::new([action])
} else {
AnalyzerActionIter::new(vec![])
}
Expand Down Expand Up @@ -141,9 +141,16 @@ impl<L: Language> From<AnalyzerAction<L>> for CodeSuggestionItem {
}

impl<L: Language> AnalyzerActionIter<L> {
pub fn new(actions: Vec<AnalyzerAction<L>>) -> Self {
pub fn new<I>(actions: I) -> Self
where
I: IntoIterator<Item = AnalyzerAction<L>>,
I::IntoIter: ExactSizeIterator,
{
Self {
analyzer_actions: actions.into_iter(),
analyzer_actions: actions
.into_iter()
.collect::<Vec<AnalyzerAction<L>>>()
.into_iter(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ fn apply_suppression_comment(payload: SuppressionCommentEmitterPayload<JsLanguag
&JsxAnyChild::JsxExpressionChild(jsx_comment.build()),
);
} else {
let new_token = current_token.with_leading_trivia(vec![
let new_token = current_token.with_leading_trivia([
(TriviaPieceKind::Newline, "\n"),
(
TriviaPieceKind::SingleLineComment,
Expand Down
68 changes: 25 additions & 43 deletions crates/rome_js_analyze/src/utils/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,31 +214,22 @@ impl JsBatchMutation for BatchMutation<JsLanguage> {
after_element: &JsxAnyChild,
new_element: &JsxAnyChild,
) -> bool {
let old_list = after_element.syntax().parent().and_then(JsxChildList::cast);
if let Some(old_list) = old_list {
let jsx_child_list = after_element
.syntax()
.parent()
.map(|parent| {
let list = JsxChildList::cast(parent)?;
let mut new_items = vec![];
for element in list {
new_items.push(element.clone());
if element == *after_element {
new_items.push(new_element.clone());
}
let old_list = after_element.parent::<JsxChildList>();
if let Some(old_list) = &old_list {
let jsx_child_list = {
let mut new_items = vec![];
for element in old_list {
new_items.push(element.clone());
if element == *after_element {
new_items.push(new_element.clone());
}
}

Some(jsx_child_list(new_items))
})
.unwrap_or(None);
jsx_child_list(new_items)
};

if let Some(jsx_child_list) = jsx_child_list {
self.replace_node(old_list, jsx_child_list);
true
} else {
false
}
self.replace_node(old_list.clone(), jsx_child_list);
true
} else {
false
}
Expand All @@ -250,30 +241,21 @@ impl JsBatchMutation for BatchMutation<JsLanguage> {
new_element: &JsxAnyChild,
) -> bool {
let old_list = after_element.syntax().parent().and_then(JsxChildList::cast);
if let Some(old_list) = old_list {
let jsx_child_list = after_element
.syntax()
.parent()
.map(|parent| {
let list = JsxChildList::cast(parent)?;
let mut new_items = vec![];
for element in list {
if element == *after_element {
new_items.push(new_element.clone());
}
new_items.push(element.clone());
if let Some(old_list) = &old_list {
let jsx_child_list = {
let mut new_items = vec![];
for element in old_list {
if element == *after_element {
new_items.push(new_element.clone());
}
new_items.push(element.clone());
}

Some(jsx_child_list(new_items))
})
.unwrap_or(None);
jsx_child_list(new_items)
};

if let Some(jsx_child_list) = jsx_child_list {
self.replace_node(old_list, jsx_child_list);
true
} else {
false
}
self.replace_node(old_list.clone(), jsx_child_list);
true
} else {
false
}
Expand Down

0 comments on commit 91cb65f

Please sign in to comment.