gob-lang 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +201 -0
- data/README.md +73 -0
- data/gob-lang.gemspec +2 -1
- data/lib/gob/cli/base.rb +2 -2
- data/lib/gob/cli/commands/fmt.rb +2 -2
- data/lib/gob/cli/completion.rb +104 -0
- data/lib/gob/cli/errors.rb +0 -2
- data/lib/gob/cli.rb +21 -3
- data/lib/gob/go/printer.rb +215 -72
- data/lib/gob/transpilation/context.rb +102 -0
- data/lib/gob/transpilation/runner.rb +1500 -0
- data/lib/gob/validation/rules/array_length.rb +45 -0
- data/lib/gob/validation/rules/attribute_type.rb +30 -0
- data/lib/gob/validation/rules/block_delimiters.rb +43 -0
- data/lib/gob/validation/rules/collection_literal.rb +107 -0
- data/lib/gob/validation/rules/config.yml +58 -4
- data/lib/gob/validation/rules/cop.rb +72 -0
- data/lib/gob/validation/rules/defer_statement.rb +33 -0
- data/lib/gob/validation/rules/error_check.rb +33 -0
- data/lib/gob/validation/rules/go_block.rb +58 -0
- data/lib/gob/validation/rules/indentation.rb +118 -0
- data/lib/gob/validation/rules/invoked_lambda.rb +48 -0
- data/lib/gob/validation/rules/lambda_signature.rb +58 -0
- data/lib/gob/validation/rules/multiline.rb +99 -0
- data/lib/gob/validation/rules/operator_assignment.rb +44 -0
- data/lib/gob/validation/rules/pointer_type.rb +36 -0
- data/lib/gob/validation/rules/quote_marks.rb +33 -0
- data/lib/gob/validation/rules/redundant_lambda_parentheses.rb +47 -0
- data/lib/gob/validation/rules/select_block.rb +85 -0
- data/lib/gob/validation/rules/signature_comment.rb +43 -0
- data/lib/gob/validation/rules/this_receiver.rb +38 -0
- data/lib/gob/validation/rules/trailing_whitespace.rb +38 -0
- data/lib/gob/validation/rules/variable_declaration.rb +32 -0
- data/lib/gob/validation/rules/word_array.rb +33 -0
- data/lib/gob/validation/rules/zero_based_range_loop.rb +63 -0
- data/lib/gob/validation/rules.rb +85 -11
- data/lib/gob/version.rb +1 -1
- data/lib/gob.rb +10 -12
- metadata +41 -9
- data/lib/gob/go/dsl.rb +0 -196
- data/lib/gob/matcher.rb +0 -34
- data/lib/gob/transpilation/rules/core.rb +0 -1184
- data/lib/gob/transpiler.rb +0 -815
- data/lib/gob/validation/rules/class_rules.rb +0 -78
- data/lib/gob/validation/rules/function_rules.rb +0 -33
- data/lib/gob/validation/rules/module_rules.rb +0 -78
- data/lib/gob/validation/rules/program_rules.rb +0 -72
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Requires Go types on lambda parameters and results.
|
|
7
|
+
class LambdaSignature < Cop
|
|
8
|
+
MSG = "Add a signature comment to the lambda, for example `|value| # (int) -> bool`"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# lambda { |value| value > 0 }
|
|
13
|
+
# lambda { |value| # (int) -> bool
|
|
14
|
+
# value > 0
|
|
15
|
+
# }
|
|
16
|
+
# ---
|
|
17
|
+
def rule(node)
|
|
18
|
+
case node
|
|
19
|
+
in SyntaxTree::MethodAddBlock[
|
|
20
|
+
call: SyntaxTree::CallNode[
|
|
21
|
+
receiver: nil,
|
|
22
|
+
operator: nil,
|
|
23
|
+
message: SyntaxTree::Ident[value: "lambda"]
|
|
24
|
+
],
|
|
25
|
+
block: SyntaxTree::BlockNode[
|
|
26
|
+
block_var: block_var,
|
|
27
|
+
bodystmt: bodystmt
|
|
28
|
+
]
|
|
29
|
+
]
|
|
30
|
+
requireds = block_var&.params&.requireds || []
|
|
31
|
+
comments = block_var&.comments || []
|
|
32
|
+
signature = comments.filter_map { |comment| comment.value if comment.is_a?(SyntaxTree::Comment) }.first
|
|
33
|
+
missing_params = !requireds.empty? && signature.nil?
|
|
34
|
+
missing_result = returns_value?(bodystmt) && !signature&.include?("->")
|
|
35
|
+
|
|
36
|
+
add_offense(range(node.location), message: MSG) if missing_params || missing_result
|
|
37
|
+
|
|
38
|
+
else
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def returns_value?(node)
|
|
44
|
+
case node
|
|
45
|
+
in SyntaxTree::ReturnNode[arguments: SyntaxTree::Args[parts: parts]] unless parts.empty?
|
|
46
|
+
true
|
|
47
|
+
in SyntaxTree::Node
|
|
48
|
+
node.child_nodes.any? { |child| returns_value?(child) }
|
|
49
|
+
in Array
|
|
50
|
+
node.any? { |child| returns_value?(child) }
|
|
51
|
+
else
|
|
52
|
+
false
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
class Multiline < Cop
|
|
7
|
+
MSG = "Put each statement of a `%<name>s` body on its own line"
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# xs.each do |k| p(k) end
|
|
12
|
+
# xs.each do |k|
|
|
13
|
+
# p(k)
|
|
14
|
+
# end
|
|
15
|
+
# ---
|
|
16
|
+
def rule(node)
|
|
17
|
+
case node
|
|
18
|
+
in SyntaxTree::Program | SyntaxTree::BodyStmt
|
|
19
|
+
nil
|
|
20
|
+
|
|
21
|
+
in { bodystmt: SyntaxTree::BodyStmt[ statements: SyntaxTree::Statements[ body: body ] ] }
|
|
22
|
+
crowded(node, body)
|
|
23
|
+
|
|
24
|
+
in { statements: SyntaxTree::Statements[ body: body ] }
|
|
25
|
+
crowded(node, body)
|
|
26
|
+
|
|
27
|
+
else
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def crowded(node, body)
|
|
33
|
+
statements = body.reject { |statement| statement.is_a?(SyntaxTree::VoidStmt) }
|
|
34
|
+
return if statements.empty?
|
|
35
|
+
return unless statements.any? { |statement| statement.location.start_line == node.location.start_line }
|
|
36
|
+
|
|
37
|
+
add_offense(range(node.location), message: format(MSG, name: keyword(node))) do |corrector|
|
|
38
|
+
statements.each do |statement|
|
|
39
|
+
next unless statement.location.start_line == node.location.start_line
|
|
40
|
+
|
|
41
|
+
corrector.replace(
|
|
42
|
+
before(statement.location),
|
|
43
|
+
"\n#{" " * (indentation(node.location) + Indentation::WIDTH)}"
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
if closes_own_line?(node, statements.last)
|
|
48
|
+
corrector.replace(
|
|
49
|
+
gap(statements.last.location),
|
|
50
|
+
"\n#{" " * indentation(node.location)}"
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def indentation(location)
|
|
57
|
+
raw = processed_source.raw_source
|
|
58
|
+
opening = raw.rindex("\n", location.start_char - 1).then { |index| index ? index + 1 : 0 }
|
|
59
|
+
|
|
60
|
+
raw[opening..].match(/\A[ \t]*/)[0].length
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The run of spaces a statement was crowded onto its opener's line by.
|
|
64
|
+
def before(location)
|
|
65
|
+
raw = processed_source.raw_source
|
|
66
|
+
opening = location.start_char
|
|
67
|
+
opening -= 1 while raw[opening - 1] == " " || raw[opening - 1] == "\t"
|
|
68
|
+
|
|
69
|
+
Parser::Source::Range.new(processed_source.buffer, opening, location.start_char)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The run of spaces between a body and the closer that follows it.
|
|
73
|
+
def gap(location)
|
|
74
|
+
raw = processed_source.raw_source
|
|
75
|
+
closing = location.end_char
|
|
76
|
+
closing += 1 while raw[closing] == " " || raw[closing] == "\t"
|
|
77
|
+
|
|
78
|
+
Parser::Source::Range.new(processed_source.buffer, location.end_char, closing)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# A clause has no closer of its own; everything else ends with `end`,
|
|
82
|
+
# which belongs on its own line.
|
|
83
|
+
def closes_own_line?(node, last)
|
|
84
|
+
case node
|
|
85
|
+
in SyntaxTree::When | SyntaxTree::Else | SyntaxTree::In
|
|
86
|
+
false
|
|
87
|
+
|
|
88
|
+
else
|
|
89
|
+
node.location.end_line == last.location.end_line
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def keyword(node)
|
|
94
|
+
node.class.name.split("::").last.sub(/Node\z/, "").downcase
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Expands Ruby's compound assignment into the binary assignment Gob reads.
|
|
7
|
+
class OperatorAssignment < Cop
|
|
8
|
+
MSG = "Expand the compound assignment into an explicit binary assignment"
|
|
9
|
+
|
|
10
|
+
OPERATORS = {
|
|
11
|
+
"+=" => "+",
|
|
12
|
+
"-=" => "-",
|
|
13
|
+
"*=" => "*",
|
|
14
|
+
"/=" => "/",
|
|
15
|
+
"%=" => "%"
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
# total += value
|
|
21
|
+
# total = total + value
|
|
22
|
+
# ---
|
|
23
|
+
def rule(node)
|
|
24
|
+
case node
|
|
25
|
+
in SyntaxTree::OpAssign[
|
|
26
|
+
target: target,
|
|
27
|
+
operator: SyntaxTree::Op[value: operator],
|
|
28
|
+
value: value
|
|
29
|
+
] if OPERATORS.key?(operator)
|
|
30
|
+
target_source = source(target.location)
|
|
31
|
+
replacement = "#{target_source} = #{target_source} #{OPERATORS.fetch(operator)} #{source(value.location)}"
|
|
32
|
+
|
|
33
|
+
add_offense(range(node.location), message: MSG) do |corrector|
|
|
34
|
+
corrector.replace(range(node.location), replacement)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
else
|
|
38
|
+
nil
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Uses Gob's class-variable spelling for Go pointer types.
|
|
7
|
+
class PointerType < Cop
|
|
8
|
+
MSG = "Write `@@Type` for a pointer type, not `ptr(Type)`"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# ptr(NotFoundError)
|
|
13
|
+
# @@NotFoundError
|
|
14
|
+
# ---
|
|
15
|
+
def rule(node)
|
|
16
|
+
case node
|
|
17
|
+
in SyntaxTree::CallNode[
|
|
18
|
+
receiver: nil,
|
|
19
|
+
operator: nil,
|
|
20
|
+
message: SyntaxTree::Ident[value: "ptr"],
|
|
21
|
+
arguments: SyntaxTree::ArgParen[
|
|
22
|
+
arguments: SyntaxTree::Args[parts: [type]]
|
|
23
|
+
]
|
|
24
|
+
]
|
|
25
|
+
add_offense(range(node.location), message: MSG) do |corrector|
|
|
26
|
+
corrector.replace(range(node.location), "@@#{source(type.location)}")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
else
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
class QuoteMarks < Cop
|
|
7
|
+
MSG = "Single quotes are a rune; use double quotes for text"
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# 'hello'
|
|
12
|
+
# "hello"
|
|
13
|
+
# ---
|
|
14
|
+
def rule(node)
|
|
15
|
+
case node
|
|
16
|
+
in SyntaxTree::StringLiteral[ parts: [SyntaxTree::TStringContent[value: text]] ] if rune?(node, text)
|
|
17
|
+
add_offense(range(node.location), message: MSG) do |corrector|
|
|
18
|
+
corrector.replace(range(node.location), text.inspect)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
else
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A rune is one character; anything longer is text written the wrong way.
|
|
27
|
+
def rune?(node, text)
|
|
28
|
+
source(node.location).start_with?("'") && text.length != 1
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Removes parentheses that hide a lambda from the transpiler.
|
|
7
|
+
class RedundantLambdaParentheses < Cop
|
|
8
|
+
MSG = "Remove parentheses around the lambda"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# return(lambda do ... end)
|
|
13
|
+
# return lambda do ... end
|
|
14
|
+
# ---
|
|
15
|
+
def rule(node)
|
|
16
|
+
case node
|
|
17
|
+
in SyntaxTree::Paren[
|
|
18
|
+
contents: SyntaxTree::Statements[body: [expression]]
|
|
19
|
+
] if lambda?(expression)
|
|
20
|
+
add_offense(range(node.location), message: MSG) do |corrector|
|
|
21
|
+
separator = processed_source.raw_source[node.location.start_char - 1]&.match?(/[[:alnum:]_]/) ? " " : ""
|
|
22
|
+
corrector.replace(range(node.location), "#{separator}#{source(expression.location)}")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
else
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def lambda?(node)
|
|
31
|
+
case node
|
|
32
|
+
in SyntaxTree::MethodAddBlock[
|
|
33
|
+
call: SyntaxTree::CallNode[
|
|
34
|
+
receiver: nil,
|
|
35
|
+
operator: nil,
|
|
36
|
+
message: SyntaxTree::Ident[value: "lambda"]
|
|
37
|
+
]
|
|
38
|
+
]
|
|
39
|
+
true
|
|
40
|
+
else
|
|
41
|
+
false
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Uses Gob's case/when spelling for a select that receives from channels.
|
|
7
|
+
class SelectBlock < Cop
|
|
8
|
+
MSG = "Write channel receives as `case` / `when` clauses"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# select do |c|
|
|
13
|
+
# c << ready() { return value }
|
|
14
|
+
# end
|
|
15
|
+
# ---
|
|
16
|
+
# case
|
|
17
|
+
# when ~ready()
|
|
18
|
+
# return value
|
|
19
|
+
# end
|
|
20
|
+
# ---
|
|
21
|
+
def rule(node)
|
|
22
|
+
case node
|
|
23
|
+
in SyntaxTree::MethodAddBlock[
|
|
24
|
+
call: SyntaxTree::CallNode[
|
|
25
|
+
receiver: nil,
|
|
26
|
+
operator: nil,
|
|
27
|
+
message: SyntaxTree::Ident[value: "select"]
|
|
28
|
+
],
|
|
29
|
+
block: SyntaxTree::BlockNode[
|
|
30
|
+
block_var: SyntaxTree::BlockVar[
|
|
31
|
+
params: SyntaxTree::Params[requireds: [channel]]
|
|
32
|
+
],
|
|
33
|
+
bodystmt: bodystmt
|
|
34
|
+
]
|
|
35
|
+
] if !body_statements(bodystmt).empty? && body_statements(bodystmt).all? { |statement| send_with_block?(statement, channel) }
|
|
36
|
+
add_offense(range(node.location), message: MSG) do |corrector|
|
|
37
|
+
corrector.replace(range(node.location), replacement(node, body_statements(bodystmt)))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
else
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def send_with_block?(node, channel)
|
|
46
|
+
case node
|
|
47
|
+
in SyntaxTree::Binary[
|
|
48
|
+
left: SyntaxTree::VarRef[value: SyntaxTree::Ident[value: name]],
|
|
49
|
+
operator: :<<,
|
|
50
|
+
right: SyntaxTree::MethodAddBlock[
|
|
51
|
+
call: call,
|
|
52
|
+
block: SyntaxTree::BlockNode[bodystmt: bodystmt]
|
|
53
|
+
]
|
|
54
|
+
] if name == channel.value && !body_statements(bodystmt).empty?
|
|
55
|
+
true
|
|
56
|
+
else
|
|
57
|
+
false
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def replacement(node, body)
|
|
62
|
+
indent = indentation(node.location)
|
|
63
|
+
clauses = body.map do |statement|
|
|
64
|
+
call = statement.right.call
|
|
65
|
+
statements = body_statements(statement.right.block.bodystmt)
|
|
66
|
+
clause_body = statements.map { |item| source(item.location) }.join("\n")
|
|
67
|
+
"#{" " * indent}when ~#{source(call.location)}\n#{indent_source(clause_body, indent + 2)}"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
"#{" " * indent}case\n#{clauses.join("\n")}\n#{" " * indent}end"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def indent_source(source_text, depth)
|
|
74
|
+
source_text.lines.map { |line| "#{" " * depth}#{line.lstrip}" }.join.rstrip
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def indentation(location)
|
|
78
|
+
raw = processed_source.raw_source
|
|
79
|
+
opening = raw.rindex("\n", location.start_char - 1).then { |index| index ? index + 1 : 0 }
|
|
80
|
+
raw[opening..].match(/\A[ \t]*/)[0].length
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
class SignatureComment < Cop
|
|
7
|
+
MSG = "Write a signature comment inline on the `def`, not on its own line"
|
|
8
|
+
|
|
9
|
+
SIGNATURE = /\A#\s*(\(|->)/
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# # (string) -> error
|
|
14
|
+
# def work(ctx)
|
|
15
|
+
# ---
|
|
16
|
+
# def work(ctx) # (string) -> error
|
|
17
|
+
# ---
|
|
18
|
+
def rule(node)
|
|
19
|
+
case node
|
|
20
|
+
in SyntaxTree::Statements[ body: body ]
|
|
21
|
+
body.each_cons(2) { |comment, definition| misplaced(comment, definition) }
|
|
22
|
+
|
|
23
|
+
else
|
|
24
|
+
nil
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def misplaced(comment, definition)
|
|
29
|
+
case [comment, definition]
|
|
30
|
+
in [SyntaxTree::Comment[value: SIGNATURE => signature], SyntaxTree::DefNode]
|
|
31
|
+
add_offense(range(comment.location), message: MSG) do |corrector|
|
|
32
|
+
corrector.remove(line(comment.location))
|
|
33
|
+
corrector.insert_after(end_of_line(definition.location), " #{signature}")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
else
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Uses the receiver name emitted by Gob's method definitions.
|
|
7
|
+
class ThisReceiver < Cop
|
|
8
|
+
MSG = "Write `%<replacement>s`, not `%<original>s`"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# this.Value
|
|
13
|
+
# self.Value
|
|
14
|
+
# @this = value
|
|
15
|
+
# @self = value
|
|
16
|
+
# ---
|
|
17
|
+
def rule(node)
|
|
18
|
+
case node
|
|
19
|
+
in SyntaxTree::VCall[value: SyntaxTree::Ident[value: "this" => original]]
|
|
20
|
+
replace(node, original, "self")
|
|
21
|
+
|
|
22
|
+
in SyntaxTree::IVar[value: "@this"]
|
|
23
|
+
replace(node, "@this", "@self")
|
|
24
|
+
|
|
25
|
+
else
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def replace(node, original, replacement)
|
|
31
|
+
add_offense(range(node.location), message: format(MSG, original: original, replacement: replacement)) do |corrector|
|
|
32
|
+
corrector.replace(range(node.location), replacement)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Removes spaces and tabs that do not contribute to the source.
|
|
7
|
+
class TrailingWhitespace < Cop
|
|
8
|
+
MSG = "Remove trailing whitespace"
|
|
9
|
+
|
|
10
|
+
def on_new_investigation
|
|
11
|
+
source = processed_source.buffer.source
|
|
12
|
+
offset = 0
|
|
13
|
+
|
|
14
|
+
source.each_line do |line|
|
|
15
|
+
match = line.match(/[ \t]+(?=\r?\n|\z)/)
|
|
16
|
+
next unless match
|
|
17
|
+
|
|
18
|
+
add_offense(whitespace_range(offset, match), message: MSG) do |corrector|
|
|
19
|
+
corrector.remove(whitespace_range(offset, match))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
offset += line.length
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def whitespace_range(offset, match)
|
|
29
|
+
Parser::Source::Range.new(
|
|
30
|
+
processed_source.buffer,
|
|
31
|
+
offset + match.begin(0),
|
|
32
|
+
offset + match.end(0)
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Uses the declaration keyword understood by the transpiler.
|
|
7
|
+
class VariableDeclaration < Cop
|
|
8
|
+
MSG = "Write `var`, not `define_var`"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# define_var :name, Type
|
|
13
|
+
# var :name, Type
|
|
14
|
+
# ---
|
|
15
|
+
def rule(node)
|
|
16
|
+
case node
|
|
17
|
+
in SyntaxTree::Command[
|
|
18
|
+
message: SyntaxTree::Ident[value: "define_var"] => message,
|
|
19
|
+
arguments: SyntaxTree::Args[parts: [SyntaxTree::SymbolLiteral, *]]
|
|
20
|
+
]
|
|
21
|
+
add_offense(range(message.location), message: MSG) do |corrector|
|
|
22
|
+
corrector.replace(range(message.location), "var")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
else
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Uses an explicitly typed string array instead of Ruby's %w literal.
|
|
7
|
+
class WordArray < Cop
|
|
8
|
+
MSG = "Write `Array[string].new(...)`, not `%w[...]`"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# %w[one two three]
|
|
13
|
+
# Array[string].new("one", "two", "three")
|
|
14
|
+
# ---
|
|
15
|
+
def rule(node)
|
|
16
|
+
case node
|
|
17
|
+
in SyntaxTree::QWords[elements: elements]
|
|
18
|
+
values = elements.filter_map do |element|
|
|
19
|
+
source(element.location).inspect if element.is_a?(SyntaxTree::TStringContent)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
add_offense(range(node.location), message: MSG) do |corrector|
|
|
23
|
+
corrector.replace(range(node.location), "Array[string].new(#{values.join(", ")})")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
else
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Spells a zero-based counting loop with Integer#times.
|
|
7
|
+
class ZeroBasedRangeLoop < Cop
|
|
8
|
+
MSG = "Write `%<count>s.times` for a zero-based counting loop"
|
|
9
|
+
MSG_UNSUPPORTED = "Start a counting loop at zero with `%<count>s.times`"
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# (0...count).each do |i| ... end
|
|
14
|
+
# count.times do |i| ... end
|
|
15
|
+
# ---
|
|
16
|
+
def rule(node)
|
|
17
|
+
case node
|
|
18
|
+
in SyntaxTree::MethodAddBlock[
|
|
19
|
+
call: SyntaxTree::CallNode[
|
|
20
|
+
receiver: SyntaxTree::Paren[
|
|
21
|
+
contents: SyntaxTree::Statements[body: [range]]
|
|
22
|
+
],
|
|
23
|
+
operator: SyntaxTree::Period[value: "."],
|
|
24
|
+
message: SyntaxTree::Ident[value: "each"]
|
|
25
|
+
],
|
|
26
|
+
block: SyntaxTree::BlockNode[
|
|
27
|
+
block_var: SyntaxTree::BlockVar[
|
|
28
|
+
params: SyntaxTree::Params[requireds: [index]]
|
|
29
|
+
]
|
|
30
|
+
]
|
|
31
|
+
] if counting_range?(range)
|
|
32
|
+
count = source(range.right.location)
|
|
33
|
+
add_offense(range(node.location), message: format(MSG, count: count)) do |corrector|
|
|
34
|
+
corrector.replace(range(node.call.location), "#{count}.times")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
in SyntaxTree::MethodAddBlock[
|
|
38
|
+
call: SyntaxTree::CallNode[
|
|
39
|
+
receiver: SyntaxTree::Paren[
|
|
40
|
+
contents: SyntaxTree::Statements[body: [range]]
|
|
41
|
+
],
|
|
42
|
+
operator: SyntaxTree::Period[value: "."],
|
|
43
|
+
message: SyntaxTree::Ident[value: "each"]
|
|
44
|
+
]
|
|
45
|
+
] if exclusive_range?(range)
|
|
46
|
+
add_offense(range(range.location), message: MSG_UNSUPPORTED)
|
|
47
|
+
|
|
48
|
+
else
|
|
49
|
+
nil
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def counting_range?(range)
|
|
54
|
+
exclusive_range?(range) && range.left.is_a?(SyntaxTree::Int) && range.left.value == "0"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def exclusive_range?(range)
|
|
58
|
+
range.is_a?(SyntaxTree::RangeNode) && range.operator.value == "..."
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|