Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3798

Variables and functions should not be declared in the global scope

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Message:
      Define this declaration in a local scope or bind explicitly the property to the global object.
    • Highlighting:
      Hide

      variable or function

      Show
      variable or function
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Covered Languages:
      JavaScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources
    • ESLint:
      no-implicit-globals

      Description

      Any variable or function declared in the global scope implicitly becomes attached to the global object (the window object in a browser environment). To make it explicit this variable or function should be a property of window. When it is meant to be used just locally, it should be declared with the const or let keywords (since ECMAScript 2015) or within an Immediately-Invoked Function Expression (IIFE).

      This rule should not be activated when modules are used.

      Noncompliant Code Example

      var myVar = 42;       // Noncompliant
      function myFunc() { } // Noncompliant
      

      Compliant Solution

      window.myVar = 42;
      window.myFunc = function() { };
      

      or

      let myVar = 42;
      let myFunc = function() { }
      

      or

      // IIFE
      (function() {
        var myVar = 42;
        function myFunc() { }
      })();
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jeanchristophe.collet Jean-Christophe Collet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: