Track issue.end_location properly

This commit is contained in:
Vitalii Elenhaupt 2018-11-24 19:38:13 +02:00
parent ad2c6bad0e
commit 9885457227
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
45 changed files with 90 additions and 20 deletions

View file

@ -323,7 +323,7 @@ module Ameba::AST
branch.to_s.should eq branch.node.to_s
end
it "delegates location to node" do
it "delegates locations to node" do
nodes = as_nodes %(
if true
a = 2
@ -332,6 +332,7 @@ module Ameba::AST
branchable = Branchable.new nodes.if_nodes.first
branch = Branch.new nodes.assign_nodes.first, branchable
branch.location.should eq branch.node.location
branch.end_location.should eq branch.node.end_location
end
end

View file

@ -16,10 +16,11 @@ module Ameba::AST
branchable.to_s.should eq node.to_s
end
it "delegates location to @node" do
it "delegates locations to @node" do
node = as_node %(a = 2 if true)
branchable = Branchable.new node
branchable.location.should eq node.location
branchable.end_location.should eq node.end_location
end
end

View file

@ -17,10 +17,11 @@ module Ameba::AST
flow_expression.to_s.should eq node.to_s
end
it "delegates location to @node" do
it "delegates locations to @node" do
node = as_node %(break if true)
flow_expression = FlowExpression.new node, false
flow_expression.location.should eq node.location
flow_expression.end_location.should eq node.end_location
end
end

View file

@ -18,6 +18,21 @@ module Ameba::AST
end
end
describe "delegation" do
it "delegates to_s to node" do
node = as_node("def foo; end")
scope = Scope.new node
scope.to_s.should eq node.to_s
end
it "delegates locations to node" do
node = as_node("def foo; end")
scope = Scope.new node
scope.location.should eq node.location
scope.end_location.should eq node.end_location
end
end
describe "#add_variable" do
it "adds a new variable to the scope" do
scope = Scope.new as_node("")

View file

@ -14,9 +14,10 @@ module Ameba::AST
end
describe "delegation" do
it "delegates location to node" do
it "delegates locations to node" do
argument = Argument.new(arg, variable)
argument.location.should eq arg.location
argument.end_location.should eq arg.end_location
end
it "delegates to_s to node" do

View file

@ -22,9 +22,10 @@ module Ameba::AST
end
describe "delegation" do
it "delegates location" do
it "delegates locations" do
assignment = Assignment.new(node, variable)
assignment.location.should eq node.location
assignment.end_location.should eq node.end_location
end
it "delegates to_s" do

View file

@ -13,9 +13,10 @@ module Ameba::AST
end
describe "delegation" do
it "delegates location" do
it "delegates locations" do
variable = Variable.new(var_node, scope)
variable.location.should eq var_node.location
variable.end_location.should eq var_node.end_location
end
it "delegates name" do

View file

@ -27,6 +27,7 @@ module Ameba::Rule::Layout
issue = source.issues.first
issue.rule.should eq subject
issue.location.to_s.should eq "source.cr:1:#{subject.max_length + 1}"
issue.end_location.should be_nil
issue.message.should eq "Line too long"
end

View file

@ -31,6 +31,7 @@ module Ameba::Rule::Layout
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:3:1"
issue.end_location.should be_nil
issue.message.should eq "Blank lines detected at the end of the file"
end
end

View file

@ -21,6 +21,7 @@ module Ameba::Rule::Layout
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:7"
issue.end_location.should be_nil
issue.message.should eq "Trailing whitespace detected"
end
end

View file

@ -106,6 +106,7 @@ module Ameba::Rule::Lint
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:9"
issue.message.should eq "Comparison to a boolean is pointless"
end
end

View file

@ -38,6 +38,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:8"
issue.message.should eq "Possible forgotten debugger statement detected"
end
end

View file

@ -62,6 +62,7 @@ module Ameba::Rule::Lint
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:3"
issue.end_location.to_s.should eq "source.cr:6:3"
issue.message.should eq "Empty `ensure` block detected"
end
end

View file

@ -104,6 +104,7 @@ module Ameba
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:5"
issue.message.should eq "Avoid empty expressions"
end
end

View file

@ -35,6 +35,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:24"
issue.message.should eq %(Duplicated keys in hash literal: "a")
end

View file

@ -66,6 +66,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:20"
issue.message.should eq "Literal value found in conditional"
end
end

View file

@ -35,6 +35,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:6"
issue.message.should eq "Literal value found in interpolation"
end
end

View file

@ -61,6 +61,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.should be_nil
issue.message.should eq(
"Symbols `,\"` may be unwanted in %w array literals"
)

View file

@ -30,6 +30,7 @@ module Ameba::Rule::Lint
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:7"
issue.message.should eq "rand(1) always returns 0"
end
end

View file

@ -159,6 +159,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:3"
issue.end_location.to_s.should eq "source.cr:2:10"
issue.message.should eq "Argument `bar` is assigned before it is used"
end
end

View file

@ -167,6 +167,7 @@ module Ameba::Rule::Lint
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:3"
issue.end_location.to_s.should eq "source.cr:4:3"
issue.message.should eq(
"Exception handler has shadowed exceptions: IndexError"
)

View file

@ -161,6 +161,7 @@ module Ameba::Rule::Lint
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:12"
issue.end_location.should be_nil
issue.message.should eq "Shadowing outer local variable `foo`"
end
end

View file

@ -682,5 +682,20 @@ module Ameba::Rule::Lint
subject.catch(s).should be_valid
end
end
it "reports message, rule, location" do
s = Source.new %(
return
:unreachable
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:1"
issue.end_location.to_s.should eq "source.cr:2:12"
issue.message.should eq "Unreachable code detected"
end
end
end

View file

@ -39,6 +39,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:5:15"
issue.end_location.to_s.should eq "source.cr:5:27"
issue.message.should eq "Useless condition in when detected"
end
end

View file

@ -43,6 +43,7 @@ module Ameba
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:9"
issue.message.should eq(
"Constant name should be screaming-cased: CONST, not Const"
)

View file

@ -118,6 +118,7 @@ module Ameba
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.should be_nil
issue.message.should match /1_200_000/
end

View file

@ -47,6 +47,7 @@ module Ameba
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:2:3"
issue.message.should eq(
"Method name should be underscore-cased: bad_name, not bad_Name"
)

View file

@ -62,6 +62,7 @@ module Ameba::Rule::Style
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:21"
issue.message.should eq "Avoid negated conditions in unless blocks"
end
end

View file

@ -41,6 +41,7 @@ module Ameba::Rule::Style
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:3"
issue.end_location.to_s.should eq "source.cr:4:5"
issue.message.should eq(
"Favour method name 'picture?' over 'has_picture?'")
end

View file

@ -220,6 +220,7 @@ module Ameba::Rule::Style
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:7:3"
issue.message.should eq "Redundant `begin` block detected"
end
end

View file

@ -52,6 +52,7 @@ module Ameba
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:2:3"
issue.message.should eq(
"Type name should be camelcased: MyClass, but it was My_class"
)

View file

@ -38,6 +38,7 @@ module Ameba::Rule::Style
issue.should_not be_nil
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:5:3"
issue.message.should eq "Favour if over unless with else"
end
end

View file

@ -53,6 +53,7 @@ module Ameba
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:7"
issue.message.should eq(
"Var name should be underscore-cased: bad_name, not badName"
)

View file

@ -36,6 +36,7 @@ module Ameba::Rule::Style
issue = source.issues.first
issue.location.to_s.should eq "source.cr:2:1"
issue.end_location.to_s.should eq "source.cr:5:3"
issue.message.should eq "While statement using true literal as condition"
end
end

View file

@ -21,6 +21,7 @@ module Ameba::AST
delegate to_s, to: @node
delegate location, to: @node
delegate end_location, to: @node
def_equals_and_hash node, location

View file

@ -25,6 +25,7 @@ module Ameba::AST
delegate to_s, to: @node
delegate location, to: @node
delegate end_location, to: @node
# Creates a new branchable
#

View file

@ -26,6 +26,7 @@ module Ameba::AST
delegate to_s, to: @node
delegate location, to: @node
delegate end_location, to: @node
# Creates a new flow expression.
#

View file

@ -21,6 +21,7 @@ module Ameba::AST
delegate to_s, to: node
delegate location, to: node
delegate end_location, to: node
def_equals_and_hash node, location
@ -44,7 +45,7 @@ module Ameba::AST
end
def add_argument(node)
add_variable Crystal::Var.new(node.name).at(node.location)
add_variable Crystal::Var.new(node.name).at(node)
arguments << Argument.new(node, variables.last)
end

View file

@ -20,6 +20,7 @@ module Ameba::AST
getter variable : Variable
delegate location, to: @node
delegate end_location, to: @node
delegate to_s, to: @node
# Creates a new argument.

View file

@ -17,6 +17,8 @@ module Ameba::AST
getter branch : Branch?
delegate to_s, to: @node
delegate location, to: @node
delegate end_location, to: @node
delegate scope, to: @variable
# Creates a new assignment.
@ -58,18 +60,18 @@ module Ameba::AST
!branch.nil?
end
# Returns the location of the current variable in the assignment.
def location
# Returns the target node of the variable in this assignment.
def target_node
case assign = node
when Crystal::Assign then assign.target.location
when Crystal::OpAssign then assign.target.location
when Crystal::UninitializedVar then assign.var.location
when Crystal::Assign then assign.target
when Crystal::OpAssign then assign.target
when Crystal::UninitializedVar then assign.var
when Crystal::MultiAssign
assign.targets.find do |target|
assign.targets.find(node) do |target|
target.is_a?(Crystal::Var) && target.name == variable.name
end.try &.location
end
else
node.location
node
end
end
end

View file

@ -18,6 +18,7 @@ module Ameba::AST
getter assign_before_reference : Crystal::ASTNode?
delegate location, to: @node
delegate end_location, to: @node
delegate name, to: @node
delegate to_s, to: @node

View file

@ -11,12 +11,12 @@ module Ameba
end
# Adds a new issue for AST *node*.
def add_issue(rule, node, message, **args)
def add_issue(rule, node : Crystal::ASTNode, message, **args)
add_issue rule, node.location, node.end_location, message, **args
end
# Adds a new issue for Crystal *token*.
def add_issue(rule, token, message, **args)
def add_issue(rule, token : Crystal::Token, message, **args)
add_issue rule, token.location, nil, message, **args
end

View file

@ -55,7 +55,7 @@ module Ameba::Rule::Lint
scope.arguments.each do |arg|
next if arg.ignored?
if scope.outer_scope.try &.find_variable(arg.name)
issue_for arg, MSG % arg.name
issue_for arg.node, MSG % arg.name
end
end
end

View file

@ -58,7 +58,7 @@ module Ameba::Rule::Lint
next if argument.ignored? || scope.references?(argument.variable)
name_suggestion = scope.node.is_a?(Crystal::Block) ? '_' : "_#{argument.name}"
issue_for argument, MSG % {argument.name, name_suggestion}
issue_for argument.node, MSG % {argument.name, name_suggestion}
end
end
end

View file

@ -43,7 +43,7 @@ module Ameba::Rule::Lint
var.assignments.each do |assign|
next if assign.referenced?
issue_for assign, MSG % var.name
issue_for assign.target_node, MSG % var.name
end
end
end