Inner functions should not be accessed at default parameter declaration

  • ACCESS_INNER_FUNC_FROM_DEFAULT_PARAM
  • Error
  • Medium
  • es6

This rule applies when a default parameter declaration references an inner function declared in the body.

Inner functions cannot be used when computing the default value of a parameter because they are defined after resolving all parameters. Trying to access them may result in a ReferenceError exception or using a wrong variable.

Noncompliant Code Example

View with compliant examples side by side
function add(x, y = getZero(x)) { // ACCESS_INNER_FUNC_FROM_DEFAULT_PARAM alarm
    function getZero(x) {
        if (typeof x == "number") return 0;
        else return "";
    }
    return x + y;
}

Compliant Code Example

View with noncompliant examples side by side
function getZero(x) {
    if (typeof x == "number") return 0;
    else return "";
}
function add(x, y = getZero(x)) {
    return x + y;
}

Version

This rule was introduced in DeepScan 1.32.0.

See

Was this documentation helpful?