Skip to content

Inconsistency in how trailing commas are handled. #34

@gregschmit

Description

@gregschmit

I am a new user of this gem, trying to embrace the Ruby style and stop bringing in baggage from other languages.

I noticed that there seems to be an inconsistency with respect to trailing commas in this project. Full disclosure: I am extremely in favor of trailing commas, so I am biased. I know we're not supposed to bikeshed here, however, the README did seem to suggest that a change may be suggested if it is an inconsistency.

Trailing commas in multiline method calls are allowed, however trailing commas in multiline array/hash literals are not. For example, in this example the trailing comma in the array literal is removed, but the trailing comma in the method arguments is not removed.

class Something
  S = Struct.new(
    :a,
    :b,
    :c,
    :d,  # <-- This comma is not removed.
  )

  CONTENT_TYPES = [
    "text/html",
    "application/json",
    "application/xml",
    "application/x-www-form-urlencoded",
    "multipart/form-data",
    "application/octet-stream",
    "application/x-www-form-urlencoded",
    "application/x-www-form-urlencoded; charset=UTF-8",  # <-- This comma is removed.
  ]

  RESPONSE_TYPES = {
    "text/html" => "html",
    "application/json" => "json",
    "application/xml" => "xml",
    "application/x-www-form-urlencoded" => "urlencoded",
    "multipart/form-data" => "form-data",  # <-- This comma is removed.
  }
end

puts Something::S.new(1, 2, 3, 4).inspect

This is because the default value for cop Style/TrailingCommaInArrayLiteral is no_comma and that cop is enabled. I believe to be consistent this should be changed, or the cop removed so at least I am allowed to put a trailing comma. I would prefer it to be set to diff_comma. I think this change should also be applied to Style/TrailingCommaInHashLiteral.

Would this project consider merging a PR if I submit one?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions