-
-
Notifications
You must be signed in to change notification settings - Fork 91
Add new Performance/ConstantRegexp cop
#174
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RuboCop | ||
| module Cop | ||
| module Performance | ||
| # This cop finds regular expressions with dynamic components that are all constants. | ||
| # | ||
| # Ruby allocates a new Regexp object every time it executes a code containing such | ||
| # a regular expression. It is more efficient to extract it into a constant | ||
| # or add an `/o` option to perform `#{}` interpolation only once and reuse that | ||
| # Regexp object. | ||
| # | ||
| # @example | ||
| # | ||
| # # bad | ||
| # def tokens(pattern) | ||
| # pattern.scan(TOKEN).reject { |token| token.match?(/\A#{SEPARATORS}\Z/) } | ||
| # end | ||
| # | ||
| # # good | ||
| # ALL_SEPARATORS = /\A#{SEPARATORS}\Z/ | ||
| # def tokens(pattern) | ||
| # pattern.scan(TOKEN).reject { |token| token.match?(ALL_SEPARATORS) } | ||
| # end | ||
| # | ||
| # # good | ||
| # def tokens(pattern) | ||
| # pattern.scan(TOKEN).reject { |token| token.match?(/\A#{SEPARATORS}\Z/o) } | ||
| # end | ||
| # | ||
| class ConstantRegexp < Base | ||
| extend AutoCorrector | ||
|
|
||
| MSG = 'Extract this regexp into a constant or append an `/o` option to its options.' | ||
|
|
||
| def on_regexp(node) | ||
| return if within_const_assignment?(node) || | ||
| !include_interpolated_const?(node) || | ||
| node.single_interpolation? | ||
|
|
||
| add_offense(node) do |corrector| | ||
| corrector.insert_after(node, 'o') | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def within_const_assignment?(node) | ||
| node.each_ancestor(:casgn).any? | ||
| end | ||
|
|
||
| def_node_matcher :regexp_escape?, <<~PATTERN | ||
| (send | ||
| (const nil? :Regexp) :escape const_type?) | ||
| PATTERN | ||
|
|
||
| def include_interpolated_const?(node) | ||
| return false unless node.interpolation? | ||
fatkodima marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| node.each_child_node(:begin).all? do |begin_node| | ||
| inner_node = begin_node.children.first | ||
| inner_node && (inner_node.const_type? || regexp_escape?(inner_node)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe RuboCop::Cop::Performance::ConstantRegexp do | ||
| subject(:cop) { described_class.new } | ||
|
|
||
| it 'registers an offense and corrects when regexp contains interpolated constant' do | ||
| expect_offense(<<~RUBY) | ||
| str.match?(/\A\#{CONST}/) | ||
| ^^^^^^^^^^^ Extract this regexp into a constant or append an `/o` option to its options. | ||
| RUBY | ||
|
|
||
| expect_correction(<<~RUBY) | ||
| str.match?(/\A\#{CONST}/o) | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers an offense and corrects when regexp contains multiple interpolated constants' do | ||
| expect_offense(<<~RUBY) | ||
| str.match?(/\A\#{CONST1}something\#{CONST2}\z/) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Extract this regexp into a constant or append an `/o` option to its options. | ||
| RUBY | ||
|
|
||
| expect_correction(<<~RUBY) | ||
| str.match?(/\A\#{CONST1}something\#{CONST2}\z/o) | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers an offense and corrects when regexp contains `Regexp.escape` on constant' do | ||
| expect_offense(<<~RUBY) | ||
| str.match?(/\A\#{CONST1}something\#{Regexp.escape(CONST2)}\z/) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Extract this regexp into a constant or append an `/o` option to its options. | ||
| RUBY | ||
|
|
||
| expect_correction(<<~RUBY) | ||
| str.match?(/\A\#{CONST1}something\#{Regexp.escape(CONST2)}\z/o) | ||
| RUBY | ||
| end | ||
|
|
||
| it 'does not register an offense when regexp does not contain interpolated constant' do | ||
| expect_no_offenses(<<~RUBY) | ||
| str.match?(/foo/) | ||
| RUBY | ||
| end | ||
|
|
||
| it 'does not register an offense when regexp is within assignment to a constant' do | ||
| expect_no_offenses(<<~RUBY) | ||
| CONST = str.match?(/\#{ANOTHER_CONST}/) | ||
| RUBY | ||
| end | ||
|
|
||
| it 'does not register an offense when regexp has `/o` option' do | ||
| expect_no_offenses(<<~RUBY) | ||
| str.match?(/\#{CONST}/o) | ||
| RUBY | ||
| end | ||
|
|
||
| it 'does not register an offense when regexp contains interpolated constant and '\ | ||
| 'and interpolated non constant' do | ||
| expect_no_offenses(<<~RUBY) | ||
| str.match?(/\#{CONST}\#{do_something(1)}/) | ||
| RUBY | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.