Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Jonas-Sander opened this issue May 4, 2021 · 1 comment
Open
Labels
lint-request type-enhancement A request for a change that isn't a bug

Comments

@Jonas-Sander
Copy link

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:
///
@Jonas-Sander Jonas-Sander added type-enhancement A request for a change that isn't a bug lint-request labels May 4, 2021
@IoanaAlexandru
Copy link

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint-request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants