Module: RuboCop::Cop::MultilineLiteralBraceLayout
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Defined in: | lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb |
Overview
Common functionality for checking the closing brace of a literal is either on the same line as the last contained elements or a new line.
Constant Summary
ConfigurableEnforcedStyle
- Included
Instance Attribute Summary
ConfigurableEnforcedStyle
- Included
Instance Method Summary
- #check(node) private
- #check_brace_layout(node) private
- #check_new_line(node) private
- #check_same_line(node) private
- #check_symmetrical(node) private
- #children(node) private
-
#closing_brace_on_same_line?(node) ⇒ Boolean
private
This method depends on the fact that we have guarded against implicit and empty literals.
- #empty_literal?(node) ⇒ Boolean private
- #ignored_literal?(node) ⇒ Boolean private
- #implicit_literal?(node) ⇒ Boolean private
-
#last_line_heredoc?(node, parent = nil) ⇒ Boolean
private
Starting with the parent node and recursively for the parent node’s children, check if the node is a HEREDOC and if the HEREDOC ends below or on the last line of the parent node.
-
#new_line_needed_before_closing_brace?(node) ⇒ Boolean
private
Returns true for the case.
-
#opening_brace_on_same_line?(node) ⇒ Boolean
private
This method depends on the fact that we have guarded against implicit and empty literals.
ConfigurableEnforcedStyle
- Included
Instance Method Details
#check(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 34
def check(node) case style when :symmetrical then check_symmetrical(node) when :new_line then check_new_line(node) when :same_line then check_same_line(node) end end
#check_brace_layout(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 12
def check_brace_layout(node) return if ignored_literal?(node) # If the last node is or contains a conflicting HEREDOC, we don't want # to adjust the brace layout because this will result in invalid code. return if last_line_heredoc?(node.children.last) check(node) end
#check_new_line(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 42
def check_new_line(node) return unless closing_brace_on_same_line?(node) add_offense(node.loc.end, message: self.class::ALWAYS_NEW_LINE_MESSAGE) do |corrector| MultilineLiteralBraceCorrector.correct(corrector, node, processed_source) end end
#check_same_line(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 50
def check_same_line(node) return if closing_brace_on_same_line?(node) add_offense(node.loc.end, message: self.class::ALWAYS_SAME_LINE_MESSAGE) do |corrector| MultilineLiteralBraceCorrector.correct(corrector, node, processed_source) end end
#check_symmetrical(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 58
def check_symmetrical(node) if opening_brace_on_same_line?(node) return if closing_brace_on_same_line?(node) add_offense(node.loc.end, message: self.class::SAME_LINE_MESSAGE) do |corrector| MultilineLiteralBraceCorrector.correct(corrector, node, processed_source) end else return unless closing_brace_on_same_line?(node) add_offense(node.loc.end, message: self.class::NEW_LINE_MESSAGE) do |corrector| MultilineLiteralBraceCorrector.correct(corrector, node, processed_source) end end end
#children(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 86
def children(node) node.children end
#closing_brace_on_same_line?(node) ⇒ Boolean
(private)
This method depends on the fact that we have guarded against implicit and empty literals.
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 98
def closing_brace_on_same_line?(node) node.loc.end.line == children(node).last.last_line end
#empty_literal?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 74
def empty_literal?(node) children(node).empty? end
#ignored_literal?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 82
def ignored_literal?(node) implicit_literal?(node) || empty_literal?(node) || node.single_line? end
#implicit_literal?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 78
def implicit_literal?(node) !node.loc.begin end
#last_line_heredoc?(node, parent = nil) ⇒ Boolean
(private)
Starting with the parent node and recursively for the parent node’s children, check if the node is a HEREDOC and if the HEREDOC ends below or on the last line of the parent node.
Example:
# node is `b: ...` parameter
# last_line_heredoc?(node) => false
foo(a,
b: {
a: 1,
c: <<-EOM
baz
EOM
}
)
# node is `b: ...` parameter
# last_line_heredoc?(node) => true
foo(a,
b: <<-EOM
baz
EOM
)
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 126
def last_line_heredoc?(node, parent = nil) parent ||= node if node.respond_to?(:loc) && node.loc.respond_to?(:heredoc_end) && node.loc.heredoc_end.last_line >= parent.last_line return true end return false unless node.respond_to?(:children) node.children.any? { |child| last_line_heredoc?(child, parent) } end
#new_line_needed_before_closing_brace?(node) ⇒ Boolean
(private)
Returns true for the case [a, b # comment ].some_method
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 26
def new_line_needed_before_closing_brace?(node) last_element_line = last_element_range_with_trailing_comma(node).last_line last_element_commented = processed_source.comment_at_line(last_element_line) last_element_commented && (node.chained? || node.argument?) end
#opening_brace_on_same_line?(node) ⇒ Boolean
(private)
This method depends on the fact that we have guarded against implicit and empty literals.
# File 'lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb', line 92
def opening_brace_on_same_line?(node) same_line?(node.loc.begin, children(node).first) end