class RuboCop::Cop::Layout::SpaceBeforeBlockBraces
}
a.bar.to_s
foo.map{ |a|
# good
}
a.bar.to_s
foo.map { |a|
# bad
@example EnforcedStyle: no_space
}
a.bar.to_s
foo.map { |a|
# good
}
a.bar.to_s
foo.map{ |a|
# bad
@example EnforcedStyle: space (default)
brace depending on configuration.
Checks that block braces have or don’t have a space before the opening
def self.autocorrect_incompatible_with
def self.autocorrect_incompatible_with [Style::SymbolProc] end
def autocorrect(range)
def autocorrect(range) lambda do |corrector| case range.source when /\s/ then corrector.remove(range) else corrector.insert_before(range, ' ') end end end
def check_empty(left_brace, space_plus_brace, used_style)
def check_empty(left_brace, space_plus_brace, used_style) return if style_for_empty_braces == used_style config_to_allow_offenses['EnforcedStyleForEmptyBraces'] = used_style.to_s if style_for_empty_braces == :space add_offense(left_brace, location: left_brace, message: MISSING_MSG) else space = range_between(space_plus_brace.begin_pos, left_brace.begin_pos) add_offense(space, location: space, message: DETECTED_MSG) end end
def check_non_empty(left_brace, space_plus_brace, used_style)
def check_non_empty(left_brace, space_plus_brace, used_style) case used_style when style then correct_style_detected when :space then space_detected(left_brace, space_plus_brace) else space_missing(left_brace) end end
def empty_braces?(loc)
def empty_braces?(loc) loc.begin.end_pos == loc.end.begin_pos end
def on_block(node)
def on_block(node) return if node.keywords? left_brace = node.loc.begin space_plus_brace = range_with_surrounding_space(range: left_brace) used_style = space_plus_brace.source.start_with?('{') ? :no_space : :space if empty_braces?(node.loc) check_empty(left_brace, space_plus_brace, used_style) else check_non_empty(left_brace, space_plus_brace, used_style) end end
def space_detected(left_brace, space_plus_brace)
def space_detected(left_brace, space_plus_brace) space = range_between(space_plus_brace.begin_pos, left_brace.begin_pos) add_offense(space, location: space, message: DETECTED_MSG) do opposite_style_detected end end
def space_missing(left_brace)
def space_missing(left_brace) add_offense(left_brace, location: left_brace, message: MISSING_MSG) do opposite_style_detected end end
def style_for_empty_braces
def style_for_empty_braces case cop_config['EnforcedStyleForEmptyBraces'] when 'space' then :space when 'no_space' then :no_space when nil then style else raise 'Unknown EnforcedStyleForEmptyBraces selected!' end end