Skip to content

Commit

Permalink
Merge branch 'main' into 08-25-refactor_linter_noredeclare_rule_use_r…
Browse files Browse the repository at this point in the history
…un_on_symbol_not_run_once_
  • Loading branch information
overlookmotel committed Aug 25, 2024
2 parents f2ba22a + aaaf26c commit 0295f9e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
27 changes: 21 additions & 6 deletions crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
}

Expand Down Expand Up @@ -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![
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_linter/src/snapshots/prefer_to_have_length.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

0 comments on commit 0295f9e

Please sign in to comment.