January 17th, 2021
Download VSCode extensions
Prettier can't identifyEditorConfig
EditorConfig allows you to define indentation style and other whitespace settings for any file type. This way your editor can automatically choose the correct settings. This is handy when developers use platforms with different line endings, e.g., Mac and Windows.
Use this plugin to format any file type that prettier and eslint can't identify.
Create .editorconfig file:
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
# # Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2Install Prettier
Prettier code formatter for most web development file types, like JavaScript, TypeScript, Flow, JSX, JSON, CSS, SCSS, Less, HTML, Vue, Angular, GraphQL, Markdown, YAML and so on.
npm install -D prettierCreate .prettierrc.yaml file:
# .prettierrc or .prettierrc.yaml
trailingComma: "es5"
tabWidth: 2
semi: true
singleQuote: trueInstall Eslint
Find and fix problems in your JavaScript code.
Install Eslint and Prettier libraries:
npm install -D eslintInstall eslint-config-airbnb:
npx install-peerdeps --dev eslint-config-airbnbInstall eslint-config-prettier(turns off all rules that are unnecessary or might conflict with Prettier) and eslint-plugin-prettier(runs Prettier as an ESLint rule and reports differences as individual ESLint issues):
npm install -D eslint-config-prettier eslint-plugin-prettierCreate .eslintrc.json file:
{
"extends": ["airbnb", "prettier"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": ["error"],
"react/jsx-filename-extension": "off",
"react/prefer-stateless-function": "off",
"react/prop-types": "off",
"react/destructuring-assignment": "off"
}
}Prettier Plugins
Prettier Plugins are automatically loaded if you have them installed in the same node_modules directory where prettier is located. Plugin package names must start with @prettier/plugin- or prettier-plugin- or @<scope>/prettier-plugin- to be registered.
Install stylelint
npm install -D stylelint stylelint-config-standardCreate .stylelintrc.json file
{
"extends": [
"stylelint-config-standard",
"stylelint-config-idiomatic-order"
]
}Default Formatter
To ensure that this extension is used over other extensions you may have installed, be sure to set it as the default formatter in your VS Code settings. This setting can be set for all languages or by a specific language.
Create .vscode/settings.json file:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
// or
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}Auto format
Format on save, "editor.formatOnSave": true
// .vscode/settings.json
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
// Use prettier format default
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
// Use eslint instead of prettier
"source.fixAll.eslint": true,
// Use tslint instead of prettier
"source.fixAll.tslint": true,
// Use stylelint instead of prettier
"source.fixAll.stylelint": true
},
// Disable prettier format js
"[javascript]": {
"editor.formatOnSave": false
},
// Disable prettier format jsx
"[javascriptreact]": {
"editor.formatOnSave": false
},
// Disable prettier format ts
"[typescript]": {
"editor.formatOnSave": false
},
// Disable prettier format tsx
"[typescriptreact]": {
"editor.formatOnSave": false
},
// Disable prettier format css
"[css]": {
"editor.formatOnSave": false
},
// Disable build-in error report
"css.validate": false,
"less.validate": false,
"scss.validate": false
}