Skip to content

Commit

Permalink
refactor(semantic): pass &str instead of Cow (#7972)
Browse files Browse the repository at this point in the history
As suggested by clippy...
  • Loading branch information
overlookmotel committed Dec 18, 2024
1 parent d4d7bc0 commit 6551dfe
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions crates/oxc_semantic/src/checker/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ pub fn check_ts_type_parameter_declaration(
}
}
/// '?' at the end of a type is not valid TypeScript syntax. Did you mean to write 'number | null | undefined'?(17019)
#[allow(clippy::needless_pass_by_value)]
fn jsdoc_type_in_annotation(
modifier: char,
is_start: bool,
span: Span,
suggested_type: Cow<str>,
suggested_type: &str,
) -> OxcDiagnostic {
let (code, start_or_end) = if is_start { ("17020", "start") } else { ("17019", "end") };

Expand All @@ -58,13 +57,19 @@ pub fn check_ts_type_annotation(annotation: &TSTypeAnnotation<'_>, ctx: &Semanti
span_with_illegal_modifier.shrink_right(1)
};

let suggestion = &ctx.source_text[valid_type_span];
let suggestion = if modifier == '?' {
Cow::Owned(format!("{} | null | undefined", &ctx.source_text[valid_type_span]))
Cow::Owned(format!("{suggestion} | null | undefined"))
} else {
Cow::Borrowed(&ctx.source_text[valid_type_span])
Cow::Borrowed(suggestion)
};

ctx.error(jsdoc_type_in_annotation(modifier, is_start, span_with_illegal_modifier, suggestion));
ctx.error(jsdoc_type_in_annotation(
modifier,
is_start,
span_with_illegal_modifier,
&suggestion,
));
}

/// Initializers are not allowed in ambient contexts. ts(1039)
Expand Down

0 comments on commit 6551dfe

Please sign in to comment.