Visual Studio Code Automatic Formatting



Formatting your code can be a hassle. Learn how to have your code automatically formatted to save yourself a lot of time and headache!Learn Build Teach - ht. Visual Studio Code Prettify JSON Extension Hit F1 or 'cmd+shift+p' and type install and then type Prettify JSON and hit enter. Likewise, how do I arrange code in Visual Studio? Format Document (Ctrl+K, Ctrl+D) so type Ctrl+K, and then Ctrl+D as it is a sequence. Format Selection (Ctrl+K, Ctrl+F). 3 Steps to Format Your VS code.1) Open Visual Studio.2) Press ( Ctrl+A ) to select All The Code.3) Press ( Ctrl + K + F ) to Format / Arrange the code.

Visual studio code auto formatvisual studio code auto format on savevisual studio code auto format macvisual studio code auto format jsonvisual studio code a.

My last few articles have focused on using Visual Studio Code to build a series of scripts to help manage Analysis Services data models. As part of this work, I’ve found Visual Studio Code to be extremely helpful in other ways too, such as a very powerful text editor with many features you may find in other advanced text editing tools. So much so, I now use Visual Studio Code as the primary tool I use when working with text files – regardless if any code is involved.

One task in particular I find helpful is to auto format JSON text to either make it more readable, or to identify issues with the structure of the JSON itself – such as missing brackets etc.

Here is a simple example showing how you can create an empty file and paste in some JSON text. Depending on the JSON, the file will auto detect you are working with JSON data, or you need to set the language using the status bar in the bottom right-hand corner.

The animation also shows how you can use the tool to help you fix issues to do with how well-formed the the JSON code is.

The key-map to auto-format the selected JSON is ALT-SHIFT-F.

This is the same key-map to auto-format other languages too, so I often find myself doing CTRL-A (for select all text) followed by ALT-SHIFT-F to fix my messy C# code after a series of cut and paste operations.

Another Visual Studio Code tip

If you find you enjoy using Visual Studio Code to work with text documents, I recommend you ensure the following settings are enabled when you install (or re-install) Visual Studio Code. Once you enabled these check-boxes, you get the option to “Open in Code” when right-clicking a file or folder in Windows Explorer. Essential stuff!

Article Rating

Tutorial

Introduction

When writing JavaScript with an editor such as Visual Studio Code, there are a number of ways you can ensure your code is syntactically correct and in line with current best practices. You can use a linter to do this. Linters check your code for syntax errors and highlight errors to make sure you can quickly find and fix them. ESLint is a linter that you can integrate into your Visual Studio Code setup in order to ensure code integrity.

ESLint can both format your code and analyze it to make suggestions for improvement. It is also configurable. This means that you can customize how your code is evaluated.

In this tutorial, you will set up ESLint on Visual Studio Code and implement a custom configuration to deal with log statements in debugging. You will also configure ESLint to automatically fix syntax errors when you save your files.

Prerequisites

To complete this tutorial, you will need the following:

  • The latest version Visual Studio Code installed on your machine. This tutorial uses Visual Studio Code version 1.43.0.
  • The latest version of Node installed on your machine. You can accomplish this by following the How to Install Node.js and Create a Local Development Environment for your machine.

Visual Studio Code Formatter

Visual Studio Code Automatic Formatting

Step 1 — Creating JavaScript Starter Code

You need to start with a demo project. Create a directory for your project with the following command:

Now that your project folder is created switch into the linting directory:

While inside of the linting directory, create a JavaScript file with the name app.js:

Open app.js in Visual Studio Code. Write the following JavaScript code in your app.js file:

From a formatting perspective, you may notice several things that could be improved:

  • Inconsistent use of quotes
  • Inconsistent use of semicolons
  • Spacing

With this JavaScript file in place, you can now initialize this project. To do this, navigate back to your command line and in the linting directory, run the following command:

Using the npm init command to initialize your project will create a package.json file in the linting directory. The package.json will store your project dependencies and other important configuration settings for your project.

Now that your JavaScript project is properly set up, you can now set up ESLint.

Step 2 — Setting Up ESLint

Formatting

Before you set up ESLint for your project, you will first need to install ESLint:

Visual Studio Code Automatic Formatting

It’s important to include the --save-dev flag because this saves the package as a dependency for development usage only. In this case, eslint is a package that is only needed when you are actively working on and making changes to your project. Once your project launches or is in production, eslint will no longer be needed. Using the --save-dev flag ensures that eslint will be listed in your package.json file as a development dependency only.

Now that ESLint is installed, you can initialize an ESLint configuration for your project using the following command:

An important piece in this command is the --init flag. The ./node_modules/.bin/eslint section of the command is the path to ESLint in your project. Using the --init flag activates ESLint for your project. Activating or initializing ESLint will create an ESLint configuration file that will allow you to customize how ESLint works with your project.

Before you can access your ESLint configuration file, you will be prompted with different questions about your project. These questions are asked to make sure that the configuration that is initialized for your project best fits your needs.

The first prompt will be:

Visual Studio Code Automatic Formatting

Choose the To check syntax, find problems, and enforce code style option.

The next prompt will be:

Choose the CommonJS option to use CommonJS global variables.

The next prompt will say:

Choose the None of these option.

The next prompt will ask:

Choose the No option.

The following prompt will say:

Choose the Browser option.

The next prompt will say:

Choose the Use a popular style guide option.

For the Which style guide do you want to follow? prompt, choose the Airbnb: https://github.com/airbnb/javascript option.

The next prompt will ask:

Choose the JSON option.

You will then see this message:

The last prompt will ask:

Choose the Yes option to install the dependencies with npm.

You will also be asked to install extra packages. Choose yes.

After completing all the prompts, you’ll notice that a file named .eslintrc.json has been added to your linting directory. ESLint is now installed. The code in app.js hasn’t changed yet. This is because ESLint needs to be integrated with Visual Studio Code.

Step 3 — Configuring ESLint

To integrate ESLint into Visual Studio Code, you will need to install the ESLint extension for Visual Studio Code. Navigate back to Visual Studio Code and search for ESLint in the Extensions tab. Click Install once you have located the extension:

Once ESLint is installed in Visual Studio Code, you’ll notice colorful underlining in your app.js file highlighting errors. These markers are color-coded based on severity. If you hover over your underlined code, you will see a message that explains the error to you. In this way, ESLint helps us find and remove code and syntax errors.

ESLint can do even more for you. ESLint can be modified to automatically fix errors every time a file is saved.

Step 4 — Formatting on Save

To configure ESLint to automatically fix syntax and formatting issues every time you save, you will need to open the settings menu. To find the settings in Visual Studio Code, click on the gear icon in the lower left, and then choose Settings.

Within the settings menu, search for Code Actions on Save. The first option will say Editor: Code Actions on Save and below that, there will be an option to Edit in settings.json. Click the link to Edit in settings.json.

The settings.json file will open inside of your code editor. For ESLint to fix errors when you save your file, you will need to write the following code in settings.json:

settings.json

With this code in your settings.json file, ESLint will now automatically correct errors and validate JavaScript on save.

Return back to your app.js file and save it. You will see some changes, including less colorful underlining. Some of the formatting issues that ESLint has fixed include:

Visual studio code auto format xml
  • Consistent use of single quotes
  • Proper indentation inside of the function
  • Consistent use of semicolons

ESLint will now automatically solve syntax errors whenever you save app.js. There are still some remaining error messages. These can be fixed by customizing the ESLint configuration to catch or ignore specific errors and formatting issues.

Step 5 — Customizing ESLint Configuration

As is, ESLint produces a highlighted message for all console.log() statements in app.js. In some cases, removing console.log statements may not be a priority. You can customize the ESLint configuration to allow console.log statements without producing an error message. ESLint configuration rules can be modified in the .eslintrc.json file.

Open up the .eslintrc.json file. This is the code you will see in that file:

At the bottom of the .eslintrc.json file, you will see a 'rules' object. To customize the errors that trigger ESLint or to disable ESLint’s response to certain pieces of code, you will add key-value pairs to the 'rules' object. The key will match the name of the rule you want to add or change. The value will match the severity level of the issue. You have three choices for severity level:

  • error - produces a red underline
  • warn - will produce a yellow underline
  • off - will not display anything.

If you do not want to produce any error messages for console.log statements, you can use the no-console rule name as the key. Input off as the value for no-console:

linting/.eslintrc.json

This removes the error messages from your console.log statements in app.js:

Some rules require multiple pieces of information, including a severity level and a value. To specify the type of quotes you want to use in your code, you have to pass in both the chosen type of quotes and the severity level:

Now, if you include single quotes in your quote, ESLint will raise an error.

Visual Studio Code Auto Formatting On Save

Conclusion

This tutorial introduces some of what you can do with linting using ESLint on Visual Studio Code. Linting tools like ESLint can help create time for more complex tasks by automating and simplifying how you verify syntax and best practices.

If you would like more information about rules and what key-value pairs you can use to customize your ESLint rules, you can check out this documentation.





Comments are closed.