rubocop-on-rbs 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 05fc386350ef8ab36fc84937e1c3d5f2abdd95e816c8fc4a3ce80f41791475aa
4
- data.tar.gz: 5b38f37836d5858ff196abd454bbfa14432ffdca49f8e89cfc88e61490da6d61
3
+ metadata.gz: 615681c73d7e70b350f6b830050bfce38cac2fc5bca2d8f9d497c4cf93ad1864
4
+ data.tar.gz: 88fdc3fd0a867edbf111c3b839da92d3e2b3289c290049f8d0eafc7c9f84c74f
5
5
  SHA512:
6
- metadata.gz: 4845db99745dab9b0c833fc4bf67e848aab97ed5cbe9e23279d2cf6ff867d100357dac144a261e1af279b415aa49848603a631caac38ea9f4b3a7a2732097001
7
- data.tar.gz: e316da95415babf17a4e9d8d59008e6cf9a47ccfef2ed745b90652be9baa76b96c88ace1d0693fa58ea6229dd46282d030413fdbc0c2f649d0db373730903c8d
6
+ metadata.gz: b9265c984b3c0d4565cb55936a320a2f0f1f29bd7f7927a15f2507dbab41f90ac171aa5d5b31cfa3af32ab5591b4a4bd3fc44615b7f1b728fda8ace3cfdff03f
7
+ data.tar.gz: fdefc26a752f70e15f6bc47b3246e1a4516e3f4ca6e36624701e69632cc38cfbee50a387717a0993cba265373351868c97e40e7149597bafe1d4095e374e31e2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
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
+
3
7
  ## [0.5.0] - 2024-06-17
4
8
 
5
9
  * Fix destructive change by @ksss in https://github.com/ksss/rubocop-on-rbs/pull/17
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
@@ -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'
@@ -23,9 +23,11 @@ module RuboCop
23
23
  @error.nil?
24
24
  end
25
25
 
26
- def tokens
26
+ def tokens(with_trivia: false)
27
27
  @tokens ||= begin
28
- ::RBS::Parser.lex(buffer).value
28
+ tokens = ::RBS::Parser.lex(buffer).value
29
+ tokens.reject! { |token| token.type == :tTRIVIA } unless with_trivia
30
+ tokens
29
31
  end
30
32
  end
31
33
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module RBS
5
- VERSION = '0.6.0'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  end
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.6.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-18 00:00:00.000000000 Z
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.41'
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.41'
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