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

feat(linter): add fixer for unicorn/no-length-as-slice-end #4780

Merged
merged 2 commits into from
Aug 9, 2024
Merged
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
32 changes: 25 additions & 7 deletions crates/oxc_linter/src/rules/unicorn/no_length_as_slice_end.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::{ast::MemberExpression, AstKind};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_span::{GetSpan, Span};

use crate::{
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_reference, AstNode,
@@ -37,7 +37,8 @@ declare_oxc_lint!(
/// foo.slice(1)
/// ```
NoLengthAsSliceEnd,
restriction
restriction,
fix
);

impl Rule for NoLengthAsSliceEnd {
@@ -76,10 +77,18 @@ impl Rule for NoLengthAsSliceEnd {
return;
}

ctx.diagnostic(no_length_as_slice_end_diagnostic(
call_expr.callee.get_member_expr().unwrap().static_property_info().unwrap().0,
second_argument.span,
));
ctx.diagnostic_with_fix(
no_length_as_slice_end_diagnostic(
call_expr.callee.get_member_expr().unwrap().static_property_info().unwrap().0,
second_argument.span,
),
|fixer| {
let start = call_expr.arguments[0].span().end;
let end = call_expr.arguments[1].span().end;
let span = Span::new(start, end);
fixer.delete(&span)
},
);
}
}

@@ -115,5 +124,14 @@ fn test() {
"foo?.slice(1, foo?.length)",
];

Tester::new(NoLengthAsSliceEnd::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("foo.slice(1, foo.length)", "foo.slice(1)"),
("foo?.slice(1, foo.length)", "foo?.slice(1)"),
("foo.slice(1, foo.length,)", "foo.slice(1,)"),
("foo.slice(1, (( foo.length )))", "foo.slice(1)"),
("foo.slice(1, foo?.length)", "foo.slice(1)"),
("foo?.slice(1, foo?.length)", "foo?.slice(1)"),
];

Tester::new(NoLengthAsSliceEnd::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}