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

(PUP-3036) Rubocop Style/AndOr autocorrect pass #2944

Closed
wants to merge 7 commits into from
Closed

(PUP-3036) Rubocop Style/AndOr autocorrect pass #2944

wants to merge 7 commits into from

Conversation

vrthra
Copy link
Contributor

@vrthra vrthra commented Aug 6, 2014

This PR replaces and or and not keywords in favor of && || and ! where it
can be done automatically (In places where the semantic meaning of the
expression does not change on replacing the and keyword with the operator ||).

This PR also makes the spacing of ! operator consistent, changing
! mymethod to !mymethod

Since we are using rubocop -a to make the changes, we are assured of the
correctness of translation. These changes have also been individually verified.

Rubocop previously relied on the parser results to check the equivalency of new
and old constructs after autocorrect. However, for our purposes, it is rather
restrictive since parser can produce different AST depending on the context,
rubocop misses quite a large number of autocorrect possibilities. My fix to
rubocop involved supplying that context so that it checks for the equivalence
better.

While incorporating parenthesis for method calls, I noticed that puppet codebase has not
standardized on a method call spacing. that is, I find instances of method( ... ),
method(...) and even method( ...) in different places. For the purposes of this
PR, I have used method(...) (simply wrapping with parenthesis). Rubocop recommends
method(...). After the puppet-dev discussion, the consensus seems to be the same, (but with spacing between arguments -- this will be taken up in a future PR)
The remaining violations are in these directories.

   1 lib/puppet/external
   1 lib/puppet/interface
   1 lib/puppet/module_tool
   1 lib/puppet/parameter
   1 lib/puppet/property.rb
   1 lib/puppet/provider.rb
   1 lib/puppet/reference
   1 lib/puppet/resource.rb
   1 lib/puppet/ssl
   1 lib/puppet/util.rb
   2 lib/puppet/indirector
   2 lib/puppet/network
   3 lib/puppet/property
   3 lib/puppet/transaction
   5 lib/puppet/settings.rb
   5 lib/puppet/type.rb
   8 lib/puppet/resource
   9 lib/puppet/parser
  12 lib/puppet/util
  17 lib/puppet/type
  23 lib/puppet/provider

@puppetcla
Copy link

CLA signed by all contributors.

@vrthra vrthra changed the title (PUP-2950) Rubocop Style/AndOr automatic pass (PUP-2950) Rubocop Style/AndOr autocorrect pass Aug 8, 2014
@vrthra vrthra changed the title (PUP-2950) Rubocop Style/AndOr autocorrect pass (PUP-3036) Rubocop Style/AndOr autocorrect pass Aug 8, 2014
@vrthra
Copy link
Contributor Author

vrthra commented Aug 13, 2014

@kylog @zaphod42 could you please take a look? Are there any concerns?
(It is a result of multiple passes, so it would be helpful to review by commit)

This commit replaces `and` and `or` keywords in favor of `&&` and `||` where it
can be done automatically (In places where the semantic meaning of the
expression does not change on replacing the `and` keyword with the operator `||`).

Since we are using `rubocop -a` to make the changes, we are assured of the
correctness of translation. These changes have also been individually verified.

We have been able to get rubocop to make further changes than previously by
identifying equivalent patterns identified as different in parsing
(See rubocop issue #1255)
This commit replaces `not` in favor of `!` where
can be done automatically (In places where the semantic meaning of the
expression does not change on replacing the `not` keyword with the operator `!`).

Since we are using `rubocop -a` to make the changes, we are assured of the
correctness of translation. These changes have also been individually verified.

This change is especially useful because after replacing such instances, the
AndOr cop can autocorrect even more instances of violations

This also enables the Style/Not cop
This commit ensures that no space is used between the application of
the operator `!` and its operand. That is, it replaces the instances of
`! mymethod` with `!mymethod`

It also enables the Style/SpaceAfterNot cop
This commit replaces `and` and `or` keywords in favor of `&&` and `||` where it
can be done automatically (In places where the semantic meaning of the
expression does not change on replacing the `and` keyword with the operator `||`).

Since we are using `rubocop -a` to make the changes, we are assured of the
correctness of translation. These changes have also been individually verified.

This commit takes advantage of the previous changes replacing `not` keyword to
autocorrect even more instances of violations.
This commit replaces `AndOr` in favor of `and` and `or` where
can be done automatically (In places where the semantic meaning of the
expression does not change on replacing the `and` keyword with the operator `or`).

Since we are using `rubocop -a` to make the changes, we are assured of the
correctness of translation. These changes have also been individually verified.

The difference in this commit is that, we specifically search for instances
where the semantics of `and` or `or` is unchanged from `&&` and `||` when
parenthesis are introduced for method calls. That is, we look for instances
of the form `mymethod a,b or mymethod c` and convert them to
`mymethod( a,b ) or mymethod( c)` before applying the transformation.
This commit replaces `AndOr` in favor of `and` and `or` where
can be done automatically (In places where the semantic meaning of the
expression does not change on replacing the `and` keyword with the operator
`or` after some preprocessing).

Since we are using `rubocop -a` to make the changes, we are assured of the
correctness of translation. These changes have also been individually verified.

The difference in this commit is that, we specifically search for instances
where the semantics of `and` or `or` is unchanged from `&&` and `||` when
parenthesis are introduced for expressions with assignments.
That is, we look for instances of the form `if a = b or c` and convert them to
`if ( a = b ) or c` before applying the transformation.

These changes have been restricted to conditionals in the interests of
readability of resulting expressions.
This commit updates the previous automatic pass where the method calls
of the format `method a,b or c` was changed to `method( a,b ) || c`
to remove the leading and trailing spaces -- to  `method(a,b) || c`.
@adrienthebo
Copy link
Contributor

We're about to remove the puppet-4 branch as part of the start of work on Puppet 4 proper, so this should be targeted as master. If it gets unexpectedly closed then it means that the branch deletion closed this.

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 this pull request may close these issues.

None yet

3 participants