diff --git a/src/ameba/rule.cr b/src/ameba/rule.cr index 25664c84..6bc6585b 100644 --- a/src/ameba/rule.cr +++ b/src/ameba/rule.cr @@ -4,6 +4,16 @@ module Ameba Rules::TrailingWhitespace, ] + macro rule(name, &block) + module Ameba::Rules + struct {{name.id}} < Rule + def test(source) + {{block.body}} + end + end + end + end + abstract struct Rule abstract def test(source : Source) diff --git a/src/ameba/rules/line_length.cr b/src/ameba/rules/line_length.cr index 520ef46e..8c627002 100644 --- a/src/ameba/rules/line_length.cr +++ b/src/ameba/rules/line_length.cr @@ -1,14 +1,8 @@ -module Ameba::Rules - # A rule that checks the size of lines in sources. - # - # This rule will report an error if there is at least - # one line longer than 79 symbols. - struct LineLength < Rule - def test(source) - source.lines.each_with_index do |line, index| - next unless line.size > 79 - source.error self, index + 1, "Line too long (#{line.size} symbols)" - end - end +# A rule that disallows lines longer than 79 symbols. + +Ameba.rule LineLength do |source| + source.lines.each_with_index do |line, index| + next unless line.size > 79 + source.error self, index + 1, "Line too long (#{line.size} symbols)" end end diff --git a/src/ameba/rules/trailing_whitespace.cr b/src/ameba/rules/trailing_whitespace.cr index 93ec84c7..53db5892 100644 --- a/src/ameba/rules/trailing_whitespace.cr +++ b/src/ameba/rules/trailing_whitespace.cr @@ -1,11 +1,8 @@ -module Ameba::Rules - # A rule that disallows trailing whitespace at the end of a line. - struct TrailingWhitespace < Rule - def test(source) - source.lines.each_with_index do |line, index| - next unless line =~ /\s$/ - source.error self, index + 1, "Trailing whitespace detected" - end - end +# A rule that disallows trailing whitespace at the end of a line. + +Ameba.rule TrailingWhitespace do |source| + source.lines.each_with_index do |line, index| + next unless line =~ /\s$/ + source.error self, index + 1, "Trailing whitespace detected" end end