Skip to content

fix: resolve 5 SonarQube issues in JavaScript files#48

Open
sonarqube-agent[bot] wants to merge 1 commit into
mainfrom
remediate-main-20260510-090206-a37529bc
Open

fix: resolve 5 SonarQube issues in JavaScript files#48
sonarqube-agent[bot] wants to merge 1 commit into
mainfrom
remediate-main-20260510-090206-a37529bc

Conversation

@sonarqube-agent
Copy link
Copy Markdown

Updated JavaScript code to follow modern best practices and SonarQube recommendations. Changes include replacing typeof checks with direct undefined comparison, using Math.min() instead of ternary expressions, preferring String#replaceAll() over replace() with global regex, using Number.isNaN() instead of isNaN(), and using Number.parseFloat() instead of the global parseFloat() function.

View Project in SonarCloud


Fixed Issues

javascript:S7741 - Compare with `undefined` directly instead of using `typeof`. • MINORView issue

Location: src/script/blocs/bloc-global-sliders.js:60

Why is this an issue?

Using typeof to check for undefined values is unnecessarily verbose and makes code harder to read. The pattern typeof value === 'undefined' was historically necessary in older JavaScript versions (pre-ES5) because the global undefined could be reassigned. However, this is no longer a concern in modern JavaScript environments.

What changed

This hunk replaces the verbose typeof(sliderElt) != 'undefined' check with a direct comparison sliderElt !== undefined. The original code used the typeof operator to check if sliderElt was undefined by comparing the result string, which is unnecessarily verbose and harder to read in modern JavaScript. The fix uses a direct strict comparison with undefined, which is more concise, expressive, and the recommended approach in modern JavaScript environments where undefined cannot be reassigned.

--- a/src/script/blocs/bloc-global-sliders.js
+++ b/src/script/blocs/bloc-global-sliders.js
@@ -60,1 +60,1 @@ var fn_slider = function (componentName, componentSliderClass, controlsPosition,
-    if(typeof(sliderElt) != 'undefined' && sliderElt != null) {
+    if(sliderElt !== undefined && sliderElt != null) {
javascript:S7766 - Prefer `Math.min()` to simplify ternary expressions. • MINORView issue

Location: src/script/blocs/bloc-m-key-figure.js:13

Why is this an issue?

Ternary expressions that implement min/max logic can be harder to read and understand at first glance. When you see height > 50 ? 50 : height, you need to mentally parse the comparison logic to understand that it’s selecting the smaller value.

What changed

Replaces the ternary expression b < H ? b : H with Math.min(b, H), which is the idiomatic way to select the minimum of two values. This makes the intent clearer and addresses the static analysis warning about preferring Math.min() over ternary expressions for min/max logic.

--- a/src/script/blocs/bloc-m-key-figure.js
+++ b/src/script/blocs/bloc-m-key-figure.js
@@ -13,1 +13,1 @@ $(function ($, win) {
-                return cb.call(el, Math.max(0, t > 0 ? H - t : (b < H ? b : H)));
+                return cb.call(el, Math.max(0, t > 0 ? H - t : Math.min(b, H)));
javascript:S7781 - Prefer `String#replaceAll()` over `String#replace()`. • MINORView issue

Location: src/script/blocs/bloc-m-key-figure.js:26

Why is this an issue?

The String#replaceAll() method was introduced in ES2021 to provide a clearer and safer way to replace all occurrences of a pattern in a string.

What changed

Replaces String#replace(/\s/g, "") with String#replaceAll(/\s/g, ""). The static analysis rule flags the use of String#replace() with a global regex that could be replaced with String#replaceAll() for clarity. Note: since \s is a character class (not a fixed literal), replaceAll with the global regex is used rather than a plain string argument, but this satisfies the analyzer's preference for replaceAll over replace with a global flag.

--- a/src/script/blocs/bloc-m-key-figure.js
+++ b/src/script/blocs/bloc-m-key-figure.js
@@ -26,1 +26,1 @@ var fn_keyfigure_animated = function () {
-            var keyfigureNumber = $(this).text().replace(/\s/g, ""); // remove white space
+            var keyfigureNumber = $(this).text().replaceAll(/\s/g, ""); // remove white space
javascript:S7773 - Prefer `Number.isNaN` over `isNaN`. • MINORView issue

Location: src/script/blocs/bloc-m-key-figure.js:31

Why is this an issue?

ECMAScript 2015 introduced static methods and properties on the Number constructor to replace several global functions and values. Using these Number equivalents provides several benefits:

What changed

Replaces the global isNaN() function with Number.isNaN(Number(keyfigureNumber)). The global isNaN() performs implicit type coercion which can lead to unexpected results, while Number.isNaN() only returns true for actual NaN values. Wrapping the argument in Number() ensures the string is explicitly converted to a number first, preserving the original coercion behavior while using the modern, preferred Number.isNaN method.

--- a/src/script/blocs/bloc-m-key-figure.js
+++ b/src/script/blocs/bloc-m-key-figure.js
@@ -31,1 +31,1 @@ var fn_keyfigure_animated = function () {
-            if (px > 0 && !this.initNumAnim && !isNaN(keyfigureNumber)) {
+            if (px > 0 && !this.initNumAnim && !Number.isNaN(Number(keyfigureNumber))) {
javascript:S7773 - Prefer `Number.parseFloat` over `parseFloat`. • MINORView issue

Location: src/script/blocs/bloc-m-key-figure.js:40

Why is this an issue?

ECMAScript 2015 introduced static methods and properties on the Number constructor to replace several global functions and values. Using these Number equivalents provides several benefits:

What changed

Replaces the global parseFloat() function with Number.parseFloat(). This addresses the static analysis warning about preferring Number constructor methods over their global equivalents, aligning with modern ES2015+ practices for better code organization and consistency.

--- a/src/script/blocs/bloc-m-key-figure.js
+++ b/src/script/blocs/bloc-m-key-figure.js
@@ -40,1 +40,1 @@ var fn_keyfigure_animated = function () {
-                        $(this).text(new Intl.NumberFormat('fr-FR').format(parseFloat(now).toFixed()));
+                        $(this).text(new Intl.NumberFormat('fr-FR').format(Number.parseFloat(now).toFixed()));

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZxrBYpYc5mXC10on-t6 for javascript:S7741 rule
- AZxrBYoic5mXC10on-t1 for javascript:S7781 rule
- AZxrBYoic5mXC10on-t2 for javascript:S7773 rule
- AZxrBYoic5mXC10on-t3 for javascript:S7773 rule
- AZxrBYoic5mXC10on-tz for javascript:S7766 rule

Generated by SonarQube Agent (task: c7835a20-af5c-4aa6-aefa-a326c4a80e16)
@sonarqube-agent
Copy link
Copy Markdown
Author

⚠️ This repository does not have a CODEOWNERS file. The PR has been created but has not been automatically assigned to any reviewer. To ensure PRs are reviewed promptly, consider adding a CODEOWNERS file to your repository.

@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants