-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add new Lint/ErbNewArguments
cop
#5845
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 Lint/ErbNewArguments
cop
#5845
Conversation
f1538d2
to
efcb149
Compare
## Summary This `Lint/ErbNewArguments` cop emulates the following Ruby warnings in Ruby 2.6. ```console % cat example.rb ERB.new('hi', nil, '-', '@output_buffer') % ruby -vrerb example.rb ruby 2.6.0dev (2018-04-08 trunk 63120) [x86_64-darwin17] example.rb:1: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments. example.rb:1: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead. example.rb:1: warning: Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead. ``` The interface of `ERB.new` will change from Ruby 2.6. > Add :trim_mode and :eoutvar keyword arguments to ERB.new. > Now non-keyword arguments other than first one are softly deprecated > and will be removed when Ruby 2.5 becomes EOL. [Feature rubocop#14256] https://github.com/ruby/ruby/blob/2311087b685e8dc0f21f4a89875f25c22f5c39a9/NEWS#stdlib-updates-outstanding-ones-only Now non-keyword arguments other than first one are softly deprecated and will be removed when Ruby 2.5 becomes EOL. `ERB.new` with non-keyword arguments is deprecated since ERB 2.2.0. Use `:trim_mode` and `:eoutvar` keyword arguments to `ERB.new`. This cop identifies places where `ERB.new(str, trim_mode, eoutvar)` can be replaced by `ERB.new(str, :trim_mode: trim_mode, eoutvar: eoutvar)`. This cop does not have autocorrect because uses of `ERB.new` depends on existing algorithm and code. ## Examples ```ruby # Target codes supports Ruby 2.6 and higher only # bad ERB.new(str, nil, '-') # Target codes supports Ruby 2.5 and lower only # good ERB.new(str, trim_mode: nil, eoutvar: '-') # Target codes supports Ruby 2.6, 2.5 and lower # bad ERB.new(str, nil, '-') # good # Ruby standard library style # ruby/ruby@3406c5d if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+ ERB.new(str, trim_mode: nil, eoutvar: '-') else ERB.new(str, nil, '-') end # good # Use `RUBY_VERSION` style if RUBY_VERSION >= '2.6' ERB.new(str, trim_mode: nil, eoutvar: '-') else ERB.new(str, nil, '-') end ```
efcb149
to
cd117aa
Compare
Great work overall, although I'm not a fan of the long messages. Thanks! |
Yes. I started implementing it with a short offense messages first, but eventually became a long offense messages emulating Ruby's warning. 😅 Thank you! |
Parser gem has been started development for Ruby 2.6 (edge Ruby). https://github.com/whitequark/parser/blob/master/CHANGELOG.md#v2510-2018-04-12 AFAIK, the next Ruby version is 2.7 after Ruby 2.6. https://bugs.ruby-lang.org/issues/14256 This PR permits Ruby 2.6, the early adapters will be able to try edge Ruby with RuboCop. rubocop#5845 has already been finished some work to support ruby 2.6. And this PR ends all the work related to permit to specify TargetRubyVersion 2.6.
Parser gem has been started development for Ruby 2.6 (edge Ruby). https://github.com/whitequark/parser/blob/master/CHANGELOG.md#v2510-2018-04-12 AFAIK, the next Ruby version is 2.7 after Ruby 2.6. https://bugs.ruby-lang.org/issues/14256 This PR permits Ruby 2.6, the early adapters will be able to try edge Ruby with RuboCop. #5845 has already been finished some work to support ruby 2.6. And this PR ends all the work related to permit to specify TargetRubyVersion 2.6.
I have the following message:
But here: https://github.com/ruby/ruby/blob/65a8d52212d3945bb5f547136a88f43022a31cf3/lib/erb.rb#L811 def initialize(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout') And my code is: ERB.new(template.read, 0, trim_mode: '>') IMHO, cop should not be offended… |
This behavior is not a false positive because it emulates the following ERB warning. % ruby -rerb -ve "ERB.new('hi', 0, trim_mode: '>')"
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin19]
-e:1: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments. |
Does it mean |
Yes. AFAIK, it's related to the following ticket. |
Summary
This
Lint/ErbNewArguments
cop emulates the following Ruby warnings in Ruby 2.6.The interface of
ERB.new
will change from Ruby 2.6.https://github.com/ruby/ruby/blob/2311087b685e8dc0f21f4a89875f25c22f5c39a9/NEWS#stdlib-updates-outstanding-ones-only
Now non-keyword arguments other than first one are softly deprecated and will be removed when Ruby 2.5 becomes EOL.
ERB.new
with non-keyword arguments is deprecated since ERB 2.2.0.Use
:trim_mode
and:eoutvar
keyword arguments toERB.new
.This cop identifies places where
ERB.new(str, trim_mode, eoutvar)
can be replaced byERB.new(str, :trim_mode: trim_mode, eoutvar: eoutvar)
.This cop does not have autocorrect because uses of
ERB.new
depends on existing algorithm and code.Examples
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
rake default
orrake parallel
. It executes all tests and RuboCop for itself, and generates the documentation.