|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# no-unnecessary-nested-functions |
| 22 | + |
| 23 | +> [ESLint rule][eslint-rules] to prevent unnecessary function nesting when inner functions don't depend on outer scope variables. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +This rule enforces a best practice to only have functions defined inside of other functions if they need access to variables defined in the outer function scope. Otherwise, functions should always be elevated to the highest level (ideally module scope) to avoid redefining the functions per invocation. |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<section class="usage"> |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +```javascript |
| 38 | +var rule = require( '@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions' ); |
| 39 | +``` |
| 40 | + |
| 41 | +#### rule |
| 42 | + |
| 43 | +[ESLint rule][eslint-rules] to prevent unnecessary function nesting when inner functions don't depend on outer scope variables. |
| 44 | + |
| 45 | +**Bad**: |
| 46 | + |
| 47 | +<!-- eslint-disable stdlib/no-unnecessary-nested-functions --> |
| 48 | + |
| 49 | +```javascript |
| 50 | +var PI = require( '@stdlib/constants/float64/pi' ); |
| 51 | + |
| 52 | +function outer( x ) { |
| 53 | + var result; |
| 54 | + |
| 55 | + function helper() { |
| 56 | + return PI; // doesn't use any variables from outer scope... |
| 57 | + } |
| 58 | + |
| 59 | + result = x * helper(); |
| 60 | + return result; |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +**Good**: |
| 65 | + |
| 66 | +```javascript |
| 67 | +var PI = require( '@stdlib/constants/float64/pi' ); |
| 68 | + |
| 69 | +// Helper moved to module scope... |
| 70 | +function helper() { |
| 71 | + return PI; |
| 72 | +} |
| 73 | + |
| 74 | +function outer( x ) { |
| 75 | + var result; |
| 76 | + result = x * helper(); |
| 77 | + return result; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +**Good** (function uses outer scope variables): |
| 82 | + |
| 83 | +```javascript |
| 84 | +function outer( x ) { |
| 85 | + var multiplier = 2; |
| 86 | + |
| 87 | + function helper() { |
| 88 | + return x * multiplier; // uses 'multiplier' from outer scope... |
| 89 | + } |
| 90 | + |
| 91 | + return helper(); |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +</section> |
| 96 | + |
| 97 | +<!-- /.usage --> |
| 98 | + |
| 99 | +<section class="notes"> |
| 100 | + |
| 101 | +## Notes |
| 102 | + |
| 103 | +- The rule only checks function **declarations**, not function expressions or arrow functions. |
| 104 | +- Functions already at module or global scope are ignored. |
| 105 | +- Functions that reference variables from outer scopes are not flagged (closures are preserved). |
| 106 | +- Functions inside IIFEs (Immediately Invoked Function Expressions) are not flagged, as IIFEs are intentionally used for scope isolation. |
| 107 | + |
| 108 | +</section> |
| 109 | + |
| 110 | +<!-- /.notes --> |
| 111 | + |
| 112 | +<section class="examples"> |
| 113 | + |
| 114 | +## Examples |
| 115 | + |
| 116 | +<!-- eslint no-undef: "error" --> |
| 117 | + |
| 118 | +```javascript |
| 119 | +var Linter = require( 'eslint' ).Linter; |
| 120 | +var rule = require( '@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions' ); |
| 121 | + |
| 122 | +var linter = new Linter(); |
| 123 | + |
| 124 | +// Generate source code containing nested function without dependencies: |
| 125 | +var code = [ |
| 126 | + 'function outer() {', |
| 127 | + ' function inner() {', |
| 128 | + ' return 42;', |
| 129 | + ' }', |
| 130 | + ' return inner();', |
| 131 | + '}' |
| 132 | +].join( '\n' ); |
| 133 | + |
| 134 | +// Define the ESLint configuration: |
| 135 | +var config = { |
| 136 | + 'rules': { |
| 137 | + 'no-unnecessary-nested-functions': 'error' |
| 138 | + } |
| 139 | +}; |
| 140 | + |
| 141 | +// Register the rule: |
| 142 | +linter.defineRule( 'no-unnecessary-nested-functions', rule ); |
| 143 | + |
| 144 | +// Lint the code: |
| 145 | +var out = linter.verify( code, config ); |
| 146 | +console.log( out ); |
| 147 | +/* => |
| 148 | + [ |
| 149 | + { |
| 150 | + 'ruleId': 'no-unnecessary-nested-functions', |
| 151 | + 'severity': 2, |
| 152 | + 'message': 'Function \'inner\' should be moved to module scope.', |
| 153 | + 'line': 2, |
| 154 | + 'column': 3, |
| 155 | + 'nodeType': 'FunctionDeclaration', |
| 156 | + 'endLine': 4, |
| 157 | + 'endColumn': 4 |
| 158 | + } |
| 159 | + ] |
| 160 | +*/ |
| 161 | +``` |
| 162 | + |
| 163 | +</section> |
| 164 | + |
| 165 | +<!-- /.examples --> |
| 166 | + |
| 167 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 168 | + |
| 169 | +<section class="related"> |
| 170 | + |
| 171 | +</section> |
| 172 | + |
| 173 | +<!-- /.related --> |
| 174 | + |
| 175 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 176 | + |
| 177 | +<section class="links"> |
| 178 | + |
| 179 | +[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules |
| 180 | + |
| 181 | +</section> |
| 182 | + |
| 183 | +<!-- /.links --> |
0 commit comments