fix: resolve 5 SonarQube code quality issues#2
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Conversation
Fixed issues: - AZXWCPaB3Vxctdtm0F36 for typescript:S4138 rule - AZXWCO973Vxctdtm0FzQ for typescript:S1940 rule - AZXWCPaX3Vxctdtm0F4D for typescript:S6594 rule - AZXWCPaX3Vxctdtm0F4F for typescript:S4138 rule - AZXWCPaX3Vxctdtm0F4G for typescript:S6594 rule Generated by SonarQube Agent (task: 031b6c23-7460-4b1d-b38a-7b78d11498ad)
SonarQube reviewer guide
|
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 quality issues by replacing traditional for loops with for-of loops for simpler iteration, using RegExp.exec() instead of String.match() for better performance, and replacing inverted equality checks with direct inequality operators. These changes improve code readability and maintain consistency with modern JavaScript best practices.
View Project in SonarCloud
Fixed Issues
typescript:S4138 - Expected a `for-of` loop instead of a `for` loop with this simple iteration. • MINOR • View issue
Location:
src/features/lightspeed/utils/scanner.ts:105Why is this an issue?
for...ofstatements are used to iterate over the values of an iterable object. Iterables are objects implementing the@@iteratormethod, which returns an object conforming to the iterator protocol. JavaScript provides many built-in iterables that can and should be used with this looping statement.What changed
This hunk replaces a traditional
forloop (for (let i = 0; i < a.length; i++)) with afor...ofloop (for (const item of a)), and correspondingly replaces the index-based accessa[i]with the loop variableitem. This directly addresses the code smell where afor...ofloop should be used instead of aforloop with simple iteration over an iterable array, improving readability and eliminating the unnecessary counter variable.typescript:S6594 - Use the "RegExp.exec()" method instead. • MINOR • View issue
Location:
src/features/lightspeed/utils/data.ts:304Why is this an issue?
String.match()behaves the same way asRegExp.exec()when the regular expression does not include the global flagg. While they work the same,RegExp.exec()can be slightly faster thanString.match(). Therefore, it should be preferred for better performance.What changed
This hunk replaces a traditional
forloop (for (let lineIndex = 0; lineIndex < documentLines.length; lineIndex++)) with afor...ofloop (for (const _ of documentLines)). SincedocumentLinesis an iterable array and the loop index was only used for simple iteration, usingfor...ofsimplifies the syntax and improves readability, addressing the code smell about preferringfor-ofloops over index-basedforloops for simple iterations.typescript:S4138 - Expected a `for-of` loop instead of a `for` loop with this simple iteration. • MINOR • View issue
Location:
src/features/lightspeed/utils/data.ts:323Why is this an issue?
for...ofstatements are used to iterate over the values of an iterable object. Iterables are objects implementing the@@iteratormethod, which returns an object conforming to the iterator protocol. JavaScript provides many built-in iterables that can and should be used with this looping statement.What changed
This hunk replaces
String.match()withRegExp.exec()on line 328. The original codedocumentLines[lineIndex].match(/^\s*/)called match on a string with a non-global regex. The fix rewrites it as/^\s*/.exec(documentLines[lineIndex]), which is semantically equivalent but slightly faster. This addresses the performance-related code smell about preferringRegExp.exec()overString.match()at line 328. Since there are two separate static analysis warnings about usingRegExp.exec()instead ofString.match()— one at line 304 and one at line 328 — this hunk specifically resolves the one reported at line 328 involving the/^\s*/regex pattern.typescript:S6594 - Use the "RegExp.exec()" method instead. • MINOR • View issue
Location:
src/features/lightspeed/utils/data.ts:328Why is this an issue?
String.match()behaves the same way asRegExp.exec()when the regular expression does not include the global flagg. While they work the same,RegExp.exec()can be slightly faster thanString.match(). Therefore, it should be preferred for better performance.What changed
This hunk replaces
String.match()withRegExp.exec()on line 328. The original codedocumentLines[lineIndex].match(/^\s*/)called match on a string with a non-global regex. The fix rewrites it as/^\s*/.exec(documentLines[lineIndex]), which is semantically equivalent but slightly faster. This addresses the performance-related code smell about preferringRegExp.exec()overString.match()at line 328. Since there are two separate static analysis warnings about usingRegExp.exec()instead ofString.match()— one at line 304 and one at line 328 — this hunk specifically resolves the one reported at line 328 involving the/^\s*/regex pattern.typescript:S1940 - Use the opposite operator (!==) instead. • MINOR • View issue
Location:
packages/ansible-language-server/src/utils/yaml.ts:53Why is this an issue?
It is needlessly complex to invert the result of a boolean comparison. The opposite comparison should be made instead.
What changed
This hunk simplifies the boolean comparison by replacing
!(type === Pair.prototype.constructor)withtype !== Pair.prototype.constructor. The original code used an inverted equality check (!(... === ...)), which is needlessly complex. The fix uses the opposite operator (!==) directly, making the code clearer and resolving the code smell about inverting boolean comparison results.SonarQube Remediation Agent uses AI. Check for mistakes.