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

Add new `Style/MultilineMemoization` cop #3604

Merged
merged 1 commit into from Oct 13, 2016

Conversation

Projects
None yet
3 participants
@Drenmi
Collaborator

Drenmi commented Oct 13, 2016

This cop checks that multiline memoizations are wrapped in a begin
and end block, e.g.:

foo ||= begin
  bar
  baz
end

This only affects nodes that use parentheses, e.g.

foo ||= (
  baz
  bax
)

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
Add new `Style/MultilineMemoization` cop
This cop checks that multiline memoizations are wrapped in a `begin`
and `end` block, e.g.:

```
foo ||= begin
  bar
  baz
end
```

This only affects nodes that use parentheses, e.g.

```
foo ||= (
  baz
  bax
)
```
@pocke

This comment has been minimized.

Show comment
Hide comment
@pocke

pocke Oct 13, 2016

Member

Why this cop recommends begin-end style? I can't find a style guide for the cop in https://github.com/bbatsov/ruby-style-guide .

And I can't find performance issues.
For example, the programs are compiled to same byte code.

begin_end = RubyVM::InstructionSequence.compile(<<-RUBY).to_a
  foo ||= begin
    1
  end
RUBY

brace = RubyVM::InstructionSequence.compile(<<-RUBY).to_a
  foo ||= (
    1
  )
RUBY

p begin_end == brace # => true

I think the style should be selectable by rubocop.yml. What do you think?

For example

Style/MultilineMemoization:
  EnforcedStyle: begin_end
  SupportedStyles:
    - begin_end
    - brace

Note:

If the cop allows brace style, there is one problem.

# Can convert brace style.
foo ||= begin
  bar
end

# Can't convert brace style!
foo ||= begin
  bar
rescue
  do_something
end
Member

pocke commented Oct 13, 2016

Why this cop recommends begin-end style? I can't find a style guide for the cop in https://github.com/bbatsov/ruby-style-guide .

And I can't find performance issues.
For example, the programs are compiled to same byte code.

begin_end = RubyVM::InstructionSequence.compile(<<-RUBY).to_a
  foo ||= begin
    1
  end
RUBY

brace = RubyVM::InstructionSequence.compile(<<-RUBY).to_a
  foo ||= (
    1
  )
RUBY

p begin_end == brace # => true

I think the style should be selectable by rubocop.yml. What do you think?

For example

Style/MultilineMemoization:
  EnforcedStyle: begin_end
  SupportedStyles:
    - begin_end
    - brace

Note:

If the cop allows brace style, there is one problem.

# Can convert brace style.
foo ||= begin
  bar
end

# Can't convert brace style!
foo ||= begin
  bar
rescue
  do_something
end
@Drenmi

This comment has been minimized.

Show comment
Hide comment
@Drenmi

Drenmi Oct 13, 2016

Collaborator

Why this cop recommends begin-end style?

This is in line with the convention of not using {} for multiline blocks.

I can't find a style guide for the cop in https://github.com/bbatsov/ruby-style-guide .

A lot of cops don't have entries in the style guide. I would be open to try and get this adopted, though.

And I can't find performance issues.

Yes. It's exclusively a style cop. 😀

I think the style should be selectable by rubocop.yml. What do you think?

If there are people actually using the brace style, I am open to adding it. 😀

Collaborator

Drenmi commented Oct 13, 2016

Why this cop recommends begin-end style?

This is in line with the convention of not using {} for multiline blocks.

I can't find a style guide for the cop in https://github.com/bbatsov/ruby-style-guide .

A lot of cops don't have entries in the style guide. I would be open to try and get this adopted, though.

And I can't find performance issues.

Yes. It's exclusively a style cop. 😀

I think the style should be selectable by rubocop.yml. What do you think?

If there are people actually using the brace style, I am open to adding it. 😀

@bbatsov bbatsov merged commit d6f9f5b into rubocop-hq:master Oct 13, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@bbatsov

This comment has been minimized.

Show comment
Hide comment
@bbatsov

bbatsov Oct 13, 2016

Collaborator

I'm fine with the cop and I'm open to adding a style guide entry for it as well (I'm pretty certain most Rubyists employ this style anyways).

Anyways, if someone wants to make this configurable - I'm fine with this as well.

Collaborator

bbatsov commented Oct 13, 2016

I'm fine with the cop and I'm open to adding a style guide entry for it as well (I'm pretty certain most Rubyists employ this style anyways).

Anyways, if someone wants to make this configurable - I'm fine with this as well.

@Drenmi Drenmi deleted the Drenmi:feature/multiline-memoization-cop branch Dec 29, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment