From e83504e4dd6fd7a65908a418c2ba983cae4571ae Mon Sep 17 00:00:00 2001 From: Margret Riegert Date: Mon, 13 Jan 2025 23:40:56 -0500 Subject: [PATCH] Remove unnecessary ECR specs and add helper method for checking if ECR is supported --- .../rule/layout/trailing_blank_lines_spec.cr | 7 ----- .../rule/layout/trailing_whitespace_spec.cr | 11 -------- spec/ameba/rule/lint/formatting_spec.cr | 13 ---------- .../rule/lint/literal_in_condition_spec.cr | 12 --------- spec/ameba/rule/lint/unused_literal_spec.cr | 15 ----------- spec/ameba/source_spec.cr | 4 +-- src/ameba.cr | 4 +++ src/ameba/glob_utils.cr | 12 ++++----- src/ameba/source.cr | 26 +++++++++---------- 9 files changed, 24 insertions(+), 80 deletions(-) diff --git a/spec/ameba/rule/layout/trailing_blank_lines_spec.cr b/spec/ameba/rule/layout/trailing_blank_lines_spec.cr index 05e53845e..028ab496b 100644 --- a/spec/ameba/rule/layout/trailing_blank_lines_spec.cr +++ b/spec/ameba/rule/layout/trailing_blank_lines_spec.cr @@ -52,12 +52,5 @@ module Ameba::Rule::Layout issue.message.should eq "Trailing newline missing" end end - - {% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %} - it "fails if there more then one blank line at the end of an ECR source" do - source = expect_issue subject, "a = 1\n \n # error: Excessive trailing newline detected", "source.ecr" - expect_no_corrections source - end - {% end %} end end diff --git a/spec/ameba/rule/layout/trailing_whitespace_spec.cr b/spec/ameba/rule/layout/trailing_whitespace_spec.cr index 4c326c91e..df376870f 100644 --- a/spec/ameba/rule/layout/trailing_whitespace_spec.cr +++ b/spec/ameba/rule/layout/trailing_whitespace_spec.cr @@ -15,16 +15,5 @@ module Ameba::Rule::Layout expect_correction source, "whitespace at the end" end - - {% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %} - it "fails if there is a line with trailing whitespace in an ECR file" do - source = expect_issue subject, - "whitespace at the end \n" \ - " # ^^ error: Trailing whitespace detected", - path: "source.ecr" - - expect_correction source, "whitespace at the end" - end - {% end %} end end diff --git a/spec/ameba/rule/lint/formatting_spec.cr b/spec/ameba/rule/lint/formatting_spec.cr index cdb667fe1..054f3c1f3 100644 --- a/spec/ameba/rule/lint/formatting_spec.cr +++ b/spec/ameba/rule/lint/formatting_spec.cr @@ -53,18 +53,5 @@ module Ameba::Rule::Lint end end end - - {% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %} - it "passes ECR files regardless of formatting" do - source = expect_no_issues subject, <<-CRYSTAL, "source.ecr" - <% - def method(a,b,c=0) - a+b+c - end - %> - - CRYSTAL - end - {% end %} end end diff --git a/spec/ameba/rule/lint/literal_in_condition_spec.cr b/spec/ameba/rule/lint/literal_in_condition_spec.cr index 307f0a389..8150ad2bd 100644 --- a/spec/ameba/rule/lint/literal_in_condition_spec.cr +++ b/spec/ameba/rule/lint/literal_in_condition_spec.cr @@ -175,17 +175,5 @@ module Ameba::Rule::Lint issue.end_location.to_s.should eq "source.cr:1:20" issue.message.should eq "Literal value found in conditional" end - - {% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %} - it "fails if there is a predicate in if conditional in an ECR file" do - s = Source.new %( - <% if "string" %> - :ok - <% end %> - ), "source.ecr" - - subject.catch(s).should_not be_valid - end - {% end %} end end diff --git a/spec/ameba/rule/lint/unused_literal_spec.cr b/spec/ameba/rule/lint/unused_literal_spec.cr index 8cd74dd1a..5bf63f56f 100644 --- a/spec/ameba/rule/lint/unused_literal_spec.cr +++ b/spec/ameba/rule/lint/unused_literal_spec.cr @@ -295,20 +295,5 @@ module Ameba::Rule::Lint CRYSTAL end {% end %} - - {% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %} - it "passes if a string is used in an assign directive in an ECR file" do - expect_no_issues subject, <<-CRYSTAL, path: "source.ecr" - <%= "hello world" %> - CRYSTAL - end - - it "fails if a string is unused in an ECR file" do - expect_issue subject, <<-CRYSTAL, path: "source.ecr" - <% "hello world" %> - # ^^^^^^^^^^^^^ error: Literal value is not used - CRYSTAL - end - {% end %} end end diff --git a/spec/ameba/source_spec.cr b/spec/ameba/source_spec.cr index 08f8f2e78..4b1cd4e3b 100644 --- a/spec/ameba/source_spec.cr +++ b/spec/ameba/source_spec.cr @@ -71,7 +71,7 @@ module Ameba end end - {% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %} + if Ameba.ecr_supported? describe "#ast" do it "parses an ECR file" do source = Source.new <<-ECR, "filename.ecr" @@ -95,6 +95,6 @@ module Ameba end end end - {% end %} + end end end diff --git a/src/ameba.cr b/src/ameba.cr index e675939ac..94b9f141c 100644 --- a/src/ameba.cr +++ b/src/ameba.cr @@ -40,4 +40,8 @@ module Ameba def run(config = Config.load) Runner.new(config).run end + + def self.ecr_supported? : Bool + {{ compare_versions(Crystal::VERSION, "1.15.0") >= 0 }} + end end diff --git a/src/ameba/glob_utils.cr b/src/ameba/glob_utils.cr index 393663d3c..ef6c6c6f9 100644 --- a/src/ameba/glob_utils.cr +++ b/src/ameba/glob_utils.cr @@ -24,13 +24,13 @@ module Ameba def expand(globs) globs .flat_map do |glob| - if File.directory?(glob) - glob += "/**/*.cr" + if File.directory?(glob) + glob += "/**/*.cr" - {% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %} - glob += "/**/*.ecr" - {% end %} - end + if Ameba.ecr_supported? + glob += "/**/*.ecr" + end + end Dir[glob] end diff --git a/src/ameba/source.cr b/src/ameba/source.cr index 5f737a601..010638feb 100644 --- a/src/ameba/source.cr +++ b/src/ameba/source.cr @@ -59,21 +59,19 @@ module Ameba getter ast : Crystal::ASTNode do code = @code - {% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %} - if @path.ends_with?(".ecr") - begin - code = ECR.process_string(code, @path) - rescue ex : ECR::Lexer::SyntaxException - # Need to rescue to add the filename - raise Crystal::SyntaxException.new( - ex.message, - ex.line_number, - ex.column_number, - @path - ) - end + if Ameba.ecr_supported? && @path.ends_with?(".ecr") + begin + code = ECR.process_string(code, @path) + rescue ex : ECR::Lexer::SyntaxException + # Need to rescue to add the filename + raise Crystal::SyntaxException.new( + ex.message, + ex.line_number, + ex.column_number, + @path + ) end - {% end %} + end Crystal::Parser.new(code) .tap(&.wants_doc = true)