Skip to content

Commit

Permalink
Few refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Jan 1, 2024
1 parent 6d0b12c commit e99a697
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 58 deletions.
28 changes: 17 additions & 11 deletions spec/ameba/ast/scope_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ module Ameba::AST
end
end
CRYSTAL
scope = Scope.new nodes.def_nodes.first

var_node = nodes.var_nodes.first
scope.add_variable var_node

scope = Scope.new nodes.def_nodes.first
scope.add_variable(var_node)
scope.inner_scopes << Scope.new(nodes.block_nodes.first, scope)

variable = Variable.new(var_node, scope)
variable.reference nodes.var_nodes.first, scope.inner_scopes.first
variable.reference(nodes.var_nodes.first, scope.inner_scopes.first)

scope.references?(variable).should be_true
end
Expand All @@ -77,13 +79,15 @@ module Ameba::AST
end
end
CRYSTAL
scope = Scope.new nodes.def_nodes.first

var_node = nodes.var_nodes.first
scope.add_variable var_node

scope = Scope.new nodes.def_nodes.first
scope.add_variable(var_node)
scope.inner_scopes << Scope.new(nodes.block_nodes.first, scope)

variable = Variable.new(var_node, scope)
variable.reference nodes.var_nodes.first, scope.inner_scopes.first
variable.reference(nodes.var_nodes.first, scope.inner_scopes.first)

scope.references?(variable, check_inner_scopes: false).should be_false
end
Expand All @@ -98,9 +102,11 @@ module Ameba::AST
end
end
CRYSTAL
scope = Scope.new nodes.def_nodes.first

var_node = nodes.var_nodes.first
scope.add_variable var_node

scope = Scope.new nodes.def_nodes.first
scope.add_variable(var_node)
scope.inner_scopes << Scope.new(nodes.block_nodes.first, scope)

variable = Variable.new(var_node, scope)
Expand All @@ -120,7 +126,7 @@ module Ameba::AST
describe "#find_variable" do
it "returns the variable in the scope by name" do
scope = Scope.new as_node("foo = 1")
scope.add_variable Crystal::Var.new "foo"
scope.add_variable(Crystal::Var.new "foo")
scope.find_variable("foo").should_not be_nil
end

Expand All @@ -133,15 +139,15 @@ module Ameba::AST
describe "#assign_variable" do
it "creates a new assignment" do
scope = Scope.new as_node("foo = 1")
scope.add_variable Crystal::Var.new "foo"
scope.add_variable(Crystal::Var.new "foo")
scope.assign_variable("foo", Crystal::Var.new "foo")
var = scope.find_variable("foo").should_not be_nil
var.assignments.size.should eq 1
end

it "does not create the assignment if variable is wrong" do
scope = Scope.new as_node("foo = 1")
scope.add_variable Crystal::Var.new "foo"
scope.add_variable(Crystal::Var.new "foo")
scope.assign_variable("bar", Crystal::Var.new "bar")
var = scope.find_variable("foo").should_not be_nil
var.assignments.size.should eq 0
Expand Down
13 changes: 9 additions & 4 deletions spec/ameba/ast/variabling/variable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ module Ameba::AST
3.times { |i| a = a + i }
end
CRYSTAL
scope = Scope.new nodes.def_nodes.first

var_node = nodes.var_nodes.first
scope.add_variable var_node

scope = Scope.new(nodes.def_nodes.first)
scope.add_variable(var_node)
scope.inner_scopes << Scope.new(nodes.block_nodes.first, scope)

variable = Variable.new(var_node, scope)
variable.reference nodes.var_nodes.last, scope.inner_scopes.last
variable.reference(nodes.var_nodes.last, scope.inner_scopes.last)

variable.captured_by_block?.should be_truthy
end

Expand All @@ -101,8 +104,10 @@ module Ameba::AST
a = 1
end
CRYSTAL
scope.add_variable Crystal::Var.new "a"

scope.add_variable(Crystal::Var.new "a")
variable = scope.variables.first

variable.captured_by_block?.should be_falsey
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ameba/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module Ameba
end

it "raises when custom config file doesn't exist" do
expect_raises(Exception, "Unable to load config file: Config file does not exist foo.yml") do
expect_raises(Exception, "Unable to load config file: Config file does not exist") do
Config.load "foo.yml"
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/ameba/rule/lint/literals_comparison_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Ameba::Rule::Lint
["foo"] === [foo]
"foo" == foo
"foo" != foo
"foo" == FOO
FOO == "foo"
foo == "foo"
foo != "foo"
CRYSTAL
Expand Down
8 changes: 4 additions & 4 deletions src/ameba/ast/variabling/variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module Ameba::AST
#
# ```
# variable = Variable.new(node, scope)
# variable.reference(var_node)
# variable.reference(var_node, some_scope)
# variable.referenced? # => true
# ```
def referenced?
Expand Down Expand Up @@ -206,9 +206,9 @@ module Ameba::AST
return if references.size > assignments.size
return if assignments.any?(&.op_assign?)

@assign_before_reference = assignments.find { |ass|
!ass.in_branch?
}.try &.node
@assign_before_reference = assignments
.find(&.in_branch?.!)
.try(&.node)
end
end
end
4 changes: 2 additions & 2 deletions src/ameba/ast/visitors/scope_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ module Ameba::AST

# :nodoc:
def visit(node : Crystal::Var)
variable = @current_scope.find_variable node.name
variable = @current_scope.find_variable(node.name)

case
when @current_scope.arg?(node) # node is an argument
@current_scope.add_argument(node)
when variable.nil? && @current_assign # node is a variable
@current_scope.add_variable(node)
when variable # node is a reference
reference = variable.reference node, @current_scope
reference = variable.reference(node, @current_scope)
if @current_assign.is_a?(Crystal::OpAssign) || !reference.target_of?(@current_assign)
variable.reference_assignments!
end
Expand Down
9 changes: 5 additions & 4 deletions src/ameba/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ class Ameba::Config
@excluded = load_array_section(config, "Excluded")
@globs = load_array_section(config, "Globs", DEFAULT_GLOBS)

return unless formatter_name = load_formatter_name(config)
self.formatter = formatter_name
if formatter_name = load_formatter_name(config)
self.formatter = formatter_name
end
end

# Loads YAML configuration file by `path`.
Expand All @@ -120,8 +121,8 @@ class Ameba::Config

protected def self.read_config(path = nil)
if path
raise ArgumentError.new("Config file does not exist #{path}") unless File.exists?(path)
return File.read(path)
return File.read(path) if File.exists?(path)
raise "Config file does not exist"
end
each_config_path do |config_path|
return File.read(config_path) if File.exists?(config_path)
Expand Down
33 changes: 19 additions & 14 deletions src/ameba/formatter/todo_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ module Ameba::Formatter
end

private def generate_todo_config(issues)
file = File.new(@config_path, mode: "w")
file << header
rule_issues_map(issues).each do |rule, rule_issues|
file << "\n# Problems found: #{rule_issues.size}"
file << "\n# Run `ameba --only #{rule.name}` for details"
file << rule_todo(rule, rule_issues).gsub("---", "")
File.open(@config_path, mode: "w") do |file|
file << header

rule_issues_map(issues).each do |rule, rule_issues|
rule_todo = rule_todo(rule, rule_issues)
rule_todo =
{rule_todo.name => rule_todo}
.to_yaml.gsub("---", "")

file << "\n# Problems found: #{rule_issues.size}"
file << "\n# Run `ameba --only #{rule.name}` for details"
file << rule_todo
end
file
end
file
ensure
file.close if file
end

private def rule_issues_map(issues)
Expand All @@ -60,11 +65,11 @@ module Ameba::Formatter
end

private def rule_todo(rule, issues)
rule.excluded = issues
.compact_map(&.location.try &.filename.try &.to_s)
.uniq!

{rule.name => rule}.to_yaml
rule.dup.tap do |rule_todo|
rule_todo.excluded = issues
.compact_map(&.location.try &.filename.try &.to_s)
.uniq!
end
end
end
end
33 changes: 15 additions & 18 deletions src/ameba/rule/style/verbose_block.cr
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,22 @@ module Ameba::Rule::Style
i
end

protected def args_to_s(io : IO, node : Crystal::Call, short_block = nil, skip_last_arg = false)
node.args.dup.tap do |args|
args.pop? if skip_last_arg
args.join io, ", "

named_args = node.named_args
if named_args
io << ", " unless args.empty? || named_args.empty?
named_args.join io, ", " do |arg, inner_io|
inner_io << arg.name << ": " << arg.value
end
protected def args_to_s(io : IO, node : Crystal::Call, short_block = nil, skip_last_arg = false) : Nil
args = node.args.dup
args.pop? if skip_last_arg
args.join io, ", "

named_args = node.named_args
if named_args
io << ", " unless args.empty? || named_args.empty?
named_args.join io, ", " do |arg, inner_io|
inner_io << arg.name << ": " << arg.value
end
end

if short_block
io << ", " unless args.empty? && (named_args.nil? || named_args.empty?)
io << short_block
end
if short_block
io << ", " unless args.empty? && (named_args.nil? || named_args.empty?)
io << short_block
end
end

Expand Down Expand Up @@ -164,9 +163,7 @@ module Ameba::Rule::Style
return unless block_end_location = block.body.end_location

block_code = source_between(block_location, block_end_location, source.lines)
return unless block_code.try(&.starts_with?("&."))

block_code
block_code if block_code.try(&.starts_with?("&."))
end

protected def call_code(source, call, body)
Expand Down

0 comments on commit e99a697

Please sign in to comment.