Skip to content

Commit

Permalink
Implement rule versioning support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Oct 7, 2024
1 parent f72f0b1 commit 0fc5e59
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/ameba/cli/cmd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module Ameba::Cli
config.autocorrect = autocorrect
config.stdin_filename = opts.stdin_filename

if version = opts.version
config.version = version
end
if globs = opts.globs
config.globs = globs
end
Expand Down Expand Up @@ -53,6 +56,7 @@ module Ameba::Cli

private class Opts
property config : Path?
property version : String?
property formatter : Symbol | String | Nil
property globs : Array(String)?
property only : Array(String)?
Expand Down Expand Up @@ -90,6 +94,11 @@ module Ameba::Cli
opts.config = Path[path] unless path.empty?
end

parser.on("-u", "--up-to-version VERSION",
"Choose a version") do |version|
opts.version = version
end

parser.on("-f", "--format FORMATTER",
"Choose an output formatter: #{Config.formatter_names}") do |formatter|
opts.formatter = formatter
Expand Down
28 changes: 28 additions & 0 deletions src/ameba/config.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "semantic_version"
require "yaml"
require "./glob_utils"

Expand Down Expand Up @@ -63,6 +64,9 @@ class Ameba::Config
getter rules : Array(Rule::Base)
property severity = Severity::Convention

# Returns an ameba version to be used by `Ameba::Runner`.
property version : SemanticVersion?

# Returns a list of paths (with wildcards) to files.
# Represents a list of sources to be inspected.
# If globs are not set, it will return default list of files.
Expand Down Expand Up @@ -100,6 +104,9 @@ class Ameba::Config
@excluded = load_array_section(config, "Excluded")
@globs = load_array_section(config, "Globs", DEFAULT_GLOBS)

if version = config["version"]?.try(&.as_s).presence
self.version = version
end
if formatter_name = load_formatter_name(config)
self.formatter = formatter_name
end
Expand Down Expand Up @@ -194,6 +201,16 @@ class Ameba::Config
@formatter = formatter.new
end

# Sets version from string.
#
# ```
# config = Ameba::Config.load
# config.version = "1.6.0"
# ```
def version=(version : String)
@version = SemanticVersion.parse(version)
end

# Updates rule properties.
#
# ```
Expand Down Expand Up @@ -329,6 +346,17 @@ class Ameba::Config
@[YAML::Field(key: "Excluded")]
property excluded : Array(String)?
{% end %}

{% unless properties["since_version".id] %}
@[YAML::Field(key: "SinceVersion")]
property since_version : String?
{% end %}

def since_version : SemanticVersion?
if version = @since_version
SemanticVersion.parse(version)
end
end
end

macro included
Expand Down
2 changes: 2 additions & 0 deletions src/ameba/formatter/todo_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ module Ameba::Formatter
# The point is for the user to remove these configuration records
# one by one as the reported problems are removed from the code base.
version: "#{VERSION}"
HEADER
end

Expand Down
12 changes: 10 additions & 2 deletions src/ameba/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ module Ameba
# Returns `true` if correctable issues should be autocorrected.
private getter? autocorrect : Bool

# Returns an ameba version up to which the rules should be ran.
property version : SemanticVersion?

# Instantiates a runner using a `config`.
#
# ```
Expand All @@ -62,15 +65,20 @@ module Ameba
@sources = config.sources
@formatter = config.formatter
@severity = config.severity
@rules = config.rules.select(&.enabled?).reject!(&.special?)
@version = config.version
@rules = config.rules.select do |rule|
rule.enabled? && !rule.special? &&
(!(version = @version) || !(since_version = rule.since_version) ||
since_version <= version)
end
@autocorrect = config.autocorrect?

@unneeded_disable_directive_rule =
config.rules
.find &.class.==(Rule::Lint::UnneededDisableDirective)
end

protected def initialize(@rules, @sources, @formatter, @severity, @autocorrect = false)
protected def initialize(@rules, @sources, @formatter, @severity, @autocorrect = false, @version = nil)
end

# Performs the inspection. Iterates through all sources and test it using
Expand Down

0 comments on commit 0fc5e59

Please sign in to comment.