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.
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
andtypescript@^4.4.4
(Only for TypeScript projects) -
vue-eslint-parser@^8.0.0
(Only for Vue 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.
- Check for an error by opening the
View
menu and clickOutput
- Select
Prettier ESLint
from the dropdown to the right
- Open a JavaScript (
.js
or.jsx
) or TypeScript (.ts
or .tsx
) file - Press
CTRL + SHIFT + P
(Windows/Linux) orCMD + SHIFT + P
(macOS) to open the command palette - Start typing
Format Document With
and selectPrettier ESLint
- Click on
Output
to open the panel - If you see Error: Cannot find module, quit and restart Visual Studio Code
- If restarting did not work:
- Make sure you have the required packages installed locally (global installations don't work sometimes)
- Repeat Step 8
Notes:
-
The Prettier extension is not required.
-
The ESLint extension is not required. However, it is needed to have lint errors show while editing your file.
- In your project, install
eslint@6.8.0
,prettier@1.9.1
, &prettier-eslint:^10.1.0
asdevDependencies
- 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
- Quit VS Code
- Restart VS Code
- Open the file you want to format
- Press
CTRL + SHIFT + P
(Windows/Linux) orCMD + SHIFT + P
(macOS) to open the command palette - Start typing
Format Document With
and select it - Choose the bottom item
Configure Default Formatter
- Choose
Prettier ESLint
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.
- Set
Prettier Eslint
as the default formatter as detailed above - Open
User
orWorkspace
settings
- On Windows/Linux -
File > Preferences > Settings
- On MacOS -
Code > Preferences > Settings
- Start typing
Format on
and select it - Check
Format on Save
(found mid-page in the right panel) - Ensure both
Format on Input
andFormat 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.
- Open
User
orWorkspace
settings
- On Windows/Linux -
File > Preferences > Settings
- On macOS -
Code > Preferences > Settings
- Start typing
Auto Save
and selectonFocusChanged
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.
(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.
(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 yourpackage.json
file. - A
.prettierrc
file, written in JSON or YAML, with optional extensions:.json/.yaml/.yml
(without extension takes precedence). - A
.prettierrc.js
orprettier.config.js
file that exports an object. - A
.prettierrc.toml
file, written in TOML (the.toml
extension is required).
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.
Thanks goes to these wonderful people (emoji key):
Rebecca Vest 💬 💻 📖 🚇 🐛 🤔 👀 |
This project follows the all-contributors specification. Contributions of any kind welcome!