mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
fix(linter): error in fixer for prefer-to-have-length (#5197)
closes #5195
This commit is contained in:
parent
f3e1662e4e
commit
aaaf26cee4
2 changed files with 29 additions and 6 deletions
|
|
@ -144,12 +144,14 @@ impl PreferToHaveLength {
|
|||
|
||||
ctx.diagnostic_with_fix(use_to_have_length(matcher.span), |fixer| {
|
||||
let code = Self::build_code(fixer, static_mem_expr, kind, property_name);
|
||||
let end = if call_expr.arguments.len() > 0 {
|
||||
call_expr.arguments.first().unwrap().span().start
|
||||
} else {
|
||||
matcher.span.end
|
||||
};
|
||||
fixer.replace(Span::new(call_expr.span.start, end - 1), code)
|
||||
let offset = u32::try_from(
|
||||
fixer
|
||||
.source_range(Span::new(matcher.span.end, call_expr.span().end))
|
||||
.find('(')
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
fixer.replace(Span::new(call_expr.span.start, matcher.span.end + offset), code)
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -217,6 +219,10 @@ fn tests() {
|
|||
"expect((meta.get('pages') as YArray<unknown>).length).toBe((originalMeta.get('pages') as YArray<unknown>).length);",
|
||||
None
|
||||
),
|
||||
(
|
||||
"expect(assetTypeContainer.getElementsByTagName('time').length).toEqual(
|
||||
0,
|
||||
);", None)
|
||||
];
|
||||
|
||||
let fix = vec![
|
||||
|
|
@ -247,6 +253,15 @@ fn tests() {
|
|||
"expect((meta.get('pages') as YArray<unknown>)).toHaveLength((originalMeta.get('pages') as YArray<unknown>).length);",
|
||||
None
|
||||
),
|
||||
(
|
||||
"expect(assetTypeContainer.getElementsByTagName('time').length).toEqual(
|
||||
0,
|
||||
);",
|
||||
"expect(assetTypeContainer.getElementsByTagName('time')).toHaveLength(
|
||||
0,
|
||||
);",
|
||||
None
|
||||
)
|
||||
];
|
||||
|
||||
Tester::new(PreferToHaveLength::NAME, pass, fail)
|
||||
|
|
|
|||
|
|
@ -77,3 +77,11 @@ source: crates/oxc_linter/src/tester.rs
|
|||
· ────
|
||||
╰────
|
||||
help: Replace `expect((meta.get('pages') as YArray<unknown>).length).toBe` with `expect((meta.get('pages') as YArray<unknown>)).toHaveLength`.
|
||||
|
||||
⚠ eslint-plugin-jest(prefer-to-have-length): Suggest using `toHaveLength()`.
|
||||
╭─[prefer_to_have_length.tsx:1:64]
|
||||
1 │ expect(assetTypeContainer.getElementsByTagName('time').length).toEqual(
|
||||
· ───────
|
||||
2 │ 0,
|
||||
╰────
|
||||
help: Replace `expect(assetTypeContainer.getElementsByTagName('time').length).toEqual` with `expect(assetTypeContainer.getElementsByTagName('time')).toHaveLength`.
|
||||
|
|
|
|||
Loading…
Reference in a new issue