Skip to content

Commit 504017b

Browse files
Add support for linting ECR files (pt. 2) (#541)
* Add support for linting ECR files Requires Crystal >= 1.15.0 * Apply suggestions from code review Co-authored-by: Sijawusz Pur Rahnama <[email protected]> * Add some specs related to ECR files * Remove unnecessary ECR specs * Macro helper method for ECR support --------- Co-authored-by: Sijawusz Pur Rahnama <[email protected]>
1 parent 262f57d commit 504017b

File tree

6 files changed

+73
-10
lines changed

6 files changed

+73
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ In this example we define default globs and exclude `src/compiler` folder:
174174
``` yaml
175175
Globs:
176176
- "**/*.cr"
177+
- "**/*.ecr"
177178
- "!lib"
178179
179180
Excluded:

spec/ameba/source_spec.cr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,31 @@ module Ameba
7070
CRYSTAL
7171
end
7272
end
73+
74+
Ameba.ecr_supported? do
75+
describe "#ast" do
76+
it "parses an ECR file" do
77+
source = Source.new <<-ECR, "filename.ecr"
78+
hello <%= "world" %>
79+
ECR
80+
81+
source.ast.to_s.should eq(<<-CRYSTAL)
82+
__str__ << "hello "
83+
("world").to_s(__str__)
84+
85+
CRYSTAL
86+
end
87+
88+
it "raises an exception when ECR parsing fails" do
89+
source = Source.new <<-ECR, "filename.ecr"
90+
hello <%= "world" >
91+
ECR
92+
93+
expect_raises(Crystal::SyntaxException) do
94+
source.ast
95+
end
96+
end
97+
end
98+
end
7399
end
74100
end

src/ameba.cr

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
require "./ameba/*"
2-
require "./ameba/ast/**"
3-
require "./ameba/ext/**"
4-
require "./ameba/rule/**"
5-
require "./ameba/formatter/*"
6-
require "./ameba/presenter/*"
7-
require "./ameba/source/**"
8-
91
# Ameba's entry module.
102
#
113
# To run the linter with default parameters:
@@ -40,4 +32,18 @@ module Ameba
4032
def run(config = Config.load)
4133
Runner.new(config).run
4234
end
35+
36+
macro ecr_supported?(&)
37+
{% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %}
38+
{{ yield }}
39+
{% end %}
40+
end
4341
end
42+
43+
require "./ameba/*"
44+
require "./ameba/ast/**"
45+
require "./ameba/ext/**"
46+
require "./ameba/rule/**"
47+
require "./ameba/formatter/*"
48+
require "./ameba/presenter/*"
49+
require "./ameba/source/**"

src/ameba/config.cr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "semantic_version"
22
require "yaml"
3+
require "ecr/processor"
34
require "./glob_utils"
45

56
# A configuration entry for `Ameba::Runner`.
@@ -62,6 +63,10 @@ class Ameba::Config
6263
!lib
6364
)
6465

66+
Ameba.ecr_supported? do
67+
DEFAULT_GLOBS << "**/*.ecr"
68+
end
69+
6570
getter rules : Array(Rule::Base)
6671
property severity = Severity::Convention
6772

@@ -167,7 +172,7 @@ class Ameba::Config
167172
# ```
168173
# config = Ameba::Config.load
169174
# config.sources # => list of default sources
170-
# config.globs = ["**/*.cr"]
175+
# config.globs = ["**/*.cr", "**/*.ecr"]
171176
# config.excluded = ["spec"]
172177
# config.sources # => list of sources pointing to files found by the wildcards
173178
# ```

src/ameba/glob_utils.cr

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ module Ameba
2424
def expand(globs)
2525
globs
2626
.flat_map do |glob|
27-
glob += "/**/*.cr" if File.directory?(glob)
27+
if File.directory?(glob)
28+
glob += "/**/*.cr"
29+
30+
Ameba.ecr_supported? do
31+
glob += "/**/*.ecr"
32+
end
33+
end
34+
2835
Dir[glob]
2936
end
3037
.uniq!

src/ameba/source.cr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ module Ameba
5757
# source.ast
5858
# ```
5959
getter ast : Crystal::ASTNode do
60+
code = @code
61+
62+
Ameba.ecr_supported? do
63+
if @path.ends_with?(".ecr")
64+
begin
65+
code = ECR.process_string(code, @path)
66+
rescue ex : ECR::Lexer::SyntaxException
67+
# Need to rescue to add the filename
68+
raise Crystal::SyntaxException.new(
69+
ex.message,
70+
ex.line_number,
71+
ex.column_number,
72+
@path
73+
)
74+
end
75+
end
76+
end
77+
6078
Crystal::Parser.new(code)
6179
.tap(&.wants_doc = true)
6280
.tap(&.filename = path)

0 commit comments

Comments
 (0)