Slide 1

Slide 1 text

Hello!

Slide 2

Slide 2 text

Божидар

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Bozhidar

Slide 5

Slide 5 text

Божo

Slide 6

Slide 6 text

Bozho cool

Slide 7

Slide 7 text

Bozo not cool

Slide 8

Slide 8 text

Bozho is not a bozo!

Slide 9

Slide 9 text

Bozho is not a bozo! (at least he claims so)

Slide 10

Slide 10 text

Bug cool

Slide 11

Slide 11 text

(The RuboCop Guy)

Slide 12

Slide 12 text

Sofia, Bulgaria Sofia, Bulgaria

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Grigor Dimitrov vs Kei Nishikori

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Lukanka

Slide 17

Slide 17 text

Bulgarian Cheese

Slide 18

Slide 18 text

Rakia Connecting People

Slide 19

Slide 19 text

Shopska Salad (use only with Rakia)

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

bbatsov

Slide 22

Slide 22 text

Ruby & Rails style guides

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

First time in Japan!!!

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

All About RuboCop by Bozhidar Batsov (a.k.a. bug)

Slide 35

Slide 35 text

Writing Lint for Ruby by Masataka Kuwabara (a.k.a. Pocke) http://rubykaigi.org/2017/presentations/p_ck_.html

Slide 36

Slide 36 text

Improve Ruby coding style rules and Lint by Koichi Ito http://rubykaigi.org/2018/presentations/koic.html#jun01

Slide 37

Slide 37 text

Something About RuboCop by Bozhidar Batsov (a.k.a. bug)

Slide 38

Slide 38 text

RuboCop in a Nutshell

Slide 39

Slide 39 text

A Ruby static code analysis tool aimed to enforce the Ruby Community Style Guide

Slide 40

Slide 40 text

In other words…

Slide 41

Slide 41 text

It keeps your codebase consistent

Slide 42

Slide 42 text

It saves you time

Slide 43

Slide 43 text

It advances the Ruby language forward

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

32,608,128

Slide 46

Slide 46 text

Provides an efficient way for codebases to be updated

Slide 47

Slide 47 text

Disclaimer

Slide 48

Slide 48 text

Lint tools are not a replacement for common sense

Slide 49

Slide 49 text

Why an entire talk about a mere lint tool?

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Notable Japanese Contributors • Yuji Nakayama (@yujinakayama) • Masataka Kuwabara (@pocke) • Koichi Ito (@koic) • Yukihiro "Matz" Matsumoto (@matz) ;-)

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

https://github.com/rubocop-jp/issues

Slide 55

Slide 55 text

https://github.com/fortissimo1997/ruby-style-guide

Slide 56

Slide 56 text

314 open issues

Slide 57

Slide 57 text

100 volunteers

Slide 58

Slide 58 text

3.14 issues/person

Slide 59

Slide 59 text

Coincidence?

Slide 60

Slide 60 text

Or providence?

Slide 61

Slide 61 text

A Brief History of Time

Slide 62

Slide 62 text

A Brief History of RuboCop

Slide 63

Slide 63 text

2011 The Ruby Style Guide

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

May, 2012 RuboCop 0.0.0

Slide 66

Slide 66 text

May, 2012 RuboCop 0.0.0

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Static code analysis with regular expressions …

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Nov, 2012 Along Came Jonas

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Spring, 2013 Return of the Jedi

Slide 74

Slide 74 text

Using a proper parser!

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Struggles with Ripper

Slide 77

Slide 77 text

Ripper supported only MRI

Slide 78

Slide 78 text

I wanted to build a cross-platform tool

Slide 79

Slide 79 text

Ripper had (has?) almost no documentation

Slide 80

Slide 80 text

Ripper is tied to the underlying Ruby runtime

Slide 81

Slide 81 text

…which means you can’t parse code as Ruby 2.3 while running Ruby 2.5

Slide 82

Slide 82 text

Ripper has a very hard to work with AST representation

Slide 83

Slide 83 text

pry(main)> Ripper.sexp('alias :some :test') => [:program, [[:alias, [:symbol_literal, [:symbol, [:@ident, "some", [1, 7]]]], [:symbol_literal, [:symbol, [:@ident, "test", [1, 13]]]]]]]

Slide 84

Slide 84 text

each(:method_add_arg, sexp) do |s| next if s[1][0] != :call receiver = s[1][1][1] method_name = s[1][3][1] if receiver && receiver[1] == 'Array' && method_name == 'new' && s[2] == [:arg_paren, nil] offences.delete(Offence.new(:convention, receiver[2].lineno, ERROR_MESSAGE)) add_offence(:convention, receiver[2].lineno, ERROR_MESSAGE) end end

Slide 85

Slide 85 text

And many quirks…

Slide 86

Slide 86 text

pry(main)> Ripper.lex(':one') => [[[1, 0], :on_symbeg, ":"], [[1, 1], :on_ident, "one"]] pry(main)> Ripper.lex(':alias') => [[[1, 0], :on_symbeg, ":"], [[1, 1], :on_kw, "alias"]]

Slide 87

Slide 87 text

pry(main)> Ripper.lex('def alias(arg)')
 => [[[1, 0], :on_kw, "def"],
 [[1, 3], :on_sp, " "],
 [[1, 4], :on_kw, "alias"],
 [[1, 9], :on_lparen, "("],
 [[1, 10], :on_ident, “arg"],
 [[1, 13], :on_rparen, ")"]] pry(main)> Ripper.lex('def aliass(arg)')
 => [[[1, 0], :on_kw, "def"],
 [[1, 3], :on_sp, " "],
 [[1, 4], :on_ident, "aliass"],
 [[1, 10], :on_lparen, "("],
 [[1, 11], :on_ident, "arg"],
 [[1, 14], :on_rparen, ")"]]

Slide 88

Slide 88 text

Which no one really considered problematic…

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

Parser: A New Hope

Slide 92

Slide 92 text

parser is cross-platform

Slide 93

Slide 93 text

well documented

Slide 94

Slide 94 text

supports multiple parsing targets (Ruby 1.9 - 2.5)

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

easy to work with ast format https://github.com/whitequark/ast

Slide 97

Slide 97 text

p Parser::CurrentRuby.parse("2 + 2") # (send # (int 2) :+ # (int 2))

Slide 98

Slide 98 text

AST node source location mappings

Slide 99

Slide 99 text

p Parser::CurrentRuby.parse("2 + 2").loc # #, # @expression=#> p Parser::CurrentRuby.parse("2 + 2").loc.selector.source # "+"

Slide 100

Slide 100 text

$ ruby-parse -L -e "2+2" (send (int 2) :+ (int 2)) 2+2 ~ selector ~~~ expression (int 2) 2+2 ~ expression (int 2) 2+2 ~ expression

Slide 101

Slide 101 text

powerful code rewriting capabilities

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

Struggles with Parser

Slide 104

Slide 104 text

Parser was brand new

Slide 105

Slide 105 text

Had no real users

Slide 106

Slide 106 text

And had plenty of bugs…

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

Peter Zotov (@whitequark)

Slide 109

Slide 109 text

28th May, 2013 State of Unity

Slide 110

Slide 110 text

RuboCop 0.8

Slide 111

Slide 111 text

1st July, 2013 RuboCop 0.9

Slide 112

Slide 112 text

Formatters

Slide 113

Slide 113 text

Auto-correct

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

Commissioner (single parsing run triggers all cops)

Slide 116

Slide 116 text

Results caching (rubocop —-cache)

Slide 117

Slide 117 text

Parallel Checks (rubocop —-parallel)

Slide 118

Slide 118 text

Pattern Matching (regex-style matching for ast nodes)

Slide 119

Slide 119 text

def on_send(node) receiver_node, method_name, *arg_nodes = *node return unless receiver_node && receiver_node.array_type? && method_name == :* && arg_nodes.first.str_type? add_offense(node, location: :selector) end Style/ArrayJoin (before)

Slide 120

Slide 120 text

Style/ArrayJoin (now) def_node_matcher :join_candidate?, '(send $array :* $str)' def on_send(node) join_candidate?(node) { add_offense(node, location: :selector) } end

Slide 121

Slide 121 text

RuboCop Today

Slide 122

Slide 122 text

Started out with no configuration at all…

Slide 123

Slide 123 text

Today things are infinitely configurable

Slide 124

Slide 124 text

Metrics/LineLength: Enabled: false

Slide 125

Slide 125 text

Metrics/LineLength: Max: 100

Slide 126

Slide 126 text

Style/StringLiterals: Enabled: false

Slide 127

Slide 127 text

Style/StringLiterals: EnforcedStyle: double_quotes

Slide 128

Slide 128 text

You can set a target Ruby/Rails version

Slide 129

Slide 129 text

You can limit cops to certain folders with Exclude/Include directives

Slide 130

Slide 130 text

You can have different settings for the same cops in different directories

Slide 131

Slide 131 text

You can inherit between configuration files

Slide 132

Slide 132 text

You can auto-generate an initial configuration for your project

Slide 133

Slide 133 text

rubocop —-auto-gen-config

Slide 134

Slide 134 text

And you can read about all of this in RuboCop’s manual…

Slide 135

Slide 135 text

rubocop.readthedocs.io

Slide 136

Slide 136 text

gry is a smarter alternative to the built-in command https://github.com/pocke/gry

Slide 137

Slide 137 text

RuboCop started as a code style checker

Slide 138

Slide 138 text

•Style •Lint •Layout •Naming •Security •Performance •Performance •Rails •Bundler •Metric •Gemspec

Slide 139

Slide 139 text

RuboCop is a great code formatter

Slide 140

Slide 140 text

rubocop —-only Layout -a

Slide 141

Slide 141 text

RuboCop does some linting better than ruby -w

Slide 142

Slide 142 text

rubocop —-lint

Slide 143

Slide 143 text

You can extend RuboCop with your own cops

Slide 144

Slide 144 text

Granite (business actions architecture for Rails applications) https://toptal.github.io/granite/

Slide 145

Slide 145 text

$ bundle exec rake new_cop[Department/Name]

Slide 146

Slide 146 text

https://rubocop.readthedocs.io/ en/latest/development/

Slide 147

Slide 147 text

class SimplifyNotEmptyWithAny < Cop MSG = 'Use `.any?` and remove the negation part.'.freeze def_node_matcher :not_empty_call?, <<-PATTERN (send (send (...) :empty?) :!) PATTERN def on_send(node) return unless not_empty_call?(node) add_offense(node) end end

Slide 148

Slide 148 text

RuboCop has a rich ecosystem of extensions

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

189!!!

Slide 152

Slide 152 text

Notable Extensions • rubocop-rspec • guard-rubocop • rubocop-sequel • rubocop-cask

Slide 153

Slide 153 text

Integration with code quality services •Code Climate •HoundCI •SideCI •Codacy

Slide 154

Slide 154 text

RuboCop has integrations with many editors and IDEs

Slide 155

Slide 155 text

Emacs 26.1 is out and it’s amazing!

Slide 156

Slide 156 text

(spacemacs)

Slide 157

Slide 157 text

The Road to RuboCop 1.0

Slide 158

Slide 158 text

#1 question I get asked about RuboCop?

Slide 159

Slide 159 text

When is RuboCop 1.0 coming?

Slide 160

Slide 160 text

Remove Rails (and maybe Performance) cops from RuboCop’s core

Slide 161

Slide 161 text

Provide a proper (and stable) API for writing RuboCop extensions

Slide 162

Slide 162 text

Introducing the brand new RuboCop HQ!

Slide 163

Slide 163 text

https://github.com/bbatsov/rubocop

Slide 164

Slide 164 text

https://github.com/rubocop-hq/rubocop

Slide 165

Slide 165 text

RuboCop HQ •RuboCop •RuboCop JP •RuboCop RSpec/Rails/Performance •guard-rubocop, etc •Ruby & Rails Style Guides •Libraries extracted from RuboCop (e.g. node extensions, node pattern matching, etc)

Slide 166

Slide 166 text

Review the config of all cops and come up with the best defaults

Slide 167

Slide 167 text

Ideally each default should be backed by solid data/rationale

Slide 168

Slide 168 text

Adjust the list of enabled by default cops

Slide 169

Slide 169 text

Come up with a less painful process for dealing with new cops and configuration changes

Slide 170

Slide 170 text

mry makes RuboCop upgrades less painful https://github.com/pocke/mry

Slide 171

Slide 171 text

Agree on what’s going to be constituting breaking changes down the road

Slide 172

Slide 172 text

Breaking changes •Cop API changes •Extension API changes •Dropping support for Ruby versions •Renaming/removing cops •Renaming/changing/removing config values

Slide 173

Slide 173 text

Cop status “New” in minor releases

Slide 174

Slide 174 text

Cop status gets changed to Enabled/Disabled in major releases

Slide 175

Slide 175 text

Extra Cop metadata •Version added •Version default config changed •Version deprecated

Slide 176

Slide 176 text

eslint.org-grade manual/website

Slide 177

Slide 177 text

RuboCop.org •Easy to navigate & mobile-friendly •Published using MkDocs (or similar) •Separate User and Developer manual •Getter getting started resources (e.g. video tutorials) •Improved cop documentation

Slide 178

Slide 178 text

No content

Slide 179

Slide 179 text

That’s a lot of work!

Slide 180

Slide 180 text

Apes Together Strong!

Slide 181

Slide 181 text

Rubyists Together Strong!

Slide 182

Slide 182 text

Felina

Slide 183

Slide 183 text

Thanks! twitter: @bbatsov github: @bbatsov http//batsov.com http://emacsredux.com RubyKaigi Sendai, Japan 31.05.2018