rubocop-on-rbs 0.5.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/config/default.yml +10 -4
- data/lib/rubocop/cop/rbs/lint/ambiguous_operator_precedence.rb +69 -0
- data/lib/rubocop/cop/rbs/lint/useless_access_modifier.rb +95 -0
- data/lib/rubocop/cop/rbs/style/redundant_parentheses.rb +12 -1
- data/lib/rubocop/cop/rbs_cops.rb +3 -1
- data/lib/rubocop/rbs/cop_base.rb +7 -3
- data/lib/rubocop/rbs/processed_rbs_source.rb +8 -2
- data/lib/rubocop/rbs/version.rb +1 -1
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 615681c73d7e70b350f6b830050bfce38cac2fc5bca2d8f9d497c4cf93ad1864
|
4
|
+
data.tar.gz: 88fdc3fd0a867edbf111c3b839da92d3e2b3289c290049f8d0eafc7c9f84c74f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9265c984b3c0d4565cb55936a320a2f0f1f29bd7f7927a15f2507dbab41f90ac171aa5d5b31cfa3af32ab5591b4a4bd3fc44615b7f1b728fda8ace3cfdff03f
|
7
|
+
data.tar.gz: fdefc26a752f70e15f6bc47b3246e1a4516e3f4ca6e36624701e69632cc38cfbee50a387717a0993cba265373351868c97e40e7149597bafe1d4095e374e31e2
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.6.0] - 2024-06-18
|
4
|
+
|
5
|
+
* Fixed problems interfering with other Cop by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/20
|
6
|
+
|
7
|
+
## [0.5.0] - 2024-06-17
|
8
|
+
|
9
|
+
* Fix destructive change by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/17
|
10
|
+
* Introduce cache by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/18
|
11
|
+
* Fix bug for EmptyArgument by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/19
|
12
|
+
|
3
13
|
## [0.4.0] - 2024-06-14
|
4
14
|
|
5
15
|
* Split RuboCop task by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/6
|
data/config/default.yml
CHANGED
@@ -78,15 +78,18 @@ RBS/Layout/TrailingWhitespace:
|
|
78
78
|
## RBS/Lint
|
79
79
|
|
80
80
|
RBS/Lint:
|
81
|
+
Severity: warning
|
82
|
+
Enabled: true
|
83
|
+
|
84
|
+
RBS/Lint/AmbiguousOperatorPrecedence:
|
85
|
+
Description: 'Check ambiguous operator precedence'
|
81
86
|
Enabled: true
|
82
87
|
|
83
88
|
RBS/Lint/DuplicateOverload:
|
84
|
-
Severity: warning
|
85
89
|
Description: 'Checks that there are no repeated overload bodies'
|
86
90
|
Enabled: true
|
87
91
|
|
88
92
|
RBS/Lint/LiteralIntersection:
|
89
|
-
Severity: warning
|
90
93
|
Description: 'Check literal intersection'
|
91
94
|
Enabled: true
|
92
95
|
|
@@ -95,13 +98,16 @@ RBS/Lint/Syntax:
|
|
95
98
|
Description: 'Check RBS syntax'
|
96
99
|
Enabled: true
|
97
100
|
|
101
|
+
RBS/Lint/UselessAccessModifier:
|
102
|
+
AutoCorrect: contextual
|
103
|
+
Description: 'Check useless access modifier'
|
104
|
+
Enabled: true
|
105
|
+
|
98
106
|
RBS/Lint/UselessOverloadTypeParams:
|
99
|
-
Severity: warning
|
100
107
|
Description: 'Check redundant overload type params'
|
101
108
|
Enabled: true
|
102
109
|
|
103
110
|
RBS/Lint/WillSyntaxError:
|
104
|
-
Severity: warning
|
105
111
|
Description: 'Check RBS will syntax error'
|
106
112
|
Enabled: true
|
107
113
|
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RBS
|
6
|
+
module Lint
|
7
|
+
# Checks that there are no repeated overload bodies
|
8
|
+
#
|
9
|
+
# @example default
|
10
|
+
# # bad
|
11
|
+
# def foo: (A | B & C) -> void
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# def foo: (A | (B & C)) -> void
|
15
|
+
#
|
16
|
+
class AmbiguousOperatorPrecedence < RuboCop::RBS::CopBase
|
17
|
+
MSG = 'Wrap expressions with varying precedence with parentheses to avoid ambiguity.'
|
18
|
+
|
19
|
+
extend AutoCorrector
|
20
|
+
|
21
|
+
def on_rbs_def(decl)
|
22
|
+
decl.overloads.each do |overload|
|
23
|
+
overload.method_type.each_type do |type|
|
24
|
+
check_type(type)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def on_rbs_constant(const)
|
30
|
+
check_type(const.type)
|
31
|
+
end
|
32
|
+
alias on_rbs_global on_rbs_constant
|
33
|
+
alias on_rbs_type_alias on_rbs_constant
|
34
|
+
alias on_rbs_attribute on_rbs_constant
|
35
|
+
|
36
|
+
def check_type(type)
|
37
|
+
on_type([::RBS::Types::Union], type) do |union|
|
38
|
+
union.types.each do |t|
|
39
|
+
case t
|
40
|
+
when ::RBS::Types::Intersection
|
41
|
+
next unless t.location
|
42
|
+
|
43
|
+
start_index = bsearch_token_index(t.location.start_pos)
|
44
|
+
end_index = bsearch_token_index(t.location.end_pos)
|
45
|
+
before = processed_rbs_source.tokens[start_index - 1]
|
46
|
+
after = processed_rbs_source.tokens[end_index]
|
47
|
+
unless before.type == :pLPAREN && after.type == :pRPAREN
|
48
|
+
range = location_to_range(t.location)
|
49
|
+
add_offense(range) do |corrector|
|
50
|
+
corrector.wrap(range, '(', ')')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def bsearch_token_index(pos)
|
61
|
+
processed_rbs_source.tokens.bsearch_index do |token|
|
62
|
+
token.location.start_pos >= pos
|
63
|
+
end or raise
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RBS
|
6
|
+
module Lint
|
7
|
+
# @example default
|
8
|
+
# # bad
|
9
|
+
# class Foo
|
10
|
+
# public # this is redundant (default access is public)
|
11
|
+
#
|
12
|
+
# def method: () -> void
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # bad
|
16
|
+
# class Foo
|
17
|
+
# # The following is redundant (methods defined on the class'
|
18
|
+
# # singleton class are not affected by the private modifier)
|
19
|
+
# private
|
20
|
+
#
|
21
|
+
# def self.method3: () -> void
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# # bad
|
25
|
+
# class Foo
|
26
|
+
# private # this is redundant (no following methods are defined)
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# class Foo
|
31
|
+
# private # this is not redundant (a method is defined)
|
32
|
+
#
|
33
|
+
# def method2
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
class UselessAccessModifier < RuboCop::RBS::CopBase
|
38
|
+
extend AutoCorrector
|
39
|
+
|
40
|
+
MSG = 'Useless `%<current>s` access modifier.'
|
41
|
+
|
42
|
+
def on_rbs_class(decl)
|
43
|
+
current = nil
|
44
|
+
unused = true
|
45
|
+
decl.members.each do |member|
|
46
|
+
next unless member.location
|
47
|
+
|
48
|
+
case member
|
49
|
+
when ::RBS::AST::Members::Public
|
50
|
+
if current.nil? || current.is_a?(::RBS::AST::Members::Public)
|
51
|
+
range = location_to_range(member.location)
|
52
|
+
add_offense(range, message: format(MSG, { current: 'public' })) do |corrector|
|
53
|
+
autocorrect(corrector, range)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
current = member
|
57
|
+
when ::RBS::AST::Members::Private
|
58
|
+
if current.is_a?(::RBS::AST::Members::Private)
|
59
|
+
range = location_to_range(member.location)
|
60
|
+
add_offense(range, message: format(MSG, { current: 'private' })) do |corrector|
|
61
|
+
autocorrect(corrector, range)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
current = member
|
65
|
+
when ::RBS::AST::Members::MethodDefinition
|
66
|
+
if member.kind != :singleton
|
67
|
+
unused = false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
if unused && current
|
73
|
+
range = location_to_range(current.location)
|
74
|
+
vis = case current
|
75
|
+
when ::RBS::AST::Members::Public
|
76
|
+
'public'
|
77
|
+
when ::RBS::AST::Members::Private
|
78
|
+
'private'
|
79
|
+
end
|
80
|
+
add_offense(range, message: format(MSG, { current: vis })) do |corrector|
|
81
|
+
autocorrect(corrector, range)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
alias on_rbs_module on_rbs_class
|
86
|
+
|
87
|
+
def autocorrect(corrector, range)
|
88
|
+
line = range_by_whole_lines(range, include_final_newline: true)
|
89
|
+
corrector.remove(line)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -72,9 +72,20 @@ module RuboCop
|
|
72
72
|
excludes = [
|
73
73
|
::RBS::Types::Union,
|
74
74
|
::RBS::Types::Intersection,
|
75
|
-
::RBS::Types::Proc
|
75
|
+
::RBS::Types::Proc,
|
76
76
|
]
|
77
77
|
@cop.on_not_type(excludes, @type) do |type|
|
78
|
+
case type
|
79
|
+
when ::RBS::Types::Optional
|
80
|
+
case type.type
|
81
|
+
when ::RBS::Types::Literal
|
82
|
+
case type.type.literal
|
83
|
+
when Symbol
|
84
|
+
# Skip optional with symbol literal (e.g. `(:sym)?`)
|
85
|
+
@skip << type.location.start_pos
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
78
89
|
check_parentheses(type)
|
79
90
|
end
|
80
91
|
end
|
data/lib/rubocop/cop/rbs_cops.rb
CHANGED
@@ -15,10 +15,12 @@ require_relative 'rbs/layout/space_before_colon'
|
|
15
15
|
require_relative 'rbs/layout/space_before_overload'
|
16
16
|
require_relative 'rbs/layout/trailing_whitespace'
|
17
17
|
|
18
|
+
require_relative 'rbs/lint/ambiguous_operator_precedence'
|
18
19
|
require_relative 'rbs/lint/duplicate_overload'
|
19
20
|
require_relative 'rbs/lint/literal_intersection'
|
20
|
-
require_relative 'rbs/lint/useless_overload_type_params'
|
21
21
|
require_relative 'rbs/lint/syntax'
|
22
|
+
require_relative 'rbs/lint/useless_access_modifier'
|
23
|
+
require_relative 'rbs/lint/useless_overload_type_params'
|
22
24
|
require_relative 'rbs/lint/will_syntax_error'
|
23
25
|
|
24
26
|
require_relative 'rbs/style/block_return_boolish'
|
data/lib/rubocop/rbs/cop_base.rb
CHANGED
@@ -51,9 +51,6 @@ module RuboCop
|
|
51
51
|
if processed_rbs_source.error
|
52
52
|
on_rbs_parsing_error()
|
53
53
|
else
|
54
|
-
# HACK: Autocorrector needs to clear diagnostics
|
55
|
-
processed_source.diagnostics.clear
|
56
|
-
|
57
54
|
on_rbs_new_investigation()
|
58
55
|
|
59
56
|
processed_rbs_source.decls.each do |decl|
|
@@ -113,6 +110,13 @@ module RuboCop
|
|
113
110
|
def tokenize(source)
|
114
111
|
::RBS::Parser.lex(source).value.reject { |t| t.type == :tTRIVIA }
|
115
112
|
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
# HACK: Required to autocorrect
|
117
|
+
def current_corrector
|
118
|
+
@current_corrector ||= RuboCop::Cop::Corrector.new(@processed_source) if @processed_rbs_source.valid_syntax?
|
119
|
+
end
|
116
120
|
end
|
117
121
|
end
|
118
122
|
end
|
@@ -19,9 +19,15 @@ module RuboCop
|
|
19
19
|
@error = e
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
22
|
+
def valid_syntax?
|
23
|
+
@error.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def tokens(with_trivia: false)
|
23
27
|
@tokens ||= begin
|
24
|
-
::RBS::Parser.lex(buffer).value
|
28
|
+
tokens = ::RBS::Parser.lex(buffer).value
|
29
|
+
tokens.reject! { |token| token.type == :tTRIVIA } unless with_trivia
|
30
|
+
tokens
|
25
31
|
end
|
26
32
|
end
|
27
33
|
end
|
data/lib/rubocop/rbs/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-on-rbs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rbs
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
33
|
+
version: '1.61'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
40
|
+
version: '1.61'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: zlib
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,9 +77,11 @@ files:
|
|
77
77
|
- lib/rubocop/cop/rbs/layout/space_before_colon.rb
|
78
78
|
- lib/rubocop/cop/rbs/layout/space_before_overload.rb
|
79
79
|
- lib/rubocop/cop/rbs/layout/trailing_whitespace.rb
|
80
|
+
- lib/rubocop/cop/rbs/lint/ambiguous_operator_precedence.rb
|
80
81
|
- lib/rubocop/cop/rbs/lint/duplicate_overload.rb
|
81
82
|
- lib/rubocop/cop/rbs/lint/literal_intersection.rb
|
82
83
|
- lib/rubocop/cop/rbs/lint/syntax.rb
|
84
|
+
- lib/rubocop/cop/rbs/lint/useless_access_modifier.rb
|
83
85
|
- lib/rubocop/cop/rbs/lint/useless_overload_type_params.rb
|
84
86
|
- lib/rubocop/cop/rbs/lint/will_syntax_error.rb
|
85
87
|
- lib/rubocop/cop/rbs/style/block_return_boolish.rb
|
@@ -101,6 +103,9 @@ homepage: https://github.com/ksss/rubocop-on-rbs
|
|
101
103
|
licenses:
|
102
104
|
- MIT
|
103
105
|
metadata:
|
106
|
+
homepage_uri: https://github.com/ksss/rubocop-on-rbs
|
107
|
+
source_code_uri: https://github.com/ksss/rubocop-on-rbs
|
108
|
+
changelog_uri: https://github.com/ksss/rubocop-on-rbs
|
104
109
|
rubygems_mfa_required: 'true'
|
105
110
|
post_install_message:
|
106
111
|
rdoc_options: []
|