mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(linter/unicorn): add fixer to prefer-set-size (#5149)
This commit is contained in:
parent
67c7802a58
commit
a6704bd293
1 changed files with 28 additions and 5 deletions
|
|
@ -6,7 +6,9 @@ use oxc_diagnostics::OxcDiagnostic;
|
||||||
use oxc_macros::declare_oxc_lint;
|
use oxc_macros::declare_oxc_lint;
|
||||||
use oxc_span::Span;
|
use oxc_span::Span;
|
||||||
|
|
||||||
use crate::{ast_util::get_declaration_of_variable, context::LintContext, rule::Rule, AstNode};
|
use crate::{
|
||||||
|
ast_util::get_declaration_of_variable, context::LintContext, fixer::Fix, rule::Rule, AstNode,
|
||||||
|
};
|
||||||
|
|
||||||
fn prefer_set_size_diagnostic(span0: Span) -> OxcDiagnostic {
|
fn prefer_set_size_diagnostic(span0: Span) -> OxcDiagnostic {
|
||||||
OxcDiagnostic::warn(
|
OxcDiagnostic::warn(
|
||||||
|
|
@ -38,7 +40,7 @@ declare_oxc_lint!(
|
||||||
/// ```
|
/// ```
|
||||||
PreferSetSize,
|
PreferSetSize,
|
||||||
correctness,
|
correctness,
|
||||||
pending
|
fix
|
||||||
);
|
);
|
||||||
|
|
||||||
impl Rule for PreferSetSize {
|
impl Rule for PreferSetSize {
|
||||||
|
|
@ -68,13 +70,22 @@ impl Rule for PreferSetSize {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let maybe_set = &spread_element.argument.without_parenthesized();
|
let maybe_set = &spread_element.argument.get_inner_expression();
|
||||||
|
|
||||||
if !is_set(maybe_set, ctx) {
|
if !is_set(maybe_set, ctx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.diagnostic(prefer_set_size_diagnostic(span));
|
ctx.diagnostic_with_fix(prefer_set_size_diagnostic(span), |_fixer| {
|
||||||
|
vec![
|
||||||
|
// remove [...
|
||||||
|
Fix::delete(Span::new(array_expr.span.start, spread_element.span.start + 3)),
|
||||||
|
// remove everything after the end of the spread element (including the `]` )
|
||||||
|
Fix::delete(Span::new(spread_element.span.end, array_expr.span.end)),
|
||||||
|
// replace .length with .size
|
||||||
|
Fix::new("size", span),
|
||||||
|
]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,5 +168,17 @@ fn test() {
|
||||||
r"[...new /* comment */ Set(array)].length",
|
r"[...new /* comment */ Set(array)].length",
|
||||||
];
|
];
|
||||||
|
|
||||||
Tester::new(PreferSetSize::NAME, pass, fail).test_and_snapshot();
|
let fix = vec![
|
||||||
|
(r"[...new Set(array)].length", r"new Set(array).size"),
|
||||||
|
(r"[...new Set(array),].length", r"new Set(array).size"),
|
||||||
|
(r"[...(( new Set(array) ))].length", r"(( new Set(array) )).size"),
|
||||||
|
(r"[...(( new Set(array as foo) ))].length", r"(( new Set(array as foo) )).size"),
|
||||||
|
(r"[...(( new Set(array) as foo ))].length", r"(( new Set(array) as foo )).size"),
|
||||||
|
(
|
||||||
|
r"[...(( new Set(array) as foo ) ) ] .length;",
|
||||||
|
r"(( new Set(array) as foo ) ) .size;",
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
Tester::new(PreferSetSize::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue