In VS Code, you can simplify self-closing tag slashes with Prettier. Prettier automatically formats your code, including removing unnecessary self-closing tag slashes.
With Prettier, you can configure it to remove self-closing tag slashes on a per-project basis. This means you can choose whether to remove them or keep them in your specific project settings.
Prettier removes self-closing tag slashes by default in newer versions. If you're using an older version, you might need to update to get this feature.
VSCode Setup
To set up Prettier in VSCode, you need to install the Prettier VSCode extension. Launch VSCode Quick Open by pressing Ctrl+P and run the command `ext install esbenp.prettier-vscode`.
You should also create a local workspace VSCode settings file, such as `.vscode/settings.json`, to ensure everyone uses the same extension to format code. This file should contain the following settings:
- `"editor.defaultFormatter":"esbenp.prettier-vscode"`
- `"editor.formatOnSave":true`
With these settings, every time you save a JavaScript file, it will be formatted using Prettier automatically.
VSCode Setup
To set up Prettier in VSCode, you need to install the Prettier VSCode extension. Launch VSCode Quick Open (Ctrl+P) and run the command "ext install esbenp.prettier-vscode".
You might have global settings related to code formatting, so it's a good idea to have a local workspace VSCode settings file in each repository. This file should be committed to source control to ensure everyone uses the same extension to format code.
The local workspace settings file is called .vscode/settings.json, and it should contain the following code:
{
"editor.defaultFormatter":"esbenp.prettier-vscode",
"editor.formatOnSave":true
}
This will format your code automatically every time you save a JavaScript file. For example, if you have a file called projectA/index.js, it will be formatted with double quotes, semi-colons, and other settings from projectA/.prettierrc.json.
You can customize the formatting settings to your liking. For instance, you can disable formatting on paste by adding "editor.formatOnPaste":false to the .vscode/settings.json file.
To ensure the Prettier extension is enabled, right-click on the Status Bar and verify that "Prettier" is displayed.
Sources
- https://teamtreehouse.com/community/should-you-or-should-you-not-close-html-void-elementsselfclosing-tags-with-a-forward-slash
- https://github.com/orgs/mdn/discussions/242
- https://www.shecodes.io/athena/6628-why-does-prettier-ask-me-to-close-body-in-vs-code
- https://glebbahmutov.com/blog/configure-prettier-in-vscode/
- https://wordpress.org/plugins/code-block-pro/
Featured Images: pexels.com