rubocop-on-rbs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/CODE_OF_CONDUCT.md +84 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +186 -0
  6. data/config/default.yml +165 -0
  7. data/lib/rubocop/cop/rbs/layout/comment_indentation.rb +69 -0
  8. data/lib/rubocop/cop/rbs/layout/empty_lines_around_overloads.rb +49 -0
  9. data/lib/rubocop/cop/rbs/layout/end_alignment.rb +55 -0
  10. data/lib/rubocop/cop/rbs/layout/extra_spacing.rb +55 -0
  11. data/lib/rubocop/cop/rbs/layout/indentation_width.rb +64 -0
  12. data/lib/rubocop/cop/rbs/layout/overload_indentation.rb +69 -0
  13. data/lib/rubocop/cop/rbs/layout/space_around_arrow.rb +56 -0
  14. data/lib/rubocop/cop/rbs/layout/space_around_braces.rb +77 -0
  15. data/lib/rubocop/cop/rbs/layout/space_before_colon.rb +43 -0
  16. data/lib/rubocop/cop/rbs/layout/space_before_overload.rb +46 -0
  17. data/lib/rubocop/cop/rbs/layout/trailing_whitespace.rb +42 -0
  18. data/lib/rubocop/cop/rbs/lint/syntax.rb +18 -0
  19. data/lib/rubocop/cop/rbs/lint/type_params_arity.rb +170 -0
  20. data/lib/rubocop/cop/rbs/lint/useless_overload_type_params.rb +57 -0
  21. data/lib/rubocop/cop/rbs/lint/will_syntax_error.rb +205 -0
  22. data/lib/rubocop/cop/rbs/style/block_return_boolish.rb +35 -0
  23. data/lib/rubocop/cop/rbs/style/classic_type.rb +73 -0
  24. data/lib/rubocop/cop/rbs/style/duplicated_type.rb +61 -0
  25. data/lib/rubocop/cop/rbs/style/initialize_return_type.rb +40 -0
  26. data/lib/rubocop/cop/rbs/style/merge_untyped.rb +91 -0
  27. data/lib/rubocop/cop/rbs/style/optional_nil.rb +50 -0
  28. data/lib/rubocop/cop/rbs/style/true_false.rb +84 -0
  29. data/lib/rubocop/cop/rbs_cops.rb +28 -0
  30. data/lib/rubocop/rbs/cop_base.rb +91 -0
  31. data/lib/rubocop/rbs/inject.rb +20 -0
  32. data/lib/rubocop/rbs/processed_rbs_source.rb +29 -0
  33. data/lib/rubocop/rbs/version.rb +7 -0
  34. data/lib/rubocop/rbs.rb +15 -0
  35. data/lib/rubocop-on-rbs.rb +13 -0
  36. metadata +106 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f2d0a08d15b11889f3e9cb30b870ab7f10708d79fc88984271cd0d1d229a6eb2
4
+ data.tar.gz: c5bbecab5e33bbbddcab3081b1df9c0370948e1c4cbd2fbe9ea1f9bb8e7cf820
5
+ SHA512:
6
+ metadata.gz: 8153af6d71ecd6f087d9511de6a3caa2ded02a2edf6a1e51833a212d1a1fee269bfcf8c398b536ce89fedebf91b86784aba445117779101a64efb83e1082ffd8
7
+ data.tar.gz: aa38d915d25385ae67af7a84d7b480b28fcd23a6e4110e273e5d42571ff1b7bf5daf0116c7803b14297cbbd4a4f4e706836b2b1d6bc770edf8d81104f76aff46
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-05-15
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at co000ri@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 ksss
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,186 @@
1
+ # RuboCop on RBS
2
+
3
+ A [RuboCop](https://github.com/rubocop/rubocop) extension focused on enforcing [RBS](https://github.com/ruby/rbs) best practices and coding conventions.
4
+
5
+ ## Synopsis
6
+
7
+ Now, We can support RBS files by rubocop.
8
+
9
+ ```console
10
+ $ cat sig/foo.rbs
11
+ module Foo
12
+ def foo: ( void) -> untyped
13
+ end
14
+
15
+ $ bundle exec rubocop sig/foo.rbs
16
+ Inspecting 1 file
17
+ W
18
+
19
+ Offenses:
20
+
21
+ sig/foo.rbs:2:1: C: [Correctable] RBS/Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.
22
+ def foo: ( void) -> untyped
23
+
24
+ sig/foo.rbs:2:11: C: [Correctable] RBS/Layout/ExtraSpacing: Unnecessary spacing detected.
25
+ def foo: ( void) -> untyped
26
+ ^^
27
+ sig/foo.rbs:2:14: W: RBS/Lint/WillSyntaxError: void type is only allowed in return type or generics parameter
28
+ def foo: ( void) -> untyped
29
+ ^^^^
30
+
31
+ 1 file inspected, 3 offenses detected, 2 offenses autocorrectable
32
+ ```
33
+
34
+ ## Support VSCode
35
+
36
+ We are currently working on a response, but it is possible to modify part of [vscode-rubocop](https://github.com/rubocop/vscode-rubocop) as a temporary hack to use it in vscode as well.
37
+
38
+ Open your installed vscode extension.
39
+
40
+ ```
41
+ $ code ~/.vscode/extensions/rubocop.vscode-rubocop-X.Y.Z/
42
+ ```
43
+
44
+ Search `documentSelector:[{`.
45
+
46
+ Then, Edit to add following to `documentSelector[]`
47
+
48
+ ```js
49
+ {scheme:"file",language:"rbs"}
50
+ ```
51
+
52
+ Additionally, by configuring the `settings.json` below, you can enable auto-correction to run on file save.
53
+
54
+ ```json
55
+ {
56
+ "[rbs]": {
57
+ "editor.formatOnSave": true,
58
+ "editor.defaultFormatter": "rubocop.vscode-rubocop"
59
+ },
60
+ }
61
+ ```
62
+
63
+ And restart VSCode.
64
+
65
+ ## Departments
66
+
67
+ ### RBS
68
+
69
+ This gem handles many cops.
70
+ All cops can be enabled by following setting by default.
71
+
72
+ ```yaml
73
+ RBS:
74
+ Enabled: true
75
+ ```
76
+
77
+ See `config/default.yml` for all Cop.
78
+
79
+ ### RBS/Layout
80
+
81
+ This department is a collection of relatively minor fixes has been gathered.
82
+
83
+ ```yaml
84
+ RBS/Layout:
85
+ Enabled: true
86
+ ```
87
+
88
+ ```console
89
+ $ bundle exec rubocop --only RBS/Layout
90
+ ```
91
+
92
+ ### RBS/Lint
93
+
94
+ This department is gathering important points that need to be fixed.
95
+
96
+ ```yaml
97
+ RBS/Lint:
98
+ Enabled: true
99
+ ```
100
+
101
+ ```console
102
+ $ bundle exec rubocop --only RBS/Lint
103
+ ```
104
+
105
+ ### RBS/Style
106
+
107
+ This department unifies multiple styles.
108
+
109
+ ```yaml
110
+ RBS/Style:
111
+ Enabled: true
112
+ ```
113
+
114
+ ```console
115
+ $ bundle exec rubocop --only RBS/Style
116
+ ```
117
+
118
+ ## Installation
119
+
120
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
121
+
122
+ Install the gem and add to the application's Gemfile by executing:
123
+
124
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
125
+
126
+ If bundler is not being used to manage dependencies, install the gem by executing:
127
+
128
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
129
+
130
+ ## Usage
131
+
132
+ You need to tell RuboCop to load the RBS extension. There are three
133
+ ways to do this:
134
+
135
+ ### RuboCop configuration file
136
+
137
+ Put this into your `.rubocop.yml`.
138
+
139
+ ```yaml
140
+ require: rubocop-on-rbs
141
+ ```
142
+
143
+ Alternatively, use the following array notation when specifying multiple extensions.
144
+
145
+ ```yaml
146
+ require:
147
+ - rubocop-other-extension
148
+ - rubocop-on-rbs
149
+ ```
150
+
151
+ Now you can run `rubocop` and it will automatically load the RuboCop on RBS
152
+ cops together with the standard cops.
153
+
154
+ ### Command line
155
+
156
+ ```sh
157
+ $ rubocop --require rubocop-on-rbs
158
+ ```
159
+
160
+ ### Rake task
161
+
162
+ ```ruby
163
+ require 'rubocop/rake_task'
164
+
165
+ RuboCop::RakeTask.new do |task|
166
+ task.requires << 'rubocop-on-rbs'
167
+ end
168
+ ```
169
+
170
+ ## Development
171
+
172
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
173
+
174
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
175
+
176
+ ## Contributing
177
+
178
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ksss/rubocop-on-rbs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ksss/rubocop-on-rbs/blob/main/CODE_OF_CONDUCT.md).
179
+
180
+ ## License
181
+
182
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
183
+
184
+ ## Code of Conduct
185
+
186
+ Everyone interacting in the RuboCop on RBS project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ksss/rubocop-on-rbs/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,165 @@
1
+ inherit_mode:
2
+ merge:
3
+ - Exclude
4
+ - Include
5
+
6
+ AllCops:
7
+ Include:
8
+ - '**/*.rbs'
9
+
10
+ Lint:
11
+ Exclude:
12
+ - '**/*.rbs'
13
+
14
+ # RBS
15
+
16
+ RBS:
17
+ Enabled: true
18
+ Include:
19
+ - '**/*.rbs'
20
+
21
+ ## RBS/Layout
22
+
23
+ RBS/Layout:
24
+ Enabled: true
25
+
26
+ RBS/Layout/CommentIndentation:
27
+ Description: 'Use 2 spaces for comment indentation'
28
+ Enabled: true
29
+
30
+ RBS/Layout/EmptyLineAroundOverloads:
31
+ Description: 'No empty line between overload'
32
+ Enabled: true
33
+
34
+ RBS/Layout/EndAlignment:
35
+ Description: 'Align `end` keyword'
36
+ Enabled: true
37
+
38
+ RBS/Layout/ExtraSpacing:
39
+ Description: 'Remove extra spacing'
40
+ Enabled: true
41
+
42
+ RBS/Layout/IndentationWidth:
43
+ Description: 'Use 2 spaces for indentation'
44
+ Enabled: true
45
+
46
+ RBS/Layout/OverloadIndentation:
47
+ Description: 'Indent overload method'
48
+ Enabled: true
49
+
50
+ RBS/Layout/SpaceAroundArrow:
51
+ Description: 'Use space around `->`'
52
+ Enabled: true
53
+
54
+ RBS/Layout/SpaceAroundBraces:
55
+ Description: 'Use space around `{}`'
56
+ Enabled: true
57
+
58
+ RBS/Layout/SpaceBeforeColon:
59
+ Description: 'Use space before `:`'
60
+ Enabled: true
61
+
62
+ RBS/Layout/SpaceBeforeOverload:
63
+ Description: 'Use space before overload method'
64
+ Enabled: true
65
+
66
+ RBS/Layout/TrailingWhitespace:
67
+ Description: 'Remove trailing whitespace'
68
+ Enabled: true
69
+
70
+ ## RBS/Lint
71
+
72
+ RBS/Lint:
73
+ Severity: warning
74
+ Enabled: true
75
+
76
+ RBS/Lint/Syntax:
77
+ Description: 'Check RBS syntax'
78
+ Enabled: true
79
+
80
+ RBS/Lint/TypeParamsArity:
81
+ Description: 'Check type params arity'
82
+ Enabled: true
83
+ Expects:
84
+ # Default expects.
85
+ # You can add expects in .rubocop.yml.
86
+
87
+ ## class/module
88
+ Array: 1
89
+ Enumerable: 1
90
+ Enumerator: 2
91
+ Enumerator::Chain: 1
92
+ Enumerator::Generator: 1
93
+ Enumerator::Lazy: 2
94
+ Enumerator::Product: 1
95
+ FrozenError: 1
96
+ Hash: 2
97
+ KeyError: 2
98
+ NameError: 1
99
+ NoMatchingPatternKeyError: 2
100
+ NoMethodError: 1
101
+ ObjectSpace::WeakKeyMap: 2
102
+ Range: 1
103
+ Set: 1
104
+ Struct: 1
105
+
106
+ ## interface
107
+ Array::_Pattern: 1
108
+ Enumerable::_NotFound: 1
109
+ Gem::_HashLike: 2
110
+ Kernel::_RationalDiv: 1
111
+ Marshal::_Proc: 1
112
+ String::_MatchAgainst: 2
113
+ _Each: 1
114
+ _EachEntry: 1
115
+ _Range: 1
116
+ _ToA: 1
117
+ _ToAry: 1
118
+ _ToH: 2
119
+ _ToHash: 2
120
+
121
+ ## type alias
122
+ array: 1
123
+ hash: 2
124
+ range: 1
125
+
126
+ RBS/Lint/UselessOverloadTypeParams:
127
+ Description: 'Check useless overload type params'
128
+ Enabled: true
129
+
130
+ RBS/Lint/WillSyntaxError:
131
+ Description: 'Check RBS will syntax error'
132
+ Enabled: true
133
+
134
+ ## RBS/Style
135
+
136
+ RBS/Style:
137
+ Enabled: true
138
+
139
+ RBS/Style/BlockReturnBoolish:
140
+ Description: 'Use `bool` for block return type'
141
+ Enabled: true
142
+
143
+ RBS/Style/ClassicType:
144
+ Description: 'Use simple type'
145
+ Enabled: true
146
+
147
+ RBS/Style/DuplicatedType:
148
+ Description: 'Check duplicated type'
149
+ Enabled: true
150
+
151
+ RBS/Style/InitializeReturnType:
152
+ Description: 'Use `void` for initialize method'
153
+ Enabled: true
154
+
155
+ RBS/Style/MergeUntyped:
156
+ Description: 'Merge to `untyped`'
157
+ Enabled: true
158
+
159
+ RBS/Style/OptionalNil:
160
+ Description: 'Use nil instead of nil?'
161
+ Enabled: true
162
+
163
+ RBS/Style/TrueFalse:
164
+ Description: 'Use bool instead of true | false'
165
+ Enabled: true
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RBS
6
+ module Layout
7
+ # @example default
8
+ # # bad
9
+ # # comment here
10
+ # def foo: () -> void
11
+ #
12
+ # # good
13
+ # # comment here
14
+ # def foo: () -> void
15
+ class CommentIndentation < RuboCop::RBS::CopBase
16
+ extend AutoCorrector
17
+
18
+ MSG = "Incorrect indentation detected (column %<expect>s instead of %<actual>s)."
19
+
20
+ def on_rbs_new_investigation
21
+ indent_start_lines = Set.new
22
+ indent_end_lines = Set.new
23
+ processed_rbs_source.decls.each do |decl|
24
+ walk_decl(decl) do |d|
25
+ indent_start_lines << d.location.start_line
26
+ indent_end_lines << d.location.end_line
27
+ end
28
+ end
29
+
30
+ expected_width = 0
31
+ processed_rbs_source.tokens.each do |token|
32
+ case token.type
33
+ when :kMODULE, :kCLASS, :kINTERFACE
34
+ next unless indent_start_lines.include?(token.location.start_line)
35
+
36
+ expected_width += 2
37
+ when :kEND
38
+ next unless indent_end_lines.include?(token.location.start_line)
39
+
40
+ expected_width -= 2
41
+ when :tLINECOMMENT
42
+ if token.location.start_column != expected_width
43
+ token_range = location_to_range(token.location)
44
+ message = format(MSG, expect: expected_width, actual: token.location.start_column)
45
+ add_offense(token_range, message: message) do |corrector|
46
+ line_start_pos = processed_source.buffer.line_range(token.location.start_line).begin_pos
47
+ indent = range_between(line_start_pos, token.location.start_pos)
48
+ corrector.replace(indent, ' ' * expected_width)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def walk_decl(decl, &block)
56
+ if decl.respond_to?(:members)
57
+ yield decl
58
+ decl.members.each { |member| walk_decl(member, &block) }
59
+ end
60
+ end
61
+
62
+ def tokenize(source)
63
+ ::RBS::Parser.lex(source).value
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RBS
6
+ module Layout
7
+ # @example default
8
+ # # bad
9
+ # class Foo
10
+ # def foo: () -> void
11
+ # end
12
+ #
13
+ # # good
14
+ # class Foo
15
+ # def foo: () -> void
16
+ # end
17
+ class EmptyLinesAroundOverloads < RuboCop::RBS::CopBase
18
+ extend AutoCorrector
19
+
20
+ MSG = 'Empty line detected around overloads.'
21
+
22
+ def on_rbs_def(decl)
23
+ return unless 1 < decl.overloads.length
24
+
25
+ decl.overloads.each_cons(2) do |overload, next_overload|
26
+ check_empty_lines(overload, next_overload)
27
+ end
28
+ end
29
+
30
+ def check_empty_lines(overload, next_overload)
31
+ return if overload.method_type.location.end_line + 1 == next_overload.method_type.location.start_line
32
+
33
+ total = 0
34
+ range = overload.method_type.location.end_line...(next_overload.method_type.location.start_line - 1)
35
+ processed_source.raw_source.each_line.each_with_index do |line, lineno|
36
+ if range.cover?(lineno) && line == "\n"
37
+ empty_line = range_between(total, total + 1)
38
+ add_offense(empty_line) do |corrector|
39
+ corrector.remove(empty_line)
40
+ end
41
+ end
42
+ total += line.length
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module RBS
6
+ module Layout
7
+ # @example default
8
+ # # bad
9
+ # class Foo
10
+ # def foo: () -> void
11
+ # end
12
+ #
13
+ # # good
14
+ # class Foo
15
+ # def foo: () -> void
16
+ # end
17
+ class EndAlignment < RuboCop::RBS::CopBase
18
+ extend AutoCorrector
19
+
20
+ def on_rbs_new_investigation
21
+ processed_rbs_source.decls.each do |decl|
22
+ check_indentation(decl, expect: 0)
23
+ end
24
+ end
25
+
26
+ def check_indentation(decl, expect:)
27
+ if decl.location.key?(:end)
28
+ # module, class and interface
29
+ check(decl, expect: expect)
30
+ decl.members.each do |member|
31
+ check_indentation(member, expect: expect + 2)
32
+ end
33
+ end
34
+ end
35
+
36
+ def check(decl, expect:)
37
+ end_loc = decl.location[:end]
38
+ actual = end_loc.start_column
39
+ if actual != expect
40
+ line_start_pos = processed_source.raw_source.rindex(/\R/, end_loc.start_pos) + 1
41
+ range = range_between(end_loc.start_pos, end_loc.end_pos)
42
+ source = decl.location.source.each_line.first.strip
43
+ message = "`end` at #{end_loc.start_line}, #{end_loc.start_column} is not aligned with " \
44
+ "`#{source}` at #{decl.location.start_line}, #{decl.location.start_column}."
45
+ add_offense(range, message: message) do |corrector|
46
+ whitespace = range_between(line_start_pos, end_loc.start_pos)
47
+ corrector.replace(whitespace, ' ' * expect)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end