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

Inlined rubocop:disable comments are formatted to a new line #819

Closed
coisnepe opened this issue Feb 8, 2021 · 8 comments · Fixed by #966
Closed

Inlined rubocop:disable comments are formatted to a new line #819

coisnepe opened this issue Feb 8, 2021 · 8 comments · Fixed by #966
Milestone

Comments

@coisnepe
Copy link
Contributor

coisnepe commented Feb 8, 2021

Metadata

  • Ruby version: 2.7.2
  • @prettier/plugin-ruby version: 1.5.2

Somewhat linked to #706 in that it's inline comments that are taken to a newline, but it's not restricted to specific keywords, i.e. raise. Inline rubocop comments are there for a reason (admittedly not always good ones), but I'm dealing with a massive codebase with over 400 inlined rubocop comments so ideally I'd like to find a way to not manually convert the inlined comments to disable\n....\nenable !

Input

  # rubocop:todo Metrics/MethodLength
  def to_props( # rubocop:todo Metrics/ParameterLists
    profile:,

Current output

  # rubocop:todo Metrics/MethodLength
  def to_props(
    # rubocop:todo Metrics/ParameterLists
    profile:,

Expected output

  # rubocop:todo Metrics/MethodLength
  def to_props( # rubocop:todo Metrics/ParameterLists
    profile:,
@coisnepe
Copy link
Contributor Author

I tried digging into the code, in places such as this, but to no avail…

!comment[:value].include?('prettier-ignore')

@coisnepe
Copy link
Contributor Author

coisnepe commented Feb 17, 2021

There's probably something I'm missing, but changing this line

expect("foo # this is an inline comment").toMatchFormat());

to:

test('basic', () => expect('foo # rubocop:disable Metrics/MethodLength').toMatchFormat())

works…

Any help or insight would be greatly appreciated!

@kddnewton
Copy link
Member

Hi @coisnepe - have you checked out the CONTRIBUTING.md docs? That should help you get an understanding of where to start. You'll need to know which AST nodes def foo( # bar is parsing, and then see how comments are getting attached.

I don't have time at the moment to handle this issue but I'll get back around to it eventually. In the meantime please feel free to dig in!

@coisnepe
Copy link
Contributor Author

coisnepe commented Feb 17, 2021

Hi @kddeisz, thank you so much for taking the time to reply. I'll dig into this doc, which I had overlooked -sorry. Thanks for pointing it out.

In the meantime I just want to add the the issue isn't about def foo( # bar so much as it is about all inlined rubocop:disable comments.

I'm in the process of migrating all the inlined comments to disable/enable multilines, as I want to get going with the "prettier" migration.

Thanks again for your help and time, implementing code formatting in my company's codebase is going to be a huge leap forward!

We'll dig into the source code eventually to try and submit a Pull Request, if relevant.

@kddnewton kddnewton added this to the v2.0.0 milestone May 12, 2021
JamesGlover pushed a commit to sanger/sequencescape that referenced this issue May 13, 2021
Using guides here:
https://prettier.io/docs/en/install.html
and here:
https://github.com/prettier/plugin-ruby

Decided to opt straight for installing via yarn, as mentions editor
integration better:

`yarn add --dev --exact prettier`
`echo {}> .prettierrc.json`

Then built up the `.prettierignore` file from `.gitignore`
and added in `.rubocop_todo.yml` as its autogenerated
and `.history/*` to handle one of my plugins.

Added
```
inherit_from:
  - node_modules/@prettier/plugin-ruby/rubocop.yml
```

To `rubocop.yml` and ran prettier.

Needed to fix duplicate keys in a couple of yaml files.

Then ran rubocop, some conflicts, but regenerated
my todo and down to 9 all Lint/RedundantCopDisableDirective, these seem
to be associated with metrics cops.

Given this is a massive commit anyway, decided to
inline all the cop todos for these.

Actually, may inline *all* metrics cops (bar line length)

Done this, had a bit of a battle with rubocop infinite
loops, but managed to fix those.

Now an eslint battle
`yarn add --dev eslint-config-prettier`
Then add 'prettier' to the esLintrc files
under extends (in last place)
Removed some custom formatting configuration

Added `config/cucumber.yml` and `config/database.yml`
to prettierignore as it
seems to be confused by the erb

Also, some battle with rubocop metrics cops still.
Mostly doesn't like inline disables, issue in to fix this:
prettier/plugin-ruby#819
Manually fixed these by switching to the block
rathet than inline style
@msakrejda
Copy link

@coisnepe I ran into the same thing trying to move formatting in our project from Rubocop to Prettier. Did you have any luck with fixing or investigating this? Thanks for looking into it!

@coisnepe
Copy link
Contributor Author

coisnepe commented Jun 2, 2021

Hey @uhoh-itsmaciek, I ended up running a regex against our code base to find the inlined rubocop disables to automatically replace them with the disable/enable pattern. It mostly worked, and in case it didn't I replaced them manually.

@msakrejda
Copy link

@coisnepe great, thanks for the info.

kddnewton added a commit that referenced this issue Sep 27, 2021
@kddnewton kddnewton mentioned this issue Sep 27, 2021
@kddnewton
Copy link
Member

I've got a fix coming for this! It will be out in the next release.

kddnewton added a commit that referenced this issue Sep 27, 2021
kddnewton added a commit that referenced this issue Sep 27, 2021
kddnewton added a commit that referenced this issue Sep 27, 2021
nbudin pushed a commit to nbudin/plugin-ruby that referenced this issue Sep 28, 2021
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.

3 participants