diff --git a/.ameba.yml b/.ameba.yml index 812b3b2..b567470 100644 --- a/.ameba.yml +++ b/.ameba.yml @@ -14,14 +14,9 @@ Naming/QueryBoolMethods: Naming/BlockParameterName: Enabled: false -Style/UnlessElse: - Enabled: false - Style/ParenthesesAroundCondition: - Enabled: false - -Style/NegatedConditionsInUnless: - Enabled: false + Enabled: true + AllowSafeAssignment: true Lint/NotNil: Excluded: diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 6a883a4..8cd47f4 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -78,11 +78,11 @@ def extract_spec_tests(file) result_start = false begin - File.open(file) do |f| + File.open(file) do |input| line_number = 0 test_tags = "" - while line = f.read_line + while (line = input.read_line) line_number += 1 line = line.gsub(/\r\n?/, "\n") break if line.includes?("") diff --git a/src/markd/html_entities.cr b/src/markd/html_entities.cr index e5ae646..b383ad1 100644 --- a/src/markd/html_entities.cr +++ b/src/markd/html_entities.cr @@ -37,7 +37,7 @@ module Markd::HTMLEntities end else entities_key = chars[0..-1] - if resolved_entity = Markd::HTMLEntities::ENTITIES_MAPPINGS[entities_key]? + if (resolved_entity = Markd::HTMLEntities::ENTITIES_MAPPINGS[entities_key]?) return resolved_entity end end @@ -48,7 +48,7 @@ module Markd::HTMLEntities def self.decode_codepoint(codepoint) return "\uFFFD" if codepoint >= 0xD800 && codepoint <= 0xDFFF || codepoint > 0x10FFF - if decoded = Markd::HTMLEntities::DECODE_MAPPINGS[codepoint]? + if (decoded = Markd::HTMLEntities::DECODE_MAPPINGS[codepoint]?) codepoint = decoded end diff --git a/src/markd/node.cr b/src/markd/node.cr index 09d2cdd..31223b3 100644 --- a/src/markd/node.cr +++ b/src/markd/node.cr @@ -84,7 +84,7 @@ module Markd child.unlink child.parent = self - if last = last_child? + if (last = last_child?) last.next = child child.prev = last @last_child = child @@ -97,9 +97,9 @@ module Markd def insert_after(sibling : Node) sibling.unlink - if nxt = next? + if (nxt = next?) nxt.prev = sibling - elsif parent = parent? + elsif (parent = parent?) parent.last_child = sibling end sibling.next = nxt @@ -110,15 +110,15 @@ module Markd end def unlink - if prev = prev? + if (prev = prev?) prev.next = next? - elsif parent = parent? + elsif (parent = parent?) parent.first_child = next? end - if nxt = next? + if (nxt = next?) nxt.prev = prev? - elsif parent = parent? + elsif (parent = parent?) parent.last_child = prev? end @@ -158,7 +158,7 @@ module Markd entering = @entering if entering && current.type.container? - if first_child = current.first_child? + if (first_child = current.first_child?) @current = first_child @entering = true else diff --git a/src/markd/parsers/block.cr b/src/markd/parsers/block.cr index 60a467b..89a2597 100644 --- a/src/markd/parsers/block.cr +++ b/src/markd/parsers/block.cr @@ -76,7 +76,7 @@ module Markd::Parser # ignore last blank line created by final newline lines_size -= 1 if source.ends_with?('\n') - while tip = tip? + while (tip = tip?) token(tip, lines_size) end end @@ -177,7 +177,7 @@ module Markd::Parser add_line # if HtmlBlock, check for end condition - if (container_type.html_block? && match_html_block?(container)) + if container_type.html_block? && match_html_block?(container) token(container, @current_line) end elsif @offset < line.size && !@blank @@ -269,7 +269,7 @@ module Markd::Parser if @line.empty? @blank = true else - while char = @line[offset]? + while (char = @line[offset]?) case char when ' ' offset += 1 @@ -330,7 +330,7 @@ module Markd::Parser end private def match_html_block?(container : Node) - if block_type = container.data["html_block_type"] + if (block_type = container.data["html_block_type"]) block_type = block_type.as(Int32) block_type >= 0 && block_type <= 4 && Rule::HTML_BLOCK_CLOSE[block_type].match(@line[@offset..-1]) else diff --git a/src/markd/parsers/inline.cr b/src/markd/parsers/inline.cr index 49815de..74e537f 100644 --- a/src/markd/parsers/inline.cr +++ b/src/markd/parsers/inline.cr @@ -177,7 +177,7 @@ module Markd::Parser num_ticks = @pos - start_pos after_open_ticks = @pos - while text = match(Rule::TICKS) + while (text = match(Rule::TICKS)) if text.bytesize == num_ticks child = Node.new(Node::Type::Code) child_text = @text.byte_slice(after_open_ticks, (@pos - num_ticks) - after_open_ticks).gsub(Rule::LINE_ENDING, " ") @@ -387,9 +387,7 @@ module Markd::Parser case closer_char when '*', '_', '~' if closer_char != '~' || (closer_char == '~' && @options.gfm) - unless opener - closer = closer.next? - else + if opener # calculate actual number of delimiters used from closer use_delims = (closer.num_delims >= 2 && opener.num_delims >= 2) ? 2 : 1 @@ -444,6 +442,8 @@ module Markd::Parser remove_delimiter(closer) closer = tmp_stack end + else + closer = closer.next? end end when '\'' @@ -476,17 +476,17 @@ module Markd::Parser end private def auto_link(node : Node) - if matched_text = match(Rule::EMAIL_AUTO_LINK) + if (matched_text = match(Rule::EMAIL_AUTO_LINK)) node.append_child(link(matched_text, true)) return true - elsif matched_text = match(Rule::AUTO_LINK) + elsif (matched_text = match(Rule::AUTO_LINK)) node.append_child(link(matched_text, false)) return true elsif @options.gfm && @options.autolink # These are all the extended autolinks from the # autolink extension - if matched_text = match(Rule::WWW_AUTO_LINK) + if (matched_text = match(Rule::WWW_AUTO_LINK)) clean_text = autolink_cleanup(matched_text) if clean_text.empty? node.append_child(text(matched_text)) @@ -496,11 +496,11 @@ module Markd::Parser node.append_child(text(post)) if post.size > 0 && matched_text != clean_text end return true - elsif matched_text = ( + elsif (matched_text = ( match(Rule::PROTOCOL_AUTO_LINK) || match(Rule::XMPP_AUTO_LINK) || match(Rule::MAILTO_AUTO_LINK) - ) + )) clean_text = autolink_cleanup(matched_text) if clean_text.empty? node.append_child(text(matched_text)) @@ -510,7 +510,7 @@ module Markd::Parser node.append_child(text(post)) if post.size > 0 && matched_text != clean_text end return true - elsif matched_text = match(Rule::EXTENDED_EMAIL_AUTO_LINK) + elsif (matched_text = match(Rule::EXTENDED_EMAIL_AUTO_LINK)) # Emails that end in - or _ are declared not to be links by the spec: # # `.`, `-`, and `_` can occur on both sides of the `@`, but only `.` may occur at @@ -532,7 +532,7 @@ module Markd::Parser end private def html_tag(node : Node) - if text = match(Rule::HTML_TAG) + if (text = match(Rule::HTML_TAG)) child = Node.new(Node::Type::HTMLInline) if @options.gfm && @options.tagfilter @@ -614,7 +614,7 @@ module Markd::Parser end private def string(node : Node) - if text = match_main + if (text = match_main) if @options.smart? text = text.gsub(Rule::ELLIPSIS, '\u{2026}') .gsub(Rule::DASH) do |chars| @@ -674,12 +674,12 @@ module Markd::Parser end private def link_destination - dest = if text = match(Rule::LINK_DESTINATION_BRACES) + dest = if (text = match(Rule::LINK_DESTINATION_BRACES)) text[1..-2] elsif char_at?(@pos) != '<' save_pos = @pos open_parens = 0 - while char = char_at?(@pos) + while (char = char_at?(@pos)) case char when '\\' @pos += 1 @@ -726,7 +726,7 @@ module Markd::Parser delimiter = Delimiter.new(char, num_delims, num_delims, child, @delimiters, nil, res[:can_open], res[:can_close]) - if prev = delimiter.previous? + if (prev = delimiter.previous?) prev.next = delimiter end @@ -736,11 +736,11 @@ module Markd::Parser end private def remove_delimiter(delimiter : Delimiter) - if prev = delimiter.previous? + if (prev = delimiter.previous?) prev.next = delimiter.next? end - if nxt = delimiter.next? + if (nxt = delimiter.next?) nxt.previous = delimiter.previous? else # top of stack @@ -897,7 +897,7 @@ module Markd::Parser # Parse zero or more space characters, including at most one newline private def spnl seen_newline = false - while c = char_at?(@pos) + while (c = char_at?(@pos)) if !seen_newline && c == '\n' seen_newline = true elsif c != ' ' @@ -912,7 +912,7 @@ module Markd::Parser private def match(regex : Regex) : String? text = @text.byte_slice(@pos) - if match = text.match(regex) + if (match = text.match(regex)) @pos += match.byte_end.not_nil! return match[0] end @@ -926,7 +926,7 @@ module Markd::Parser private def match_main : String? start_pos = @pos - while char = char_at?(@pos) + while (char = char_at?(@pos)) # If we detected a special string (like a URL), and it's # not the beggining of the string, we need to break right away. # diff --git a/src/markd/renderer.cr b/src/markd/renderer.cr index c8d3d1e..e4bd30f 100644 --- a/src/markd/renderer.cr +++ b/src/markd/renderer.cr @@ -75,7 +75,7 @@ module Markd def render(document : Node, formatter : T?) forall T Utils.timer("rendering", @options.time) do walker = document.walker - while event = walker.next + while (event = walker.next) node, entering = event case node.type diff --git a/src/markd/renderers/html_renderer.cr b/src/markd/renderers/html_renderer.cr index a7df9fc..d7f2705 100644 --- a/src/markd/renderers/html_renderer.cr +++ b/src/markd/renderers/html_renderer.cr @@ -283,7 +283,7 @@ module Markd def strong(node : Node, entering : Bool) : Nil @strong_stack -= 1 if @options.gfm && !entering - tag("strong", end_tag: !entering) if (@strong_stack == 0) + tag("strong", end_tag: !entering) if @strong_stack == 0 @strong_stack += 1 if @options.gfm && entering end diff --git a/src/markd/rules/heading.cr b/src/markd/rules/heading.cr index 4c304dc..23c1db1 100644 --- a/src/markd/rules/heading.cr +++ b/src/markd/rules/heading.cr @@ -6,7 +6,7 @@ module Markd::Rule SETEXT_HEADING_MARKER = /^(?:=+|-+)[ \t]*$/ def match(parser : Parser, container : Node) : MatchValue - if match = match?(parser, ATX_HEADING_MARKER) + if (match = match?(parser, ATX_HEADING_MARKER)) # ATX Heading matched parser.advance_next_nonspace parser.advance_offset(match[0].size, false) diff --git a/src/markd/rules/html_block.cr b/src/markd/rules/html_block.cr index c3df5ae..60d73bd 100644 --- a/src/markd/rules/html_block.cr +++ b/src/markd/rules/html_block.cr @@ -8,8 +8,8 @@ module Markd::Rule block_type_size = Rule::HTML_BLOCK_OPEN.size - 1 Rule::HTML_BLOCK_OPEN.each_with_index do |regex, index| - if (text.match(regex) && - (index < block_type_size || !container.type.paragraph?)) + if text.match(regex) && + (index < block_type_size || !container.type.paragraph?) parser.close_unmatched_blocks # We don't adjust parser.offset; # spaces are part of the HTML block: @@ -47,18 +47,18 @@ module Markd::Rule end def self.escape_disallowed_html(text : String) : String - String.build do |s| + String.build do |string| pos = 0 text.scan(/<\/?\s*(#{GFM_DISALLOWED_HTML_TAGS.join('|')})\b/i) do |match| start = text.index(match[0], pos) next if start.nil? - s << text[pos...start] << "<#{match[0][1..]}" + string << text[pos...start] << "<#{match[0][1..]}" pos = start + match[0].size end - s << text[pos..-1] + string << text[pos..-1] end end end diff --git a/src/markd/rules/list.cr b/src/markd/rules/list.cr index 73bc764..5f45639 100644 --- a/src/markd/rules/list.cr +++ b/src/markd/rules/list.cr @@ -6,9 +6,9 @@ module Markd::Rule ORDERED_LIST_MARKERS = {'.', ')'} def match(parser : Parser, container : Node) : MatchValue - if (!parser.indented || container.type.list?) + if !parser.indented || container.type.list? data = parse_list_marker(parser, container) - return MatchValue::None unless data && !data.empty? + return MatchValue::None if !data || data.empty? parser.close_unmatched_blocks if !parser.tip.type.list? || !list_match?(container.data, data) @@ -167,7 +167,7 @@ module Markd::Rule while container return true if container.last_line_blank? - break unless !container.last_line_checked? && container.type.in?(Node::Type::List, Node::Type::Item) + break if container.last_line_checked? || !container.type.in?(Node::Type::List, Node::Type::Item) container.last_line_checked = true container = container.last_child? end diff --git a/src/markd/rules/table.cr b/src/markd/rules/table.cr index cda107c..af35c70 100644 --- a/src/markd/rules/table.cr +++ b/src/markd/rules/table.cr @@ -37,8 +37,8 @@ module Markd::Rule def token(parser : Parser, container : Node) : Nil lines = container.text.strip.split("\n") - row_sizes = lines[...2].map do |l| - strip_pipe(l.strip).split(TABLE_CELL_SEPARATOR).size + row_sizes = lines[...2].map do |line| + strip_pipe(line.strip).split(TABLE_CELL_SEPARATOR).size end.uniq! # Do we have a real table?