2023-11-12 01:50:17 +00:00
|
|
|
require "./ast/util"
|
|
|
|
|
2018-06-10 21:15:12 +00:00
|
|
|
module Ameba
|
|
|
|
# Represents a module used to report issues.
|
|
|
|
module Reportable
|
2023-11-12 01:50:17 +00:00
|
|
|
include AST::Util
|
|
|
|
|
2018-06-10 21:15:12 +00:00
|
|
|
# List of reported issues.
|
|
|
|
getter issues = [] of Issue
|
|
|
|
|
|
|
|
# Adds a new issue to the list of issues.
|
2022-08-01 21:50:30 +00:00
|
|
|
def add_issue(rule,
|
|
|
|
location : Crystal::Location?,
|
|
|
|
end_location : Crystal::Location?,
|
|
|
|
message : String,
|
|
|
|
status : Issue::Status? = nil,
|
|
|
|
block : (Source::Corrector ->)? = nil) : Issue
|
2021-01-26 06:38:19 +00:00
|
|
|
status ||=
|
|
|
|
Issue::Status::Disabled if location_disabled?(location, rule)
|
|
|
|
|
2021-10-25 22:09:39 +00:00
|
|
|
Issue.new(code, rule, location, end_location, message, status, block).tap do |issue|
|
2021-01-26 06:38:19 +00:00
|
|
|
issues << issue
|
|
|
|
end
|
2018-06-10 21:15:12 +00:00
|
|
|
end
|
|
|
|
|
2021-10-24 18:56:01 +00:00
|
|
|
# :ditto:
|
2022-08-01 21:50:30 +00:00
|
|
|
def add_issue(rule,
|
2022-12-08 12:59:03 +00:00
|
|
|
location : Crystal::Location?,
|
|
|
|
end_location : Crystal::Location?,
|
2022-08-01 21:50:30 +00:00
|
|
|
message : String,
|
|
|
|
status : Issue::Status? = nil,
|
|
|
|
&block : Source::Corrector ->) : Issue
|
2021-10-24 18:56:01 +00:00
|
|
|
add_issue rule, location, end_location, message, status, block
|
|
|
|
end
|
|
|
|
|
2021-01-26 06:38:19 +00:00
|
|
|
# Adds a new issue for Crystal AST *node*.
|
2023-11-12 01:50:17 +00:00
|
|
|
def add_issue(rule, node : Crystal::ASTNode, message, status : Issue::Status? = nil, block : (Source::Corrector ->)? = nil, *, prefer_name_location = false) : Issue
|
|
|
|
location = name_location(node) if prefer_name_location
|
|
|
|
location ||= node.location
|
|
|
|
|
|
|
|
end_location = name_end_location(node) if prefer_name_location
|
|
|
|
end_location ||= node.end_location
|
|
|
|
|
|
|
|
add_issue rule, location, end_location, message, status, block
|
2021-10-24 18:56:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# :ditto:
|
2023-11-12 01:50:17 +00:00
|
|
|
def add_issue(rule, node : Crystal::ASTNode, message, status : Issue::Status? = nil, *, prefer_name_location = false, &block : Source::Corrector ->) : Issue
|
|
|
|
add_issue rule, node, message, status, block, prefer_name_location: prefer_name_location
|
2018-06-10 21:15:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Adds a new issue for Crystal *token*.
|
2021-10-24 18:56:01 +00:00
|
|
|
def add_issue(rule, token : Crystal::Token, message, status : Issue::Status? = nil, block : (Source::Corrector ->)? = nil) : Issue
|
|
|
|
add_issue rule, token.location, nil, message, status, block
|
|
|
|
end
|
|
|
|
|
|
|
|
# :ditto:
|
|
|
|
def add_issue(rule, token : Crystal::Token, message, status : Issue::Status? = nil, &block : Source::Corrector ->) : Issue
|
|
|
|
add_issue rule, token, message, status, block
|
2018-06-10 21:15:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Adds a new issue for *location* defined by line and column numbers.
|
2021-10-24 18:56:01 +00:00
|
|
|
def add_issue(rule, location : {Int32, Int32}, message, status : Issue::Status? = nil, block : (Source::Corrector ->)? = nil) : Issue
|
2021-01-26 06:38:19 +00:00
|
|
|
location =
|
|
|
|
Crystal::Location.new(path, *location)
|
|
|
|
|
2021-10-24 18:56:01 +00:00
|
|
|
add_issue rule, location, nil, message, status, block
|
|
|
|
end
|
|
|
|
|
|
|
|
# :ditto:
|
|
|
|
def add_issue(rule, location : {Int32, Int32}, message, status : Issue::Status? = nil, &block : Source::Corrector ->) : Issue
|
|
|
|
add_issue rule, location, message, status, block
|
2018-06-10 21:15:12 +00:00
|
|
|
end
|
|
|
|
|
2018-11-24 22:54:25 +00:00
|
|
|
# Adds a new issue for *location* and *end_location* defined by line and column numbers.
|
2021-10-24 18:56:01 +00:00
|
|
|
def add_issue(rule,
|
|
|
|
location : {Int32, Int32},
|
|
|
|
end_location : {Int32, Int32},
|
|
|
|
message,
|
|
|
|
status : Issue::Status? = nil,
|
|
|
|
block : (Source::Corrector ->)? = nil) : Issue
|
2021-01-26 06:38:19 +00:00
|
|
|
location =
|
|
|
|
Crystal::Location.new(path, *location)
|
|
|
|
end_location =
|
|
|
|
Crystal::Location.new(path, *end_location)
|
|
|
|
|
2021-10-24 18:56:01 +00:00
|
|
|
add_issue rule, location, end_location, message, status, block
|
|
|
|
end
|
|
|
|
|
|
|
|
# :ditto:
|
|
|
|
def add_issue(rule,
|
|
|
|
location : {Int32, Int32},
|
|
|
|
end_location : {Int32, Int32},
|
|
|
|
message,
|
|
|
|
status : Issue::Status? = nil,
|
|
|
|
&block : Source::Corrector ->) : Issue
|
|
|
|
add_issue rule, location, end_location, message, status, block
|
2018-11-24 22:54:25 +00:00
|
|
|
end
|
|
|
|
|
2021-01-26 06:38:19 +00:00
|
|
|
# Returns `true` if the list of not disabled issues is empty, `false` otherwise.
|
2018-06-10 21:15:12 +00:00
|
|
|
def valid?
|
2021-01-26 06:38:19 +00:00
|
|
|
issues.none?(&.enabled?)
|
2018-06-10 21:15:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|