Skip to content

Commit

Permalink
Add support for linting ECR files
Browse files Browse the repository at this point in the history
Requires Crystal >= 1.15.0
  • Loading branch information
nobodywasishere committed Jan 12, 2025
1 parent e113669 commit aed905d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ In this example we define default globs and exclude `src/compiler` folder:
``` yaml
Globs:
- "**/*.cr"
- "**/*.ecr"
- "!lib"
Excluded:
Expand Down
19 changes: 14 additions & 5 deletions src/ameba/config.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "semantic_version"
require "yaml"
require "ecr/processor"
require "./glob_utils"

# A configuration entry for `Ameba::Runner`.
Expand Down Expand Up @@ -57,10 +58,18 @@ class Ameba::Config
Path[XDG_CONFIG_HOME] / "ameba/config.yml",
}

DEFAULT_GLOBS = %w(
**/*.cr
!lib
)
{% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %}
DEFAULT_GLOBS = %w(
**/*.cr
**/*.ecr
!lib
)
{% else %}
DEFAULT_GLOBS = %w(
**/*.cr
!lib
)
{% end %}

getter rules : Array(Rule::Base)
property severity = Severity::Convention
Expand Down Expand Up @@ -167,7 +176,7 @@ class Ameba::Config
# ```
# config = Ameba::Config.load
# config.sources # => list of default sources
# config.globs = ["**/*.cr"]
# config.globs = ["**/*.cr", "**/*.ecr"]
# config.excluded = ["spec"]
# config.sources # => list of sources pointing to files found by the wildcards
# ```
Expand Down
5 changes: 5 additions & 0 deletions src/ameba/glob_utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ module Ameba
globs
.flat_map do |glob|
glob += "/**/*.cr" if File.directory?(glob)

{% if compare_versions(Crystal::VERSION, "1.15.0") >= 0 %}
glob += "/**/*.ecr" if File.directory?(glob)
{% end %}

Dir[glob]
end
.uniq!
Expand Down
18 changes: 18 additions & 0 deletions src/ameba/source.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ module Ameba
# source.ast
# ```
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 : Crystal::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)
.tap(&.filename = path)
Expand Down

0 comments on commit aed905d

Please sign in to comment.