Skip to content

Latest commit

 

History

History
151 lines (95 loc) · 8.06 KB

README.md

File metadata and controls

151 lines (95 loc) · 8.06 KB

All Contributors

A Visual Studio Code Extension to format JavaScript and TypeScript code using the prettier-eslint package.

Please create an issue before adding a rating. Keep in mind that I work full-time and I am the only contributor currently. I'd LOVE to have more contributors. See the Contributing section below.

Prerequisites

This extension requires the following packages to be installed either locally or globally:

  • prettier@^2.4.1

  • eslint@^7.32.0 (ESLint 8 has breaking changes thus not supported)

  • @typescript-eslint/parser@^5.0.1 and typescript@^4.4.4 (Only for TypeScript projects)

  • vue-eslint-parser@^8.0.0 (Only for Vue projects)

Example Projects

These projects are setup to work with the VS Code Prettier ESLint extension. Use them as a boilerplate for your project or as a reference for an existing project. If you don't see an example for your tech stack, create a PR of a working example.

Troubleshooting

  1. Check for an error by opening the View menu and click Output
  2. Select Prettier ESLint from the dropdown to the right

troubleshooting

  1. Open a JavaScript (.js or .jsx) or TypeScript (.ts or .tsx) file
  2. Press CTRL + SHIFT + P (Windows/Linux) or CMD + SHIFT + P (macOS) to open the command palette
  3. Start typing Format Document With and select Prettier ESLint
  4. Click on Output to open the panel
  5. If you see Error: Cannot find module, quit and restart Visual Studio Code
  6. If restarting did not work:
    • Make sure you have the required packages installed locally (global installations don't work sometimes)
    • Repeat Step 8

Installation

Notes:

Install Dependencies

  1. In your project, install eslint@6.8.0, prettier@1.9.1, & prettier-eslint:^10.1.0 as devDependencies
  2. Additionally install:
    • @typescript-eslint/parser@^3.0.0 for TypeScript projects (.ts & .tsx files)
    • vue-eslint-parser@~7.1.0 for Vue projects (.vue files)

Install the Extension

  1. Install the extension
  2. Quit VS Code
  3. Restart VS Code

Set as Default Formatter

  1. Open the file you want to format
  2. Press CTRL + SHIFT + P (Windows/Linux) or CMD + SHIFT + P (macOS) to open the command palette
  3. Start typing Format Document With and select it
  4. Choose the bottom item Configure Default Formatter
  5. Choose Prettier ESLint

Run the Formatter

Once installed, open a JavaScript (.js or .jsx) or TypeScript (.ts or .tsx) file. Press SHIFT + OPTION + F (macOS) or SHIFT + ALT + F (Windows/Linux) to format a file.

You can enable the Format on Save setting to avoid entering the command all the time.

Setup Format on Save

  1. Set Prettier Eslint as the default formatter as detailed above
  2. Open User or Workspace settings
  • On Windows/Linux - File > Preferences > Settings
  • On MacOS - Code > Preferences > Settings
  1. Start typing Format on and select it
  2. Check Format on Save (found mid-page in the right panel)
  3. Ensure both Format on Input and Format on Paste are disabled. They are not supported functionality

Afterward, the file should format automatically once you save it

For the best performance, change Auto Save to onFocusChanged. This will have the editor save changes when you switch to another file or another program. The default setting makes the file save after you stop typing for an interval. This causes the formatter to run frequently thus taking more computer resources.

  1. Open User or Workspace settings
  • On Windows/Linux - File > Preferences > Settings
  • On macOS - Code > Preferences > Settings
  1. Start typing Auto Save and select onFocusChanged

Configuration

The extension uses your ESLint and Prettier configuration files. These files are resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn't) found.

ESLint Configuration File

(From https://eslint.org/docs/user-guide/configuring)

Use a JavaScript, JSON or YAML file to specify configuration information for an entire directory and all of its subdirectories. This can be in the form of an .eslintrc.* file or an eslintConfig field in a package.json file.

Prettier Configuration File

(From https://prettier.io/docs/en/configuration.html)

Prettier uses cosmiconfig for configuration file support. This means you can configure prettier via (in order of precedence):

  • A "prettier" key in your package.json file.
  • A .prettierrc file, written in JSON or YAML, with optional extensions: .json/.yaml/.yml (without extension takes precedence).
  • A .prettierrc.js or prettier.config.js file that exports an object.
  • A .prettierrc.toml file, written in TOML (the .toml extension is required).

Contributing

If you have suggestions for how this extension could be improved, or want to report a bug, open an issue! I'd love all and any contributions. If you are interested in contributing to the project, check out the Contributing Guide.

Current Contributors ✨

Thanks goes to these wonderful people (emoji key):


Rebecca Vest

💬 💻 📖 🚇 🐛 🤔 👀 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!