Skip to content

fix: resolve 4 SonarQube code quality issues in JavaScript#46

Open
sonarqube-agent[bot] wants to merge 1 commit intomainfrom
remediate-main-20260508-090132-ba1ab54c
Open

fix: resolve 4 SonarQube code quality issues in JavaScript#46
sonarqube-agent[bot] wants to merge 1 commit intomainfrom
remediate-main-20260508-090132-ba1ab54c

Conversation

@sonarqube-agent
Copy link
Copy Markdown

Fixed code smells by replacing typeof checks with direct undefined comparisons, replacing global isNaN() with Number.isNaN() for proper NaN detection, and using globalThis instead of window for environment-agnostic global object access. These changes align with modern JavaScript best practices and improve code reliability.

View Project in SonarCloud


Fixed Issues

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

Location: src/script/blocs/bloc-o-carte-interactive.js:113

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 two typeof ... !== 'undefined' checks with direct comparisons to undefined. Specifically, typeof drupalSettings.asip !== 'undefined' becomes drupalSettings.asip !== undefined and typeof drupalSettings.asip.map !== 'undefined' becomes drupalSettings.asip.map !== undefined. This addresses the code smell about using unnecessarily verbose typeof checks for undefined values, making the code more concise and readable by using direct undefined comparisons instead.

--- a/src/script/blocs/bloc-o-carte-interactive.js
+++ b/src/script/blocs/bloc-o-carte-interactive.js
@@ -113,2 +113,2 @@ $(document).ready(function () {
-    typeof drupalSettings.asip !== 'undefined' &&
-    typeof drupalSettings.asip.map !== 'undefined' ) {
+    drupalSettings.asip !== undefined &&
+    drupalSettings.asip.map !== undefined ) {
javascript:S7773 - Prefer `Number.isNaN` over `isNaN`. • MINORView issue

Location: src/script/app/_widget-access.js:35

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

This hunk replaces both occurrences of the global isNaN() function with Number.isNaN() on line 35. The static analysis flagged two uses of the global isNaN on that line — isNaN(x) and isNaN(y) — as code smells because the global isNaN() performs type coercion before checking, which can lead to unexpected results. By changing to Number.isNaN(x) and Number.isNaN(y), the code now uses the modern ES2015+ equivalent that only returns true for actual NaN values without type coercion, aligning with modern JavaScript best practices and resolving both warnings.

--- a/src/script/app/_widget-access.js
+++ b/src/script/app/_widget-access.js
@@ -35,1 +35,1 @@ if (!Array.prototype.includes) {
-          return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
+          return x === y || (typeof x === 'number' && typeof y === 'number' && Number.isNaN(x) && Number.isNaN(y));
javascript:S7773 - Prefer `Number.isNaN` over `isNaN`. • MINORView issue

Location: src/script/app/_widget-access.js:35

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

This hunk replaces both occurrences of the global isNaN() function with Number.isNaN() on line 35. The static analysis flagged two uses of the global isNaN on that line — isNaN(x) and isNaN(y) — as code smells because the global isNaN() performs type coercion before checking, which can lead to unexpected results. By changing to Number.isNaN(x) and Number.isNaN(y), the code now uses the modern ES2015+ equivalent that only returns true for actual NaN values without type coercion, aligning with modern JavaScript best practices and resolving both warnings.

--- a/src/script/app/_widget-access.js
+++ b/src/script/app/_widget-access.js
@@ -35,1 +35,1 @@ if (!Array.prototype.includes) {
-          return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
+          return x === y || (typeof x === 'number' && typeof y === 'number' && Number.isNaN(x) && Number.isNaN(y));
javascript:S7764 - Prefer `globalThis` over `window`. • MINORView issue

Location: src/script/app/app.js:6

Why is this an issue?

globalThis is the standardized way to access the global object across all JavaScript environments. Before globalThis, developers had to use different global references depending on the environment:

What changed

This hunk replaces window with globalThis in the jQuery focus call $(window).focus()$(globalThis).focus(). This directly fixes the code smell warning about preferring globalThis over window for accessing the global object, as globalThis is the standardized, environment-agnostic way to reference the global object across all JavaScript environments (browsers, Node.js, Web Workers).

--- a/src/script/app/app.js
+++ b/src/script/app/app.js
@@ -6,1 +6,1 @@
-$(window).focus();
+$(globalThis).focus();

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


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZxrBYnQc5mXC10on-tv for javascript:S7764 rule
- AZxrBYo0c5mXC10on-t4 for javascript:S7741 rule
- AZxrBYnAc5mXC10on-tt for javascript:S7773 rule
- AZxrBYnAc5mXC10on-tu for javascript:S7773 rule

Generated by SonarQube Agent (task: 9717c2b3-d84d-4719-ba6d-45dee5a52dbd)
@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

sonarqubecloud Bot commented May 8, 2026

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