How A Visual Studio Code Linter Works

August 6, 2017

I blogged about what a basic Visual Studio Code language extension should contain a few months ago, and today we focus on linter.

Linter Basic

The essential responsibility of a linter is to scan your source file and report back warnings and errors. Many programming languages do have good command line linters, so the remaining task of an extension is quite simple,

  • Pass the file information to linter.
  • Collect linter output and display.

Study By Example

Now let’s analyze the ruby-linter extension to better understand how it works.

First, this extension must be triggered by a special event “onLanguage”,"activationEvents": [ "onLanguage:ruby" ]. That means when a Ruby file is opened, the extension would be triggered.

Second, the extension initializes a linting provider to wrap the actual command line linter. The linting provider in turn calls ruby -wc to executing the task.

Finally, the command line output of the linter should be parsed.

Visual Studio Code defines the Diagnostic type to store warnings and errors, so that we can easily use regular expressions to extract information from command line output and pass it to the editor.

As the infrastructure is quite simple, you can make use of the code to implement a similar linter for another programming language in a just a few hours (I did this for reStructuredText).

All rights reserved. © Lex Li, 2005-2021