dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.59k forks source link

New lint for leaving space between comment slashes (`//`) and content. #58388

Open Jonas-Sander opened 3 years ago

Jonas-Sander commented 3 years ago

Describe the rule you'd like to see implemented The lint should flag comments where there is no space between the slashes (// or ///) and the content of the comment.
This is the style used by Flutter and Dart.

Examples Bad:

//Bad comment
///Bad doc-comment

Good:

// Good comment
/// Good doc comment

Some edge cases:

// An empty comment with no spaces after is okay:
//
//

// What about this:
///////////heey
// //Is this alright?

// An empty comment at the end of the file is probably also okay:
///
IoanaAlexandru commented 3 years ago

Just in case it helps anybody else - I'm using danger.systems and GitHub Actions to make this check automatically. The relevant part of my Dangerfile is:

files = git.added_files + git.modified_files
files.each do |f|
    diff = git.diff_for_file(f)

    # Check comment formatting
    if f =~ /.*\.dart/ and diff.patch =~ /((\/\/)[^ \n\/])|((\/\/\/)[^ \n])|(\/\*)/m
        File.readlines(f).each_with_index do |line, index|
            if line =~ /\/\*/
                warn("Don't use block comments", file: f, line: index+1)
            elsif line =~ /(\/\/\/)[^ \n]/
                if line =~ /\/{4}/
                    warn("That's probably too many slashes", file: f, line: index+1)
                else
                    warn("Add a space between \"///\" and the actual comment", file: f, line: index+1)
                end
            elsif line =~ /(\/\/)[^ \n\/]/
                warn("Add a space between \"//\" and the actual comment", file: f, line: index+1)
            end
        end
    end
end

Thanks @Jonas-Sander for providing the edge cases, they helped me write and test this code :)