refactor(span): disallow struct expression constructor for Span (#2625)

This commit is contained in:
Boshen 2024-03-06 15:28:59 +08:00 committed by GitHub
parent 7605cd3ecf
commit 798a6dfe46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 80 additions and 90 deletions

View file

@ -386,8 +386,8 @@ mod test {
fn sort_no_fix_messages_correctly() {
let result = get_fix_result(vec![
create_message(ReplaceId, Some(REPLACE_ID)),
Message::new(NoFix2(Span { start: 1, end: 7 }).into(), None),
Message::new(NoFix1(Span { start: 1, end: 3 }).into(), None),
Message::new(NoFix2(Span::new(1, 7)).into(), None),
Message::new(NoFix1(Span::new(1, 3)).into(), None),
]);
assert_eq!(result.fixed_code, TEST_CODE.replace("answer", "foo"));
assert_eq!(result.messages.len(), 2);

View file

@ -108,7 +108,7 @@ impl Rule for Eqeqeq {
|| {
let start = binary_expr.left.span().end;
let end = binary_expr.right.span().start;
Fix::new(preferred_operator_with_padding, Span { start, end })
Fix::new(preferred_operator_with_padding, Span::new(start, end))
},
);
} else {

View file

@ -80,7 +80,7 @@ impl NoRegexSpaces {
let start = literal.span.start + u32::try_from(idx_start).unwrap() + 1;
let end = literal.span.start + u32::try_from(idx_end).unwrap() + 2;
return Some(Span { start, end });
return Some(Span::new(start, end));
}
None
@ -104,7 +104,7 @@ impl NoRegexSpaces {
let start = pattern.span.start + u32::try_from(idx_start).unwrap() + 1;
let end = pattern.span.start + u32::try_from(idx_end).unwrap() + 2;
return Some(Span { start, end });
return Some(Span::new(start, end));
}
}

View file

@ -63,10 +63,10 @@ impl Rule for NoVoid {
};
if unary_expr.operator == UnaryOperator::Void {
ctx.diagnostic(NoVoidDiagnostic(Span {
start: unary_expr.span.start,
end: unary_expr.span.start + 4,
}));
ctx.diagnostic(NoVoidDiagnostic(Span::new(
unary_expr.span.start,
unary_expr.span.start + 4,
)));
}
}
}

View file

@ -72,7 +72,8 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
if let Some(method_name) = BadAliasMethodName::from_str(alias.as_ref()) {
let (name, canonical_name) = method_name.name_with_canonical();
let Span { mut start, mut end } = matcher.span;
let mut start = matcher.span.start;
let mut end = matcher.span.end;
// expect(a).not['toThrowError']()
// matcher is the node of `toThrowError`, we only what to replace the content in the quotes.
if matcher.element.is_string_literal() {
@ -82,7 +83,7 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
ctx.diagnostic_with_fix(
NoAliasMethodsDiagnostic(name, canonical_name, matcher.span),
|| Fix::new(canonical_name, Span { start, end }),
|| Fix::new(canonical_name, Span::new(start, end)),
);
}
}

View file

@ -78,7 +78,7 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
if name.starts_with('f') {
ctx.diagnostic_with_fix(NoFocusedTestsDiagnostic(call_expr.span), || {
let start = call_expr.span.start;
Fix::delete(Span { start, end: start + 1 })
Fix::delete(Span::new(start, start + 1))
});
return;
@ -94,7 +94,7 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
} else {
span.end + 1
};
Fix::delete(Span { start, end })
Fix::delete(Span::new(start, end))
});
}
}

View file

@ -123,10 +123,7 @@ impl NoRestrictedMatchers {
.collect::<Vec<_>>()
.join(".");
let span = Span {
start: members.first().unwrap().span.start,
end: members.last().unwrap().span.end,
};
let span = Span::new(members.first().unwrap().span.start, members.last().unwrap().span.end);
for (restriction, message) in &self.restricted_matchers {
if Self::check_restriction(chain_call.as_str(), restriction.as_str()) {

View file

@ -120,10 +120,10 @@ fn check_test_return_statement<'a>(func_body: &OBox<'_, FunctionBody<'a>>, ctx:
return;
}
ctx.diagnostic(NoTestReturnStatementDiagnostic(Span {
start: return_stmt.span().start,
end: call_expr.span.start - 1,
}));
ctx.diagnostic(NoTestReturnStatementDiagnostic(Span::new(
return_stmt.span().start,
call_expr.span.start - 1,
)));
}
#[test]

View file

@ -155,7 +155,7 @@ impl PreferToHaveLength {
} else {
matcher.span.end
};
Fix::new(code, Span { start: call_expr.span.start, end: end - 1 })
Fix::new(code, Span::new(call_expr.span.start, end - 1))
});
}

View file

@ -133,9 +133,7 @@ fn is_empty_function(expr: &CallExpression) -> bool {
fn get_fix_content<'a>(expr: &'a CallExpression<'a>) -> (&'a str, Span) {
match &expr.callee {
Expression::Identifier(ident) => {
(".todo", Span { start: ident.span.end, end: ident.span.end })
}
Expression::Identifier(ident) => (".todo", Span::new(ident.span.end, ident.span.end)),
Expression::MemberExpression(mem_expr) => {
if let Some((span, _)) = mem_expr.static_property_info() {
return ("todo", span);

View file

@ -54,10 +54,10 @@ impl Rule for NoUnescapedEntities {
if let Some(escapes) = DEFAULTS.get(&char) {
#[allow(clippy::cast_possible_truncation)]
ctx.diagnostic(NoUnescapedEntitiesDiagnostic(
Span {
start: jsx_text.span.start + i as u32,
end: jsx_text.span.start + i as u32 + 1,
},
Span::new(
jsx_text.span.start + i as u32,
jsx_text.span.start + i as u32 + 1,
),
char,
escapes.join(" or "),
));

View file

@ -227,7 +227,7 @@ fn check_and_report_error_generic(
Fix::new(
array_type_identifier.to_string() + "<" + type_text + ">",
Span { start: type_reference_span.start, end: type_reference_span.end },
Span::new(type_reference_span.start, type_reference_span.end),
)
});
}

View file

@ -151,7 +151,7 @@ impl Rule for BanTsComment {
if *on {
ctx.diagnostic(BanTsCommentDiagnostic::Comment(
directive.to_string(),
Span { start: *start, end: comment.end() },
Span::new(*start, comment.end()),
));
}
}
@ -160,7 +160,7 @@ impl Rule for BanTsComment {
ctx.diagnostic(BanTsCommentDiagnostic::CommentRequiresDescription(
directive.to_string(),
self.minimum_description_length,
Span { start: *start, end: comment.end() },
Span::new(*start, comment.end()),
));
}
@ -170,7 +170,7 @@ impl Rule for BanTsComment {
BanTsCommentDiagnostic::CommentDescriptionNotMatchPattern(
directive.to_string(),
re.to_string(),
Span { start: *start, end: comment.end() },
Span::new(*start, comment.end()),
),
);
}

View file

@ -77,7 +77,7 @@ fn get_full_comment(source_text_len: usize, start: u32, end: u32, is_multi_line:
comment_end += 1;
}
Span { start: comment_start, end: comment_end }
Span::new(comment_start, comment_end)
}
#[test]

View file

@ -122,7 +122,7 @@ fn check_and_report(
ctx.diagnostic_with_fix(PreferAsConstDiagnostic(span), || {
let start = span.start;
let end = span.end;
Fix::new("const", Span { start, end })
Fix::new("const", Span::new(start, end))
});
} else {
ctx.diagnostic(PreferAsConstDiagnostic(span));

View file

@ -138,7 +138,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
|| {
Fix::new(
format!("type {type_name} = {suggestion};"),
Span { start: node_start, end: node_end },
Span::new(node_start, node_end),
)
},
);
@ -209,7 +209,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
&interface_decl.id.name,
&suggestion
),
Span { start: node_start, end: node_end },
Span::new(node_start, node_end),
);
}
@ -218,7 +218,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
"type {} = {};",
&interface_decl.id.name, &suggestion
),
Span { start: node_start, end: node_end },
Span::new(node_start, node_end),
)
},
);
@ -238,10 +238,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
|| {
Fix::new(
format!("({suggestion})"),
Span {
start: literal.span.start,
end: literal.span.end,
},
Span::new(literal.span.start, literal.span.end),
)
},
);
@ -254,7 +251,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
|| {
Fix::new(
suggestion.to_string(),
Span { start: literal.span.start, end: literal.span.end },
Span::new(literal.span.start, literal.span.end),
)
},
),
@ -280,10 +277,10 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
|| {
Fix::new(
format!("({suggestion})"),
Span {
start: literal.span.start,
end: literal.span.end,
},
Span::new(
literal.span.start,
literal.span.end,
),
)
},
);
@ -305,10 +302,10 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
|| {
Fix::new(
format!("({suggestion})"),
Span {
start: literal.span.start,
end: literal.span.end,
},
Span::new(
literal.span.start,
literal.span.end,
),
)
},
);
@ -322,7 +319,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
|| {
Fix::new(
suggestion.to_string(),
Span { start: literal.span.start, end: literal.span.end },
Span::new(literal.span.start, literal.span.end),
)
},
),

View file

@ -121,12 +121,12 @@ impl Rule for TripleSlashReference {
{
ctx.diagnostic(TripleSlashReferenceDiagnostic(
group2.to_string(),
Span { start: *start - 2, end: comment.end() },
Span::new(*start - 2, comment.end()),
));
}
if group1 == "types" && self.types == TypesOption::PreferImport {
refs_for_import.insert(group2, Span { start: *start - 2, end: comment.end() });
refs_for_import.insert(group2, Span::new(*start - 2, comment.end()));
}
}
}

View file

@ -37,7 +37,8 @@ impl Rule for EmptyBraceSpaces {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
AstKind::StaticBlock(static_block) => {
let Span { start, end } = static_block.span;
let start = static_block.span.start;
let end = static_block.span.end;
let static_leading_count = get_static_leading_count(static_block.span, ctx);
@ -75,8 +76,8 @@ impl Rule for EmptyBraceSpaces {
}
fn remove_empty_braces_spaces(ctx: &LintContext, is_empty_body: bool, span: Span) {
// dbg!(class);
let Span { start, end } = span;
let start = span.start;
let end = span.end;
if is_empty_body && end - start > 2 && !ctx.semantic().trivias().has_comments_between(span) {
// length of "{}"

View file

@ -138,7 +138,7 @@ fn report_diagnostic(
};
ctx.diagnostic_with_fix(NoConsoleSpacesDiagnostic(direction, ident.to_string(), span), || {
Fix::new(fix, Span { start, end })
Fix::new(fix, Span::new(start, end))
});
}

View file

@ -85,8 +85,8 @@ impl Rule for NoLonelyIf {
};
ctx.diagnostic(NoLonelyIfDiagnostic(
Span { start: if_stmt.span.start, end: if_stmt.span.start + 2 },
Span { start: parent_if_stmt_span.start, end: parent_if_stmt_span.start + 2 },
Span::new(if_stmt.span.start, if_stmt.span.start + 2),
Span::new(parent_if_stmt_span.start, parent_if_stmt_span.start + 2),
));
}
}

View file

@ -105,10 +105,7 @@ fn diagnose_variable_declarator(
&& matches!(parent_kind, Some(AstKind::VariableDeclaration(var_declaration)) if !var_declaration.kind.is_const() )
{
ctx.diagnostic_with_fix(RemoveNullDiagnostic(null_literal.span), || {
Fix::delete(Span {
start: variable_declarator.id.span().end,
end: null_literal.span.end,
})
Fix::delete(Span::new(variable_declarator.id.span().end, null_literal.span.end))
});
return;

View file

@ -143,7 +143,7 @@ fn check_useless_spread_in_list<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) {
return;
};
let span = Span { start: spread_elem.span.start, end: spread_elem.span.start + 3 };
let span = Span::new(spread_elem.span.start, spread_elem.span.start + 3);
match node.kind() {
AstKind::ObjectExpression(_) => {
@ -186,7 +186,7 @@ fn check_useless_iterable_to_array<'a>(
return;
};
let span = Span { start: spread_elem.span.start, end: spread_elem.span.start + 3 };
let span = Span::new(spread_elem.span.start, spread_elem.span.start + 3);
let parent = if let AstKind::Argument(_) = parent.kind() {
let Some(parent) = outermost_paren_parent(parent, ctx) else {
@ -298,7 +298,7 @@ fn check_useless_array_clone<'a>(array_expr: &ArrayExpression<'a>, ctx: &LintCon
return;
};
let span = Span { start: spread_elem.span.start, end: spread_elem.span.start + 3 };
let span = Span::new(spread_elem.span.start, spread_elem.span.start + 3);
if let Expression::CallExpression(call_expr) = &spread_elem.argument {
if !(is_method_call(

View file

@ -92,7 +92,7 @@ fn check_number_literal(
if number_literal.starts_with("0B") || number_literal.starts_with("0O") {
return Some((
NumberLiteralCaseDiagnostic::UppercasePrefix(
Span { start: raw_span.start + 1, end: raw_span.start + 2 },
Span::new(raw_span.start + 1, raw_span.start + 2),
if number_literal.starts_with("0B") { "0b" } else { "0o" },
),
number_literal.to_lowercase(),
@ -112,7 +112,7 @@ fn check_number_literal(
if has_uppercase_prefix {
return Some((
NumberLiteralCaseDiagnostic::UppercasePrefix(
Span { start: raw_span.start + 1, end: raw_span.start + 2 },
Span::new(raw_span.start + 1, raw_span.start + 2),
"0x",
),
"0x".to_owned() + &number_literal[2..],
@ -120,10 +120,10 @@ fn check_number_literal(
}
if has_lowercase_digits {
return Some((
NumberLiteralCaseDiagnostic::LowercaseHexadecimalDigits(Span {
start: raw_span.start + 2,
end: raw_span.end,
}),
NumberLiteralCaseDiagnostic::LowercaseHexadecimalDigits(Span::new(
raw_span.start + 2,
raw_span.end,
)),
"0x".to_owned() + &digits_to_uppercase(&number_literal[2..]),
));
}
@ -132,10 +132,10 @@ fn check_number_literal(
if let Some(index) = number_literal.find('E') {
let char_position = raw_span.start + index as u32;
return Some((
NumberLiteralCaseDiagnostic::UppercaseExponentialNotation(Span {
start: char_position,
end: char_position + 1,
}),
NumberLiteralCaseDiagnostic::UppercaseExponentialNotation(Span::new(
char_position,
char_position + 1,
)),
number_literal.to_lowercase(),
));
}

View file

@ -63,10 +63,10 @@ impl Rule for RequireArrayJoinSeparator {
&& !call_expr.optional
&& !matches!(member_expr, MemberExpression::ComputedMemberExpression(_))
{
ctx.diagnostic(RequireArrayJoinSeparatorDiagnostic(Span {
start: member_expr.span().end,
end: call_expr.span.end,
}));
ctx.diagnostic(RequireArrayJoinSeparatorDiagnostic(Span::new(
member_expr.span().end,
call_expr.span.end,
)));
}
// `[].join.call(foo)` and `Array.prototype.join.call(foo)`
@ -77,10 +77,10 @@ impl Rule for RequireArrayJoinSeparator {
&& !call_expr.arguments.iter().any(oxc_ast::ast::Argument::is_spread)
&& is_array_prototype_property(member_expr_obj, "join")
{
ctx.diagnostic(RequireArrayJoinSeparatorDiagnostic(Span {
start: member_expr.span().end,
end: call_expr.span.end,
}));
ctx.diagnostic(RequireArrayJoinSeparatorDiagnostic(Span::new(
member_expr.span().end,
call_expr.span.end,
)));
}
}
}

View file

@ -58,7 +58,7 @@ impl Rule for RequireNumberToFixedDigitsArgument {
if let Some(property_name) = member.static_property_name() {
if property_name == "toFixed" {
let parenthesis_span = Span { start: member.span().end, end: expr.span.end };
let parenthesis_span = Span::new(member.span().end, expr.span.end);
ctx.diagnostic_with_fix(
RequireNumberToFixedDigitsArgumentDiagnostic(parenthesis_span),

View file

@ -69,10 +69,8 @@ impl Rule for SwitchCaseBraces {
return;
};
let case_body_span = Span {
start: first_statement.span().start,
end: last_statement.span().end,
};
let case_body_span =
Span::new(first_statement.span().start, last_statement.span().end);
ctx.diagnostic_with_fix(SwitchCaseBracesDiagnostic(case_body_span), || {
use oxc_codegen::{Context, Gen};

View file

@ -15,6 +15,7 @@ pub const SPAN: Span = Span::new(0, 0);
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(all(feature = "serde", feature = "wasm"), derive(tsify::Tsify))]
#[non_exhaustive] // disallow struct expression constructor `Span {}`
pub struct Span {
pub start: u32,
pub end: u32,