Skip to content

Commit

Permalink
Remove unnecessary ECR specs and add helper method for checking if EC…
Browse files Browse the repository at this point in the history
…R is supported
  • Loading branch information
nobodywasishere committed Jan 14, 2025
1 parent c0967fd commit e83504e
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 80 deletions.
7 changes: 0 additions & 7 deletions spec/ameba/rule/layout/trailing_blank_lines_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 0 additions & 11 deletions spec/ameba/rule/layout/trailing_whitespace_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 0 additions & 13 deletions spec/ameba/rule/lint/formatting_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 0 additions & 12 deletions spec/ameba/rule/lint/literal_in_condition_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 0 additions & 15 deletions spec/ameba/rule/lint/unused_literal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/ameba/source_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -95,6 +95,6 @@ module Ameba
end
end
end
{% end %}
end
end
end
4 changes: 4 additions & 0 deletions src/ameba.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions src/ameba/glob_utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 12 additions & 14 deletions src/ameba/source.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e83504e

Please sign in to comment.