Skip to content

Commit

Permalink
[rubocop#3558] StringLiteralCorrector extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
garettarrowood committed Dec 17, 2017
1 parent 1187911 commit 3b05a62
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
require_relative 'rubocop/cop/correctors/ordered_gem_corrector'
require_relative 'rubocop/cop/correctors/parentheses_corrector'
require_relative 'rubocop/cop/correctors/punctuation_corrector'
require_relative 'rubocop/cop/correctors/string_literal_corrector'

require_relative 'rubocop/cop/bundler/duplicated_gem'
require_relative 'rubocop/cop/bundler/insecure_protocol_source'
Expand Down
25 changes: 25 additions & 0 deletions lib/rubocop/cop/correctors/string_literal_corrector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module RuboCop
module Cop
# This auto-corrects string literals
class StringLiteralCorrector
extend Util

class << self
def correct(node, style)
return if node.dstr_type?

lambda do |corrector|
str = node.str_content
if style == :single_quotes
corrector.replace(node.source_range, to_string_literal(str))
else
corrector.replace(node.source_range, str.inspect)
end
end
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/rubocop/cop/mixin/statement_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module RuboCop
module Cop
# Common functionality for modifier cops.
module StatementModifier
private

def single_line_as_modifier?(node)
return false if non_eligible_node?(node) ||
non_eligible_body?(node.body) ||
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/mixin/string_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def on_regexp(node)
ignore_node(node)
end

private

def inside_interpolation?(node)
# A :begin node inside a :dstr node is an interpolation.
node.ancestors.drop_while { |a| !a.begin_type? }.any?(&:dstr_type?)
Expand Down
15 changes: 2 additions & 13 deletions lib/rubocop/cop/mixin/string_literals_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Cop
module StringLiteralsHelp
include StringHelp

private

def wrong_quotes?(node)
src = node.source
return false if src.start_with?('%', '?')
Expand All @@ -15,19 +17,6 @@ def wrong_quotes?(node)
src !~ /" | \\[^'] | \#(@|\{)/x
end
end

def autocorrect(node)
return if node.dstr_type?

lambda do |corrector|
str = node.str_content
if style == :single_quotes
corrector.replace(node.source_range, to_string_literal(str))
else
corrector.replace(node.source_range, str.inspect)
end
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/string_literals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def on_dstr(node)
ignore_node(node)
end

def autocorrect(node)
StringLiteralCorrector.correct(node, style)
end

private

def all_string_literals?(nodes)
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/string_literals_in_interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class StringLiteralsInInterpolation < Cop
include ConfigurableEnforcedStyle
include StringLiteralsHelp

def autocorrect(node)
StringLiteralCorrector.correct(node, style)
end

private

def message(*)
Expand Down

0 comments on commit 3b05a62

Please sign in to comment.