-
-
Notifications
You must be signed in to change notification settings - Fork 896
build: add lint rule to enforce functions are moved to outermost scopes #8006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kgryte
merged 10 commits into
develop
from
philipp/add-lint-rule-no-unnecessary-nested-functions
Sep 8, 2025
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9a5aedd
build: add lint rule to enforce functions are moved to outermost poss…
Planeshifter 0aa42e3
docs: move variable declarations
kgryte eb0aaa7
docs: move variable declarations
kgryte 60fbeb8
style: fix indentation
kgryte b8ea311
test: move declaration
kgryte 265c5ce
test: move declaration
kgryte 9cd7118
test: move declaration
kgryte b806184
Apply suggestions from code review
kgryte 02b8335
build: remove special handling for functions accessing `this` and `ar…
Planeshifter 1a7e056
docs: update copy
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
183 changes: 183 additions & 0 deletions
183
...e_modules/@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions/README.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# no-unnecessary-nested-functions | ||
|
||
> [ESLint rule][eslint-rules] to prevent unnecessary function nesting when inner functions don't depend on outer scope variables. | ||
|
||
<section class="intro"> | ||
|
||
This rule enforces a convention in stdlib 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. | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var rule = require( '@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions' ); | ||
``` | ||
|
||
#### rule | ||
|
||
[ESLint rule][eslint-rules] to prevent unnecessary function nesting when inner functions don't depend on outer scope variables. | ||
|
||
**Bad**: | ||
|
||
<!-- eslint-disable stdlib/no-unnecessary-nested-functions --> | ||
|
||
```javascript | ||
var PI = require( '@stdlib/constants/float64/pi' ); | ||
|
||
function outer( x ) { | ||
var result; | ||
|
||
function helper() { | ||
return PI; // doesn't use any variables from outer scope... | ||
} | ||
|
||
result = x * helper(); | ||
return result; | ||
} | ||
``` | ||
|
||
**Good**: | ||
|
||
```javascript | ||
var PI = require( '@stdlib/constants/float64/pi' ); | ||
|
||
// Helper moved to module scope... | ||
function helper() { | ||
return PI; | ||
} | ||
|
||
function outer( x ) { | ||
var result; | ||
result = x * helper(); | ||
return result; | ||
} | ||
``` | ||
|
||
**Good** (function uses outer scope variables): | ||
|
||
```javascript | ||
function outer( x ) { | ||
var multiplier = 2; | ||
|
||
function helper() { | ||
return x * multiplier; // uses 'multiplier' from outer scope... | ||
} | ||
|
||
return helper(); | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- The rule only checks function **declarations**, not function expressions or arrow functions. | ||
- Functions already at module or global scope are ignored. | ||
- Functions that reference variables from outer scopes are not flagged (closures are preserved). | ||
- Functions inside IIFEs (Immediately Invoked Function Expressions) are not flagged, as IIFEs are intentionally used for scope isolation. | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var Linter = require( 'eslint' ).Linter; | ||
var rule = require( '@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions' ); | ||
|
||
var linter = new Linter(); | ||
|
||
// Generate source code containing nested function without dependencies: | ||
var code = [ | ||
'function outer() {', | ||
' function inner() {', | ||
' return 42;', | ||
' }', | ||
' return inner();', | ||
'}' | ||
].join( '\n' ); | ||
|
||
// Define the ESLint configuration: | ||
var config = { | ||
'rules': { | ||
'no-unnecessary-nested-functions': 'error' | ||
} | ||
}; | ||
|
||
// Register the rule: | ||
linter.defineRule( 'no-unnecessary-nested-functions', rule ); | ||
|
||
// Lint the code: | ||
var out = linter.verify( code, config ); | ||
console.log( out ); | ||
/* => | ||
[ | ||
{ | ||
'ruleId': 'no-unnecessary-nested-functions', | ||
'severity': 2, | ||
'message': 'Function \'inner\' should be moved to module scope.', | ||
'line': 2, | ||
'column': 3, | ||
'nodeType': 'FunctionDeclaration', | ||
'endLine': 4, | ||
'endColumn': 4 | ||
} | ||
] | ||
*/ | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
61 changes: 61 additions & 0 deletions
61
...ode_modules/@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions/examples/index.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var Linter = require( 'eslint' ).Linter; | ||
var rule = require( './../lib' ); | ||
|
||
var linter = new Linter(); | ||
|
||
// Generate source code containing nested function without dependencies: | ||
var code = [ | ||
'function outer() {', | ||
' function inner() {', | ||
' return 42;', | ||
' }', | ||
' return inner();', | ||
'}' | ||
].join( '\n' ); | ||
|
||
// Define the ESLint configuration: | ||
var config = { | ||
'rules': { | ||
'no-unnecessary-nested-functions': 'error' | ||
} | ||
}; | ||
|
||
// Register the rule: | ||
linter.defineRule( 'no-unnecessary-nested-functions', rule ); | ||
|
||
// Lint the code: | ||
var out = linter.verify( code, config ); | ||
console.log( out ); | ||
/* => | ||
[ | ||
{ | ||
'ruleId': 'no-unnecessary-nested-functions', | ||
'severity': 2, | ||
'message': 'Function declaration \'inner\' does not use outer scope variables and should be moved to module scope.', | ||
'line': 2, | ||
'column': 3, | ||
'nodeType': 'FunctionDeclaration', | ||
... | ||
} | ||
] | ||
*/ |
39 changes: 39 additions & 0 deletions
39
lib/node_modules/@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions/lib/index.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* ESLint rule to prevent unnecessary function nesting when inner functions don't depend on outer scope variables. | ||
* | ||
* @module @stdlib/_tools/eslint/rules/no-unnecessary-nested-functions | ||
* | ||
* @example | ||
* var rule = require( '@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions' ); | ||
* | ||
* console.log( rule ); | ||
*/ | ||
|
||
// MODULES // | ||
|
||
var main = require( './main.js' ); | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = main; |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.