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(lint): add rule noUselessLengthCheck #4060

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

GunseiKPaseri
Copy link
Contributor

Summary

Implement noUselessLengthCheck(unicorn/no-useless-length-check)

closes #3941

Test Plan

  • Added tests and snapshots

@github-actions github-actions bot added A-CLI Area: CLI A-Project Area: project A-Linter Area: linter L-JavaScript Language: JavaScript and super languages A-Diagnostic Area: diagnostocis labels Sep 24, 2024
Copy link

codspeed-hq bot commented Sep 24, 2024

CodSpeed Performance Report

Merging #4060 will improve performances by 7.11%

Comparing GunseiKPaseri:no-useless-length-check (530a61a) with main (a3483e4)

Summary

⚡ 2 improvements
✅ 105 untouched benchmarks

Benchmarks breakdown

Benchmark main GunseiKPaseri:no-useless-length-check Change
dojo_11880045762646467684.js[cached] 8.4 ms 7.9 ms +7.11%
eucjp_1600564308684076393.json[uncached] 1,023.8 µs 964.2 µs +6.17%

};
// ( hoge )
if let Some(parenthesized_exp_parent) = JsParenthesizedExpression::cast(parent.clone()) {
return is_logical_exp_child(&AnyJsExpression::from(parenthesized_exp_parent), operator);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is recursive, which we generally try to avoid for performance. You should be able to traverse up the tree by calling node.syntaxI().ancestors().

Example:

fn is_react_export(binding: &Binding, lib: ReactLibrary) -> bool {
binding
.syntax()
.ancestors()
.find_map(|ancestor| JsImport::cast(ancestor)?.source_text().ok())
.is_some_and(|source| lib.import_names().contains(&source.text()))
}

comparing_length(&left, operator, &right, expect_error)
}

fn comparing_length(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to leave doc comments explaining what these utility functions do. Doesn't need to be long, just 1-2 sentences explaining the context. It helps a lot when the rule needs to be updated by somebody else later.


pub type Replacer = (JsLogicalExpression, AnyJsExpression, TextRange);

fn search_logical_exp(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this, add a doc comment.

};
let left = logical_exp.left().ok()?;
let left_replacer = (logical_exp.clone(), logical_exp.right().ok()?, left.range());
search_logical_exp(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also recursion, though I'm not entirely sure how you would avoid it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is performing a complete search within the range of descendant logical expressions. While using a stack instead of a recursive function comes to mind, I believe it would only complicate things further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-CLI Area: CLI A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Implement noUselessLengthCheck - unicorn/no-useless-length-check
2 participants