Skip to content

Commit

Permalink
[Fix rubocop#50] Make EndWith/StartWith autocorrects nil-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed May 14, 2019
1 parent df48417 commit e7924f7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

## master (unreleased)

### Bug fixes

* [#50](https://github.com/rubocop-hq/rubocop-performance/issues/50): Make `Performance/EndWith` and `Performance/StartWith` autocorrects nil-safe. ([@dduugg][])

## 1.3.0 (2019-05-13)

### Bug fixes

* [#48](https://github.com/rubocop-hq/rubocop-performance/issues/48): Reduce `Performance/RegexpMatch` false positive by only flagging `match` used with Regexp/String/Symbol literals. ([@dduugg][])
* [#48](https://github.com/rubocop-hq/rubocop-performance/issues/48): Reduce `Performance/RegexpMatch` false positives by only flagging `match` used with Regexp/String/Symbol literals. ([@dduugg][])

### Changes

Expand Down
2 changes: 0 additions & 2 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Performance/EndWith:
# object. Switching these methods has to be done with knowledge of the types
# of the variables which rubocop doesn't have.
SafeAutoCorrect: false
AutoCorrect: false
Enabled: true
VersionAdded: '0.36'
VersionChanged: '0.44'
Expand Down Expand Up @@ -176,7 +175,6 @@ Performance/StartWith:
# object. Switching these methods has to be done with knowledge of the types
# of the variables which rubocop doesn't have.
SafeAutoCorrect: false
AutoCorrect: false
Enabled: true
VersionAdded: '0.36'
VersionChanged: '0.44'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/end_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def autocorrect(node)
regex_str = interpret_string_escapes(regex_str)

lambda do |corrector|
new_source = receiver.source + '.end_with?(' +
new_source = receiver.source + '&.end_with?(' +
to_string_literal(regex_str) + ')'
corrector.replace(node.source_range, new_source)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/start_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def autocorrect(node)
regex_str = interpret_string_escapes(regex_str)

lambda do |corrector|
new_source = receiver.source + '.start_with?(' +
new_source = receiver.source + '&.start_with?(' +
to_string_literal(regex_str) + ')'
corrector.replace(node.source_range, new_source)
end
Expand Down
4 changes: 2 additions & 2 deletions manual/cops_performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ would suffice.

Name | Default value | Configurable values
--- | --- | ---
AutoCorrect | `false` | Boolean
AutoCorrect | `true` | Boolean

### References

Expand Down Expand Up @@ -775,7 +775,7 @@ This cop identifies unnecessary use of a regex where

Name | Default value | Configurable values
--- | --- | ---
AutoCorrect | `false` | Boolean
AutoCorrect | `true` | Boolean

### References

Expand Down
14 changes: 7 additions & 7 deletions spec/rubocop/cop/performance/end_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
shared_examples 'different match methods' do |method|
it "autocorrects #{method} /abc\\z/" do
new_source = autocorrect_source("str#{method} /abc\\z/")
expect(new_source).to eq "str.end_with?('abc')"
expect(new_source).to eq "str&.end_with?('abc')"
end

it "autocorrects #{method} /\\n\\z/" do
new_source = autocorrect_source("str#{method} /\\n\\z/")
expect(new_source).to eq 'str.end_with?("\n")'
expect(new_source).to eq 'str&.end_with?("\n")'
end

it "autocorrects #{method} /\\t\\z/" do
new_source = autocorrect_source("str#{method} /\\t\\z/")
expect(new_source).to eq 'str.end_with?("\t")'
expect(new_source).to eq 'str&.end_with?("\t")'
end

%w[. $ ^ |].each do |str|
it "autocorrects #{method} /\\#{str}\\z/" do
new_source = autocorrect_source("str#{method} /\\#{str}\\z/")
expect(new_source).to eq "str.end_with?('#{str}')"
expect(new_source).to eq "str&.end_with?('#{str}')"
end

it "doesn't register an error for #{method} /#{str}\\z/" do
Expand All @@ -36,7 +36,7 @@
%w[a e f r t v].each do |str|
it "autocorrects #{method} /\\#{str}\\z/" do
new_source = autocorrect_source("str#{method} /\\#{str}\\z/")
expect(new_source).to eq %{str.end_with?("\\#{str}")}
expect(new_source).to eq %{str&.end_with?("\\#{str}")}
end
end

Expand All @@ -51,7 +51,7 @@
%w[i j l m o q y].each do |str|
it "autocorrects #{method} /\\#{str}\\z/" do
new_source = autocorrect_source("str#{method} /\\#{str}\\z/")
expect(new_source).to eq "str.end_with?('#{str}')"
expect(new_source).to eq "str&.end_with?('#{str}')"
end
end

Expand All @@ -64,7 +64,7 @@

it "autocorrects #{method} /\\\\\\z/" do
new_source = autocorrect_source("str#{method} /\\\\\\z/")
expect(new_source).to eq("str.end_with?('\\\\')")
expect(new_source).to eq("str&.end_with?('\\\\')")
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/rubocop/cop/performance/start_with_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
shared_examples 'different match methods' do |method|
it "autocorrects #{method} /\\Aabc/" do
new_source = autocorrect_source("str#{method} /\\Aabc/")
expect(new_source).to eq "str.start_with?('abc')"
expect(new_source).to eq "str&.start_with?('abc')"
end

# escapes like "\n"
Expand All @@ -15,15 +15,15 @@
%w[a e f r t v].each do |str|
it "autocorrects #{method} /\\A\\#{str}/" do
new_source = autocorrect_source("str#{method} /\\A\\#{str}/")
expect(new_source).to eq %{str.start_with?("\\#{str}")}
expect(new_source).to eq %{str&.start_with?("\\#{str}")}
end
end

# regexp metacharacters
%w[. * ? $ ^ |].each do |str|
it "autocorrects #{method} /\\A\\#{str}/" do
new_source = autocorrect_source("str#{method} /\\A\\#{str}/")
expect(new_source).to eq "str.start_with?('#{str}')"
expect(new_source).to eq "str&.start_with?('#{str}')"
end

it "doesn't register an error for #{method} /\\A#{str}/" do
Expand All @@ -42,7 +42,7 @@
%w[i j l m o q y].each do |str|
it "autocorrects #{method} /\\A\\#{str}/" do
new_source = autocorrect_source("str#{method} /\\A\\#{str}/")
expect(new_source).to eq "str.start_with?('#{str}')"
expect(new_source).to eq "str&.start_with?('#{str}')"
end
end

Expand All @@ -55,7 +55,7 @@

it "autocorrects #{method} /\\A\\\\/" do
new_source = autocorrect_source("str#{method} /\\A\\\\/")
expect(new_source).to eq("str.start_with?('\\\\')")
expect(new_source).to eq("str&.start_with?('\\\\')")
end
end

Expand Down

0 comments on commit e7924f7

Please sign in to comment.