";let n=document.getElementById("TableOfContents");n&&(n.innerHTML=e)}rerender(){this.renderFilterMenu(),this.renderPageContent(),this.populateRightNav(),this.runHooks("afterRerender")}renderPageContent(){let e={};Object.keys(this.ifFunctionsByRef).forEach(t=>{let s=this.ifFunctionsByRef[t],o=s.value,n=(0,h.reresolveFunctionNode)(s,{variables:this.selectedValsByTraitId});this.ifFunctionsByRef[t]=n,o!==n.value&&(e[t]=n.value)});let t=document.getElementsByClassName("cdoc__toggleable");for(let n=0;n{this.fitCustomizationMenuToScreen()})}addDropdownEventListeners(){let e=document.getElementsByClassName("cdoc-dropdown");for(let t=0;t{let t=e.target;for(;!t.classList.contains("cdoc-dropdown")&&t.parentElement;)t=t.parentElement;let n=t.classList.toggle("cdoc-dropdown__expanded");t.setAttribute("aria-expanded",n.toString())});document.addEventListener("keydown",e=>{if(e.key==="Enter"){let t=e.target;t.classList.contains("cdoc-filter__option")&&t.click()}}),document.addEventListener("click",t=>{for(let n=0;nthis.handleFilterSelectionChange(e));this.addDropdownEventListeners()}locateFilterSelectorEl(){let e=document.getElementById("cdoc-selector");return!!e&&(this.filterSelectorEl=e,!0)}applyFilterSelectionOverrides(){let s=Object.keys(this.selectedValsByTraitId),e=!1,t=this.browserStorage.getTraitVals();Object.keys(t).forEach(n=>{s.includes(n)&&this.selectedValsByTraitId[n]!==t[n]&&(this.selectedValsByTraitId[n]=t[n],e=!0)});let n=(0,j.getTraitValsFromUrl)({url:new URL(window.location.href),traitIds:s});return Object.keys(n).forEach(t=>{this.selectedValsByTraitId[t]!==n[t]&&(this.selectedValsByTraitId[t]=n[t],e=!0)}),e}updateEditButton(){let t=document.getElementsByClassName("toc-edit-btn")[0];if(!t)return;let e=t.getElementsByTagName("a")[0];e&&(e.href=e.href.replace(/\.md\/$/,".mdoc.md/"))}revealPage(){this.runHooks("beforeReveal"),this.filterSelectorEl&&(this.filterSelectorEl.style.position="sticky",this.filterSelectorEl.style.backgroundColor="white",this.filterSelectorEl.style.paddingTop="10px",this.filterSelectorEl.style.visibility="visible",this.filterSelectorEl.style.zIndex="1000");let e=document.getElementById("cdoc-content");e&&(e.style.visibility="visible"),this.runHooks("afterReveal")}renderFilterMenu(){if(!this.filterSelectorEl||!this.filtersManifest)throw new Error("Cannot render filter selector without filtersManifest and filterSelectorEl");let e=(0,l.resolveFilters)({filtersManifest:this.filtersManifest,valsByTraitId:this.selectedValsByTraitId});Object.keys(e).forEach(t=>{let n=e[t];this.selectedValsByTraitId[t]=n.currentValue});let t=(0,y.buildCustomizationMenuUi)(e);this.filterSelectorEl.innerHTML=t,this.fitCustomizationMenuToScreen(),this.addFilterSelectorEventListeners()}fitCustomizationMenuToScreen(){let e=document.getElementById(g);if(!e)return;let s=e.classList.contains(n),t=document.getElementById(v);if(!t)throw new Error("Dropdown menu not found");let o=document.getElementById(b);if(!o)throw new Error("Menu wrapper not found");let i=e.scrollWidth>o.clientWidth;!s&&i?(e.classList.add(n),t.classList.remove(n)):s&&!i&&(e.classList.remove(n),t.classList.add(n))}get cdocsState(){return{selectedValsByTraitId:this.selectedValsByTraitId,ifFunctionsByRef:this.ifFunctionsByRef,filtersManifest:this.filtersManifest,browserStorage:this.browserStorage,filterSelectorEl:this.filterSelectorEl}}};e.ClientFiltersManager=r,t=r,s={value:void 0}}),y=e(e=>{Object.defineProperty(e,"__esModule",{value:!0});var t=j();window.clientFiltersManager=t.ClientFiltersManager.instance}),y()})()Closures should not have too many lines
This product is not supported for your selected Datadog site. ().
Metadata
ID:swift-code-style/closure-max-lines
Language: Unknown
Severity: Notice
Category: Code Style
Description
Closures are designed to be a concise way of injecting behavior without defining a separate function. However, when a closure grows beyond a few lines, it becomes harder to read and maintain. Large closures reduce clarity and make the code less adaptable. To keep the source clean and understandable, closures should remain short, and more complex logic should be extracted into dedicated functions.
Arguments
max-lines: Maximum number of lines allowed in a closure. Default: 40.
Non-Compliant Code Examples
// A list of numbers to process.letnumbers=[1,2,3,4,5,6,7,8,9,10]// NON-COMPLIANT: The closure passed to `forEach` is too long and complex.// It mixes data transformation, filtering, and printing, harming readability.numbers.forEach{numberin// 1. First, let's double the numberletdoubled=number*2print("Processing \(number)...")// 2. Check if the doubled number is a multiple of fourifdoubled.isMultiple(of:4){// 3. Now, let's perform a more complex check// Imagine some business logic hereletisSpecialCase=(doubled+number)%3==0ifisSpecialCase{print("Found a special case for \(number)!")}print("Final doubled value is \(doubled)")}else{print("\(doubled) is not a multiple of 4.")}}
Compliant Code Examples
importFoundationletnumbers=[1,2,3,4,5,6,7,8,9,10]/// Processes a single number with all the required business logic./// By moving the logic here, we make it testable and reusable.funcprocessNumber(_number:Int){letdoubled=number*2print("Processing \(number)...")ifdoubled.isMultiple(of:4){letisSpecialCase=(doubled+number)%3==0ifisSpecialCase{print("Found a special case for \(number)!")}print("Final doubled value is \(doubled)")}else{print("\(doubled) is not a multiple of 4.")}}// COMPLIANT: The closure is now a simple, one-line call// to a well-named function. The code is readable and maintainable.numbers.forEach(processNumber)// An alternative compliant example using a trailing closure syntax:numbers.forEach{numberinprocessNumber(number)}
Seamless integrations. Try Datadog Code Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
How to use this rule
1
2
rulesets:- swift-code-style # Rules to enforce Unknown code style.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Security scans to your CI pipelines