JavaScript Linting (Static Code Analysis) in VS Code

Lint is a static code analysis tool that is used to identify various kinds of problems, with the goal of making source code more consistent and avoiding bugs. There are two popular linters for JavaScript: ESLint and JSHint.

Etymology of Lint

The term lint originates from a Unix utility that examined C language source code. It was derived from the name of the tiny bits of fiber and fluff shed by clothing (source: Wikipedia).

ESLint or JSHint?

Both linters are well-supported, neither is a bad choice. ESLint, however, seems to be more popular. Extensions for Visual Studio Code are available both for JSHint and ESLint.

JavaScript Linting in VS Code

Getting Started With ESLint

Installation

Install ESLint globally:

npm install -g eslint

Install the ESLint extension from the Visual Studio Marketplace.

Create an .eslintrc Config File

If you don’t have one yet, create an .eslintrc configuration file for ESLint (see below for migrating from JSHint). You can do so by invoking the command palette (Ctrl+Shift+P) and selecting Create ESLint Configuration.
For your reference, below is a config file we use for the uberAgent browser extension:

{
  "parserOptions": {
    "ecmaVersion": 2015
  },  
  "env": {
    "browser": true
  },
  "globals": {
    "browser": true,
    "console": true
  },
  "rules": {
    "strict": [
      2,
      "global"
    ]
  }
}

Allow ESLint Library Execution

Bring up the command palette (Ctrl+Shift+P) and select ESLint: Manage Library Execution.

Inspect Errors and Warnings

  • Navigate to the next/previous error or warning: F8/Shift+F8
  • Open/close the errors and warnings panel: Ctrl+Shift+M

Migrating from JSHint to ESLint

Configurations can be converted between JSHint and ESLint with Polyjuice. Install and run it as follows:

# Install Polyjuice globally
npm install -g polyjuice

# Convert JSHint to ESLint
# Run this in the directory that contains the .jshintrc file
polyjuice --jshint .jshintrc > .eslintrc

Comments

Related Posts

Fixing VS Code UI Unresponsiveness Caused by GitHub Copilot Extension

Fixing VS Code UI Unresponsiveness Caused by GitHub Copilot Extension
This article shows a simple solution to a problem that doesn’t seem to be adequately documented: VS Code UI lags, freezes, and delays caused by the GitHub Copilot extension. Problem: VS Code UI Becoming Slow Having worked on a Python project for a while, I noticed that VS Code’s UI was frequently freezing for seconds at a time. This was happening in various places of the UI: the editor itself but also in the GitHub Copilot Chat window. Copilot also seemed to be taking more and more time getting ready to answer, and the extension-host process would fully saturate one CPU core for long periods of time.
Applications

Latest Posts

Filebrowser to Be Archived. Migrate to a Modern Alternative: Sambee

Filebrowser to Be Archived. Migrate to a Modern Alternative: Sambee
Filebrowser is being archived. As the author writes, the codebase dates back to when he was but 15 years old. Having created such a popular file management tool is a more than impressive achievement for anyone, let alone a teenager. However, when it comes to security and reliability, Filebrowser seems to be built on a less than ideal foundation, so much so that the author states:
Home Automation, Networking & Self-Hosting