The Allman style is not applied by ESLint in VSCode to all languages, such as JSON

My disdain for Prettier stems from the fact that it restricts my freedom to utilize my preferred brace style.

In my workflow, I rely on tools like CSSComb, PHP CS Fixer, and SCSS Allman Formatter as they support Allman style. While VSCode offers native JavaScript and TypeScript brace style settings:

{
 "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true,
 "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
 "typescript.format.placeOpenBraceOnNewLineForControlBlocks": true,
 "typescript.format.placeOpenBraceOnNewLineForFunctions": true,
}

The issue arises when these settings are limited to specific languages only. My desire is to format all types of data in Allman style, including database structures, markup languages, and various programming languages, with the exception of Python.

I have come across recommendations to use ESLint as an alternative to Prettier and JS Beautifier.

To familiarize myself with ESLint, I extensively studied their documentation sections like Getting started and Configuring ESLint.

  1. I initiated the setup by creating a package.json file where I included ESLint development dependencies for the project. The contents of project/package.json are as follows:

    {
      "editor.codeActionsOnSave": 
      {
        "source.fixAll.eslint": true
      },
     "eslint.format.enable": true,
     "eslint.alwaysShowStatus": true,
     "eslint.validate": 
     [
       "css",
       "javascript",
       "json",
       "jsonc",
       "markdown",
       "scss",
       "typescript"
     ],
    }
    
  2. I then created a .eslintrc file in YAML format within the project folder. The content of project/.eslintrc.yml is as below:

    env:
      browser: true
      es2021: true
      node: true
    overrides: []
    parserOptions:
      ecmaVersion: latest
    rules:
      brace-style:
        - error
        - allman
    
  3. Lastly, I configured the settings in project/.vscode/settings.json:

    {
      "editor.codeActionsOnSave": 
      {
        "source.fixAll.eslint": true
      },
      "eslint.format.enable": true,
      "eslint.alwaysShowStatus": true,
      "eslint.validate": 
      [
        "css",
        "javascript",
        "json",
        "jsonc",
        "markdown",
        "scss",
        "typescript"
      ],
    }
    

Despite my efforts to run ESLint by selecting Format Document from the Command Palette, it did not yield the desired formatting results.

Experimenting with creating an eslintrc.json file showed promising outcomes, yet JSON files were still not formatted according to Allman style preferences.

Reflecting on past experiences with Atom, I reminisce about the flexibility it offered in allowing me to apply Allman style across numerous languages.

Answer №1

Regrettably, the current version of eslint-plugin-json does not have auto-fixing capability. Fortunately, there is a different independent package called eslint-plugin-json-format that does.

Firstly, ensure that you have eslint installed either globally or locally. Secondly, press Ctrl+Shift+P > Open UI Settings, search for eslint.probe, and add json if it's missing. Then, execute either of the following commands:

  • npm install eslint-plugin-json-format --save-dev
  • yarn add -D eslint-plugin-json-format

Next, include this in your .eslintrc.json:

{
  "plugins": [
    "json-format"
  ]
}

Now, create a JSON file with comments or misalignment and run:

eslint --fix yourfile.json

To witness it in operation.

Answer №2

If you're looking to set a default formatter for JSON files, try adding the following snippet to your vscode settings.json file. This will ensure that vscode uses your preferred editor formatter for JSON files, like ESLint.

  "[json]": {
    "editor.defaultFormatter": "vscode.json-language-features"
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Oscillator node in the Web Audio API is unable to be started more than once due to an error

Every time I attempt to start, stop, and then start my oscillator again, I encounter the following error message: Uncaught InvalidStateError: Failed to execute 'start' on 'OscillatorNode': cannot call start more than once. Although usi ...

What is the process for updating the internal TypeScript version in VS Code?

When using VS Code, I noticed it comes with its own TypeScript version: Is there a way to update this? The current version is 4.9.3. https://i.sstatic.net/vEx85.png ...

Issue encountered with the Selenium JavaScript Chrome WebDriver

When it comes to testing an application, I always rely on using Selenium chromewebdriver. For beginners like me, the starting point was following this insightful Tutorial: https://code.google.com/p/selenium/wiki/WebDriverJs#Getting_Started After download ...

When attempting to parse a JSON string with Newtonsoft, it results in a null reference exception being raised

I need help parsing this JSON string: { "layers":[ { "metadata":{ "cells":2, "records":42887000, "dataset":"uk_da", "query":"155ms", "sample":10, "calibrate":100 } }, { "lookups":{ ...

Using Regular Expressions to Isolate Twitter Usernames and IDs in URLs

I'm attempting to extract the tweet URL from a message using this regex pattern: #^https?://twitter\.com/(?:\#!/)?(\w+)/status(es)?/(\d+)$#is However, it seems that my regex is not working correctly to extract the tweet URL. Here ...

The onChange method seems to be malfunctioning when used with radio buttons

I'm having an issue with my form's radio button. It is supposed to do something when the selected item changes, but it only works when the page first loads and not when I select a different item. Below is the code snippet: <div key={item} cla ...

Executing JavaScript from the Code Behind file in ASP.NET

The NOTIFY plugin is being used on an html input to inform the user if a record has been saved successfully. It works properly when used directly, but does not work when called from the code behind file. Despite attempting to use RegisterStartupScript, the ...

Using React to trigger a child component's function upon clicking a tab

I need assistance with creating a React app that includes two tabs (Tab1, Tab2). When Tab2 is clicked, I want a message to appear in the console saying 'Function A is called'. Unfortunately, the code I have currently does not display the message ...

Is it possible to use an angular expression as the ng-click function?

I have been searching everywhere for the answer to this question. Within my code, there is an angular object called column.addOnParams. This object includes a child element named rowClick, which can be accessed using column.addOnParams.rowClick. The val ...

unable to retrieve / interpret data from herdsmen using fetch

When sending a request to a node server, the server responds with headers and a blank body. Despite being able to view the headers in the network activity panel within dev-tools, I faced difficulties reading them using the following code: let uploaded ...

Unable to display any posts in my React application, no content is being produced

I am currently in the process of developing a blogging site using React by following a tutorial. However, I have encountered an issue where the posts are not appearing on the screen. Here is the link to my GitHub repository: https://github.com/AkshatGarg2 ...

What is the best way to capture a screenshot using selenium in conjunction with synchronous JavaScript?

Currently, I am developing an automated test using javaScript and leveraging a node library called webdriver-sync. This library simplifies writing selenium tests by eliminating the need for callbacks and promises, and it utilizes the java Webdriver API. Su ...

verifying the presence of a string or value within an array

I'm currently working on a feature that involves adding an element to an array by typing something into a text field and clicking a button. I've also implemented an if statement to check if the input already exists in the table, and show an alert ...

What is the simplest way to extract only the error message?

Having this code snippet. $('div#create_result').text(XMLHttpRequest.responseText); If we look at the content of XMLHttpRequest, it shows: responseText: Content-Type: application/json; charset=utf-8 {"error" : "User sdf doesn't exist"} st ...

The form is not being submitted when I click on the submit button because I have two buttons that trigger AJAX requests

I am facing an issue with a form where there is a submit button inside it, as well as another button within the form that has attributes type=button and onclick=somefunction(). The button with the onclick function works perfectly fine, but the other button ...

css: positioning images with overlap in a responsive layout

Two divs have been created with background images, both displaying the same image but in different colors - one grey and the other green. These images are waveforms. Initially, only the grey image (div1) is visible while the green image (div2) remains hid ...

Using restKit for handling specific JSON responses that do not directly correspond to the core data managed objects

Let's consider the scenario where we are working with an API that provides data in JSON format and we aim to utilize Restkit for mapping to two core data objects: "products" and "resources". These two objects have a many-to-one relationship, meaning o ...

The app has crashed, currently waiting for any file changes before starting again

Can someone assist me? I am encountering an issue while trying to run npm start. The error pertains to the server.js file. [nodemon] restarting due to changes... [nodemon] starting `node server.js` /Users/johnngo/Desktop/LambdaSchool/HTTP-AJAX/server.js ...

Updating CSS stylesheets dynamically when onchange event occurs

I am currently able to dynamically change style rules using JS, but I feel that this is limiting. I would prefer to be able to swap entire CSS stylesheet files easily. Do you have any suggestions on how to accomplish this? Here is my code: <label>T ...

"Discover the magic of jQuery: Unveiling the hidden div with one simple CSS visibility change

I want to implement a functionality on my screen where the Previous button is hidden initially and only becomes visible when the user clicks the Next button. I have set the CSS property for the Previous button to hide it by default, but despite using an if ...