Replace find() with some() for existence checks and convert array to Set#7355
Closed
sonarqube-agent[bot] wants to merge 1 commit into
Closed
Replace find() with some() for existence checks and convert array to Set#7355sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZ7yEu69hshm9reY56An for typescript:S7754 rule - AZ7yEuVOhshm9reY56Ai for typescript:S7754 rule - AZ7yEuwPhshm9reY56Al for typescript:S7754 rule - AZ7yEuzNhshm9reY56Am for typescript:S7754 rule - AZ7yEusshshm9reY56Ak for typescript:S7776 rule Generated by SonarQube Agent (task: 5a0cdbcb-c46f-4539-8dbd-149cb25567b9)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes 5 SonarQube code smells by replacing .find() with .some() in 4 files where the intent is to check for element existence rather than retrieve the element, and by converting BLOCKCHAIN_MODULES from an array to a Set for O(1) lookup performance. These changes improve code clarity, performance, and align with JavaScript best practices.
View Project in SonarCloud
Fixed Issues
typescript:S7776 - `BLOCKCHAIN_MODULES` should be a `Set`, and use `BLOCKCHAIN_MODULES.has()` to check existence or non-existence. • MINOR • View issue
Location:
packages/analysis/src/jsts/rules/S7639/rule.ts:25Why is this an issue?
Using
Array#includes()for existence checks can lead to performance problems, especially with larger datasets.What changed
Converts the
BLOCKCHAIN_MODULESvariable from an array literal to aSetby wrapping it withnew Set(...). This addresses the code smell whereBLOCKCHAIN_MODULESwas declared as an array but used primarily for existence checking. Using aSetprovides O(1) average time complexity for lookups instead of O(n) withArray#includes().typescript:S7754 - Prefer `.some(…)` over `.find(…)`. • MINOR • View issue
Location:
packages/analysis/src/jsts/rules/S131/rule.ts:52Why is this an issue?
JavaScript’s
Array.prototype.some()method is specifically designed to test whether at least one element in an array passes a provided test function. Using other array methods for this purpose creates several problems:What changed
This hunk replaces the use of
.find()(which returns the found element or undefined) with.some()(which returns a boolean) when checking whether any switch case has a null test (i.e., is a default clause). The original code usedcases.find(c => c.test === null)and then checked if the result was falsy, but.some()is the semantically correct method for testing whether at least one element matches a condition. This change improves readability, performance (short-circuits on first match), and follows JavaScript best practices as recommended by the 'prefer-array-some' rule.typescript:S7754 - Prefer `.some(…)` over `.find(…)`. • MINOR • View issue
Location:
packages/analysis/src/jsts/rules/S4323/rule.ts:51Why is this an issue?
JavaScript’s
Array.prototype.some()method is specifically designed to test whether at least one element in an array passes a provided test function. Using other array methods for this purpose creates several problems:What changed
This hunk replaces
.find(with.some(on theancestorsarray. The static analysis rule flags the use of.find()when the intent is to check whether any element in an array matches a condition, because.some()is semantically clearer and more efficient for existence checks. By changingancestors.find(toancestors.some(, the code now uses the appropriate method for testing if at least one ancestor meets the condition, which improves readability, performance (short-circuits on first match), and follows JavaScript best practices.typescript:S7754 - Prefer `.some(…)` over `.find(…)`. • MINOR • View issue
Location:
packages/analysis/src/jsts/rules/S5736/rule.ts:67Why is this an issue?
JavaScript’s
Array.prototype.some()method is specifically designed to test whether at least one element in an array passes a provided test function. Using other array methods for this purpose creates several problems:What changed
Replaces
.find(…)with.some(…)for checking if any array element matches a condition. The static analysis rule flagged the use of.find()as a code smell because.some()is the semantically correct method for existence checking — it's more readable, more efficient (short-circuits on first match), and clearly communicates intent. The variable is also renamed fromsensitiveValuetohasSensitiveValueto reflect that it now holds a boolean rather than the found element.typescript:S7754 - Prefer `.some(…)` over `.find(…)`. • MINOR • View issue
Location:
packages/analysis/src/jsts/rules/S6351/rule.ts:127Why is this an issue?
JavaScript’s
Array.prototype.some()method is specifically designed to test whether at least one element in an array passes a provided test function. Using other array methods for this purpose creates several problems:What changed
This hunk replaces
.find(r => r.node === value)followed by a truthiness check (if (regex)) with.some(r => r.node === value). The original code used.find()to check whether any element in theregexesarray matched a condition, but.find()returns the matched element (orundefined), which is less semantically clear for an existence check. The.some()method is specifically designed for this purpose — it returns a boolean indicating whether at least one element passes the test, and it short-circuits on the first match. This directly addresses the code smell about preferring.some(…)over.find(…)for existence checks.SonarQube Remediation Agent uses AI. Check for mistakes.