rubocop-sensei 0.1.5 → 0.1.7

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: 5479d9e29fc19485cd4debdeb0f6441dc444003ed7f2472227abd0e4cd95680d
4
- data.tar.gz: 524580de696dc8e3c808f683c92414f083b108c06e1e5a8ae393e12d5bca6a6a
3
+ metadata.gz: c13f70806132906347c4d5e1f38a1f173635fe7c7d799a3734bce52e2f6ed9ea
4
+ data.tar.gz: 6a56681e15b4aa00effdebd37452606022d79ba1766d01c36dbba8ed2b426056
5
5
  SHA512:
6
- metadata.gz: ed56878a610c3dcfd5457d1ed94303dc002dfcc95fdd699ed34ec16a732f666b87496f27ad316386550865f362f17dfb9248cf0ac72b8e4e66ce47e303555897
7
- data.tar.gz: 379348550eb3a117028c506a435410d707feeee23c3150827afb98ba240ae78cff9146d36be98a1eebe8d3badc54da5c1ea98f376dfa70214970eee0ff00695f
6
+ metadata.gz: f91f6c01b096d5dbf77474ddb3e5e8fc2c53f535df88dff4440b04272f8094cac459733ab6050b48e0fa19d4ff4a09c7fdd5ab6aee700ceec6f9fb7ec39be544
7
+ data.tar.gz: 32ed8ab0037586822119e8e5c9ba5cc36f233c9808c5e433826b856155de77db93dc1d180d4c7f9cbba6221142dea4f16d5966be7525fa3a0e46b30a69a51ef9
data/.rubocop.yml CHANGED
@@ -6,3 +6,9 @@ AllCops:
6
6
  Naming/FileName:
7
7
  Exclude:
8
8
  - lib/rubocop-sensei.rb
9
+
10
+ require:
11
+ - rubocop-sensei
12
+
13
+ Lecture:
14
+ Enabled: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,40 @@
1
+ # Change log
2
+
3
+ ## 0.1.7 (2023-10-25)
4
+
5
+ - Lecture/ExplainBangBang
6
+ - Lecture/ExplainOrAssign
7
+ - Lecture/ExplainLonelyOperator
8
+
9
+ ## 0.1.6 (2023-07-18)
10
+
11
+ - Lecture/ExplainPatternMatchIn
12
+ - Lecture/ExplainPatternMatchRocket
13
+
14
+ ## 0.1.5 (2023-07-15)
15
+
16
+ - Lecture/ExplainPatternMatchCaseIn
17
+ - Lecture/ExplainNumberedParameters
18
+
19
+ ## 0.1.4 (2023-01-21)
20
+
21
+ - Lecture/PreferMap
22
+ - Lecture/PreferSymbolToProc
23
+ - Lecture/ExplainSymbolToProc
24
+
25
+ ## 0.1.3 (2023-01-05)
26
+
27
+ - Lecture/UnlessElse
28
+
29
+ ## 0.1.2 (2023-01-02)
30
+
31
+ - bug fix
32
+
33
+ ## 0.1.1 (2022-12-31)
34
+
35
+ - bug fix
36
+
37
+ ## 0.1.0 (2022-12-30)
38
+
39
+ - Initial Release
40
+ - Lecture/ReplaceElsif
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-sensei (0.1.5)
4
+ rubocop-sensei (0.1.7)
5
5
  rubocop
6
6
  typeprof
7
7
 
@@ -9,6 +9,7 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  ast (2.4.2)
12
+ base64 (0.1.1)
12
13
  debug (1.8.0)
13
14
  irb (>= 1.5.0)
14
15
  reline (>= 0.3.1)
@@ -20,17 +21,17 @@ GEM
20
21
  language_server-protocol (3.17.0.3)
21
22
  minitest (5.18.1)
22
23
  parallel (1.23.0)
23
- parser (3.2.2.3)
24
+ parser (3.2.2.4)
24
25
  ast (~> 2.4.1)
25
26
  racc
26
27
  racc (1.7.1)
27
28
  rainbow (3.1.1)
28
29
  rake (13.0.6)
29
- rbs (3.1.0)
30
- regexp_parser (2.8.1)
30
+ rbs (3.2.2)
31
+ regexp_parser (2.8.2)
31
32
  reline (0.3.6)
32
33
  io-console (~> 0.5)
33
- rexml (3.2.5)
34
+ rexml (3.2.6)
34
35
  rspec (3.12.0)
35
36
  rspec-core (~> 3.12.0)
36
37
  rspec-expectations (~> 3.12.0)
@@ -44,23 +45,24 @@ GEM
44
45
  diff-lcs (>= 1.2.0, < 2.0)
45
46
  rspec-support (~> 3.12.0)
46
47
  rspec-support (3.12.1)
47
- rubocop (1.54.2)
48
+ rubocop (1.57.1)
49
+ base64 (~> 0.1.1)
48
50
  json (~> 2.3)
49
51
  language_server-protocol (>= 3.17.0)
50
52
  parallel (~> 1.10)
51
- parser (>= 3.2.2.3)
53
+ parser (>= 3.2.2.4)
52
54
  rainbow (>= 2.2.2, < 4.0)
53
55
  regexp_parser (>= 1.8, < 3.0)
54
56
  rexml (>= 3.2.5, < 4.0)
55
- rubocop-ast (>= 1.28.0, < 2.0)
57
+ rubocop-ast (>= 1.28.1, < 2.0)
56
58
  ruby-progressbar (~> 1.7)
57
59
  unicode-display_width (>= 2.4.0, < 3.0)
58
60
  rubocop-ast (1.29.0)
59
61
  parser (>= 3.2.1.0)
60
62
  ruby-progressbar (1.13.0)
61
- typeprof (0.21.7)
63
+ typeprof (0.21.8)
62
64
  rbs (>= 1.8.1)
63
- unicode-display_width (2.4.2)
65
+ unicode-display_width (2.5.0)
64
66
 
65
67
  PLATFORMS
66
68
  arm64-darwin-21
data/README.md CHANGED
@@ -5,8 +5,13 @@
5
5
  - rubocopコマンドで良い書き方のアドバイスが表示されます
6
6
  - RuboCop に標準で入っているLSPをつかうとエディタ上で書いたコードのそばに表示できます
7
7
 
8
+ ![demo](demo.png)
8
9
  ![demo](demo.gif)
9
10
 
11
+ # 実装されているアドバイス
12
+
13
+ - [config/default.yml](config/default.yml)
14
+
10
15
  # つかい方
11
16
 
12
17
  ## インストール
data/config/default.yml CHANGED
@@ -1,34 +1,59 @@
1
- Lecture/ExplainPatternMatchCaseIn:
2
- Description: 'Explain pattern match case in'
1
+ Lecture/ExplainBangBang:
2
+ Description: "!!variable の説明"
3
+ Enabled: true
4
+ VersionAdded: "0.1.7"
5
+
6
+ Lecture/ExplainLonelyOperator:
7
+ Description: "&. の説明"
3
8
  Enabled: true
4
- VersionAdded: '0.1.5'
9
+ VersionAdded: "0.1.7"
5
10
 
6
11
  Lecture/ExplainNumberedParameters:
7
- Description: 'Explain numbered parameters'
12
+ Description: "ナンバーパラメータの説明"
8
13
  Enabled: true
9
- VersionAdded: '0.1.5'
14
+ VersionAdded: "0.1.5"
10
15
 
11
- Lecture/ExplainSymbolToProc :
12
- Description: 'Explain receiver.method(&:method)'
16
+ Lecture/ExplainOrAssign:
17
+ Description: "||= の説明"
13
18
  Enabled: true
14
- VersionAdded: '0.1.4'
19
+ VersionAdded: "0.1.7"
15
20
 
16
- Lecture/PreferSymbolToProc:
17
- Description: 'might be replaced with method(&:method)'
21
+ Lecture/ExplainPatternMatchCaseIn:
22
+ Description: "パターンマッチ case in の説明"
23
+ Enabled: true
24
+ VersionAdded: "0.1.5"
25
+
26
+ Lecture/ExplainPatternMatchIn:
27
+ Description: "1行パターンマッチ in の説明"
18
28
  Enabled: true
19
- VersionAdded: '0.1.4'
29
+ VersionAdded: "0.1.6"
30
+
31
+ Lecture/ExplainPatternMatchRocket:
32
+ Description: "1行パターンマッチ => の説明"
33
+ Enabled: true
34
+ VersionAdded: "0.1.6"
35
+
36
+ Lecture/ExplainSymbolToProc :
37
+ Description: "receiver.method(&:method) の説明"
38
+ Enabled: true
39
+ VersionAdded: "0.1.4"
20
40
 
21
41
  Lecture/PreferMap:
22
- Description: '`each` can be replaced with `map`'
42
+ Description: "この each メソッドは map メソッドで書き換えられるかもしれません"
43
+ Enabled: true
44
+ VersionAdded: "0.1.4"
45
+
46
+ Lecture/PreferSymbolToProc:
47
+ Description: "method(&:method) で書き換えられるかもしれません"
23
48
  Enabled: true
24
- VersionAdded: '0.1.3'
49
+ VersionAdded: "0.1.4"
25
50
 
26
51
  Lecture/ReplaceElsif:
27
- Description: "`elsif` can be replaced with `case`"
52
+ Description: "elsif case で書き換えよう"
28
53
  Enabled: true
29
54
  VersionAdded: "0.1.0"
30
55
 
31
56
  Lecture/UnlessElse:
32
- Description: "Do not use `unless` with `else`. Rewrite these with the positive case first."
57
+ Description: "unless else はやめて if に書き換えよう"
33
58
  Enabled: true
34
59
  VersionAdded: "0.1.3"
data/demo.png ADDED
Binary file
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lecture
6
+ # `!!`はオブジェクトをtrueまたはfalseに変換する書き方です。
7
+ # 真偽反転の!が2回つかわれていると解釈すれば意味を取りやすいです。
8
+ # nilまたはfalseのときはfalseへ、それ以外はtrueへ変換します。
9
+ # !!は必ずtrueまたはfalseに変換するので、任意のオブジェクトやnilをメソッドから返したくないときによくつかわれます。
10
+ # メソッド名の末尾が?で終わるメソッドはtrue/falseを返す慣習になっているので、!!が便利につかえる場面が多いです。
11
+ # 一方で、ifで判定するときなど、メソッドの戻り値としてつかわないときは!!をつけずにそのままつかうことが多いです。
12
+ #
13
+ # @example
14
+ # # good
15
+ # foo = 1
16
+ # !!foo
17
+ #
18
+ class ExplainBangBang < Base
19
+ MSG = <<~STRING
20
+ `!!`はオブジェクトをtrueまたはfalseに変換する書き方です。
21
+ 真偽反転の!が2回つかわれていると解釈すれば意味を取りやすいです。
22
+ nilまたはfalseのときはfalseへ、それ以外はtrueへ変換します。
23
+
24
+ !!は必ずtrueまたはfalseに変換するので、任意のオブジェクトやnilをメソッドから返したくないときによくつかわれます。
25
+ メソッド名の末尾が?で終わるメソッドはtrue/falseを返す慣習になっているので、!!が便利につかえる場面が多いです。
26
+
27
+ 一方で、ifで判定するときなど、メソッドの戻り値としてつかわないときは!!をつけずにそのままつかうことが多いです。
28
+ STRING
29
+
30
+ RESTRICT_ON_SEND = %i[!].freeze
31
+
32
+ def on_send(node)
33
+ if node.send_type? && node.method_name == :!
34
+ if node.children.any?{|n| n.respond_to?(:send_type?) && n.send_type? && n.method_name == :! }
35
+ add_offense(node)
36
+ return
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lecture
6
+ # `&.`はレシーバのnilチェックをしてメソッド呼び出しをする演算子です。
7
+ # foo&.methodでは、fooがnilのときはメソッド呼び出しをせずnilを返し、fooがnil以外ではmethodメソッドを呼び出します。
8
+ #
9
+ # &.は通称「ぼっち演算子」とも呼ばれます。人が1人でひざを抱えて座っているように見えるからです。
10
+ #
11
+ # @example
12
+ # # good
13
+ # foo = nil
14
+ # foo&.upcase #=> nil
15
+ #
16
+ # # good
17
+ # foo = "ruby"
18
+ # foo&.upcase #=> "RUBY"
19
+ #
20
+ class ExplainLonelyOperator < Base
21
+ MSG = <<~STRING
22
+ `&.`はレシーバのnilチェックをしてメソッド呼び出しをする演算子です。
23
+ foo&.methodでは、fooがnilのときはメソッド呼び出しをせずnilを返し、fooがnil以外ではmethodメソッドを呼び出します。
24
+
25
+ &.は通称「ぼっち演算子」とも呼ばれます。人が1人でひざを抱えて座っているように見えるからです。
26
+ STRING
27
+
28
+ def on_csend(node)
29
+ add_offense(node)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lecture
6
+ # `foo ||= 1` は変数fooがnilまたはfalseのときに変数fooへ1を代入し、それ以外のときは何もしません。
7
+ # 変数 ||= 初期値 という形で初期化時によくつかわれます。
8
+ #
9
+ # `foo ||= 1` は `foo = foo || 1` と同じです。
10
+ # || は最初の項がnilまたはfalseのときだけ2項目を戻り値にして、それ以外のときは最初の項を戻り値にします。
11
+ #
12
+ # @example
13
+ # # good
14
+ # foo = 555
15
+ # foo ||= 1
16
+ # p foo #=> 555
17
+ #
18
+ # foo = nil
19
+ # foo ||= 1
20
+ # p foo #=> 1
21
+ #
22
+ # # bad
23
+ # foo = false
24
+ # foo ||= true
25
+ # p foo #=> true # 既に代入済みのfalseが上書きされてしまった
26
+ #
27
+ class ExplainOrAssign < Base
28
+ MSG = <<~STRING
29
+ `foo ||= 1` は変数fooがnilまたはfalseのときに変数fooへ1を代入し、それ以外のときは何もしません。
30
+ 変数 ||= 初期値 という形で初期化時によくつかわれます。
31
+
32
+ `foo ||= 1` は `foo = foo || 1` と同じです。
33
+ || は最初の項がnilまたはfalseのときだけ2項目を戻り値にして、それ以外のときは最初の項を戻り値にします。
34
+
35
+ ざっくりと
36
+ 「変数に何か入っていたらそのまま、何も入っていなかったら||=の右に書かれたオブジェクトを初期値としてつかう」
37
+ と考えると覚えやすいかもしれません。
38
+
39
+ ただし、falseやnilを初期値につかうときは気をつけてください。
40
+ 事前に意図的にfalseやnilが代入されているときにも代入されるので、意図しない動作になるかもしれません。
41
+ STRING
42
+
43
+ def on_or_asgn(node)
44
+ add_offense(node)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lecture
6
+ # 1行パターンマッチです。
7
+ # [1,2,3] in [x,y,3]
8
+ # p x #=> 1
9
+ # p y #=> 2
10
+ # inの右側には変数入りのパターンを書くことができ、左側と対応させ、それぞれの変数に代入します。
11
+ #
12
+ # @example
13
+ # # good
14
+ # 5 => x
15
+ # p x #=> 5
16
+ #
17
+ # # good
18
+ # [1,2,3] in [x,y,3]
19
+ # p x #=> 1
20
+ # p y #=> 2
21
+ #
22
+ class ExplainPatternMatchIn < Base
23
+ MSG = <<~STRING
24
+ 1行パターンマッチです。
25
+ inの右側には変数入りのパターンを書くことができ、左側と対応させ、それぞれの変数に代入します。
26
+
27
+ [1,2,3] in [x,y,3]
28
+ p x #=> 1
29
+ p y #=> 2
30
+
31
+ 5 in x
32
+ p x #=> 5
33
+ STRING
34
+
35
+ def on_match_pattern_p(node)
36
+ add_offense(node)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lecture
6
+ # 1行パターンマッチです。
7
+ # [1,2,3] => [x,y,3]
8
+ # p x #=> 1
9
+ # p y #=> 2
10
+ # =>の右側には変数入りのパターンを書くことができ、左側と対応させ、それぞれの変数に代入します。
11
+ #
12
+ # @example
13
+ # # good
14
+ # 5 => x
15
+ # p x #=> 5
16
+ #
17
+ # # good
18
+ # [1,2,3] => [x,y,3]
19
+ # p x #=> 1
20
+ # p y #=> 2
21
+ #
22
+ class ExplainPatternMatchRocket < Base
23
+ MSG = <<~STRING
24
+ 1行パターンマッチです。
25
+ =>の右側には変数入りのパターンを書くことができ、左側と対応させ、それぞれの変数に代入します。
26
+
27
+ [1,2,3] => [x,y,3]
28
+ p x #=> 1
29
+ p y #=> 2
30
+
31
+ 5 => x
32
+ p x #=> 5
33
+ STRING
34
+
35
+ def on_match_pattern(node)
36
+ add_offense(node)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,9 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'lecture/explain_bang_bang'
4
+ require_relative 'lecture/explain_lonely_operator'
3
5
  require_relative 'lecture/explain_numbered_parameters'
6
+ require_relative 'lecture/explain_or_assign'
4
7
  require_relative 'lecture/explain_pattern_match_case_in'
5
- require_relative 'lecture/prefer_symbol_to_proc'
8
+ require_relative 'lecture/explain_pattern_match_in'
9
+ require_relative 'lecture/explain_pattern_match_rocket'
6
10
  require_relative 'lecture/explain_symbol_to_proc'
11
+ require_relative 'lecture/prefer_symbol_to_proc'
7
12
  require_relative 'lecture/prefer_map'
8
13
  require_relative 'lecture/replace_elsif'
9
14
  require_relative 'lecture/unless_else'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Sensei
5
- VERSION = "0.1.5"
5
+ VERSION = "0.1.7"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-sensei
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuniaki Igarashi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-15 00:00:00.000000000 Z
11
+ date: 2023-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -47,17 +47,23 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ".rspec"
49
49
  - ".rubocop.yml"
50
+ - CHANGELOG.md
50
51
  - Gemfile
51
52
  - Gemfile.lock
52
- - 'LICENSE '
53
53
  - LICENSE.txt
54
54
  - README.md
55
55
  - Rakefile
56
56
  - config/default.yml
57
57
  - demo.gif
58
+ - demo.png
58
59
  - lib/rubocop-sensei.rb
60
+ - lib/rubocop/cop/lecture/explain_bang_bang.rb
61
+ - lib/rubocop/cop/lecture/explain_lonely_operator.rb
59
62
  - lib/rubocop/cop/lecture/explain_numbered_parameters.rb
63
+ - lib/rubocop/cop/lecture/explain_or_assign.rb
60
64
  - lib/rubocop/cop/lecture/explain_pattern_match_case_in.rb
65
+ - lib/rubocop/cop/lecture/explain_pattern_match_in.rb
66
+ - lib/rubocop/cop/lecture/explain_pattern_match_rocket.rb
61
67
  - lib/rubocop/cop/lecture/explain_symbol_to_proc.rb
62
68
  - lib/rubocop/cop/lecture/prefer_map.rb
63
69
  - lib/rubocop/cop/lecture/prefer_symbol_to_proc.rb
@@ -93,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
99
  - !ruby/object:Gem::Version
94
100
  version: '0'
95
101
  requirements: []
96
- rubygems_version: 3.4.16
102
+ rubygems_version: 3.4.20
97
103
  signing_key:
98
104
  specification_version: 4
99
105
  summary: Rubocop Sensei
data/LICENSE DELETED
@@ -1,9 +0,0 @@
1
- MIT License
2
-
3
- Copyright 2022 Kuniaki Igarashi
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.