Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSS Gate Workshop: Tokyo: 2018-10-27: shoy75: rubocop: Work log #982

Closed
shoy75 opened this issue Oct 27, 2018 · 33 comments
Closed

OSS Gate Workshop: Tokyo: 2018-10-27: shoy75: rubocop: Work log #982

shoy75 opened this issue Oct 27, 2018 · 33 comments
Assignees

Comments

@shoy75
Copy link
Contributor

shoy75 commented Oct 27, 2018

This is a work log of a "OSS Gate workshop".
"OSS Gate workshop" is an activity to increase OSS developers.
Here's been discussed in Japanese. Thanks.

作業ログ作成時の説明

以下のテンプレートを埋めてタイトルに設定します。埋め方例はスクロールすると見えてきます。

OSS Gate Workshop: ${LOCATION}: ${YEAR}-${MONTH}-${DAY}: ${ACCOUNT_NAME}: ${OSS_NAME}: Work log

タイトル例↓:

OSS Gate Workshop: Tokyo: 2017-01-16: kou: Rabbit: Work log

OSS Gateワークショップ関連情報

  • スライド:ワークショップの進行に使っているスライドがあります。
  • チャット:OSS開発に関することならなんでも相談できます。ワークショップが終わった後もオンラインで相談しながら継続的にOSSの開発に参加しましょう!
  • シナリオ:ワークショップの目的・内容・進め方の詳細が書いています。
  • 過去のビギナーの作業ログ:他の人の作業ログから学べることがいろいろあるはずです。
@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

ライセンスを確認した

@june29
Copy link
Contributor

june29 commented Oct 27, 2018

サポーターの @june29 です、せいいっぱいがんばります ✨ 💪

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

Githubを見つけた
https://github.com/rubocop-hq/rubocop

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

READMEを読んでみる

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

とりあえずインストールする

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

Rubyスタイルガイドに沿ったルールがデフォルトっぽい
https://github.com/rubocop-hq/ruby-style-guide

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

gitterのリンクとかもREADMEにあっておもしろい

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

インストールする

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

sho@~/ $ gem install rubocop
Fetching: jaro_winkler-1.5.1-x86_64-darwin-17.gem (100%)
Building native extensions. This could take a while...
Successfully installed jaro_winkler-1.5.1-x86_64-darwin-17
Fetching: parallel-1.12.1.gem (100%)
Successfully installed parallel-1.12.1
Fetching: ast-2.4.0.gem (100%)
Successfully installed ast-2.4.0
Fetching: parser-2.5.1.2.gem (100%)
Successfully installed parser-2.5.1.2
Fetching: powerpack-0.1.2.gem (100%)
Successfully installed powerpack-0.1.2
Fetching: rainbow-3.0.0.gem (100%)
Successfully installed rainbow-3.0.0
Fetching: ruby-progressbar-1.10.0.gem (100%)
Successfully installed ruby-progressbar-1.10.0
Fetching: unicode-display_width-1.4.0.gem (100%)
Successfully installed unicode-display_width-1.4.0
Fetching: rubocop-0.60.0.gem (100%)
Successfully installed rubocop-0.60.0
Parsing documentation for jaro_winkler-1.5.1-x86_64-darwin-17
Installing ri documentation for jaro_winkler-1.5.1-x86_64-darwin-17
Parsing documentation for parallel-1.12.1
Installing ri documentation for parallel-1.12.1
Parsing documentation for ast-2.4.0
Installing ri documentation for ast-2.4.0
Parsing documentation for parser-2.5.1.2
Installing ri documentation for parser-2.5.1.2
Parsing documentation for powerpack-0.1.2
Installing ri documentation for powerpack-0.1.2
Parsing documentation for rainbow-3.0.0
Installing ri documentation for rainbow-3.0.0
Parsing documentation for ruby-progressbar-1.10.0
Installing ri documentation for ruby-progressbar-1.10.0
Parsing documentation for unicode-display_width-1.4.0
Installing ri documentation for unicode-display_width-1.4.0
Parsing documentation for rubocop-0.60.0
Installing ri documentation for rubocop-0.60.0
Done installing documentation for jaro_winkler, parallel, ast, parser, powerpack, rainbow, ruby-progressbar, unicode-display_width, rubocop after 22 seconds
9 gems installed
sho@~/ $

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

Quickstartしてみる

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

capistranoをダウンロードしてrubocopを動かしてみた

sho@capistrano/ master$ rubocop
.rubocop.yml: Style/FileName has the wrong namespace - should be Naming
.rubocop.yml: Style/IndentHeredoc has the wrong namespace - should be Layout
.rubocop.yml: Style/SpaceAroundEqualsInParameterDefault has the wrong namespace - should be Layout
.rubocop.yml: Style/PredicateName has the wrong namespace - should be Naming
.rubocop.yml: Style/AccessorMethodName has the wrong namespace - should be Naming
Error: Unsupported Ruby version 2.0 found in `TargetRubyVersion` parameter (in .rubocop.yml). 2.0-compatible analysis was dropped after version 0.50.
Supported versions: 2.2, 2.3, 2.4, 2.5, 2.6
sho@capistrano/ master$

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

tips

sho@capistrano/ master$ g grep -A10 'Style/FileName'
.rubocop.yml:Style/FileName:
.rubocop.yml-  Exclude:
.rubocop.yml-    - "Dangerfile"
.rubocop.yml-Style/IndentHeredoc:
.rubocop.yml-  Enabled: false
.rubocop.yml-Style/SpaceAroundEqualsInParameterDefault:
.rubocop.yml-  EnforcedStyle: no_space
.rubocop.yml-Style/StringLiterals:
.rubocop.yml-  EnforcedStyle: double_quotes
.rubocop.yml-Style/TrivialAccessors:
.rubocop.yml-  AllowPredicates: true
sho@capistrano/ master$ g grep -B10 'Style/FileName'
.rubocop.yml-Metrics/BlockLength:
.rubocop.yml-  Exclude:
.rubocop.yml-    - "spec/**/*"
.rubocop.yml-    - "lib/**/*.rake"
.rubocop.yml-Style/BarePercentLiterals:
.rubocop.yml-  EnforcedStyle: percent_q
.rubocop.yml-Style/ClassAndModuleChildren:
.rubocop.yml-  Enabled: false
.rubocop.yml-Style/DoubleNegation:
.rubocop.yml-  Enabled: false
.rubocop.yml:Style/FileName:
sho@capistrano/ master$ g grep -C5 'Style/FileName'
.rubocop.yml-  EnforcedStyle: percent_q
.rubocop.yml-Style/ClassAndModuleChildren:
.rubocop.yml-  Enabled: false
.rubocop.yml-Style/DoubleNegation:
.rubocop.yml-  Enabled: false
.rubocop.yml:Style/FileName:
.rubocop.yml-  Exclude:
.rubocop.yml-    - "Dangerfile"
.rubocop.yml-Style/IndentHeredoc:
.rubocop.yml-  Enabled: false
.rubocop.yml-Style/SpaceAroundEqualsInParameterDefault:

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

Official manualがある
https://docs.rubocop.org/en/latest/

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

コントリビューションのガイドライン見てみる
https://github.com/rubocop-hq/rubocop/blob/master/CONTRIBUTING.md

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

バージョン確認方法ものってた

sho@capistrano/ master *$ rubocop -V
0.60.0 (using Parser 2.5.1.2, running on ruby 2.5.3 x86_64-darwin17)
sho@capistrano/ master *$

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

プルリク出すときちゃんと読む
https://github.com/rubocop-hq/rubocop/blob/master/CONTRIBUTING.md#pull-requests

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

金じゃない話をしにきた

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

チェンジログ見てみる
https://github.com/rubocop-hq/rubocop/blob/master/CHANGELOG.md

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

READMEを読み終わった!
rubocopに限らず今までREADMEをきちんと読み込んでおらず申し訳ない気持ちでいっぱいになりました。

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

Issueを見てみる!
https://github.com/rubocop-hq/rubocop/issues

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

なんとなくいけそうだけど、、
rubocop/rubocop#6406 (comment)

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

再現してみる

# test.rb
puts <<~EOF
aaaa
EOF
$ rubocop -a test.rb
Inspecting 1 file
C

Offenses:

test.rb:1:6: C: [Corrected] Layout/SpaceAroundOperators: Surrounding space missing for operator <<.
puts <<~EOF
     ^^

1 file inspected, 1 offense detected, 1 offense corrected
puts << ~EOF
aaaa
EOF

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

#982 (comment)
対応するrubocopのバージョンが違う可能性があるよ、というメッセージを出すのはどうでしょう?
という提案もできるのではというアドバイスをいただいた!

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

Layout/SpaceAroundOperators というチェックをしているところがよろしくないのかな

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

rubocopをローカルでbundle installしてbinding.irbとかで修正箇所を特定するのが良さそう

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

sho@oss_gate_robocop/ $ bundle exec rubocop
Inspecting 2 files
CC

Offenses:

test.rb:1:6: C: Layout/SpaceAroundOperators: Surrounding space missing for operator <<.
puts <<~EOF
     ^^

<< をヒアドキュメントではなく、演算子として扱っているようだ

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

puts <<FIRST
  aaaa
FIRST
puts <<-SECOND
  bbbb
SECOND
puts <<~THIRD
  cccc
THIRD
sho@oss_gate_robocop/ $ bundle exec rubocop
Inspecting 2 files
CC

Offenses:

test.rb:7:6: C: Layout/SpaceAroundOperators: Surrounding space missing for operator <<.
puts <<~THIRD
     ^^
test.rb:8:3: C: Layout/IndentationConsistency: Inconsistent indentation detected.
  cccc
  ^^^^

<<- は問題ないが、 <<~ の場合のみ怒られる

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

エラーメッセージを生成しているところで binding.irb で止めてみる

    130:         def offense_message(operator, with_space, right_operand)
    131:           if operator.is?('**')
    132:             'Space around operator `**` detected.' unless with_space.is?('**')
    133:           elsif with_space.source !~ /^\s.*\s$/
 => 134:             binding.irb
    135:             "Surrounding space missing for operator `#{operator.source}`."

with_space を調べてみる

irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007faa8cfd42e0>):002:0> with_space
=> #<Parser::Source::Range /Users/sho/work/oss_gate_robocop/test.rb 59...62>
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007faa8cfd42e0>):003:0> with_space.to_s
=> "/Users/sho/work/oss_gate_robocop/test.rb:7:5"
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007faa8cfd42e0>):004:0> with_space.source
=> " <<"

with_space.source <<~ でないのが問題かもしれない

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

<<- の場合 with_space.source がどうなっているのか確認してみる

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

sho@oss_gate_robocop/ $ bundle exec rubocop
Inspecting 2 files

From: /Users/sho/work/oss_gate_robocop/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/layout/space_around_operators.rb @ line 118 :

    113:             !IRREGULAR_METHODS.include?(send_node.method_name)
    114:         end
    115:
    116:         def check_operator(operator, right_operand)
    117:           with_space = range_with_surrounding_space(range: operator)
 => 118:           binding.irb
    119:           return if with_space.source.start_with?("\n")
    120:
    121:           offense(operator, with_space, right_operand) do |msg|
    122:             add_offense(with_space, location: operator, message: msg)
    123:           end

irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007fc733d34288>):001:0> with_space.source
=> " <<"
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007fc733d34288>):002:0> operator
=> #<Parser::Source::Range /Users/sho/work/oss_gate_robocop/test.rb 60...62>
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007fc733d34288>):003:0> operator.to_s
=> "/Users/sho/work/oss_gate_robocop/test.rb:7:6"
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007fc733d34288>):004:0> right_operand
=> #<Parser::Source::Range /Users/sho/work/oss_gate_robocop/test.rb 62...68>
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007fc733d34288>):005:0> right_operand.source
=> "~THIRD"
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007fc733d34288>):006:0> operator.source
=> "<<"

止めるとこ違ったけど、 right_operand.source~ が入ってしまっている

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018


irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):001:0> node
=> s(:send, nil, :puts,
  s(:str, "  bbbb\n"))
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):002:0> quit

From: /Users/sho/work/oss_gate_robocop/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/layout/space_around_operators.rb @ line 59 :

    54:
    55:           check_operator(node.loc.assoc, variable.source_range)
    56:         end
    57:
    58:         def on_send(node)
 => 59:           binding.irb
    60:           if node.setter_method?
    61:             on_special_asgn(node)
    62:           elsif regular_operator?(node)
    63:             check_operator(node.loc.selector, node.first_argument.source_range)
    64:           end

irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):001:0> node
=> s(:send,
  s(:send, nil, :puts), :<<,
  s(:send,
    s(:const, nil, :THIRD), :~))
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):002:0> node.first_argument.source_range
=> #<Parser::Source::Range /Users/sho/work/oss_gate_robocop/test.rb 62...68>
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):003:0> node.first_argument.source_range.source
=> "~THIRD"
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):004:0> node.loc.selector.source
=> "<<"
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):005:0> node.setter_method?
=> nil
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):006:0> regular_operator?(node)
=> true
irb(#<RuboCop::Cop::Layout::SpaceAroundOperators:0x00007f86f8a39ae0>):007:0>

@shoy75
Copy link
Contributor Author

shoy75 commented Oct 27, 2018

sho@oss_gate_robocop/ $ bundle exec rubocop
Inspecting 2 files

From: /Users/sho/work/oss_gate_robocop/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/layout/indent_heredoc.rb @ line 90 :

    85:           active_support: 'strip_heredoc',
    86:           powerpack: 'strip_indent'
    87:         }.freeze
    88:
    89:         def on_heredoc(node)
 => 90:           binding.irb
    91:           body = heredoc_body(node)
    92:           return if body.strip.empty?
    93:
    94:           body_indent_level = indent_level(body)
    95:

irb(#<RuboCop::Cop::Layout::IndentHeredoc:0x00007fb9d6ced960>):001:0> node
=> s(:str, "  aaaa\n")
irb(#<RuboCop::Cop::Layout::IndentHeredoc:0x00007fb9d6ced960>):002:0> heredoc_body(node)
=> "  aaaa\n"
irb(#<RuboCop::Cop::Layout::IndentHeredoc:0x00007fb9d6ced960>):003:0> node.source
=> "<<FIRST"
irb(#<RuboCop::Cop::Layout::IndentHeredoc:0x00007fb9d6ced960>):004:0> quit

From: /Users/sho/work/oss_gate_robocop/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/layout/indent_heredoc.rb @ line 90 :

    85:           active_support: 'strip_heredoc',
    86:           powerpack: 'strip_indent'
    87:         }.freeze
    88:
    89:         def on_heredoc(node)
 => 90:           binding.irb
    91:           body = heredoc_body(node)
    92:           return if body.strip.empty?
    93:
    94:           body_indent_level = indent_level(body)
    95:

irb(#<RuboCop::Cop::Layout::IndentHeredoc:0x00007fb9d6ced960>):001:0> node
=> s(:str, "  bbbb\n")
irb(#<RuboCop::Cop::Layout::IndentHeredoc:0x00007fb9d6ced960>):003:0> node.loc
=> #<Parser::Source::Map::Heredoc:0x00007fb9d6dc7188 @heredoc_body=#<Parser::Source::Range /Users/sho/work/oss_gate_robocop/test.rb 41...48>, @heredoc_end=#<Parser::Source::Range /Users/sho/work/oss_gate_robocop/test.rb 48...54>, @expression=#<Parser::Source::Range /Users/sho/work/oss_gate_robocop/test.rb 31...40>, @node=s(:str, "  bbbb\n")>
irb(#<RuboCop::Cop::Layout::IndentHeredoc:0x00007fb9d6ced960>):004:0> node.source
=> "<<-SECOND"
irb(#<RuboCop::Cop::Layout::IndentHeredoc:0x00007fb9d6ced960>):005:0> quit
CC

Offenses:

test.rb:7:6: C: Layout/SpaceAroundOperators: Surrounding space missing for operator <<.
puts <<~THIRD
     ^^
test.rb:8:3: C: Layout/IndentationConsistency: Inconsistent indentation detected.
  cccc
  ^^^^
Gemfile:1:8: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
source "https://rubygems.org"
       ^^^^^^^^^^^^^^^^^^^^^^
Gemfile:3:5: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
gem "rubocop"
    ^^^^^^^^^

2 files inspected, 4 offenses detected
sho@oss_gate_robocop/ $

<<-SECOND はheredocとみなされているが、 <<~THIRD はみなされていなさそう

@oss-gate-issue-cleaner
Copy link

おつかれさまでした!

後日引き続きワークショップがある場合

後日のワークショップで再開する際に、issueタイトルの日付を更新してreopenしてください!

ワークショップ終了後に続きがしたい場合

issueはクローズしますが、その後も作業を続けていただいて大丈夫です!

是非このissueを活用なさってください。

今後のイベントの案内

現在以下のイベントの開催を予定しています。

参加をお待ちしてます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants