Reduce function nesting depth to meet SonarQube standards#4286
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Reduce function nesting depth to meet SonarQube standards#4286sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZObcE-7vuTsgZ3mrklt for typescript:S2004 rule - AZbPCi1lp-jr4SR56zmn for typescript:S2004 rule - AZTbbOa0QYLNS-EXxw7B for typescript:S2004 rule - AZcv7V9QlBqAYD-b54hx for typescript:S2004 rule - AY7ww-qPBNT4s_jVn-2A for typescript:S2004 rule Generated by SonarQube Agent (task: 0574019f-2d09-4dbd-b45e-9d1e2db16c78)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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 refactors five instances of deeply nested functions across multiple files to comply with SonarQube's 4-level nesting limit. By extracting nested callbacks and inner functions into standalone helper functions at the module or component level, the code becomes more maintainable and eliminates critical code smells that indicate potential cognitive complexity issues.
View Project in SonarCloud
Fixed Issues
typescript:S2004 - Refactor this code to not nest functions more than 4 levels deep. • CRITICAL • View issue
Location:
src/node-lib/educator-api/helpers/saveUnits/useMyLibrary.tsx:142Why is this an issue?
Nested functions refer to the practice of defining a function within another function. These inner functions have access to the variables and parameters of the outer function, creating a closure.
What changed
Extracts the deeply nested inline error handler functions into standalone helper functions (handleSaveError and handleUnsaveError) at a lower nesting level. This reduces the function nesting depth that was previously exceeding the 4-level limit. The filter callback
(slug) => slug !== unitProgrammeSlugthat was at nesting level 5 inside the inline anonymous function is now only at nesting level 3 (useMyLibrary -> handleSaveError -> setLocallySavedUnits callback -> filter callback), which is within the allowed threshold.typescript:S2004 - Refactor this code to not nest functions more than 4 levels deep. • CRITICAL • View issue
Location:
src/node-lib/educator-api/helpers/saveUnits/useSaveUnits.tsx:90Why is this an issue?
Nested functions refer to the practice of defining a function within another function. These inner functions have access to the variables and parameters of the outer function, creating a closure.
What changed
Extracts the deeply nested inline error-handling callbacks into named helper functions (
handleSaveErrorandhandleUnsaveError) at a shallower nesting level. This reduces the function nesting depth so that the inner arrow functions (likeprev.filter((unit) => unit !== unitSlug)) no longer exceed the 4-level nesting threshold, directly addressing the code smell about nesting functions more than 4 levels deep.typescript:S2004 - Refactor this code to not nest functions more than 4 levels deep. • CRITICAL • View issue
Location:
src/pages-helpers/curriculum/docx/builder/10_threadsDetail.ts:137Why is this an issue?
Nested functions refer to the practice of defining a function within another function. These inner functions have access to the variables and parameters of the outer function, creating a closure.
What changed
Defines a new top-level helper function
filterUnitsByThreadthat extracts the previously nested function out of thegeneratefunction. This reduces the nesting depth of the inner lambda functions (the.filterand.findIndexcallbacks) by moving them from being nested 4+ levels deep insidegenerateto being only 2 levels deep inside this standalone function, directly addressing the code smell about nesting functions more than 4 levels deep.typescript:S2004 - Refactor this code to not nest functions more than 4 levels deep. • CRITICAL • View issue
Location:
src/node-lib/curriculum-api-2023/queries/unitListing/helpers/getAllCategories.ts:66Why is this an issue?
Nested functions refer to the practice of defining a function within another function. These inner functions have access to the variables and parameters of the outer function, creating a closure.
What changed
Extracts the deeply nested logic into a standalone top-level helper function
applySlugToUnit. This reduces the nesting depth insideapplySlugsToUnitCategoriesby moving the inner function body (which contained nested arrow functions for.map()and.find()) out to the module level, thereby eliminating the excessive function nesting that exceeded the 4-level threshold.typescript:S2004 - Refactor this code to not nest functions more than 4 levels deep. • CRITICAL • View issue
Location:
src/components/TeacherViews/LessonOverview/hasLessonMathJax.ts:39Why is this an issue?
Nested functions refer to the practice of defining a function within another function. These inner functions have access to the variables and parameters of the outer function, creating a closure.
What changed
Extracts the deeply nested
findMatchfunction to the module level asfindMatchingMathJax. This reduces the nesting depth of functions insidehasQuizMathJax, directly addressing the code smell about nesting functions more than 4 levels deep. The arrow function(a) => a.type === "text"that was previously at nesting level 5 is now at a much shallower level since the function is defined at the top level of the module.SonarQube Remediation Agent uses AI. Check for mistakes.