fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.85k stars 296 forks source link

Setup auto formatting of source code #3596

Open MangelMaxime opened 7 months ago

MangelMaxime commented 7 months ago

It would be nice to have auto formatting of the code so we can unify the styling in the project.

For F# we can use Fantomas

Here is a setting configuration that I often use

[*.{fs,fsx}]
max_line_length = 80
fsharp_alternative_long_member_definitions = true
fsharp_multi_line_lambda_closing_newline = true
fsharp_bar_before_discriminated_union_declaration = true
fsharp_multiline_bracket_style = aligned
fsharp_keep_max_number_of_blank_lines = 2
fsharp_record_multiline_formatter = number_of_items
fsharp_array_or_list_multiline_formatter = number_of_items
fsharp_align_function_signature_to_indentation = true
fsharp_multi_line_lambda_closing_newline = true
fsharp_max_if_then_else_short_width = 0

For Python, we can use ruff

ncave commented 7 months ago

@MangelMaxime Please exclude the src/fcs-fable folder, as it needs to retain the original formatting from upstream.

MangelMaxime commented 7 months ago

@ncave I will make sure to ignore that folder 👍

nojaf commented 7 months ago

I want to pick this one up, what do you want to do with the existing settings: https://github.com/fable-compiler/Fable/blob/2184f8515f5d1042de4a16ce205396d9949686e4/.editorconfig#L13-L17

MangelMaxime commented 7 months ago

@nojaf It looks like the old setting is a subset of the one I proposed.

So for me, I am in favour of replacing them we what I proposed but obviously I am biased ^^.

To explains the motivations of my setting is that I try to have a code formatting which is consistent independently from the numbers of elements. If the array/record/member has 1, 2 or 10 arguments I prefer to have it formatted in the same way.


The implementation I had in mind is to have the CI check if the code is formatted correctly. I don't know if all the tools allows that but I know that fantomas does.

And also, try to leverage git hooks to auto format the code on commits. Like that this is something we can "forget" about as it is done automatically.

nojaf commented 7 months ago

@nojaf It looks like the old setting is a subset of the one I proposed.

Well, it has fsharp_multiline_bracket_style = stroustrup and you are proposing aligned. Which one do you want to go with?

MangelMaxime commented 7 months ago

Oh my bad, I missed that difference.

Now I questioning my whole life as I always though I was using stroustrup, let's go with aglined as you did in the PR