Class: RuboCop::Cop::Performance::ConstantRegexp
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Performance::ConstantRegexp
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/performance/constant_regexp.rb
Overview
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, memoize it, or add an ‘/o` option to perform `#{}` interpolation only once and reuse that Regexp object.
Constant Summary collapse
- MSG =
'Extract this regexp into a constant, memoize it, or append an `/o` option to its options.'
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.autocorrect_incompatible_with ⇒ Object
41 42 43 |
# File 'lib/rubocop/cop/performance/constant_regexp.rb', line 41 def self.autocorrect_incompatible_with [RegexpMatch] end |
Instance Method Details
#on_regexp(node) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/rubocop/cop/performance/constant_regexp.rb', line 45 def on_regexp(node) return if within_allowed_assignment?(node) || !include_interpolated_const?(node) || node.single_interpolation? add_offense(node) do |corrector| corrector.insert_after(node, 'o') end end |