Skip to content

Commit

Permalink
1.3 (#40)
Browse files Browse the repository at this point in the history
* Started refactoring

* Update docker image and add more comments

* Update representer dependecy

* Update weighing machine comment system

* Fix path

* Fix result
meatball133 authored Aug 22, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 54a68ef commit 947aa2a
Showing 12 changed files with 98 additions and 25 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.3.0

- Upgraded representer to 1.3.0
- Crystal 1.13 support
- Cleanup and refactor

# 1.2.0

- 4 Custom comments for exercises
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM crystallang/crystal:1.11.2-alpine as Builder
FROM crystallang/crystal:1.13.1-alpine as Builder

Check warning on line 1 in Dockerfile

GitHub Actions / Tests

Stage names should be lowercase

StageNameCasing: Stage name 'Builder' should be lowercase More info: https://docs.docker.com/go/dockerfile/rule/stage-name-casing/

Check warning on line 1 in Dockerfile

GitHub Actions / Tests

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/

Check warning on line 1 in Dockerfile

GitHub Actions / build-and-push-image / build-and-push

Stage names should be lowercase

StageNameCasing: Stage name 'Builder' should be lowercase More info: https://docs.docker.com/go/dockerfile/rule/stage-name-casing/

Check warning on line 1 in Dockerfile

GitHub Actions / build-and-push-image / build-and-push

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/

Check warning on line 1 in Dockerfile

GitHub Actions / build-and-push-image / build-and-push

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/

Check warning on line 1 in Dockerfile

GitHub Actions / build-and-push-image / build-and-push

Stage names should be lowercase

StageNameCasing: Stage name 'Builder' should be lowercase More info: https://docs.docker.com/go/dockerfile/rule/stage-name-casing/

Check warning on line 1 in Dockerfile

GitHub Actions / build-and-push-image / build-and-push

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/

Check warning on line 1 in Dockerfile

GitHub Actions / build-and-push-image / build-and-push

Stage names should be lowercase

StageNameCasing: Stage name 'Builder' should be lowercase More info: https://docs.docker.com/go/dockerfile/rule/stage-name-casing/

# install packages required to run the representer
RUN apk add --no-cache bash coreutils yaml-dev musl-dev make
@@ -15,9 +15,9 @@ WORKDIR /lib/ameba

RUN make clean && make

FROM alpine:3.17
FROM alpine:3.20

RUN apk add --update --no-cache --force-overwrite pcre-dev pcre2-dev bash jq coreutils libgcc yaml libevent gc
RUN apk add --update --no-cache --force-overwrite pcre2-dev bash jq coreutils libgcc yaml libevent gc
WORKDIR /opt/analyzer

COPY . .
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: analyser
version: 1.2.0
version: 1.3.0

development_dependencies:
ameba:
@@ -9,4 +9,4 @@ development_dependencies:
dependencies:
representer:
github: exercism/crystal-representer
version: ~> 1.3.0
version: ~> 1.3.2
17 changes: 3 additions & 14 deletions src/analyzer.cr
Original file line number Diff line number Diff line change
@@ -7,9 +7,7 @@ class Analysis
getter summery : String?
property comments : Array(Comments)

def initialize(summery : String?, comments : Array(Comments))
@summery = summery
@comments = comments
def initialize(@comments : Array(Comments), @summery : String? = nil)
end
end

@@ -19,20 +17,11 @@ class Comments
getter params : Hash(String, String | Int32)
getter type : String

def initialize(comment : String, params : Hash(String, String | Int32), type : String)
@comment = comment
@params = params
@type = type
def initialize(@comment : String, @params : Hash(String, String | Int32), @type : String)
end
end

class Analyzer
Types = {
"Error" => "error",
"Warning" => "warning",
"Convention" => "convention",
}

@result : Array(Comments) = [] of Comments

property result
@@ -90,5 +79,5 @@ if ARGV.size >= 4
anylyser.analyze(ARGV[3], ARGV[2])
result = anylyser.result

File.write(ARGV[1], Analysis.new(nil, result).to_json)
File.write(ARGV[1], Analysis.new(result).to_json)
end
23 changes: 20 additions & 3 deletions src/exercise-analyser.cr
Original file line number Diff line number Diff line change
@@ -4,11 +4,10 @@ require "./visitor_analyzer"

class ExerciseAnayzer
getter exercise, anlyzation
property comments, concepts
property comments

@comments = Array(Comments).new
@anlyzation : Array(Types) = Array(Types).new
@concepts : Array(String) = Array(String).new

def initialize(exercise : String, path : String)
file_content = File.read(path)
@@ -18,7 +17,6 @@ class ExerciseAnayzer
analyser = GeneralAnalyzer.new
analyser.accept(ast)
@anlyzation = analyser.types
@concepts = analyser.concepts # Adding the possibilty of marking an exercise as having a certain concept
rescue
p "Error parsing the file"
end
@@ -137,6 +135,25 @@ class ExerciseAnayzer
else
@comments << Comments.new("crystal.castle-dinner.uses_or", Hash(String, String | Int32).new, "celebratory")
end
when "darts"
if anlyzation.any? { |x| x.options["type"] == "Call" && (x.options["name"] == "**" || x.options["name"] == "pow") }
@comments << Comments.new("crystal.darts.hypot", Hash(String, String | Int32).new, "actionable")
end
if anlyzation.any? { |x| x.options["type"] == "Call" && x.options["receiver"] == "hypot" }
@comments << Comments.new("crystal.darts.uses_hypot", Hash(String, String | Int32).new, "celebratory")
end
when "weighing-machine"
unless anlyzation.any? { |x| x.options["type"] == "Call" && x.argumments.any? { |x| x["name"].to_s.starts_with?("precision") } && x.inside_class == "WeighingMachine" }
@comments << Comments.new("crystal.weighing-machine.missing_method", Hash(String, String | Int32) {"method_name" => "getter"}, "essential")
end

unless anlyzation.any? { |x| x.options["type"] == "Call" && x.argumments.any? { |x| x["name"].to_s.starts_with?("metric") } && x.inside_class == "WeighingMachine" }
@comments << Comments.new("crystal.weighing-machine.missing_method", Hash(String, String | Int32) {"method_name" => "setter"}, "essential")
end

unless anlyzation.any? { |x| x.options["type"] == "Call" && x.argumments.any? { |x| x["name"].to_s.starts_with?("weight") } && x.inside_class == "WeighingMachine" }
@comments << Comments.new("crystal.weighing-machine.missing_method", Hash(String, String | Int32) {"method_name" => "property"}, "essential")
end
end
end
end
5 changes: 2 additions & 3 deletions src/visitor_analyzer.cr
Original file line number Diff line number Diff line change
@@ -69,14 +69,13 @@ class GeneralAnalyzer < Crystal::Visitor
end

def visit(node : Crystal::If)
options = Hash(String, String?){"name" => node.cond.to_s, "type" => "If"}
options = Hash(String, String?) {"name" => node.cond.to_s, "type" => "If"}
types << Types.new(options, Array(Hash(String, String?)).new, @inside_method, @inside_class, @inside_struct, @inside_enum, @inside_module)
true
end

def visit(node : Crystal::Assign)
options = Hash(String, String?).new
options.merge!({"name" => node.target.to_s, "type" => "Assign", "value" => node.value.to_s})
options = Hash(String, String?) {"name" => node.target.to_s, "type" => "Assign", "value" => node.value.to_s}
types << Types.new(options, Array(Hash(String, String?)).new, @inside_method, @inside_class, @inside_struct, @inside_enum, @inside_module)
true
end
7 changes: 7 additions & 0 deletions tests/darts/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": {
"solution": [
"src/darts.cr"
]
}
}
1 change: 1 addition & 0 deletions tests/darts/expected_analysis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"comments":[{"comment":"crystal.ameba.warning","params":{"message":"Useless assignment to variable `value`","line_number":5},"type":"actionable"},{"comment":"crystal.darts.hypot","params":{},"type":"actionable"},{"comment":"crystal.darts.uses_hypot","params":{},"type":"celebratory"}]}
17 changes: 17 additions & 0 deletions tests/darts/src/darts.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Darts
extend self

def score(x : Number, y : Number) : Number
value = x ** 2 + y ** 2
hypot = Math.hypot(x, y)
if hypot <= 1
10
elsif hypot <= 5
5
elsif hypot <= 10
1
else
0
end
end
end
7 changes: 7 additions & 0 deletions tests/weighing-machine/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": {
"solution": [
"src/weighing_machine.cr"
]
}
}
1 change: 1 addition & 0 deletions tests/weighing-machine/expected_analysis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"comments":[{"comment":"crystal.ameba.convention","params":{"message":"Trailing whitespace detected","line_number":17},"type":"informative"},{"comment":"crystal.weighing-machine.missing_method","params":{"method_name":"getter"},"type":"essential"},{"comment":"crystal.weighing-machine.missing_method","params":{"method_name":"setter"},"type":"essential"},{"comment":"crystal.weighing-machine.missing_method","params":{"method_name":"property"},"type":"essential"}]}
29 changes: 29 additions & 0 deletions tests/weighing-machine/src/weighing_machine.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class WeighingMachine
def precision : Int32
@precision
end

def metric=(value : Bool) : Bool
@metric = value
end

def weight : Float64
@weight
end

def weight=(value : Float64) : Float64
@weight = value
end

def initialize(precision : Int32, metric : Bool = true)
@precision = precision
@metric = metric
end

# DO NOT MODIFY ANYTHING BELOW THIS LINE
def weigh : String
weight = @metric ? @weight : @weight * 2.20462
weight = weight.round(@precision)
weight.to_s
end
end

0 comments on commit 947aa2a

Please sign in to comment.