Skip to main content

noFunctionAssign (since v0.7.0)

This rule is recommended by Rome.

Disallow reassigning function declarations.

Examples

Invalid

function foo() { };
foo = bar;
error[js/noFunctionAssign]: Do not reassign a function declaration.
   js/noFunctionAssign.js:1:10
  
1  function foo() { };
            ^^^
2  foo = bar;
   --- Reassigned here.

=  note: Use a local variable instead.

function foo() {
foo = bar;
}
error[js/noFunctionAssign]: Do not reassign a function declaration.
   js/noFunctionAssign.js:1:10
  
1  function foo() {
            ^^^
2      foo = bar;
       --- Reassigned here.

=  note: Use a local variable instead.

foo = bar;
function foo() { };
error[js/noFunctionAssign]: Do not reassign a function declaration.
   js/noFunctionAssign.js:2:10
  
1  foo = bar;
   --- Reassigned here.
2  function foo() { };
            ^^^

=  note: Reassignment happens here because the function declaration is hoisted.
=  note: Use a local variable instead.

[foo] = bar;
function foo() { };
error[js/noFunctionAssign]: Do not reassign a function declaration.
   js/noFunctionAssign.js:2:10
  
1  [foo] = bar;
    --- Reassigned here.
2  function foo() { };
            ^^^

=  note: Reassignment happens here because the function declaration is hoisted.
=  note: Use a local variable instead.

({ x: foo = 0 } = bar);
function foo() { };
error[js/noFunctionAssign]: Do not reassign a function declaration.
   js/noFunctionAssign.js:2:10
  
1  ({ x: foo = 0 } = bar);
         --- Reassigned here.
2  function foo() { };
            ^^^

=  note: Reassignment happens here because the function declaration is hoisted.
=  note: Use a local variable instead.

function foo() {
[foo] = bar;
}
error[js/noFunctionAssign]: Do not reassign a function declaration.
   js/noFunctionAssign.js:1:10
  
1  function foo() {
            ^^^
2      [foo] = bar;
        --- Reassigned here.

=  note: Use a local variable instead.

(function () {
({ x: foo = 0 } = bar);
function foo() { };
})();
error[js/noFunctionAssign]: Do not reassign a function declaration.
   js/noFunctionAssign.js:3:14
  
2      ({ x: foo = 0 } = bar);
             --- Reassigned here.
3      function foo() { };
                ^^^

=  note: Reassignment happens here because the function declaration is hoisted.
=  note: Use a local variable instead.

Valid

function foo() {
var foo = bar;
}
function foo(foo) {
foo = bar;
}
function foo() {
var foo;
foo = bar;
}
var foo = () => {};
foo = bar;
var foo = function() {};
foo = bar;
var foo = function() {
foo = bar;
};
import bar from 'bar';
function foo() {
var foo = bar;
}