ESLint V10 & Scope Manager: Handling Globals In TypeScript

by Alex Johnson 59 views

Are you ready for some significant changes coming to the way ESLint handles your TypeScript code? Specifically, we're diving into the scope-manager and how it's evolving to keep up with ESLint v10. This is important because it directly impacts how your code is analyzed and how ESLint identifies global variables. Let's break down the crucial updates and what they mean for you, the developer.

The Heart of the Matter: Scope Manager and Globals

At the core of ESLint's code analysis is the ScopeManager. Think of it as the brain that understands the different scopes within your code: global, function, block, and so on. It figures out where variables are declared and how they're used. Now, with ESLint v10, the ScopeManager is getting a makeover, particularly when it comes to dealing with global variables. This is where things get interesting and where you might need to make some changes to your custom setups, if you have them.

What are Globals?

Before we go any further, let's make sure we're all on the same page about globals. Global variables are those declared outside of any specific function or block. They're accessible from anywhere in your code. In JavaScript and TypeScript, these can be declared in a few ways. For example, var and function declarations at the top level are classic examples of global variables. Also any variable declared without let, const, or var is also a global variable, like the window object in a browser environment. Understanding how ESLint identifies and manages these globals is key to writing clean, maintainable code.

The Breaking Changes in ESLint v10

ESLint v10 introduces two major changes that affect how the ScopeManager works with globals. If you've been working with custom ScopeManager implementations or deeply integrating ESLint into your workflow, these are especially relevant to you.

Change 1: Pre-resolved References

The first change is that ESLint now expects your custom ScopeManager to handle all references to global variables before it even gets to the parsing stage. Previously, ESLint would do some of the heavy lifting by resolving unresolved references. Now, it's your responsibility to ensure that all global variables declared in your code, including those declared with var and function, have already been resolved. This change shifts the responsibility and requires a more proactive approach in your code analysis.

In simpler terms: Imagine you have a variable declared globally. In older versions of ESLint, ESLint might have figured out that it was a global variable on its own. Now, you need to tell it that this variable is global before the analysis begins.

Change 2: The addGlobals() Method

The second major change is the introduction of a new instance method: addGlobals(names: string[]). This method is designed to create variables with the given names in the global scope and resolve any references to them. This provides a direct way to tell the ScopeManager about global variables. This is a very important change that helps streamline the process of including the global variables.

Essentially: You'll be able to explicitly tell the ScopeManager,