Skip to content

Instantly share code, notes, and snippets.

@montasim
Created July 6, 2024 14:20
Show Gist options
  • Save montasim/baf91fa5808634747e209c30e9247968 to your computer and use it in GitHub Desktop.
Save montasim/baf91fa5808634747e209c30e9247968 to your computer and use it in GitHub Desktop.
Using a .prettierrc file in your project ensures that everyone contributing to the codebase follows the same formatting rules. This eliminates discussions over style in code review, saves time, and reduces inconsistencies that can lead to errors. It can also make it easier for new developers to adhere to project standards from the outset. Pretti…
{
"semi": true,
"singleQuote": true,
"arrowParens": "always",
"trailingComma": "es5",
"bracketSpacing": true,
"tabWidth": 4,
"useTabs": false,
"endOfLine": "crlf",
"overrides": [
{
"files": "*.json",
"options": {
"tabWidth": 2
}
}
]
}
@montasim
Copy link
Author

montasim commented Jul 6, 2024

Configuration Overview

.prettierrc defines the formatting rules for Prettier. This configuration ensures that your codebase maintains a consistent style, which can improve readability and reduce the amount of time spent formatting code manually.

Configuration Options

"semi": true: This option ensures that statements are always terminated with semicolons. It helps catch potential errors in JavaScript where automatic semicolon insertion may lead to unexpected behavior.

"singleQuote": true: Enforces the use of single quotes for strings instead of double quotes, which can help keep the code cleaner and more consistent with other programming languages that prefer single quotes.

"arrowParens": "always": Always include parentheses around a single arrow function parameter. This increases clarity, making it immediately clear that you're working with a function.

"trailingComma": "es5": Allows trailing commas in object literals, arrays, and function parameters but only where supported in ES5 (mainly in arrays and objects). This can make version control diffs cleaner and editing code slightly more convenient.

"bracketSpacing": true: Prints spaces between brackets in object literals. This can make objects easier to read and can align with style guides that prefer whitespace.

"tabWidth": 4: Sets the number of spaces per indentation-level. A tabWidth of 4 can improve readability, especially in more nested code, although it is more space-consuming than a smaller width.

"useTabs": false: Indent lines with spaces, not tabs. This can help ensure that the code looks the same in any environment, as the size of a tab can vary between editors.

"endOfLine": "crlf": Enforces the use of Windows-style line endings (carriage return and line feed). This is useful if your team is using Windows or if your project needs consistent line endings across multiple operating systems.

Overrides

"overrides": This section allows you to apply specific formatting rules to files matching certain patterns, overriding the global settings.
"files": "*.json": Applies the override to all JSON files.
"options": { "tabWidth": 2 }: Sets the indentation for JSON files to 2 spaces. This is common for JSON files, as it helps keep the data structures compact and readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment