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,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Folds literal shifts in array sizes, where `<<` would mean a channel send.
|
|
7
|
+
class ArrayLength < Cop
|
|
8
|
+
MSG = "Use the evaluated array length `%<length>d`, not `%<source>s`"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# Array[byte, 1 << 20]
|
|
13
|
+
# Array[byte, 1_048_576]
|
|
14
|
+
# ---
|
|
15
|
+
def rule(node)
|
|
16
|
+
case node
|
|
17
|
+
in SyntaxTree::ARef[
|
|
18
|
+
collection: SyntaxTree::VarRef[value: SyntaxTree::Const[value: "Array"]],
|
|
19
|
+
index: SyntaxTree::Args[parts: [_, length]]
|
|
20
|
+
] if (evaluated = literal_shift(length))
|
|
21
|
+
add_offense(range(length.location), message: format(MSG, length: evaluated, source: source(length.location))) do |corrector|
|
|
22
|
+
corrector.replace(range(length.location), evaluated.to_s)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
else
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def literal_shift(node)
|
|
31
|
+
case node
|
|
32
|
+
in SyntaxTree::Binary[
|
|
33
|
+
left: SyntaxTree::Int[value: left],
|
|
34
|
+
operator: :<<,
|
|
35
|
+
right: SyntaxTree::Int[value: right]
|
|
36
|
+
]
|
|
37
|
+
left.to_i << right.to_i
|
|
38
|
+
else
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Requires a Go type for every generated struct field.
|
|
7
|
+
class AttributeType < Cop
|
|
8
|
+
MSG = "Write `attr_accessor :name, Type` so the field has a Go type"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# attr_accessor :Name
|
|
13
|
+
# attr_accessor :Name, string
|
|
14
|
+
# ---
|
|
15
|
+
def rule(node)
|
|
16
|
+
case node
|
|
17
|
+
in SyntaxTree::Command[
|
|
18
|
+
message: SyntaxTree::Ident[value: "attr_accessor"],
|
|
19
|
+
arguments: SyntaxTree::Args[parts: [SyntaxTree::SymbolLiteral]]
|
|
20
|
+
]
|
|
21
|
+
add_offense(range(node.location), message: MSG)
|
|
22
|
+
|
|
23
|
+
else
|
|
24
|
+
nil
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
class BlockDelimiters < Cop
|
|
7
|
+
MSG = "Write a `%<name>s` block as `do ... end`; `{ ... }` is not read as logic"
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# demos.each { |k| names = append(names, k) }
|
|
12
|
+
# demos.each do |k| names = append(names, k) end
|
|
13
|
+
# ---
|
|
14
|
+
def rule(node)
|
|
15
|
+
case node
|
|
16
|
+
in SyntaxTree::MethodAddBlock[
|
|
17
|
+
call: SyntaxTree::CallNode[
|
|
18
|
+
message: SyntaxTree::Ident[value: "each" | "times" | "loop" => name]
|
|
19
|
+
],
|
|
20
|
+
block: SyntaxTree::BlockNode[ opening: SyntaxTree::LBrace => opening ] => block
|
|
21
|
+
]
|
|
22
|
+
add_offense(range(opening.location), message: format(MSG, name: name)) do |corrector|
|
|
23
|
+
corrector.replace(range(opening.location), "do")
|
|
24
|
+
corrector.replace(closing(block), "end")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
else
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The `}` is the last character of the block.
|
|
33
|
+
def closing(block)
|
|
34
|
+
Parser::Source::Range.new(
|
|
35
|
+
processed_source.buffer,
|
|
36
|
+
block.location.end_char - 1,
|
|
37
|
+
block.location.end_char
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Gives collection literals an explicit Go element type.
|
|
7
|
+
class CollectionLiteral < Cop
|
|
8
|
+
ARRAY_MSG = "Write `Array[%<type>s].new(...)`, not an untyped array literal"
|
|
9
|
+
HASH_MSG = "Write `Hash[%<key>s, %<value>s].new(...)`, not an untyped hash literal"
|
|
10
|
+
ARRAY_UNKNOWN_MSG = "Write an explicitly typed `Array[Type].new(...)`"
|
|
11
|
+
HASH_UNKNOWN_MSG = "Write an explicitly typed `Hash[Key, Value].new(...)`"
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
# [1, 2, 3]
|
|
16
|
+
# Array[int].new(1, 2, 3)
|
|
17
|
+
# { "one" => 1 }
|
|
18
|
+
# Hash[string, int].new("one" => 1)
|
|
19
|
+
# ---
|
|
20
|
+
def rule(node)
|
|
21
|
+
return if @ancestors&.[](0...-1)&.any? do |ancestor|
|
|
22
|
+
ancestor.is_a?(SyntaxTree::ArrayLiteral) || ancestor.is_a?(SyntaxTree::HashLiteral)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
case node
|
|
26
|
+
in SyntaxTree::ArrayLiteral[contents: SyntaxTree::Args[parts: parts]]
|
|
27
|
+
literal(node, "Array", common_type(parts.map { |part| type_of(part) }), ARRAY_MSG, ARRAY_UNKNOWN_MSG)
|
|
28
|
+
|
|
29
|
+
in SyntaxTree::HashLiteral[assocs: assocs]
|
|
30
|
+
keys = assocs.map { |assoc| type_of(assoc.key) }
|
|
31
|
+
values = assocs.map { |assoc| type_of(assoc.value) }
|
|
32
|
+
key_type = common_type(keys)
|
|
33
|
+
value_type = common_type(values)
|
|
34
|
+
literal(node, "Hash", key_type && value_type && [key_type, value_type], HASH_MSG, HASH_UNKNOWN_MSG)
|
|
35
|
+
|
|
36
|
+
else
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def literal(node, constructor, type, message, unknown_message)
|
|
42
|
+
if type
|
|
43
|
+
replacement = "#{constructor}[#{Array(type).join(", ")}].new(#{contents(node)})"
|
|
44
|
+
add_offense(range(node.location), message: format(message, type: Array(type).join(", "), key: type[0], value: type[1])) do |corrector|
|
|
45
|
+
corrector.replace(range(node.location), replacement)
|
|
46
|
+
end
|
|
47
|
+
else
|
|
48
|
+
add_offense(range(node.location), message: unknown_message)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def contents(node)
|
|
53
|
+
source(node.location)[1...-1].strip
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def common_type(types)
|
|
57
|
+
return nil if types.empty? || types.any?(&:nil?)
|
|
58
|
+
|
|
59
|
+
types.uniq.length == 1 ? types.first : nil
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def type_of(node)
|
|
63
|
+
case node
|
|
64
|
+
in SyntaxTree::Int
|
|
65
|
+
"int"
|
|
66
|
+
in SyntaxTree::FloatLiteral
|
|
67
|
+
"float64"
|
|
68
|
+
in SyntaxTree::StringLiteral
|
|
69
|
+
"string"
|
|
70
|
+
in SyntaxTree::XStringLiteral
|
|
71
|
+
"string"
|
|
72
|
+
in SyntaxTree::VarRef[value: SyntaxTree::Kw[value: "true" | "false"]]
|
|
73
|
+
"bool"
|
|
74
|
+
in SyntaxTree::VarRef[value: SyntaxTree::Kw[value: "nil"]]
|
|
75
|
+
nil
|
|
76
|
+
in SyntaxTree::Unary[operator: "-", statement: SyntaxTree::Int]
|
|
77
|
+
"int"
|
|
78
|
+
in SyntaxTree::Unary[operator: "-", statement: SyntaxTree::FloatLiteral]
|
|
79
|
+
"float64"
|
|
80
|
+
in SyntaxTree::ArrayLiteral[contents: SyntaxTree::Args[parts: parts]]
|
|
81
|
+
element = common_type(parts.map { |part| type_of(part) })
|
|
82
|
+
element && "Array[#{element}]"
|
|
83
|
+
in SyntaxTree::HashLiteral[assocs: assocs]
|
|
84
|
+
key = common_type(assocs.map { |assoc| type_of(assoc.key) })
|
|
85
|
+
value = common_type(assocs.map { |assoc| type_of(assoc.value) })
|
|
86
|
+
key && value && "Hash[#{key}, #{value}]"
|
|
87
|
+
in SyntaxTree::CallNode[
|
|
88
|
+
receiver: receiver,
|
|
89
|
+
operator: SyntaxTree::Period[value: "."],
|
|
90
|
+
message: SyntaxTree::Ident[value: "new"]
|
|
91
|
+
]
|
|
92
|
+
source(receiver.location)
|
|
93
|
+
in SyntaxTree::CallNode[
|
|
94
|
+
receiver: nil,
|
|
95
|
+
operator: nil,
|
|
96
|
+
message: { value: name },
|
|
97
|
+
arguments: SyntaxTree::ArgParen[arguments: SyntaxTree::Args[parts: [_]]]
|
|
98
|
+
] if %w[int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 string bool byte rune].include?(name)
|
|
99
|
+
name
|
|
100
|
+
else
|
|
101
|
+
nil
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -3,14 +3,68 @@ AllCops:
|
|
|
3
3
|
TargetRubyVersion: 3.4
|
|
4
4
|
NewCops: disable
|
|
5
5
|
|
|
6
|
-
Gob/
|
|
6
|
+
Gob/BlockDelimiters:
|
|
7
7
|
Enabled: true
|
|
8
8
|
|
|
9
|
-
Gob/
|
|
9
|
+
Gob/DeferStatement:
|
|
10
10
|
Enabled: true
|
|
11
11
|
|
|
12
|
-
Gob/
|
|
12
|
+
Gob/ErrorCheck:
|
|
13
13
|
Enabled: true
|
|
14
14
|
|
|
15
|
-
Gob/
|
|
15
|
+
Gob/Indentation:
|
|
16
|
+
Enabled: true
|
|
17
|
+
|
|
18
|
+
Gob/Multiline:
|
|
19
|
+
Enabled: true
|
|
20
|
+
|
|
21
|
+
Gob/QuoteMarks:
|
|
22
|
+
Enabled: true
|
|
23
|
+
|
|
24
|
+
Gob/SignatureComment:
|
|
25
|
+
Enabled: true
|
|
26
|
+
|
|
27
|
+
Gob/TrailingWhitespace:
|
|
28
|
+
Enabled: true
|
|
29
|
+
|
|
30
|
+
Gob/VariableDeclaration:
|
|
31
|
+
Enabled: true
|
|
32
|
+
|
|
33
|
+
Gob/PointerType:
|
|
34
|
+
Enabled: true
|
|
35
|
+
|
|
36
|
+
Gob/OperatorAssignment:
|
|
37
|
+
Enabled: true
|
|
38
|
+
|
|
39
|
+
Gob/ZeroBasedRangeLoop:
|
|
40
|
+
Enabled: true
|
|
41
|
+
|
|
42
|
+
Gob/WordArray:
|
|
43
|
+
Enabled: true
|
|
44
|
+
|
|
45
|
+
Gob/GoBlock:
|
|
46
|
+
Enabled: true
|
|
47
|
+
|
|
48
|
+
Gob/RedundantLambdaParentheses:
|
|
49
|
+
Enabled: true
|
|
50
|
+
|
|
51
|
+
Gob/ThisReceiver:
|
|
52
|
+
Enabled: true
|
|
53
|
+
|
|
54
|
+
Gob/ArrayLength:
|
|
55
|
+
Enabled: true
|
|
56
|
+
|
|
57
|
+
Gob/CollectionLiteral:
|
|
58
|
+
Enabled: true
|
|
59
|
+
|
|
60
|
+
Gob/LambdaSignature:
|
|
61
|
+
Enabled: true
|
|
62
|
+
|
|
63
|
+
Gob/InvokedLambda:
|
|
64
|
+
Enabled: true
|
|
65
|
+
|
|
66
|
+
Gob/AttributeType:
|
|
67
|
+
Enabled: true
|
|
68
|
+
|
|
69
|
+
Gob/SelectBlock:
|
|
16
70
|
Enabled: true
|
|
@@ -5,10 +5,82 @@ module Gob
|
|
|
5
5
|
module Rules
|
|
6
6
|
class Cop < RuboCop::Cop::Base
|
|
7
7
|
exclude_from_registry
|
|
8
|
+
extend RuboCop::Cop::AutoCorrector
|
|
8
9
|
|
|
9
10
|
def self.badge
|
|
10
11
|
@badge ||= RuboCop::Cop::Badge.parse("Gob/#{name.split("::").last}")
|
|
11
12
|
end
|
|
13
|
+
|
|
14
|
+
# A rule matches the source the same way the transpiler does, so that
|
|
15
|
+
# what is legal here is exactly what the transpiler knows how to read.
|
|
16
|
+
def on_new_investigation
|
|
17
|
+
walk(SyntaxTree.parse(processed_source.raw_source))
|
|
18
|
+
rescue SyntaxTree::Parser::ParseError
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def walk(node)
|
|
25
|
+
case node
|
|
26
|
+
in Array
|
|
27
|
+
node.each { |child| walk(child) }
|
|
28
|
+
|
|
29
|
+
in SyntaxTree::Node
|
|
30
|
+
@ancestors ||= []
|
|
31
|
+
@ancestors << node
|
|
32
|
+
rule(node)
|
|
33
|
+
walk(node.child_nodes)
|
|
34
|
+
@ancestors.pop
|
|
35
|
+
|
|
36
|
+
else
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def body_statements(bodystmt)
|
|
42
|
+
case bodystmt
|
|
43
|
+
in SyntaxTree::BodyStmt[statements: SyntaxTree::Statements[body: body]]
|
|
44
|
+
body
|
|
45
|
+
in SyntaxTree::Statements[body: body]
|
|
46
|
+
body
|
|
47
|
+
else
|
|
48
|
+
[]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def rule(node)
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def source(location)
|
|
57
|
+
processed_source.raw_source[location.start_char...location.end_char]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def range(location)
|
|
61
|
+
Parser::Source::Range.new(
|
|
62
|
+
processed_source.buffer,
|
|
63
|
+
location.start_char,
|
|
64
|
+
location.end_char
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The whole line a node sits on, newline included.
|
|
69
|
+
def line(location)
|
|
70
|
+
raw = processed_source.raw_source
|
|
71
|
+
opening = raw.rindex("\n", location.start_char - 1).then { |index| index ? index + 1 : 0 }
|
|
72
|
+
closing = raw.index("\n", location.end_char).then { |index| index ? index + 1 : raw.length }
|
|
73
|
+
|
|
74
|
+
Parser::Source::Range.new(processed_source.buffer, opening, closing)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The position just before the newline that ends a node's first line.
|
|
78
|
+
def end_of_line(location)
|
|
79
|
+
raw = processed_source.raw_source
|
|
80
|
+
closing = raw.index("\n", location.start_char) || raw.length
|
|
81
|
+
|
|
82
|
+
Parser::Source::Range.new(processed_source.buffer, closing, closing)
|
|
83
|
+
end
|
|
12
84
|
end
|
|
13
85
|
end
|
|
14
86
|
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
class DeferStatement < Cop
|
|
7
|
+
MSG = "Write `defer expression`, never `defer { expression }`"
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# defer { srv.Close() }
|
|
12
|
+
# defer srv.Close()
|
|
13
|
+
# ---
|
|
14
|
+
def rule(node)
|
|
15
|
+
case node
|
|
16
|
+
in SyntaxTree::MethodAddBlock[
|
|
17
|
+
call: SyntaxTree::CallNode[ message: SyntaxTree::Ident[value: "defer"] ],
|
|
18
|
+
block: SyntaxTree::BlockNode[
|
|
19
|
+
bodystmt: SyntaxTree::Statements[ body: [ deferred ] ]
|
|
20
|
+
]
|
|
21
|
+
]
|
|
22
|
+
add_offense(range(node.location), message: MSG) do |corrector|
|
|
23
|
+
corrector.replace(range(node.location), "defer #{source(deferred.location)}")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
else
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
class ErrorCheck < Cop
|
|
7
|
+
MSG = "Write `err`; the comparison against nil is implied"
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# if err != nil
|
|
12
|
+
# if err
|
|
13
|
+
# ---
|
|
14
|
+
def rule(node)
|
|
15
|
+
case node
|
|
16
|
+
in SyntaxTree::Binary[
|
|
17
|
+
left: SyntaxTree::VCall[value: SyntaxTree::Ident[value: "err"]] |
|
|
18
|
+
SyntaxTree::VarRef[value: SyntaxTree::Ident[value: "err"]],
|
|
19
|
+
operator: :!=,
|
|
20
|
+
right: SyntaxTree::VarRef[value: SyntaxTree::Kw[value: "nil"]]
|
|
21
|
+
]
|
|
22
|
+
add_offense(range(node.location), message: MSG) do |corrector|
|
|
23
|
+
corrector.replace(range(node.location), "err")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
else
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Uses the lambda call accepted by the transpiler for goroutines.
|
|
7
|
+
class GoBlock < Cop
|
|
8
|
+
MSG = "Write `go lambda { ... }.()`"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# go do
|
|
13
|
+
# work
|
|
14
|
+
# end.()
|
|
15
|
+
# ---
|
|
16
|
+
# go lambda {
|
|
17
|
+
# work
|
|
18
|
+
# }.()
|
|
19
|
+
# ---
|
|
20
|
+
def rule(node)
|
|
21
|
+
case node
|
|
22
|
+
in SyntaxTree::MethodAddBlock[
|
|
23
|
+
call: SyntaxTree::CallNode[
|
|
24
|
+
receiver: nil,
|
|
25
|
+
operator: nil,
|
|
26
|
+
message: SyntaxTree::Ident[value: "go"] => message,
|
|
27
|
+
arguments: SyntaxTree::Args[parts: []]
|
|
28
|
+
],
|
|
29
|
+
block: SyntaxTree::BlockNode[
|
|
30
|
+
opening: SyntaxTree::Kw[value: "do"] => opening
|
|
31
|
+
] => block
|
|
32
|
+
]
|
|
33
|
+
add_offense(range(block.location), message: MSG) do |corrector|
|
|
34
|
+
corrector.replace(range(message.location), "go lambda")
|
|
35
|
+
corrector.replace(range(opening.location), "{")
|
|
36
|
+
corrector.replace(closing(block), invoked?(block) ? "}" : "}.()")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
else
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def closing(block)
|
|
45
|
+
Parser::Source::Range.new(
|
|
46
|
+
processed_source.buffer,
|
|
47
|
+
block.location.end_char - 3,
|
|
48
|
+
block.location.end_char
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def invoked?(block)
|
|
53
|
+
processed_source.raw_source[block.location.end_char, 3] == ".()"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
class Indentation < Cop
|
|
7
|
+
MSG = "Indent this %<depth>d spaces, not %<actual>d"
|
|
8
|
+
|
|
9
|
+
WIDTH = 2
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# Every scope indents its body exactly one level past its opener.
|
|
14
|
+
def rule(node)
|
|
15
|
+
case node
|
|
16
|
+
in SyntaxTree::Program | SyntaxTree::BodyStmt
|
|
17
|
+
nil
|
|
18
|
+
|
|
19
|
+
in { bodystmt: SyntaxTree::BodyStmt[ statements: SyntaxTree::Statements[ body: body ] ] }
|
|
20
|
+
nest(node, body)
|
|
21
|
+
closes(node)
|
|
22
|
+
|
|
23
|
+
in { statements: SyntaxTree::Statements[ body: body ] }
|
|
24
|
+
nest(node, body)
|
|
25
|
+
closes(node)
|
|
26
|
+
|
|
27
|
+
else
|
|
28
|
+
nil
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def nest(node, body)
|
|
33
|
+
depth = indentation(node.location) + WIDTH
|
|
34
|
+
|
|
35
|
+
body.each do |statement|
|
|
36
|
+
case statement
|
|
37
|
+
in SyntaxTree::VoidStmt
|
|
38
|
+
nil
|
|
39
|
+
|
|
40
|
+
else
|
|
41
|
+
misindented(statement, depth)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def misindented(statement, depth)
|
|
47
|
+
actual = statement.location.start_column
|
|
48
|
+
return if actual == depth
|
|
49
|
+
return unless opens_line?(statement)
|
|
50
|
+
|
|
51
|
+
add_offense(range(statement.location), message: format(MSG, depth: depth, actual: actual)) do |corrector|
|
|
52
|
+
corrector.replace(leading(statement.location), " " * depth)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# `end` lines up with the line that opened the scope.
|
|
57
|
+
def closes(node)
|
|
58
|
+
closer = closer(node)
|
|
59
|
+
return if closer.nil?
|
|
60
|
+
|
|
61
|
+
depth = indentation(node.location)
|
|
62
|
+
actual = indentation_of(closer.begin_pos)
|
|
63
|
+
return if actual == depth
|
|
64
|
+
return unless begins_line?(closer.begin_pos)
|
|
65
|
+
|
|
66
|
+
add_offense(closer, message: format(MSG, depth: depth, actual: actual)) do |corrector|
|
|
67
|
+
corrector.replace(leading_at(closer.begin_pos), " " * depth)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def closer(node)
|
|
72
|
+
raw = processed_source.raw_source
|
|
73
|
+
closing = node.location.end_char
|
|
74
|
+
return nil unless raw[(closing - 3)...closing] == "end"
|
|
75
|
+
|
|
76
|
+
Parser::Source::Range.new(processed_source.buffer, closing - 3, closing)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Only the statement that begins its line owns that line's indentation.
|
|
80
|
+
def opens_line?(location_owner)
|
|
81
|
+
raw = processed_source.raw_source
|
|
82
|
+
start = location_owner.location.start_char
|
|
83
|
+
opening = raw.rindex("\n", start - 1).then { |index| index ? index + 1 : 0 }
|
|
84
|
+
|
|
85
|
+
raw[opening...start].match?(/\A[ \t]*\z/)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A body is indented past the line its opener sits on, not past the
|
|
89
|
+
# opener itself -- `xs.each do` opens a scope from column 4, not 11.
|
|
90
|
+
def indentation(location)
|
|
91
|
+
raw = processed_source.raw_source
|
|
92
|
+
opening = raw.rindex("\n", location.start_char - 1).then { |index| index ? index + 1 : 0 }
|
|
93
|
+
|
|
94
|
+
raw[opening..].match(/\A[ \t]*/)[0].length
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def leading(location)
|
|
98
|
+
leading_at(location.start_char)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def leading_at(start)
|
|
102
|
+
raw = processed_source.raw_source
|
|
103
|
+
opening = raw.rindex("\n", start - 1).then { |index| index ? index + 1 : 0 }
|
|
104
|
+
|
|
105
|
+
Parser::Source::Range.new(processed_source.buffer, opening, start)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def indentation_of(start)
|
|
109
|
+
leading_at(start).source.length
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def begins_line?(start)
|
|
113
|
+
leading_at(start).source.match?(/\A[ \t]*\z/)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gob
|
|
4
|
+
module Validation
|
|
5
|
+
module Rules
|
|
6
|
+
# Rejects immediately invoked lambdas except Gob's supported goroutine form.
|
|
7
|
+
class InvokedLambda < Cop
|
|
8
|
+
MSG = "Do not invoke a lambda immediately; only `go lambda { ... }.()` is supported"
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# lambda { work }.()
|
|
13
|
+
# go lambda { work }.()
|
|
14
|
+
# ---
|
|
15
|
+
def rule(node)
|
|
16
|
+
case node
|
|
17
|
+
in SyntaxTree::CallNode[
|
|
18
|
+
receiver: SyntaxTree::MethodAddBlock[
|
|
19
|
+
call: SyntaxTree::CallNode[
|
|
20
|
+
receiver: nil,
|
|
21
|
+
operator: nil,
|
|
22
|
+
message: SyntaxTree::Ident[value: "lambda"]
|
|
23
|
+
]
|
|
24
|
+
],
|
|
25
|
+
operator: SyntaxTree::Period[value: "."],
|
|
26
|
+
message: SyntaxTree::Ident[value: "call"] | :call
|
|
27
|
+
]
|
|
28
|
+
add_offense(range(node.location), message: MSG) unless goroutine_argument?
|
|
29
|
+
|
|
30
|
+
else
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def goroutine_argument?
|
|
36
|
+
@ancestors&.any? do |ancestor|
|
|
37
|
+
case ancestor
|
|
38
|
+
in SyntaxTree::Command[message: SyntaxTree::Ident[value: "go"]]
|
|
39
|
+
true
|
|
40
|
+
else
|
|
41
|
+
false
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|