From cb02a18b0360cb7b831832a65eebd595b290dd71 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sun, 4 Aug 2024 20:09:15 -0300 Subject: [PATCH] lint --- .ameba.yml | 19 +++++++++++++++++++ src/actions.cr | 15 ++++++++++++--- src/rules.cr | 10 ++++++---- src/tartrazine.cr | 22 ++++++++++++++++------ 4 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 .ameba.yml diff --git a/.ameba.yml b/.ameba.yml new file mode 100644 index 0000000..4a61a73 --- /dev/null +++ b/.ameba.yml @@ -0,0 +1,19 @@ +# This configuration file was generated by `ameba --gen-config` +# on 2024-08-04 23:09:09 UTC using Ameba version 1.6.1. +# The point is for the user to remove these configuration records +# one by one as the reported problems are removed from the code base. + +# Problems found: 2 +# Run `ameba --only Documentation/DocumentationAdmonition` for details +Documentation/DocumentationAdmonition: + Description: Reports documentation admonitions + Timezone: UTC + Excluded: + - src/tartrazine.cr + - src/actions.cr + Admonitions: + - TODO + - FIXME + - BUG + Enabled: true + Severity: Warning diff --git a/src/actions.cr b/src/actions.cr index 72eb9f7..3a289a7 100644 --- a/src/actions.cr +++ b/src/actions.cr @@ -23,13 +23,16 @@ module Tartrazine end end + # ameba:disable Metrics/CyclomaticComplexity def emit(match : Regex::MatchData?, lexer : Lexer, match_group = 0) : Array(Token) case type when "token" raise Exception.new "Can't have a token without a match" if match.nil? [Token.new(type: xml["type"], value: match[match_group])] when "push" - states_to_push = xml.attributes.select { |a| a.name == "state" }.map &.content + states_to_push = xml.attributes.select { |attrib| + attrib.name == "state" + }.map &.content if states_to_push.empty? # Push without a state means push the current state states_to_push = [lexer.state_stack.last] @@ -89,8 +92,14 @@ module Tartrazine new_lexer.tokenize(match[match_group], usingself: true) when "combined" # Combine two states into one anonymous state - states = xml.attributes.select { |a| a.name == "state" }.map &.content - new_state = states.map { |name| lexer.states[name] }.reduce { |s1, s2| s1 + s2 } + states = xml.attributes.select { |attrib| + attrib.name == "state" + }.map &.content + new_state = states.map { |name| + lexer.states[name] + }.reduce { |state1, state2| + state1 + state2 + } lexer.states[new_state.name] = new_state lexer.state_stack << new_state.name [] of Token diff --git a/src/rules.cr b/src/rules.cr index eb59114..cfacda9 100644 --- a/src/rules.cr +++ b/src/rules.cr @@ -33,9 +33,9 @@ module Tartrazine end def add_actions(node : XML::Node) - node.children.each do |node| - next unless node.element? - @actions << Action.new(node.name, node) + node.children.each do |child| + next unless child.element? + @actions << Action.new(child.name, child) end end end @@ -57,7 +57,9 @@ module Tartrazine def initialize(node : XML::Node) @xml = node.to_s - include_node = node.children.find { |n| n.name == "include" } + include_node = node.children.find { |child| + child.name == "include" + } @state = include_node["state"] if include_node add_actions(node) end diff --git a/src/tartrazine.cr b/src/tartrazine.cr index 6434b86..e7c200e 100644 --- a/src/tartrazine.cr +++ b/src/tartrazine.cr @@ -79,15 +79,18 @@ module Tartrazine pos += 1 end end - tokens.reject { |t| t[:value] == "" } + tokens.reject { |token| token[:value] == "" } end + # ameba:disable Metrics/CyclomaticComplexity def self.from_xml(xml : String) : Lexer l = Lexer.new l.xml = xml lexer = XML.parse(xml).first_element_child if lexer - config = lexer.children.find { |n| n.name == "config" } + config = lexer.children.find { |node| + node.name == "config" + } if config l.config = { name: xml_to_s(config, name) || "", @@ -96,17 +99,22 @@ module Tartrazine mime_types: xml_to_a(config, mime_type) || [] of String, priority: xml_to_f(config, priority) || 0.0, not_multiline: xml_to_s(config, not_multiline) == "true", - # FIXME: This has no effect yet (see ) + # FIXME: Because Crystal's multiline flag forces dot_all this + # doesn't work perfectly yet. dot_all: xml_to_s(config, dot_all) == "true", case_insensitive: xml_to_s(config, case_insensitive) == "true", ensure_nl: xml_to_s(config, ensure_nl) == "true", } end - rules = lexer.children.find { |n| n.name == "rules" } + rules = lexer.children.find { |node| + node.name == "rules" + } if rules # Rules contains states 🤷 - rules.children.select { |n| n.name == "state" }.each do |state_node| + rules.children.select { |node| + node.name == "state" + }.each do |state_node| state = State.new state.name = state_node["name"] if l.states.has_key?(state.name) @@ -115,7 +123,9 @@ module Tartrazine l.states[state.name] = state end # And states contain rules 🤷 - state_node.children.select { |n| n.name == "rule" }.each do |rule_node| + state_node.children.select { |node| + node.name == "rule" + }.each do |rule_node| case rule_node["pattern"]? when nil if rule_node.first_element_child.try &.name == "include"