Ensuring Code Formatting Consistency in Open Source Contributions
Implement guidelines and tools to maintain consistent code formatting in open source projects.
0 likes
9 views
Rule Content
--- description: Enforce consistent code formatting across all source files in open source projects. globs: ['**/*.js', '**/*.ts', '**/*.jsx', '**/*.tsx', '**/*.py', '**/*.java', '**/*.c', '**/*.cpp', '**/*.cs', '**/*.php', '**/*.rb', '**/*.go'] tags: [code-formatting, open-source, consistency] priority: 1 version: 1.0.0 --- # Ensuring Code Formatting Consistency in Open Source Contributions ## Context - Applicable to all source code files in open source projects. - Ensures uniform code style to enhance readability and maintainability. ## Requirements - **Indentation**: Use spaces for indentation. The number of spaces per indentation level should be: - JavaScript/TypeScript: 2 spaces - Python: 4 spaces - Java: 4 spaces - C/C++: 4 spaces - C#: 4 spaces - PHP: 4 spaces - Ruby: 2 spaces - Go: Tabs (equivalent to 8 spaces) - **Line Length**: Limit lines to a maximum of 80 characters. - **Trailing Whitespace**: Remove trailing whitespace at the end of lines. - **Newlines**: Ensure a single newline at the end of each file. - **Braces Placement**: - JavaScript/TypeScript: Opening brace on the same line as the statement. - Python: Use indentation to define code blocks; braces are not used. - Java: Opening brace on the same line as the statement. - C/C++: Opening brace on the same line as the statement. - C#: Opening brace on the same line as the statement. - PHP: Opening brace on the same line as the statement. - Ruby: Use `do...end` for multi-line blocks; braces `{}` for single-line blocks. - Go: Opening brace on the same line as the statement. - **Naming Conventions**: - Use descriptive names for variables, functions, and classes. - Follow language-specific naming conventions: - JavaScript/TypeScript: camelCase for variables and functions; PascalCase for classes. - Python: snake_case for variables and functions; PascalCase for classes. - Java: camelCase for variables and methods; PascalCase for classes. - C/C++: snake_case or camelCase for variables and functions; PascalCase for classes. - C#: camelCase for variables and methods; PascalCase for classes. - PHP: camelCase for variables and methods; PascalCase for classes. - Ruby: snake_case for variables and methods; CamelCase for classes. - Go: camelCase for variables and functions; PascalCase for exported names. - **Comments**: - Provide meaningful comments to explain complex logic. - Avoid over-commenting; do not explain obvious code. - Use language-specific comment styles: - JavaScript/TypeScript: `//` for single-line, `/* */` for multi-line. - Python: `#` for single-line; triple quotes `"""` for multi-line docstrings. - Java: `//` for single-line, `/* */` for multi-line. - C/C++: `//` for single-line, `/* */` for multi-line. - C#: `//` for single-line, `/* */` for multi-line. - PHP: `//` or `#` for single-line, `/* */` for multi-line. - Ruby: `#` for single-line; `=begin` and `=end` for multi-line. - Go: `//` for single-line, `/* */` for multi-line. - **Tools**: - Utilize code formatters and linters to enforce these standards: - JavaScript/TypeScript: [ESLint](https://eslint.org/), [Prettier](https://prettier.io/) - Python: [Black](https://black.readthedocs.io/en/stable/), [Flake8](https://flake8.pycqa.org/en/latest/) - Java: [Checkstyle](https://checkstyle.org/) - C/C++: [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) - C#: [StyleCop](https://github.com/StyleCop/StyleCop) - PHP: [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) - Ruby: [RuboCop](https://rubocop.org/) - Go: [gofmt](https://golang.org/cmd/gofmt/) ## Examples // Good: Consistent indentation and brace placement function calculateTotal(price, tax) { const total = price + (price * tax); return total; } // Bad: Inconsistent indentation and brace placement function calculateTotal(price, tax) { const total = price + (price * tax); return total; } # Good: Consistent indentation and line length def calculate_total(price, tax): total = price + (price * tax) return total # Bad: Inconsistent indentation and line length def calculate_total(price, tax): total = price + (price * tax) return total