Prettier (.prettierrc)
Last updated
Last updated
Note: requires Prettier (VSCode extension)
Additional Setup:
"on save":
Cmd+Shift+P
> Preferences: Open [Workspace/User/...] Settings (JSON)
{
"editor.formatOnSave": true
}
{
// Optionally, specify formatting for specific file types
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
Finding within VSCode settings:
(top menu bar) > Code (File on Windows ?) > Settings > Settings
Type format on save
Ensure "Format on save" is checked
{
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"jsxBracketSameLine": false,
"arrowParens": "always",
"bracketSpacing": true,
"useTabs": false
}
semi
(Semicolons):
Option: "semi": true
or "semi": false
Use: Controls whether semicolons are added at the end of statements.
Popularity: Some developers prefer omitting semicolons in JavaScript for a cleaner syntax (as they are not mandatory), while others prefer keeping them for clarity and consistency with other languages.
singleQuote
(Single Quotes):
Option: "singleQuote": true
or "singleQuote": false
Use: Switches between single and double quotes in JavaScript code.
Popularity: Single quotes are often preferred in JavaScript for consistency with JSON and to differentiate string literals from other languages that use double quotes, like HTML.
printWidth
(Maximum Line Length):
Option: "printWidth": 80
(or another number)
Use: Sets the maximum line length Prettier will wrap on.
Popularity: A shorter printWidth
(like 80) helps in maintaining code readability, especially in code reviews and side-by-side diff views. It also improves readability on smaller screens.
tabWidth
(Tab Width):
Option: "tabWidth": 2
(or another number)
Use: Sets the number of spaces per indentation level.
Popularity: A tabWidth
of 2 is common in JavaScript communities for a balance between readability and conserving horizontal space.
trailingComma
(Trailing Commas):
Option: "trailingComma": "none"
, "es5"
, or "all"
Use: Controls where trailing commas are added.
Popularity: "es5"
is a popular choice as it adds trailing commas where valid in ES5 (objects, arrays, etc.), aiding in cleaner git diffs. "all"
can be used in codebases where modern JavaScript syntax is fully supported.
jsxBracketSameLine
(JSX Brackets):
Option: "jsxBracketSameLine": true
or false
Use: Controls the placement of the closing bracket of a JSX tag.
Popularity: Keeping the closing bracket on the same line as the last prop is often seen as more visually aligned with HTML style.
arrowParens
(Arrow Function Parentheses):
Option: "arrowParens": "avoid"
or "always"
Use: Controls the inclusion of parentheses around a sole arrow function parameter.
Popularity: "avoid"
is used to simplify the syntax of single-parameter arrow functions, while "always"
is preferred for consistency, especially in codebases with frequent use of TypeScript.
bracketSpacing
(Object Bracket Spacing):
Option: "bracketSpacing": true
or false
Use: Controls whether spaces are added inside object literal brackets.
Popularity: Having spaces ({ foo: bar }
) is common as it aligns with the conventional JavaScript object syntax and improves readability.
useTabs
(Use Tabs for Indentation):
Option: "useTabs": true
or false
Use: Determines whether to use tabs instead of spaces for indentation.
Popularity: This is more of a personal or team preference. Some prefer tabs for the ability to adjust visual indentation in different editors.
endOfLine
(End of Line):
Option: "endOfLine": "lf"
, "crlf"
, "cr"
, or "auto"
Use: Ensures a consistent line ending style across various operating systems.
Popularity: "lf"
(line feed) is commonly used in Unix/Linux/macOS environments, while "crlf"
(carriage return and line feed) is often preferred in Windows environments. "auto"
maintains existing line endings.
htmlWhitespaceSensitivity
(HTML Whitespace Sensitivity):
Option: "htmlWhitespaceSensitivity": "css"
, "strict"
, or "ignore"
Use: Controls how whitespaces in HTML, Vue, Angular, and Handlebars are handled.
Popularity: "css"
respects the display
property in CSS, making it a balanced choice for projects where HTML is styled with CSS.
jsxSingleQuote
(Single Quotes in JSX):
Option: "jsxSingleQuote": true
or false
Use: Uses single quotes instead of double quotes in JSX.
Popularity: This is useful for developers who prefer single quotes for consistency with JavaScript code, although JSX resembles HTML which typically uses double quotes.
quoteProps
(Object Property Quoting):
Option: "quoteProps": "as-needed"
, "consistent"
, or "preserve"
Use: Controls when quotes around object properties are required.
Popularity: "as-needed"
only adds quotes when necessary, which is cleaner, while "consistent"
requires all properties in an object to be quoted if at least one needs it, ensuring uniformity.
requirePragma
(Require Prettier Pragma):
Option: "requirePragma": true
or false
Use: Prettier will only format files that contain a special comment, called a pragma, at the top.
Popularity: Useful in large codebases where gradually introducing Prettier. It allows developers to mark files that are ready for Prettier formatting.
insertPragma
(Insert Prettier Pragma):
Option: "insertPragma": true
or false
Use: When a file is formatted with Prettier, it inserts a pragma at the top of the file.
Popularity: Handy for automatically marking files as formatted by Prettier, especially in combination with requirePragma
.
proseWrap
(Prose Wrapping):
Option: "proseWrap": "always"
, "never"
, or "preserve"
Use: Controls how Markdown and other prose are wrapped.
Popularity: "always"
ensures consistent line lengths in Markdown, which can be important for readability in certain editors or diff tools.
vueIndentScriptAndStyle
(Vue Files Script and Style Tag Indentation):
Option: "vueIndentScriptAndStyle": true
or false
Use: Determines whether <script>
and <style>
tags in Vue files should be indented.
Popularity: Setting this to true
can improve readability in Vue single-file components by visually separating the script and style sections from the template.