Skip to content

Commit d4a0b97

Browse files
committed
build: remove special handling for functions accessing this and arguments
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 1c12d15 commit d4a0b97

File tree

4 files changed

+81
-61
lines changed

4 files changed

+81
-61
lines changed

lib/node_modules/@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ function outer( x ) {
103103
- The rule only checks function **declarations**, not function expressions or arrow functions.
104104
- Functions already at module or global scope are ignored.
105105
- Functions that reference variables from outer scopes are not flagged (closures are preserved).
106-
- Functions that use `this` context from outer scope are not flagged.
107-
- Functions that use `arguments` from outer scope are not flagged.
108106
- Functions inside IIFEs (Immediately Invoked Function Expressions) are not flagged, as IIFEs are intentionally used for scope isolation.
109107

110108
</section>
@@ -151,7 +149,7 @@ console.log( out );
151149
{
152150
'ruleId': 'no-unnecessary-nested-functions',
153151
'severity': 2,
154-
'message': 'Function declaration \'inner\' does not use outer scope variables and should be moved to module scope.',
152+
'message': 'Function \'inner\' should be moved to module scope.',
155153
'line': 2,
156154
'column': 3,
157155
'nodeType': 'FunctionDeclaration',

lib/node_modules/@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions/lib/main.js

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ function isInsideIIFE( node ) {
6060
*/
6161
function referencesOuterVariables( node, context ) {
6262
var identifierName;
63-
var parentScope;
6463
var sourceCode;
6564
var checkScope;
6665
var reference;
@@ -75,30 +74,12 @@ function referencesOuterVariables( node, context ) {
7574
sourceCode = context.getSourceCode();
7675
scope = sourceCode.getScope( node );
7776

78-
// Check for 'this' usage:
79-
if ( scope.thisFound ) {
80-
return true;
81-
}
82-
8377
// Check all references in this scope...
8478
for ( i = 0; i < scope.references.length; i++ ) {
8579
reference = scope.references[ i ];
8680
variable = reference.resolved;
8781
identifierName = reference.identifier.name;
8882

89-
// Handle 'arguments' specially
90-
if ( identifierName === 'arguments' ) {
91-
// Check if we're in a function that could have arguments:
92-
parentScope = scope.upper;
93-
while ( parentScope ) {
94-
if ( parentScope.type === 'function' ) {
95-
return true;
96-
}
97-
parentScope = parentScope.upper;
98-
}
99-
continue;
100-
}
101-
10283
// If variable is resolved and not in current scope, it's from outer scope...
10384
if ( variable && variable.scope !== scope ) {
10485
// Make sure it's not a global or built-in:
@@ -180,7 +161,6 @@ function findVariableScope( varName, startScope ) {
180161
* @returns {(Object|null)} highest scope object or null if cannot be moved
181162
*/
182163
function getHighestPossibleScope( node, context ) {
183-
var parentFuncScope;
184164
var definingScope;
185165
var highestScope;
186166
var currentScope;
@@ -207,18 +187,6 @@ function getHighestPossibleScope( node, context ) {
207187
reference = currentScope.references[ i ];
208188
variable = reference.resolved;
209189

210-
// Handle 'arguments' specially - can't move higher than current function scope...
211-
if ( reference.identifier.name === 'arguments' ) {
212-
parentFuncScope = currentScope.upper;
213-
while ( parentFuncScope && parentFuncScope.type !== 'function' ) {
214-
parentFuncScope = parentFuncScope.upper;
215-
}
216-
if ( parentFuncScope ) {
217-
highestScope = parentFuncScope;
218-
}
219-
continue;
220-
}
221-
222190
// If variable is resolved and not in current scope, find where it's defined...
223191
if ( variable && variable.scope !== currentScope && variable.scope.type !== 'global' ) {
224192
// Can't move higher than the scope where this variable is defined...

lib/node_modules/@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions/test/fixtures/invalid.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,86 @@ test = {
368368
};
369369
invalid.push( test );
370370

371+
// Function uses 'arguments' directly (can be moved since it refers to its own arguments):
372+
test = {
373+
'code': [
374+
'function outer() {',
375+
' function helper() {',
376+
' return arguments.length;',
377+
' }',
378+
' return helper(1, 2, 3);',
379+
'}'
380+
].join( '\n' ),
381+
'errors': [
382+
{
383+
'message': 'Function \'helper\' should be moved to module scope.',
384+
'type': 'FunctionDeclaration'
385+
}
386+
]
387+
};
388+
invalid.push( test );
389+
390+
// Function uses 'this' directly (can be moved since strict mode makes this undefined anyway):
391+
test = {
392+
'code': [
393+
'function outer() {',
394+
' function helper() {',
395+
' return this.value * 2;',
396+
' }',
397+
' return helper.call({ value: 5 });',
398+
'}'
399+
].join( '\n' ),
400+
'errors': [
401+
{
402+
'message': 'Function \'helper\' should be moved to module scope.',
403+
'type': 'FunctionDeclaration'
404+
}
405+
]
406+
};
407+
invalid.push( test );
408+
409+
// Object method with nested function using 'this':
410+
test = {
411+
'code': [
412+
'var obj = {',
413+
' value: 10,',
414+
' method: function() {',
415+
' function helper() {',
416+
' return this.value * 2;',
417+
' }',
418+
' return helper.call(this);',
419+
' }',
420+
'};'
421+
].join( '\n' ),
422+
'errors': [
423+
{
424+
'message': 'Function \'helper\' should be moved to module scope.',
425+
'type': 'FunctionDeclaration'
426+
}
427+
]
428+
};
429+
invalid.push( test );
430+
431+
// Constructor with nested function using 'this':
432+
test = {
433+
'code': [
434+
'function MyClass() {',
435+
' this.value = 42;',
436+
' function initialize() {',
437+
' this.initialized = true;',
438+
' }',
439+
' initialize.call(this);',
440+
'}'
441+
].join( '\n' ),
442+
'errors': [
443+
{
444+
'message': 'Function \'initialize\' should be moved to module scope.',
445+
'type': 'FunctionDeclaration'
446+
}
447+
]
448+
};
449+
invalid.push( test );
450+
371451

372452
// EXPORTS //
373453

lib/node_modules/@stdlib/_tools/eslint/rules/no-unnecessary-nested-functions/test/fixtures/valid.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,6 @@ test = {
156156
};
157157
valid.push( test );
158158

159-
// Function uses 'this' directly:
160-
test = {
161-
'code': [
162-
'function outer() {',
163-
' function helper() {',
164-
' return this.value * 2;',
165-
' }',
166-
' return helper.call({ value: 5 });',
167-
'}'
168-
].join( '\n' )
169-
};
170-
valid.push( test );
171-
172-
// Function uses 'arguments' directly:
173-
test = {
174-
'code': [
175-
'function outer() {',
176-
' function helper() {',
177-
' return arguments.length;',
178-
' }',
179-
' return helper(1, 2, 3);',
180-
'}'
181-
].join( '\n' )
182-
};
183-
valid.push( test );
184-
185159
// Mutually recursive functions:
186160
test = {
187161
'code': [

0 commit comments

Comments
 (0)