Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Привет, ребята!

Slide 3

Slide 3 text

Божидар

Slide 4

Slide 4 text

Божo cool

Slide 5

Slide 5 text

(The RuboCop Guy)

Slide 6

Slide 6 text

Sofia, Bulgaria Sofia, Bulgaria

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Bulgarian Cheese

Slide 9

Slide 9 text

Болгарка

Slide 10

Slide 10 text

Слънчев Бряг

Slide 11

Slide 11 text

Златни пясъци

Slide 12

Slide 12 text

Банско

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Grigor Dimitrov vs Andrey Rublev

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Lukanka

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 SPB!!!

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

#RubyKaraoke

Slide 33

Slide 33 text

Много городов у нас в России, Нету пальцев столько на ногах,

Slide 34

Slide 34 text

С каждым годом всё они красивей, Утопают в солнце и в снегах.

Slide 35

Slide 35 text

В Ростове шикарные плюхи, Размером с большую печать,

Slide 36

Slide 36 text

В Москве оху*тельно нюхать,

Slide 37

Slide 37 text

В Челябинске лучше торчать.

Slide 38

Slide 38 text

А в Питере…

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

rubocop.readthedocs.io

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

RuboCop in a Nutshell

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

In other words…

Slide 47

Slide 47 text

It keeps your codebase consistent

Slide 48

Slide 48 text

It saves you time

Slide 49

Slide 49 text

It advances the Ruby language forward

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

32,608,128

Slide 52

Slide 52 text

Provides an efficient way for codebases to be updated

Slide 53

Slide 53 text

Disclaimer

Slide 54

Slide 54 text

Lint tools are not a replacement for common sense

Slide 55

Slide 55 text

Why an entire talk about a mere lint tool?

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

314 open issues

Slide 59

Slide 59 text

100 volunteers

Slide 60

Slide 60 text

3.14 issues/person

Slide 61

Slide 61 text

Coincidence?

Slide 62

Slide 62 text

Or providence?

Slide 63

Slide 63 text

A Brief History of Time

Slide 64

Slide 64 text

A Brief History of RuboCop

Slide 65

Slide 65 text

2011 The Ruby Style Guide

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

May, 2012 RuboCop 0.0.0

Slide 68

Slide 68 text

May, 2012 RuboCop 0.0.0

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Static code analysis with regular expressions …

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Nov, 2012 Along Came Jonas

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Spring, 2013 Return of the Jedi

Slide 76

Slide 76 text

Using a proper parser!

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Struggles with Ripper

Slide 79

Slide 79 text

Ripper supported only MRI

Slide 80

Slide 80 text

I wanted to build a cross-platform tool

Slide 81

Slide 81 text

Ripper had (has?) almost no documentation

Slide 82

Slide 82 text

Ripper is tied to the underlying Ruby runtime

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

Ripper has a very hard to work with AST representation

Slide 85

Slide 85 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 86

Slide 86 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 87

Slide 87 text

And many quirks…

Slide 88

Slide 88 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 89

Slide 89 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 90

Slide 90 text

Which no one really considered problematic…

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Parser: A New Hope From Russia with Love

Slide 94

Slide 94 text

parser is cross-platform

Slide 95

Slide 95 text

well documented

Slide 96

Slide 96 text

supports multiple parsing targets (Ruby 1.9 - 2.5)

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

AST node source location mappings

Slide 101

Slide 101 text

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

Slide 102

Slide 102 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 103

Slide 103 text

powerful code rewriting capabilities

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Struggles with Parser

Slide 106

Slide 106 text

Parser was brand new

Slide 107

Slide 107 text

Had no real users

Slide 108

Slide 108 text

And had plenty of bugs…

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

Peter Zotov (@whitequark)

Slide 111

Slide 111 text

28th May, 2013 State of Unity

Slide 112

Slide 112 text

RuboCop 0.8

Slide 113

Slide 113 text

1st July, 2013 RuboCop 0.9

Slide 114

Slide 114 text

Formatters

Slide 115

Slide 115 text

Auto-correct

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

Commissioner (single parsing run triggers all cops)

Slide 118

Slide 118 text

Results caching (rubocop —-cache)

Slide 119

Slide 119 text

Parallel Checks (rubocop —-parallel)

Slide 120

Slide 120 text

Pattern Matching (regex-style matching for ast nodes)

Slide 121

Slide 121 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 122

Slide 122 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 123

Slide 123 text

RuboCop Today

Slide 124

Slide 124 text

Started out with no configuration at all…

Slide 125

Slide 125 text

Today things are infinitely configurable

Slide 126

Slide 126 text

Metrics/LineLength: Enabled: false

Slide 127

Slide 127 text

Metrics/LineLength: Max: 100

Slide 128

Slide 128 text

Style/StringLiterals: Enabled: false

Slide 129

Slide 129 text

Style/StringLiterals: EnforcedStyle: double_quotes

Slide 130

Slide 130 text

You can set a target Ruby/Rails version

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

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

Slide 133

Slide 133 text

You can inherit between configuration files

Slide 134

Slide 134 text

You can auto-generate an initial configuration for your project

Slide 135

Slide 135 text

rubocop —-auto-gen-config

Slide 136

Slide 136 text

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

Slide 137

Slide 137 text

rubocop.readthedocs.io

Slide 138

Slide 138 text

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

Slide 139

Slide 139 text

RuboCop started as a code style checker

Slide 140

Slide 140 text

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

Slide 141

Slide 141 text

RuboCop is a great code formatter

Slide 142

Slide 142 text

rubocop —-only Layout -a

Slide 143

Slide 143 text

RuboCop does some linting better than ruby -w

Slide 144

Slide 144 text

rubocop —-lint

Slide 145

Slide 145 text

You can extend RuboCop with your own cops

Slide 146

Slide 146 text

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

Slide 147

Slide 147 text

$ bundle exec rake new_cop[Department/Name]

Slide 148

Slide 148 text

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

Slide 149

Slide 149 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 150

Slide 150 text

RuboCop has a rich ecosystem of extensions

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

189!!!

Slide 154

Slide 154 text

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

Slide 155

Slide 155 text

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

Slide 156

Slide 156 text

RuboCop has integrations with many editors and IDEs

Slide 157

Slide 157 text

Emacs 26.1 is out and it’s amazing!

Slide 158

Slide 158 text

(spacemacs)

Slide 159

Slide 159 text

The Road to RuboCop 1.0

Slide 160

Slide 160 text

#1 question I get asked about RuboCop?

Slide 161

Slide 161 text

When is RuboCop 1.0 coming?

Slide 162

Slide 162 text

“Using RuboCop is exciting. You never know if —auto-correct is going to work or not!” — Dick Sutterer

Slide 163

Slide 163 text

RuboCop changed my life! I used to go to horror movies to get an adrenaline rush - now I simply upgrade RuboCop! — Anton Devilov

Slide 164

Slide 164 text

RuboCop’s learning curve is amazing! I’m still amazed it never ends! — Aaron Punisherson (@hardlove)

Slide 165

Slide 165 text

“When faced with overwhelming odds just release 1.0 and it will all be OK!”

Slide 166

Slide 166 text

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

Slide 167

Slide 167 text

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

Slide 168

Slide 168 text

Introducing the brand new RuboCop HQ!

Slide 169

Slide 169 text

https://github.com/bbatsov/rubocop

Slide 170

Slide 170 text

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

Slide 171

Slide 171 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 172

Slide 172 text

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

Slide 173

Slide 173 text

Ideally each default should be backed by solid data/rationale

Slide 174

Slide 174 text

Adjust the list of enabled by default cops

Slide 175

Slide 175 text

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

Slide 176

Slide 176 text

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

Slide 177

Slide 177 text

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

Slide 178

Slide 178 text

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

Slide 179

Slide 179 text

Cop status “New” in minor releases

Slide 180

Slide 180 text

Cop status gets changed to Enabled/Disabled in major releases

Slide 181

Slide 181 text

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

Slide 182

Slide 182 text

eslint.org-grade manual/website

Slide 183

Slide 183 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 184

Slide 184 text

No content

Slide 185

Slide 185 text

That’s a lot of work!

Slide 186

Slide 186 text

Apes Together Strong!

Slide 187

Slide 187 text

Rubyists Together Strong!

Slide 188

Slide 188 text

Felina

Slide 189

Slide 189 text

Thanks! twitter: @bbatsov github: @bbatsov http//batsov.com http://emacsredux.com Saint P RubyConf Saint Petersburg, Russia 10.06.2018