-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter) eslint-plugin-unicorn no length as slice end (#4514)
- Loading branch information
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
crates/oxc_linter/src/rules/unicorn/no_length_as_slice_end.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use oxc_ast::{ast::MemberExpression, AstKind}; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
|
||
use crate::{ | ||
ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_reference, AstNode, | ||
}; | ||
|
||
fn no_length_as_slice_end_diagnostic(call_span: Span, arg_span: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("Passing `length` as the end argument of a `slice` call is unnecessary.") | ||
.with_help("Remove the second argument.") | ||
.with_labels([ | ||
call_span.label("`.slice` called here."), | ||
arg_span.label("Invalid argument here"), | ||
]) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoLengthAsSliceEnd; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// Disallow using `length` as the end argument of a `slice` call. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// Passing `length` as the end argument of a `slice` call is unnecessary and can be confusing. | ||
/// | ||
/// ### Example | ||
/// ```javascript | ||
/// // Bad | ||
/// foo.slice(1, foo.length) | ||
/// | ||
/// // Good | ||
/// foo.slice(1) | ||
/// ``` | ||
NoLengthAsSliceEnd, | ||
restriction | ||
); | ||
|
||
impl Rule for NoLengthAsSliceEnd { | ||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
let AstKind::CallExpression(call_expr) = node.kind() else { | ||
return; | ||
}; | ||
|
||
if !is_method_call(call_expr, None, Some(&["slice"]), Some(2), Some(2)) | ||
|| call_expr.optional | ||
{ | ||
return; | ||
} | ||
|
||
if call_expr.arguments.iter().any(oxc_ast::ast::Argument::is_spread) { | ||
return; | ||
} | ||
|
||
let Some(MemberExpression::StaticMemberExpression(second_argument)) = call_expr.arguments | ||
[1] | ||
.as_expression() | ||
.map(oxc_ast::ast::Expression::without_parenthesized) | ||
.and_then(|e| e.get_member_expr()) else { | ||
return; | ||
}; | ||
|
||
if second_argument.property.name != "length" { | ||
return; | ||
} | ||
|
||
if !is_same_reference( | ||
call_expr.callee.as_member_expression().unwrap().object(), | ||
&second_argument.object, | ||
ctx, | ||
) { | ||
return; | ||
} | ||
|
||
ctx.diagnostic(no_length_as_slice_end_diagnostic( | ||
call_expr.callee.get_member_expr().unwrap().static_property_info().unwrap().0, | ||
second_argument.span, | ||
)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
"foo.slice?.(1, foo.length)", | ||
"foo.slice(foo.length, 1)", | ||
"foo.slice()", | ||
"foo.slice(1)", | ||
"foo.slice(1, foo.length - 1)", | ||
"foo.slice(1, foo.length, extraArgument)", | ||
"foo.slice(...[1], foo.length)", | ||
"foo.notSlice(1, foo.length)", | ||
"new foo.slice(1, foo.length)", | ||
"slice(1, foo.length)", | ||
"foo.slice(1, foo.notLength)", | ||
"foo.slice(1, length)", | ||
"foo[slice](1, foo.length)", | ||
"foo.slice(1, foo[length])", | ||
"foo.slice(1, bar.length)", | ||
"foo().slice(1, foo().length)", | ||
]; | ||
|
||
let fail = vec![ | ||
"foo.slice(1, foo.length)", | ||
"foo?.slice(1, foo.length)", | ||
"foo.slice(1, foo.length,)", | ||
"foo.slice(1, (( foo.length )))", | ||
"foo.slice(1, foo?.length)", | ||
"foo?.slice(1, foo?.length)", | ||
]; | ||
|
||
Tester::new(NoLengthAsSliceEnd::NAME, pass, fail).test_and_snapshot(); | ||
} |
56 changes: 56 additions & 0 deletions
56
crates/oxc_linter/src/snapshots/no_length_as_slice_end.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
--- | ||
⚠ eslint-plugin-unicorn(no-length-as-slice-end): Passing `length` as the end argument of a `slice` call is unnecessary. | ||
╭─[no_length_as_slice_end.tsx:1:5] | ||
1 │ foo.slice(1, foo.length) | ||
· ──┬── ─────┬──── | ||
· │ ╰── Invalid argument here | ||
· ╰── `.slice` called here. | ||
╰──── | ||
help: Remove the second argument. | ||
|
||
⚠ eslint-plugin-unicorn(no-length-as-slice-end): Passing `length` as the end argument of a `slice` call is unnecessary. | ||
╭─[no_length_as_slice_end.tsx:1:6] | ||
1 │ foo?.slice(1, foo.length) | ||
· ──┬── ─────┬──── | ||
· │ ╰── Invalid argument here | ||
· ╰── `.slice` called here. | ||
╰──── | ||
help: Remove the second argument. | ||
|
||
⚠ eslint-plugin-unicorn(no-length-as-slice-end): Passing `length` as the end argument of a `slice` call is unnecessary. | ||
╭─[no_length_as_slice_end.tsx:1:5] | ||
1 │ foo.slice(1, foo.length,) | ||
· ──┬── ─────┬──── | ||
· │ ╰── Invalid argument here | ||
· ╰── `.slice` called here. | ||
╰──── | ||
help: Remove the second argument. | ||
|
||
⚠ eslint-plugin-unicorn(no-length-as-slice-end): Passing `length` as the end argument of a `slice` call is unnecessary. | ||
╭─[no_length_as_slice_end.tsx:1:5] | ||
1 │ foo.slice(1, (( foo.length ))) | ||
· ──┬── ─────┬──── | ||
· │ ╰── Invalid argument here | ||
· ╰── `.slice` called here. | ||
╰──── | ||
help: Remove the second argument. | ||
|
||
⚠ eslint-plugin-unicorn(no-length-as-slice-end): Passing `length` as the end argument of a `slice` call is unnecessary. | ||
╭─[no_length_as_slice_end.tsx:1:5] | ||
1 │ foo.slice(1, foo?.length) | ||
· ──┬── ─────┬───── | ||
· │ ╰── Invalid argument here | ||
· ╰── `.slice` called here. | ||
╰──── | ||
help: Remove the second argument. | ||
|
||
⚠ eslint-plugin-unicorn(no-length-as-slice-end): Passing `length` as the end argument of a `slice` call is unnecessary. | ||
╭─[no_length_as_slice_end.tsx:1:6] | ||
1 │ foo?.slice(1, foo?.length) | ||
· ──┬── ─────┬───── | ||
· │ ╰── Invalid argument here | ||
· ╰── `.slice` called here. | ||
╰──── | ||
help: Remove the second argument. |