Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(span): all methods take owned Span #8297

Merged
merged 1 commit into from
Jan 18, 2025
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: 1 addition & 1 deletion crates/oxc_linter/src/fixer/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl GetSpan for CompositeFix<'_> {
match self {
CompositeFix::Single(fix) => fix.span,
CompositeFix::Multiple(fixes) => {
fixes.iter().map(|fix| fix.span).reduce(|a, b| a.merge(&b)).unwrap_or(SPAN)
fixes.iter().map(|fix| fix.span).reduce(Span::merge).unwrap_or(SPAN)
}
CompositeFix::None => SPAN,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Rule for NoConsole {
&& !self.allow.iter().any(|s| mem.static_property_name().is_some_and(|f| f == s))
{
if let Some((mem_span, _)) = mem.static_property_info() {
let diagnostic_span = ident.span().merge(&mem_span);
let diagnostic_span = ident.span().merge(mem_span);
ctx.diagnostic_with_suggestion(no_console_diagnostic(diagnostic_span), |fixer| {
remove_console(fixer, ctx, node)
});
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/sort_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl SortImports {

if is_fixable {
// Safe to index because we know that `specifiers` is at least 2 element long
let specifiers_span = specifiers[0].span.merge(&specifiers[specifiers.len() - 1].span);
let specifiers_span = specifiers[0].span.merge(specifiers[specifiers.len() - 1].span);
ctx.diagnostic_with_fix(
sort_members_alphabetically_diagnostic(unsorted_name, unsorted_span),
|fixer| {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jsdoc/require_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Rule for RequireProperty {

let r#type = type_part.parsed();
if r#type == "Object" || r#type == "object" || r#type == "PlainObject" {
should_report = Some(tag.kind.span.merge(&type_part.span));
should_report = Some(tag.kind.span.merge(type_part.span));
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/react/no_render_return_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Rule for NoRenderReturnValue {
| AstKind::AssignmentExpression(_)
) {
ctx.diagnostic(no_render_return_value_diagnostic(
ident.span.merge(&property_span),
ident.span.merge(property_span),
));
}

Expand All @@ -80,7 +80,7 @@ impl Rule for NoRenderReturnValue {
{
if e.expression {
ctx.diagnostic(no_render_return_value_diagnostic(
ident.span.merge(&property_span),
ident.span.merge(property_span),
));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/typescript/prefer_for_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl Rule for PreferForOf {
return;
}

let span = for_stmt_init.span.merge(&test_expr.span).merge(&update_expr.span());
let span = for_stmt_init.span.merge(test_expr.span).merge(update_expr.span());
ctx.diagnostic(prefer_for_of_diagnostic(span));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Rule for PreferQuerySelector {
}
_ => literal_value.to_string(),
};
let span = property_span.merge(&argument_expr.span());
let span = property_span.merge(argument_expr.span());
fixer.replace(
span,
format!("{preferred_selector}({quotes_symbol}{argument}{quotes_symbol}"),
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_parser/src/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ impl GetSpan for Modifiers<'_> {
debug_assert!(!modifiers.is_empty());
// SAFETY: One of Modifier's invariants is that Some(modifiers) always
// contains a non-empty Vec; otherwise it must be `None`.

unsafe { modifiers.iter().map(|m| m.span).reduce(|a, b| a.merge(&b)).unwrap_unchecked() }
unsafe { modifiers.iter().map(|m| m.span).reduce(Span::merge).unwrap_unchecked() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ impl<'a> PatternParser<'a> {

body.push(ast::CharacterClassContents::CharacterClassRange(Box::new_in(
ast::CharacterClassRange {
span: from.span.merge(&to.span),
span: from.span.merge(to.span),
min: **from,
max: **to,
},
Expand Down Expand Up @@ -1236,15 +1236,15 @@ impl<'a> PatternParser<'a> {
// It is a Syntax Error if the CharacterValue of the first ClassSetCharacter is strictly greater than the CharacterValue of the second ClassSetCharacter.
if class_set_character_to.value < class_set_character.value {
return Err(diagnostics::character_class_range_out_of_order(
class_set_character.span.merge(&class_set_character_to.span),
class_set_character.span.merge(class_set_character_to.span),
"class set",
));
}

return Ok(Some(ast::CharacterClassContents::CharacterClassRange(
Box::new_in(
ast::CharacterClassRange {
span: class_set_character.span.merge(&class_set_character_to.span),
span: class_set_character.span.merge(class_set_character_to.span),
min: class_set_character,
max: class_set_character_to,
},
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct JSDocTag<'a> {

impl<'a> JSDocTag<'a> {
pub fn new(kind: JSDocTagKindPart<'a>, body_content: &'a str, body_span: Span) -> JSDocTag<'a> {
Self { span: kind.span.merge(&body_span), kind, body_raw: body_content, body_span }
Self { span: kind.span.merge(body_span), kind, body_raw: body_content, body_span }
}

/// Use for various simple tags like `@access`, `@deprecated`, ...etc.
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_span/src/span/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Span {
/// assert_eq!(Span::new(0, 5).size(), 5);
/// assert_eq!(Span::new(5, 10).size(), 5);
/// ```
pub const fn size(&self) -> u32 {
pub const fn size(self) -> u32 {
debug_assert!(self.start <= self.end);
self.end - self.start
}
Expand All @@ -92,7 +92,7 @@ impl Span {
/// assert!(Span::new(5, 5).is_empty());
/// assert!(!Span::new(0, 5).is_empty());
/// ```
pub const fn is_empty(&self) -> bool {
pub const fn is_empty(self) -> bool {
debug_assert!(self.start <= self.end);
self.start == self.end
}
Expand All @@ -108,7 +108,7 @@ impl Span {
/// assert!(!Span::new(0, 5).is_unspanned());
/// assert!(!Span::new(5, 5).is_unspanned());
/// ```
pub const fn is_unspanned(&self) -> bool {
pub const fn is_unspanned(self) -> bool {
self.start == SPAN.start && self.end == SPAN.end
}

Expand Down Expand Up @@ -148,7 +148,7 @@ impl Span {
/// assert_eq!(merged_span, Span::new(0, 8));
/// ```
#[must_use]
pub fn merge(&self, other: &Self) -> Self {
pub fn merge(self, other: Self) -> Self {
Self::new(self.start.min(other.start), self.end.max(other.end))
}

Expand Down Expand Up @@ -324,7 +324,7 @@ impl Span {
/// let name = name_span.source_text(source);
/// assert_eq!(name_span.size(), name.len() as u32);
/// ```
pub fn source_text<'a>(&self, source_text: &'a str) -> &'a str {
pub fn source_text(self, source_text: &str) -> &str {
&source_text[self.start as usize..self.end as usize]
}

Expand Down
Loading