mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Add documentation
This commit is contained in:
parent
437584f9db
commit
749c53527e
6 changed files with 21 additions and 1 deletions
|
@ -54,6 +54,7 @@ class Ameba::Config
|
||||||
# ```
|
# ```
|
||||||
property excluded : Array(String)
|
property excluded : Array(String)
|
||||||
|
|
||||||
|
# Returns true if correctable issues should be autocorrected.
|
||||||
property? autocorrect = false
|
property? autocorrect = false
|
||||||
|
|
||||||
@rule_groups : Hash(String, Array(Rule::Base))
|
@rule_groups : Hash(String, Array(Rule::Base))
|
||||||
|
|
|
@ -50,6 +50,7 @@ module Ameba
|
||||||
# Checks for unneeded disable directives. Always inspects a source last
|
# Checks for unneeded disable directives. Always inspects a source last
|
||||||
@unneeded_disable_directive_rule : Rule::Base?
|
@unneeded_disable_directive_rule : Rule::Base?
|
||||||
|
|
||||||
|
# Returns true if correctable issues should be autocorrected.
|
||||||
private getter? autocorrect : Bool
|
private getter? autocorrect : Bool
|
||||||
|
|
||||||
# Instantiates a runner using a `config`.
|
# Instantiates a runner using a `config`.
|
||||||
|
|
|
@ -25,6 +25,8 @@ module Ameba
|
||||||
def initialize(@code, @path = "")
|
def initialize(@code, @path = "")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Corrects any correctable issues and updates `code`.
|
||||||
|
# Returns `false` if no issues were corrected.
|
||||||
def correct
|
def correct
|
||||||
corrector = Corrector.new(code)
|
corrector = Corrector.new(code)
|
||||||
issues.each(&.correct(corrector))
|
issues.each(&.correct(corrector))
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
require "./rewriter"
|
require "./rewriter"
|
||||||
|
|
||||||
class Ameba::Source
|
class Ameba::Source
|
||||||
|
# This class takes source code and rewrites it based
|
||||||
|
# on the different correction actions supplied.
|
||||||
class Corrector
|
class Corrector
|
||||||
@line_sizes : Array(Int32)
|
@line_sizes : Array(Int32)
|
||||||
|
|
||||||
|
@ -9,30 +11,37 @@ class Ameba::Source
|
||||||
@line_sizes = code.lines(chomp: false).map(&.size)
|
@line_sizes = code.lines(chomp: false).map(&.size)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Replaces the code of the given range with *content*.
|
||||||
def replace(location, end_location, content)
|
def replace(location, end_location, content)
|
||||||
@rewriter.replace(loc_to_pos(location), loc_to_pos(end_location) + 1, content)
|
@rewriter.replace(loc_to_pos(location), loc_to_pos(end_location) + 1, content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Inserts the given strings before and after the given range.
|
||||||
def wrap(location, end_location, insert_before, insert_after)
|
def wrap(location, end_location, insert_before, insert_after)
|
||||||
@rewriter.wrap(loc_to_pos(location), loc_to_pos(end_location) + 1, insert_before, insert_after)
|
@rewriter.wrap(loc_to_pos(location), loc_to_pos(end_location) + 1, insert_before, insert_after)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for `replace(location, end_location, "")`
|
||||||
def remove(location, end_location)
|
def remove(location, end_location)
|
||||||
@rewriter.remove(loc_to_pos(location), loc_to_pos(end_location) + 1)
|
@rewriter.remove(loc_to_pos(location), loc_to_pos(end_location) + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for `wrap(location, end_location, content, nil)`
|
||||||
def insert_before(location, end_location, content)
|
def insert_before(location, end_location, content)
|
||||||
@rewriter.insert_before(loc_to_pos(location), loc_to_pos(end_location) + 1, content)
|
@rewriter.insert_before(loc_to_pos(location), loc_to_pos(end_location) + 1, content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for `wrap(location, end_location, nil, content)`
|
||||||
def insert_after(location, end_location, content)
|
def insert_after(location, end_location, content)
|
||||||
@rewriter.insert_after(loc_to_pos(location), loc_to_pos(end_location) + 1, content)
|
@rewriter.insert_after(loc_to_pos(location), loc_to_pos(end_location) + 1, content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for `insert_before(location, location, content)`
|
||||||
def insert_before(location, content)
|
def insert_before(location, content)
|
||||||
@rewriter.insert_before(loc_to_pos(location), content)
|
@rewriter.insert_before(loc_to_pos(location), content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for `insert_after(location, location, content)`
|
||||||
def insert_after(location, content)
|
def insert_after(location, content)
|
||||||
@rewriter.insert_after(loc_to_pos(location) + 1, content)
|
@rewriter.insert_after(loc_to_pos(location) + 1, content)
|
||||||
end
|
end
|
||||||
|
@ -46,22 +55,27 @@ class Ameba::Source
|
||||||
@line_sizes[0...line - 1].sum + (column - 1)
|
@line_sizes[0...line - 1].sum + (column - 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Replaces the code of the given node with *content*.
|
||||||
def replace(node : Crystal::ASTNode, content)
|
def replace(node : Crystal::ASTNode, content)
|
||||||
replace(location(node), end_location(node), content)
|
replace(location(node), end_location(node), content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Inserts the given strings before and after the given node.
|
||||||
def wrap(node : Crystal::ASTNode, insert_before, insert_after)
|
def wrap(node : Crystal::ASTNode, insert_before, insert_after)
|
||||||
wrap(location(node), end_location(node), insert_before, insert_after)
|
wrap(location(node), end_location(node), insert_before, insert_after)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for `replace(node, "")`
|
||||||
def remove(node : Crystal::ASTNode)
|
def remove(node : Crystal::ASTNode)
|
||||||
remove(location(node), end_location(node))
|
remove(location(node), end_location(node))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for `wrap(node, content, nil)`
|
||||||
def insert_before(node : Crystal::ASTNode, content)
|
def insert_before(node : Crystal::ASTNode, content)
|
||||||
insert_before(location(node), content)
|
insert_before(location(node), content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Shortcut for `wrap(node, nil, content)`
|
||||||
def insert_after(node : Crystal::ASTNode, content)
|
def insert_after(node : Crystal::ASTNode, content)
|
||||||
insert_after(end_location(node), content)
|
insert_after(end_location(node), content)
|
||||||
end
|
end
|
||||||
|
@ -74,6 +88,7 @@ class Ameba::Source
|
||||||
node.end_location || raise "Missing end location"
|
node.end_location || raise "Missing end location"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Applies all scheduled changes and returns modified source as a new string.
|
||||||
def process
|
def process
|
||||||
@rewriter.process
|
@rewriter.process
|
||||||
end
|
end
|
||||||
|
|
|
@ -70,7 +70,7 @@ class Ameba::Source
|
||||||
@action_root.empty?
|
@action_root.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Replaces the code of the given range with `content`.
|
# Replaces the code of the given range with *content*.
|
||||||
def replace(begin_pos, end_pos, content)
|
def replace(begin_pos, end_pos, content)
|
||||||
combine(begin_pos, end_pos, replacement: content.to_s)
|
combine(begin_pos, end_pos, replacement: content.to_s)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
class Ameba::Source::Rewriter
|
class Ameba::Source::Rewriter
|
||||||
|
# :nodoc:
|
||||||
# Actions are arranged in a tree and get combined so that:
|
# Actions are arranged in a tree and get combined so that:
|
||||||
# - children are strictly contained by their parent
|
# - children are strictly contained by their parent
|
||||||
# - siblings all disjoint from one another and ordered
|
# - siblings all disjoint from one another and ordered
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue