[Idea]: Introduce Linter & Formatter
Overview of the requested feature
We will bring order to the code by introducing a Linter and Formatter with predefined coding rules into the development environment.
Reason for the request
Pull Requests from beginners or first-time OSS contributors often lack readability and consistency, which can hinder discussions on the implementation of new features or bug fixes.
To reduce the overhead of reviewing non-essential aspects of the code, introducing a Linter and Formatter is highly effective.
Related Issues
N/A
Check List
- [x] The proposed feature is beneficial to a wide range of users and not just a personal preference.
- [x] Checked that no similar requests already exist.
Regarding the coding style, I would like to introduce a Linter and Formatter in the future.
Which linter and formatter do you have in mind?
@mor39a I wasn't aware of the de facto standard for Linters in the Visual Studio and C# environment, but after looking into it, it seems that there are built-in features like EditorConfig and "dotnet format".
Let's discuss the Linter and Formatter in an Issue. → #283
Originally posted by @Kyome22 in https://github.com/Kyome22/RunCat365/issues/282#issuecomment-3831131372
In one of my first contributions to this project, I started with the idea of adding an .editorconfig file. Although I set it aside to move forward with other tasks, I have a draft that I can share with you. I don’t remember exactly where I left off, but here it is.
Edit: Together with Claude, I just remembered and improved the .editorconfig file:
root = true
# C# files
[*.cs]
charset = utf-8-bom
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
################################
# CODE FORMATTING
################################
# Braces always on new line
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true
csharp_new_line_between_members_of_anonymous_types = true
csharp_new_line_between_members_of_object_initializers = true
csharp_new_line_within_query_expression_clauses = true
# Indentation
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Spacing
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_after_cast = false
# Parentheses
csharp_space_between_parentheses = false
csharp_space_between_attribute_brackets = false
################################
# CODE STYLE
################################
# var usage (allowed everywhere)
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion
# this. qualification (not required)
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Using directives
dotnet_sort_system_directives_first = true:error
dotnet_separate_import_directive_groups = true:error
dotnet_remove_unnecessary_usings = true:error
# Null handling
dotnet_style_null_propagation = true:warning
dotnet_style_coalesce_expression = true:warning
# Prefer "is null" over == null
csharp_style_prefer_is_null_check_over_reference_equality_method = true:error
# Braces preferences - allow single line if without braces
csharp_prefer_braces = when_multiline:suggestion
################################
# STYLECOP ANALYZERS
################################
# For info about StyleCop Rules => https://github.com/DotNetAnalyzers/StyleCopAnalyzers/tree/master/documentation
# Documentation rules
dotnet_diagnostic.SA1600.severity = none # Elements should be documented
dotnet_diagnostic.SA1611.severity = none # Parameters should be documented in XML comments
dotnet_diagnostic.SA1615.severity = none # Return values should be documented in XML comments
dotnet_diagnostic.SA1633.severity = error # Require copyright message
dotnet_diagnostic.SA1636.severity = error # Correct copyright message
# Spacing Rules
dotnet_diagnostic.SA1003.severity = error # The spacing around an operator symbol is incorrect, within a C# code file.
dotnet_diagnostic.SA1004.severity = error # A line within a documentation header above a C# element does not begin with a single space.
dotnet_diagnostic.SA1010.severity = suggestion # An opening square bracket within a C# statement is not spaced correctly.
# Readability Rules
dotnet_diagnostic.SX1101.severity = warning # A call to an instance member of the local class or a base class is prefixed with 'this.'.
dotnet_diagnostic.SA1101.severity = none # A call to an instance member of the local class or a base class is not prefixed with 'this.', within a C# code file.
dotnet_diagnostic.SA1106.severity = error # The C# code contains an extra semicolon.
dotnet_diagnostic.SA1131.severity = warning # A comparison was made between a variable and a literal or constant value, and the variable appeared on the right-hand side of the expression.
# Ordering Rules
dotnet_diagnostic.SA1200.severity = error # A C# using directive is placed outside of a namespace element.
dotnet_diagnostic.SA1201.severity = error # An element within a C# code file is out of order in relation to the other elements in the code.
dotnet_diagnostic.SA1202.severity = error # An element within a C# code file is out of order within regard to access level, in relation to other elements in the code.
dotnet_diagnostic.SA1206.severity = error # A using directive which declares a member of the System namespace appears after a using directive which declares a member of a different namespace, within a C# code file.
dotnet_diagnostic.SA1208.severity = error # System.* namespaces must appear first
dotnet_diagnostic.SA1210.severity = warning # The using directives within a C# code file are not sorted alphabetically by namespace.
dotnet_diagnostic.SA1211.severity = warning # The using-alias directives within a C# code file are not sorted alphabetically by alias name.
dotnet_diagnostic.SA1212.severity = warning # A get accessor appears after a set or init accessor within a property or indexer.
dotnet_diagnostic.SA1214.severity = suggestion # A readonly field is positioned beneath a non-readonly field.
# Naming Rules
dotnet_diagnostic.SA1310.severity = suggestion # A field name in C# contains an underscore.
# Maintainability Rules
dotnet_diagnostic.SA1400.severity = error # The access modifier for a C# element has not been explicitly defined.
dotnet_diagnostic.SA1403.severity = error # A C# code file contains more than one namespace.
dotnet_diagnostic.SA1405.severity = error # A call to Debug.Assert in C# code does not include a descriptive message.
dotnet_diagnostic.SA1406.severity = error # A call to Debug.Fail in C# code does not include a descriptive message.
dotnet_diagnostic.SA1407.severity = error # A C# statement contains a complex arithmetic expression which omits parenthesis around operators.
dotnet_diagnostic.SA1408.severity = error # A C# statement contains a complex conditional expression which omits parenthesis around operators.
dotnet_diagnostic.SA1412.severity = warning # The encoding of the file is not UTF-8 with byte order mark.
# Layout Rules
dotnet_diagnostic.SA1500.severity = error # The opening or closing brace within a C# statement, element, or expression is not placed on its own line.
dotnet_diagnostic.SA1506.severity = error # An element documentation header above a C# element is followed by a blank line.
dotnet_diagnostic.SA1509.severity = error # An opening brace within a C# element, statement, or expression is preceded by a blank line.
dotnet_diagnostic.SA1510.severity = error # Chained C# statements are separated by a blank line.
dotnet_diagnostic.SA1511.severity = error # The while footer at the bottom of a do-while statement is separated from the statement by a blank line.
dotnet_diagnostic.SA1514.severity = error # An element documentation header above a C# element is not preceded by a blank line.
dotnet_diagnostic.SA1517.severity = error # The code file has blank lines at the start.
dotnet_diagnostic.SA1520.severity = error # The opening and closing braces of a chained 'if'/'else if'/'else' construct were included for some clauses, but omitted for others.
First, as someone who usually writes Swift, the Allman style is unfamiliar to me. Since I’ve already compromised by deciding to follow C# culture from the start, I don’t have strong preferences as long as there is some flexibility and we follow general rules.
I’m not sure how much flexibility .editorconfig offers, so I will look into what kind of rules can be set.
Also, I need to consider a solution for cases where unformatted source code might be committed when someone edits using VS Code or other IDEs instead of Visual Studio.
Also, I need to consider a solution for cases where unformatted source code might be committed when someone edits using VS Code or other IDEs instead of Visual Studio.
I’m still relatively new to this, but in my research I found that we can ask contributors to run dotnet format. We could include this instruction in both the PR template and CONTRIBUTING.md. Additionally, we could set up a GitHub Action to automatically run the command for them.