datarockets-style 0.9.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/doc/STYLE_GUIDE.md CHANGED
@@ -392,6 +392,126 @@ def foo
392
392
  end
393
393
  ```
394
394
 
395
+ * <a name="style-trailing-comma-in-arguments"></a>
396
+ Requires a comma after the last argument, but only for parenthesized method calls where each argument is on its own line.
397
+ <sup>[[link](#style-trailing-comma-in-arguments)]</sup>
398
+
399
+ ```ruby
400
+ # bad
401
+ method(1, 2,)
402
+
403
+ # good
404
+ method(1, 2)
405
+
406
+ # bad
407
+ method(
408
+ 1, 2,
409
+ 3,
410
+ )
411
+
412
+ # good
413
+ method(
414
+ 1, 2,
415
+ 3
416
+ )
417
+
418
+ # bad
419
+ method(
420
+ 1, 2, 3,
421
+ )
422
+
423
+ # good
424
+ method(
425
+ 1, 2, 3
426
+ )
427
+
428
+ # good
429
+ method(
430
+ 1,
431
+ 2,
432
+ )
433
+ ```
434
+
435
+ * <a name="style-trailing-comma-in-array-literals"></a>
436
+ Requires a comma after last item in an array, but only when each item is on its own line.
437
+ <sup>[[link](#style-trailing-comma-in-array-literals)]</sup>
438
+
439
+ ```ruby
440
+ # bad
441
+ a = [1, 2,]
442
+
443
+ # good
444
+ a = [1, 2]
445
+
446
+ # bad
447
+ a = [
448
+ 1, 2,
449
+ 3,
450
+ ]
451
+
452
+ # good
453
+ a = [
454
+ 1, 2,
455
+ 3
456
+ ]
457
+
458
+ # bad
459
+ a = [
460
+ 1, 2, 3,
461
+ ]
462
+
463
+ # good
464
+ a = [
465
+ 1, 2, 3
466
+ ]
467
+
468
+ # good
469
+ a = [
470
+ 1,
471
+ 2,
472
+ ]
473
+ ```
474
+
475
+ * <a name="style-trailing-comma-in-hash-literal"></a>
476
+ Requires a comma after the last item in a hash.
477
+ <sup>[[link](#style-trailing-comma-in-hash-literal)]</sup>
478
+
479
+ ```ruby
480
+ # bad
481
+ a = { foo: 1, bar: 2, }
482
+
483
+ # good
484
+ a = { foo: 1, bar: 2 }
485
+
486
+ # bad
487
+ a = {
488
+ foo: 1, bar: 2,
489
+ qux: 3,
490
+ }
491
+
492
+ # good
493
+ a = {
494
+ foo: 1, bar: 2,
495
+ qux: 3
496
+ }
497
+
498
+ # bad
499
+ a = {
500
+ foo: 1, bar: 2, qux: 3,
501
+ }
502
+
503
+ # good
504
+ a = {
505
+ foo: 1, bar: 2, qux: 3
506
+ }
507
+
508
+ # good
509
+ a = {
510
+ foo: 1,
511
+ bar: 2,
512
+ }
513
+ ```
514
+
395
515
  * <a name="style-magic-link"></a>
396
516
  There are not any required rules for `frozen_string_literal` magic url.
397
517
  Set up [this cop](https://rubocop.readthedocs.io/en/latest/cops_style/#stylefrozenstringliteralcomment) depends on the project.
@@ -15,6 +15,10 @@ This style guide recommends best practices for writing a clear Rspec tests and e
15
15
  So there are not any limits for deep or nested groups.
16
16
  <sup>[[link](#rspec-nested-groups)]</sup>
17
17
 
18
+ * <a name="rspec-let-count"></a>
19
+ We're not limiting a number of `let` blocks in describe and context blocks.
20
+ <sup>[[link](#rspec-let-count)]</sup>
21
+
18
22
  * <a name="rspec-subject"></a>
19
23
  Each subject should be named, and we should not use `subject` in our test cases.
20
24
  Prefer to use `is_expected` that `expect(subject_name)` for small tests.
@@ -182,3 +186,19 @@ it "returns the last widget" do
182
186
  expect(Widget.last).to eq my_widget
183
187
  end
184
188
  ```
189
+
190
+ * <a name="rspec-prefer-before"></a>
191
+ Prefer using `before` instead of `setup`.
192
+ <sup>[[link](#rspec-prefer-before)]</sup>
193
+
194
+ ```ruby
195
+ # bad
196
+ setup do
197
+ allow(post).to receive(:publish!)
198
+ end
199
+
200
+ # good
201
+ before do
202
+ allow(post).to receive(:publish!)
203
+ end
204
+ ```
@@ -0,0 +1,13 @@
1
+ require "rubocop"
2
+ require "datarockets_style/formatter/todo_list_formatter"
3
+
4
+ require "datarockets_style/version"
5
+
6
+ require "datarockets_style/cop/layout/array_alignment_extended"
7
+ require "datarockets_style/cop/style/nested_interpolation"
8
+ require "datarockets_style/cop/rspec/prefer_before_over_setup"
9
+
10
+ # Top level module for datarockets-style
11
+ module DatarocketsStyle
12
+ # Datarickors sharable config
13
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatarocketsStyle
4
+ module Cop
5
+ module Layout
6
+ # Here we check if the elements of a multi-line array literal are
7
+ # aligned.
8
+ #
9
+ # @example EnforcedStyle: with_first_argument (default)
10
+ # # good
11
+ #
12
+ # array = [1, 2, 3,
13
+ # 4, 5, 6]
14
+ # array = ['run',
15
+ # 'forrest',
16
+ # 'run']
17
+ #
18
+ # # bad
19
+ #
20
+ # array = [1, 2, 3,
21
+ # 4, 5, 6]
22
+ # array = ['run',
23
+ # 'forrest',
24
+ # 'run']
25
+ #
26
+ # @example EnforcedStyle: with_fixed_indentation
27
+ # # good
28
+ #
29
+ # array = [1, 2, 3,
30
+ # 4, 5, 6]
31
+ #
32
+ # # bad
33
+ #
34
+ # array = [1, 2, 3,
35
+ # 4, 5, 6]
36
+ class ArrayAlignmentExtended < RuboCop::Cop::Cop
37
+ include RuboCop::Cop::Alignment
38
+
39
+ ALIGN_PARAMS_MSG = "Align the elements of an array literal if they span more than one line."
40
+
41
+ FIXED_INDENT_MSG = "Use one level of indentation for elements " \
42
+ "following the first line of a multi-line array."
43
+
44
+ def on_array(node)
45
+ return if node.children.size < 2
46
+
47
+ check_alignment(node.children, base_column(node, node.children))
48
+ end
49
+
50
+ def autocorrect(node)
51
+ RuboCop::Cop::AlignmentCorrector.correct(processed_source, node, column_delta)
52
+ end
53
+
54
+ private
55
+
56
+ def message(_node)
57
+ fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG
58
+ end
59
+
60
+ def fixed_indentation?
61
+ cop_config["EnforcedStyle"] == "with_fixed_indentation"
62
+ end
63
+
64
+ def base_column(node, args)
65
+ fixed_indentation? ? line_indentation(node) : display_column(args.first.source_range)
66
+ end
67
+
68
+ def line_indentation(node)
69
+ lineno = target_method_lineno(node)
70
+ line = node.source_range.source_buffer.source_line(lineno)
71
+ line_indentation = /\S.*/.match(line).begin(0)
72
+ line_indentation + configured_indentation_width
73
+ end
74
+
75
+ def target_method_lineno(node)
76
+ node.loc.line
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatarocketsStyle
4
+ module Cop
5
+ module RSpec
6
+ # Checks that tests use `before` instead of RoR unit-test `setup` method (part of `rspec-rails` gem)
7
+ #
8
+ # bad
9
+ #
10
+ # setup do
11
+ # allow(post).to receive(:publish!)
12
+ # end
13
+ #
14
+ # good
15
+ #
16
+ # before do
17
+ # allow(post).to receive(:publish!)
18
+ # end
19
+ class PreferBeforeOverSetup < RuboCop::Cop::Cop
20
+ MSG = "Use `before` instead of `setup`."
21
+
22
+ def_node_matcher :setup_call?, <<-PATTERN
23
+ (block
24
+ (send _ :setup)
25
+ (args) _)
26
+ PATTERN
27
+
28
+ def on_block(node)
29
+ return unless setup_call?(node)
30
+
31
+ add_offense(node)
32
+ end
33
+
34
+ def autocorrect(node)
35
+ lambda do |corrector|
36
+ block_internals = node.source.split(/ /)
37
+ corrector.replace node.loc.expression, ["before", *block_internals[1..-1]].join(" ")
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ module DatarocketsStyle
2
+ module Cop
3
+ module Style
4
+ # This cop checks nested interpolations
5
+ #
6
+ # @example
7
+ #
8
+ # # bad
9
+ # "Hello, #{user.blank? ? 'guest' : "dear #{user.name}"}"
10
+ #
11
+ # # good
12
+ # user_name = user.blank? ? 'guest' : "dear #{user.name}"
13
+ # "Hello, #{user_name}"
14
+ class NestedInterpolation < RuboCop::Cop::Cop
15
+ include RuboCop::Cop::Interpolation
16
+
17
+ MSG = "Redundant nested interpolation.".freeze
18
+
19
+ def on_interpolation(node)
20
+ node.each_descendant(:dstr) do |descendant_node|
21
+ detect_double_interpolation(descendant_node)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def detect_double_interpolation(node)
28
+ node.each_child_node(:begin) do |begin_node|
29
+ add_offense(begin_node)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -42,9 +42,10 @@ class TodoListFormatter < RuboCop::Formatter::ProgressFormatter
42
42
  def finished(inspected_files)
43
43
  report_summary(inspected_files.length,
44
44
  @total_offense_count,
45
- @total_correction_count)
45
+ @total_correction_count,
46
+ @total_correctable_count)
46
47
  output.puts
47
48
 
48
- Datarockets::Style::Formatter::TodoListFormatter::ReportSummary.new(offense_list).call(output)
49
+ DatarocketsStyle::Formatter::TodoListFormatter::ReportSummary.new(offense_list).call(output)
49
50
  end
50
51
  end
@@ -0,0 +1,55 @@
1
+ module DatarocketsStyle
2
+ module Formatter
3
+ module TodoListFormatter
4
+ # Get file of pairs: file path and cop name - and prepare report for ToDo list formatter.
5
+ #
6
+ # Example of result:
7
+ #
8
+ # LineLength
9
+ # Exclude:
10
+ # - "really/bad/file.rb" # 100500
11
+ # - "almost/ok.rb" # 1
12
+ class ReportSummary
13
+ attr_reader :offense_list
14
+
15
+ FileGroup = Struct.new(:file, :offenses_count) do
16
+ def print(output)
17
+ output.puts " - '#{file}' # #{offenses_count}"
18
+ end
19
+ end
20
+
21
+ OffenseGroup = Struct.new(:cop_name, :offenses) do
22
+ def file_groups
23
+ @_file_groups ||= offenses.group_by(&:file_path).map do |file, offenses|
24
+ FileGroup.new(file, offenses.length)
25
+ end
26
+ end
27
+
28
+ def print(output)
29
+ output.puts("#{cop_name}:")
30
+ output.puts(" Exclude:")
31
+ file_groups.sort_by(&:file).each do |file_group|
32
+ file_group.print(output)
33
+ end
34
+ output.puts
35
+ end
36
+ end
37
+
38
+ def initialize(offense_list)
39
+ @offense_list = offense_list
40
+ end
41
+
42
+ def call(output)
43
+ offense_groups.sort_by(&:cop_name).each { |group| group.print(output) }
44
+ end
45
+
46
+ private
47
+
48
+ def offense_groups
49
+ @_offense_groups ||= offense_list.group_by(&:cop_name)
50
+ .map { |cop_name, offenses| OffenseGroup.new(cop_name, offenses) }
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module DatarocketsStyle
2
+ VERSION = "1.2.0".freeze
3
+ end
@@ -52,6 +52,30 @@ Name | Default value | Configurable values
52
52
  EnforcedStyle | `with_first_parameter` | `with_first_parameter`, `with_fixed_indentation`
53
53
  IndentationWidth | `<none>` | Integer
54
54
 
55
+ # RSpec
56
+
57
+ ## RSpec/PreferBeforeOverSetup
58
+
59
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
60
+ --- | --- | --- | --- | ---
61
+ Enabled | Yes | Yes | 1.2.0 | -
62
+
63
+ Checks that tests use `before` instead of RoR unit-test `setup` method (part of `rspec-rails` gem)
64
+
65
+ ### Example
66
+
67
+ ```ruby
68
+ # bad
69
+ setup do
70
+ allow(post).to receive(:publish!)
71
+ end
72
+
73
+ # good
74
+ before do
75
+ allow(post).to receive(:publish!)
76
+ end
77
+ ```
78
+
55
79
  # Style
56
80
 
57
81
  ## Style/NestedInterpolation
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datarockets-style
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Dubrovsky
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-26 00:00:00.000000000 Z
11
+ date: 2021-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -16,43 +16,85 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.84'
19
+ version: '1.10'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.84'
26
+ version: '1.10'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rubocop-rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 2.5.2
33
+ version: '2.9'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 2.5.2
40
+ version: '2.9'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rubocop-rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.39'
47
+ version: '2.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.39'
55
- description:
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.10'
97
+ description:
56
98
  email:
57
99
  - r.dubrovsky@datarockets.com
58
100
  executables: []
@@ -70,7 +112,6 @@ files:
70
112
  - CODE_OF_CONDUCT.md
71
113
  - CONTRIBUTING.md
72
114
  - Gemfile
73
- - Gemfile.lock
74
115
  - LICENSE.txt
75
116
  - Makefile
76
117
  - README.md
@@ -86,19 +127,20 @@ files:
86
127
  - doc/STYLE_GUIDE.md
87
128
  - doc/STYLE_GUIDE_RAILS.md
88
129
  - doc/STYLE_GUIDE_RSPEC.md
89
- - lib/datarockets/style.rb
90
- - lib/datarockets/style/cop/layout/array_alignment_extended.rb
91
- - lib/datarockets/style/cop/style/nested_interpolation.rb
92
- - lib/datarockets/style/formatter/todo_list_formatter.rb
93
- - lib/datarockets/style/formatter/todo_list_formatter/report_summary.rb
94
- - lib/datarockets/style/version.rb
130
+ - lib/datarockets_style.rb
131
+ - lib/datarockets_style/cop/layout/array_alignment_extended.rb
132
+ - lib/datarockets_style/cop/rspec/prefer_before_over_setup.rb
133
+ - lib/datarockets_style/cop/style/nested_interpolation.rb
134
+ - lib/datarockets_style/formatter/todo_list_formatter.rb
135
+ - lib/datarockets_style/formatter/todo_list_formatter/report_summary.rb
136
+ - lib/datarockets_style/version.rb
95
137
  - manual/cops_layout.md
96
138
  homepage: https://github.com/datarockets/datarockets-style
97
139
  licenses:
98
140
  - MIT
99
141
  metadata:
100
142
  allowed_push_host: https://rubygems.org
101
- post_install_message:
143
+ post_install_message:
102
144
  rdoc_options: []
103
145
  require_paths:
104
146
  - lib
@@ -113,9 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
155
  - !ruby/object:Gem::Version
114
156
  version: '0'
115
157
  requirements: []
116
- rubyforge_project:
117
- rubygems_version: 2.6.14
118
- signing_key:
158
+ rubygems_version: 3.1.4
159
+ signing_key:
119
160
  specification_version: 4
120
161
  summary: Datarockets style guides and shared style configs
121
162
  test_files: []