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

Comments in a method chain can get removed after multiple format rounds #943

Closed
valscion opened this issue Sep 6, 2021 · 3 comments · Fixed by #980
Closed

Comments in a method chain can get removed after multiple format rounds #943

valscion opened this issue Sep 6, 2021 · 3 comments · Fixed by #980
Milestone

Comments

@valscion
Copy link
Contributor

valscion commented Sep 6, 2021

Metadata

  • Operating system: macOS
  • Ruby version: 2.6.5
  • Node version: v12.16.1
  • @prettier/plugin-ruby or prettier gem version: latest (1.6.1)
  • Options:
    • rubyArrayLiteral - true
    • rubyHashLabel - true
    • rubyModifier - true
    • rubyNetcatCommand - null
    • rubySingleQuote - true
    • rubyToProc - false
    • trailingComma - "none"

Input

Organization.includes(
  :users,
  :venues
)
  .where(id: org_ids)
  .flatten.
  # first remove rows with duplicate names
  uniq { |contact| "#{contact[:first_name]}#{contact[:last_name]}#{contact[:org_name]}" }.
  # then remove remaining rows with duplicate emails
  uniq { |contact| contact[:email] }
  .tap { |res|
    CSV.open(OUTPUT_PATH, "wb") { |csv|
      csv << HEADERS
      res.each { |d| csv << d.values }
    }
  }

Current output after first format round

Organization
  .includes(:users, :venues)
  .where(id: org_ids)
  .flatten
  .# first remove rows with duplicate names
  uniq do |contact|
    "#{contact[:first_name]}#{contact[:last_name]}#{contact[:org_name]}"
  end
  .# then remove remaining rows with duplicate emails
  uniq { |contact| contact[:email] }
  .tap do |res|
    CSV.open(OUTPUT_PATH, 'wb') do |csv|
      csv << HEADERS
      res.each { |d| csv << d.values }
    end
  end

Output after another format round

Organization
  .includes(:users, :venues)
  .where(id: org_ids)
  .flatten
  .uniq do |contact|  # first remove rows with duplicate names
    "#{contact[:first_name]}#{contact[:last_name]}#{contact[:org_name]}"
  end
  .uniq { |contact| contact[:email] } # then remove remaining rows with duplicate emails
  .tap do |res|
    CSV.open(OUTPUT_PATH, 'wb') do |csv|
      csv << HEADERS
      res.each { |d| csv << d.values }
    end
  end

Output after third format round (and it no longer changes)

Organization
  .includes(:users, :venues)
  .where(id: org_ids)
  .flatten
  .uniq do |contact|
    # first remove rows with duplicate names
    "#{contact[:first_name]}#{contact[:last_name]}#{contact[:org_name]}"
  end
  .uniq { |contact| contact[:email] }
  .tap do |res|
    CSV.open(OUTPUT_PATH, 'wb') do |csv|
      csv << HEADERS
      res.each { |d| csv << d.values }
    end
  end

Notice how the # then remove remaining rows with duplicate emails comment got removed here altogether.

Expected output

Organization
  .includes(:users, :venues)
  .where(id: org_ids)
  .flatten
  .uniq do |contact|
    # first remove rows with duplicate names
    "#{contact[:first_name]}#{contact[:last_name]}#{contact[:org_name]}"
  end
  .uniq do |contact|
    # then remove remaining rows with duplicate emails
    contact[:email]
  end
  .tap do |res|
    CSV.open(OUTPUT_PATH, 'wb') do |csv|
      csv << HEADERS
      res.each { |d| csv << d.values }
    end
  end

That is, going directly to the last format round that no longer changes and keeps the comments somewhere in context.

@valscion valscion changed the title Comments in a method chain need multiple format rounds to stabilize Comments in a method chain can get removed after multiple format rounds Sep 6, 2021
@kddnewton
Copy link
Member

Wow that's weird!

@kddnewton
Copy link
Member

Okay looking into this it boils down to:

foo.
  # bar
  baz

the comment gets attached to the baz ident as a leading comment. When that node ends up getting printed, the . has already been printed. The way to fix this is going to be to detect this kind of case and to call addLeadingComment on the operator node instead.

@kddnewton kddnewton added this to the v2.0.0 milestone Sep 29, 2021
kddnewton added a commit that referenced this issue Sep 30, 2021
@kddnewton kddnewton mentioned this issue Sep 30, 2021
kddnewton added a commit that referenced this issue Sep 30, 2021
@kddnewton
Copy link
Member

Out in the next release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants