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 sorbet #13

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 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 .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sorbet/* linguist-generated=true
3 changes: 2 additions & 1 deletion codeclimate_engine.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", ">= 1.9", "< 3.0"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.3"
spec.add_dependency "virtus", "~> 1.0"
spec.add_development_dependency "sorbet", "~> 0.5"
spec.add_dependency "sorbet-runtime", "~> 0.5"
end
# rubocop:enable Metrics/LineLength
10 changes: 10 additions & 0 deletions lib/cc_engine/category.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
# typed: strict
module CCEngine
module Category
extend T::Sig

sig {returns(String)}
def self.bug_risk
"Bug Risk"
end

sig {returns(String)}
def self.clarity
"Clarity"
end

sig {returns(String)}
def self.compatibility
"Compatibility"
end

sig {returns(String)}
def self.complexity
"Complexity"
end

sig {returns(String)}
def self.duplication
"Duplication"
end

sig {returns(String)}
def self.security
"Security"
end

sig {returns(String)}
def self.style
"Style"
end
Expand Down
10 changes: 10 additions & 0 deletions lib/cc_engine/config.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
# typed: strict
require "json"

module CCEngine
class Config
extend T::Sig

sig { params(json_string: String).void }
def initialize(json_string)
@json_string = json_string
end

sig { returns(T::Array[String]) }
def include_paths
parsed_json.fetch("include_paths")
end

sig { returns(T::Array[String]) }
def exclude_paths
parsed_json.fetch("exclude_paths")
end

sig { returns(T::Boolean) }
def enabled?
parsed_json.fetch("enabled")
end

protected

sig { returns(String) }
attr_reader :json_string

private

sig {returns(T::Hash[String, T.untyped])}
def parsed_json
@parsed_json = T.let(nil, T.nilable(T::Hash[String, T.untyped]))
@parsed_json ||= JSON.parse(json_string)
end
end
Expand Down
66 changes: 57 additions & 9 deletions lib/cc_engine/issue.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
require "virtus"
# typed: strict
require "json"

module CCEngine
class Issue
include Virtus.model(strict: true)
extend T::Sig

attribute :check_name, String
attribute :description, String
attribute :categories, Array[String]
attribute :location
attribute :remediation_points
attribute :content
attribute :fingerprint
sig {
params(
check_name: String,
description: String,
categories: T::Array[String],
location: T.any(CCEngine::Location::LineRange, CCEngine::Location::Position),
remediation_points: T.nilable(Integer),
content: T.nilable(String),
fingerprint: T.nilable(String)
).void
}
def initialize(
check_name:,
description:,
categories:,
location:,
remediation_points:,
content:,
fingerprint:
)
@check_name = check_name
@description = description
@categories = categories
@location = location
@remediation_points = remediation_points
@content = content
@fingerprint = fingerprint
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea what Virtus is doing, but this looks really similar to what T::Struct does. You might want to take a look and see if it fits your needs if you haven't already.


sig {returns(String)}
def render
to_hash.to_json + "\0"
end

sig {returns(String)}
def to_json
to_hash.to_json
end

sig {returns(T::Hash[Symbol, T.untyped])}
def to_hash
{
type: "issue",
Expand All @@ -33,6 +57,7 @@ def to_hash

private

sig {returns(T::Hash[Symbol, T.untyped])}
def remediation_points_hash
return {} unless remediation_points

Expand All @@ -41,6 +66,7 @@ def remediation_points_hash
}
end

sig {returns(T::Hash[Symbol, T.untyped])}
def content_hash
return {} unless content

Expand All @@ -51,12 +77,34 @@ def content_hash
}
end

sig {returns(T::Hash[Symbol, T.untyped])}
def fingerprint_hash
return {} unless fingerprint

{
fingerprint: fingerprint
}
end

sig { returns(String) }
attr_reader :check_name

sig { returns(String) }
attr_reader :description

sig { returns(T::Array[String]) }
attr_reader :categories

sig { returns(T.any(CCEngine::Location::LineRange, CCEngine::Location::Position)) }
attr_reader :location

sig { returns(T.nilable(Integer)) }
attr_reader :remediation_points

sig { returns(T.nilable(String)) }
attr_reader :content

sig { returns(T.nilable(String)) }
attr_reader :fingerprint
end
end
1 change: 1 addition & 0 deletions lib/cc_engine/location.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# typed: strict
require "cc_engine/location/line_range"
require "cc_engine/location/position"
25 changes: 21 additions & 4 deletions lib/cc_engine/location/line_range.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
require "virtus"
# typed: strict

module CCEngine
module Location
class LineRange
include Virtus.model(strict: true)
extend T::Sig

attribute :path, String
attribute :line_range, Range
sig {
params(
path: String,
line_range: T::Range[Integer],
).void
}
def initialize(path:, line_range:)
@path = path
@line_range = line_range
end

sig { returns(T::Hash[Symbol, T.untyped]) }
def to_hash
{
path: path,
Expand All @@ -17,6 +26,14 @@ def to_hash
}
}
end

private

sig { returns(String) }
attr_reader :path

sig { returns(T::Range[Integer]) }
attr_reader :line_range
end
end
end
32 changes: 26 additions & 6 deletions lib/cc_engine/location/position.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
require "virtus"
# typed: strict

module CCEngine
module Location
class Position
include Virtus.model(strict: true)

attribute :path, String
attribute :start_position
attribute :end_position
extend T::Sig
sig {
params(
path: String,
start_position: T.any(CCEngine::Position::Offset, CCEngine::Position::Grid),
end_position: T.any(CCEngine::Position::Offset, CCEngine::Position::Grid),
).void
}
def initialize(path:, start_position:, end_position:)
@path = path
@start_position = start_position
@end_position = end_position
end

sig { returns(T::Hash[Symbol, T.untyped]) }
def to_hash
{
path: path,
Expand All @@ -18,6 +27,17 @@ def to_hash
}
}
end

private

sig { returns(String) }
attr_reader :path

sig { returns(T.any(CCEngine::Position::Offset, CCEngine::Position::Grid)) }
attr_reader :start_position

sig { returns(T.any(CCEngine::Position::Offset, CCEngine::Position::Grid)) }
attr_reader :end_position
end
end
end
19 changes: 14 additions & 5 deletions lib/cc_engine/position/grid.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
require "virtus"

# typed: strict
module CCEngine
module Position
class Grid
include Virtus.model(strict: true)
extend T::Sig

attribute :line, Integer
attribute :column, Integer
sig {params(line: Integer, column: Integer).void}
def initialize(line:, column:)
@line = line
@column = column
end

sig {returns(T::Hash[Symbol, T.untyped])}
def to_hash
{
line: line,
column: column
}
end

sig {returns(Integer)}
attr_reader :line

sig {returns(Integer)}
attr_reader :column
end
end
end
16 changes: 12 additions & 4 deletions lib/cc_engine/position/offset.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
require "virtus"
# typed: strict

module CCEngine
module Position
class Offset
include Virtus.model(strict: true)

attribute :offset, Integer
extend T::Sig
sig {params(offset: Integer).void}
def initialize(offset:)
@offset = offset
end

sig {returns(T::Hash[Symbol, T.untyped])}
def to_hash
{
offset: offset
}
end

private

sig { returns(Integer) }
attr_reader :offset
end
end
end
1 change: 1 addition & 0 deletions lib/cc_engine/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# typed: strict
module CCEngine
VERSION = "0.4.1"
end
3 changes: 3 additions & 0 deletions lib/codeclimate_engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# typed: strict
require "sorbet-runtime"

require "cc_engine/version"
require "cc_engine/issue"
require "cc_engine/location"
Expand Down
2 changes: 2 additions & 0 deletions sorbet/config

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading