Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for linting ECR files #536

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -174,6 +174,7 @@ In this example we define default globs and exclude `src/compiler` folder:
``` yaml
Globs:
- "**/*.cr"
- "**/*.ecr"
- "!lib"
Excluded:
26 changes: 26 additions & 0 deletions spec/ameba/source_spec.cr
Original file line number Diff line number Diff line change
@@ -70,5 +70,31 @@ module Ameba
CRYSTAL
end
end

if Ameba.ecr_supported?
describe "#ast" do
it "parses an ECR file" do
source = Source.new <<-ECR, "filename.ecr"
hello <%= "world" %>
ECR

source.ast.to_s.should eq(<<-CRYSTAL)
__str__ << "hello "
("world").to_s(__str__)

CRYSTAL
end

it "raises an exception when ECR parsing fails" do
source = Source.new <<-ECR, "filename.ecr"
hello <%= "world" >
ECR

expect_raises(Crystal::SyntaxException) do
source.ast
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
@@ -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
7 changes: 6 additions & 1 deletion 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`.
@@ -62,6 +63,10 @@ class Ameba::Config
!lib
)

if Ameba.ecr_supported?
DEFAULT_GLOBS << "**/*.ecr"
end

getter rules : Array(Rule::Base)
property severity = Severity::Convention

@@ -167,7 +172,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
# ```
9 changes: 8 additions & 1 deletion src/ameba/glob_utils.cr
Original file line number Diff line number Diff line change
@@ -24,7 +24,14 @@ module Ameba
def expand(globs)
globs
.flat_map do |glob|
glob += "/**/*.cr" if File.directory?(glob)
if File.directory?(glob)
glob += "/**/*.cr"

if Ameba.ecr_supported?
glob += "/**/*.ecr"
end
end

Dir[glob]
end
.uniq!
16 changes: 16 additions & 0 deletions src/ameba/source.cr
Original file line number Diff line number Diff line change
@@ -57,6 +57,22 @@ module Ameba
# source.ast
# ```
getter ast : Crystal::ASTNode do
code = @code

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

Crystal::Parser.new(code)
.tap(&.wants_doc = true)
.tap(&.filename = path)