Lexical variables (let, const and class) must be declared before they are used

  • REFERENCE_BEFORE_LEXICAL_DECL
  • Error
  • High
  • es6

This rule applies when lexical variables to be declared with let, const or class are referenced before their declarations.

Referencing the variables before their declarations throws a ReferenceError exception.

Therefore, the lexical variables with let, const or class should be declared before they are referenced.

Noncompliant Code Example

function foo() {
    console.log(v1); // REFERENCE_BEFORE_LEXICAL_DECL alarm
    let v1 = 2;

    v2 = 1; // REFERENCE_BEFORE_LEXICAL_DECL alarm
    const v2 = 3;

    new A(); // REFERENCE_BEFORE_LEXICAL_DECL alarm
    class A {};
}

Compliant Code Example

function foo() {
    let v1 = 2;
    console.log(v1);

    const v2 = 3;

    class A {};
    new A();
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.