rubocop-ordered_methods 0.8 → 0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc5f933eab16f22d4ce9e90bac7b7da25af7af45f7fd1204974ea26d98287476
4
- data.tar.gz: c3e5a535a8667acf2c88e8c127219b8b8e330d10e6575df5748391ec1c7758ba
3
+ metadata.gz: 2f2d2e3b61a0a11602f8fe67a27acf70a93494ca9cab1356f69b8acd88021f1f
4
+ data.tar.gz: bbf8c175b74c26ea08440b0283fab7a97437475757cb9ff0275e1d241777dc75
5
5
  SHA512:
6
- metadata.gz: c83ba399a7357517935466f59e4be28b75fbb399a571041b54ba4be1c387797b1867be69fcac2abe8e60e89572b34d35d9a327a171880cec2d9807ee258766ca
7
- data.tar.gz: dc2897b646af1eb6e2b84dbe2e693be8a0a48de8645caf9551557c6dfe36809a34c92c4166428f9f0b17bd541c3a5c8bf60d349ea3db3286c09ebb4897467e7e
6
+ metadata.gz: 8bdb062792695cb201c3fc2311f2ab4f990e0bbc809d1c5930ced67b456661335db31268d283a7e009ede8f872ba41fc601293dd2fb14fab59b3430f80b01bbb
7
+ data.tar.gz: 50c1683cd8866ef961612a48be343896da74a5a03c269802fc1ac1e1aae2cee91d360be7bd553d694b72ce8cce9eba32fd8f126afb30e4d4d421820abc8e9794
data/.rubocop.yml CHANGED
@@ -7,6 +7,12 @@ AllCops:
7
7
  SuggestExtensions: false
8
8
  TargetRubyVersion: 2.4
9
9
 
10
+ # Subtle, left to author's discretion. In a long method with many guard clauses,
11
+ # a blank line may help. But, in a short method, especially with only a single
12
+ # guard clause, a blank line can be disruptive.
13
+ Layout/EmptyLineAfterGuardClause:
14
+ Enabled: false
15
+
10
16
  Metrics/BlockLength:
11
17
  Exclude:
12
18
  - spec/**/*
@@ -18,3 +24,9 @@ Metrics/MethodLength:
18
24
  Naming/FileName:
19
25
  Exclude:
20
26
  - lib/rubocop-ordered_methods.rb
27
+
28
+ # Use the semantic style. If a block has side effects use `do`, and if it is
29
+ # pure use `{}`. This style is too nuanced for a linter, so the cop is
30
+ # disabled.
31
+ Style/BlockDelimiters:
32
+ Enabled: false
data/.travis.yml CHANGED
@@ -3,10 +3,10 @@ sudo: false
3
3
  language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
- - 2.3
7
6
  - 2.4
8
7
  - 2.5
9
8
  - 2.6
9
+ - 2.7
10
10
  script: bundle exec rake
11
11
  before_install:
12
12
  - gem install bundler || gem install bundler --version '< 2'
data/CHANGELOG.md CHANGED
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.9] - 2021-03-10
10
+
11
+ ### Added
12
+
13
+ - Autocorrection support for Sorbet signatures
14
+
9
15
  ## [0.8] - 2021-02-01
10
16
 
11
17
  ### Fixed
data/README.md CHANGED
@@ -74,8 +74,19 @@ rubocop --require rubocop-ordered_methods
74
74
 
75
75
  Name | Default value | Configurable values
76
76
  --- | --- | ---
77
- EnforcedStyle | `alphabetical` | `alphabetical`
78
- IgnoredMethods | `initialize` | Array
77
+ EnforcedStyle | `'alphabetical'` | `'alphabetical'`
78
+ IgnoredMethods | `['initialize']` | Array
79
+ Signature | `nil` | `'sorbet'`, `nil`
80
+
81
+ #### Example
82
+
83
+ ```
84
+ # .rubocop.yml
85
+ Layout/OrderedMethods:
86
+ EnforcedStyle: alphabetical
87
+ IgnoredMethods: initialize
88
+ Signature: sorbet
89
+ ```
79
90
 
80
91
  ### Corrector
81
92
 
@@ -125,6 +136,13 @@ protected :instance_a
125
136
  public :instance_a
126
137
  ```
127
138
 
139
+ #### Method signatures
140
+
141
+ Support for (Sorbet) method signatures was added to the corrector by
142
+ [#7](https://github.com/shanecav84/rubocop-ordered_methods/pull/7).
143
+ It is off by default due to performance concerns (not yet benchmarked). Enable
144
+ with `Signature: sorbet`.
145
+
128
146
  #### Caveats
129
147
 
130
148
  * The corrector will warn and refuse to order a method if it were to be
data/config/default.yml CHANGED
@@ -4,3 +4,4 @@ Layout/OrderedMethods:
4
4
  EnforcedStyle: 'alphabetical'
5
5
  IgnoredMethods:
6
6
  - initialize
7
+ Signature: ~
@@ -10,9 +10,11 @@ module RuboCop
10
10
  class OrderedMethodsCorrector
11
11
  include QualifierNodeMatchers
12
12
 
13
- def initialize(comments, siblings)
14
- @comments = comments
13
+ # @param cop_config ::RuboCop::Config
14
+ def initialize(comment_locations, siblings, cop_config)
15
+ @comment_locations = comment_locations
15
16
  @siblings = siblings
17
+ @cop_config = cop_config
16
18
  end
17
19
 
18
20
  def correct(node, previous_node)
@@ -45,13 +47,19 @@ module RuboCop
45
47
  (qualifier?(next_sibling) || alias?(next_sibling)) == node.method_name
46
48
  end
47
49
 
50
+ # @param node RuboCop::AST::DefNode
51
+ # @param source_range Parser::Source::Range
52
+ # @return Parser::Source::Range
48
53
  def join_comments(node, source_range)
49
- @comments[node].each do |comment|
54
+ @comment_locations[node.loc].each do |comment|
50
55
  source_range = source_range.join(comment.loc.expression)
51
56
  end
52
57
  source_range
53
58
  end
54
59
 
60
+ # @param node RuboCop::AST::DefNode
61
+ # @param source_range Parser::Source::Range
62
+ # @return Parser::Source::Range
55
63
  def join_modifiers_and_aliases(node, source_range)
56
64
  preceding_qualifier_index = node.sibling_index
57
65
  last_qualifier_index = find_last_qualifier_index(node)
@@ -64,12 +72,46 @@ module RuboCop
64
72
  source_range
65
73
  end
66
74
 
75
+ # @param node RuboCop::AST::DefNode
76
+ # @param source_range Parser::Source::Range
77
+ # @return Parser::Source::Range
78
+ def join_signature(node, source_range)
79
+ sib = node.left_sibling
80
+ if signature?(sib)
81
+ # If there is a comment directly above the sig, first calculate the
82
+ # range that covers both.
83
+ with_comment = join_comments(sib, sib.source_range)
84
+ source_range.join(with_comment)
85
+ else
86
+ source_range
87
+ end
88
+ end
89
+
90
+ def join_signature?
91
+ @cop_config['Signature'] == 'sorbet'
92
+ end
93
+
94
+ # @param node RuboCop::AST::DefNode
95
+ # @return Parser::Source::Range
67
96
  def join_surroundings(node)
68
97
  with_modifiers_and_aliases = join_modifiers_and_aliases(
69
98
  node,
70
99
  node.source_range
71
100
  )
72
- join_comments(node, with_modifiers_and_aliases)
101
+ with_comments = join_comments(node, with_modifiers_and_aliases)
102
+ if join_signature?
103
+ join_signature(node, with_comments)
104
+ else
105
+ with_comments
106
+ end
107
+ end
108
+
109
+ # https://sorbet.org/docs/sigs
110
+ # @param node RuboCop::AST::Node
111
+ def signature?(node)
112
+ return false unless node&.type == :block
113
+ child = node.children.first
114
+ child&.type == :send && child.method_name == :sig
73
115
  end
74
116
  end
75
117
  end
@@ -74,12 +74,17 @@ module RuboCop
74
74
  @siblings = node.children
75
75
 
76
76
  # Init the corrector with the cache to avoid traversing the AST in
77
- # the corrector. We always init the @corrector, even if
78
- # @options[:auto_correct] is nil, because `add_offense` always
79
- # attempts correction. This correction attempt is how RuboCop knows
80
- # if the offense can be labeled "[Correctable]".
81
- comments = processed_source.ast_with_comments
82
- @corrector = OrderedMethodsCorrector.new(comments, @siblings)
77
+ # the corrector.
78
+ #
79
+ # We always init the @corrector, even if @options[:auto_correct] is
80
+ # nil, because `add_offense` always attempts correction. This
81
+ # correction attempt is how RuboCop knows if the offense can be
82
+ # labeled "[Correctable]".
83
+ comment_locations = ::Parser::Source::Comment.associate_locations(
84
+ processed_source.ast,
85
+ processed_source.comments
86
+ )
87
+ @corrector = OrderedMethodsCorrector.new(comment_locations, @siblings, cop_config)
83
88
  end
84
89
  end
85
90
 
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'rubocop-ordered_methods'
8
- spec.version = '0.8'
8
+ spec.version = '0.9'
9
9
  spec.authors = ['Shane Cavanaugh']
10
10
  spec.email = ['shane@shanecav.net']
11
11
 
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_runtime_dependency 'rubocop', '>= 1.0'
30
30
 
31
31
  spec.add_development_dependency 'bundler'
32
+ spec.add_development_dependency 'byebug'
32
33
  spec.add_development_dependency 'rake', '~> 12.3.3'
33
34
  spec.add_development_dependency 'rspec', '~> 3.0'
34
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-ordered_methods
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.8'
4
+ version: '0.9'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Cavanaugh
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-01 00:00:00.000000000 Z
11
+ date: 2021-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +80,7 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '3.0'
69
- description:
83
+ description:
70
84
  email:
71
85
  - shane@shanecav.net
72
86
  executables: []
@@ -97,7 +111,7 @@ homepage: https://github.com/shanecav84/rubocop-ordered_methods
97
111
  licenses:
98
112
  - MIT
99
113
  metadata: {}
100
- post_install_message:
114
+ post_install_message:
101
115
  rdoc_options: []
102
116
  require_paths:
103
117
  - lib
@@ -112,8 +126,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
126
  - !ruby/object:Gem::Version
113
127
  version: '0'
114
128
  requirements: []
115
- rubygems_version: 3.1.2
116
- signing_key:
129
+ rubygems_version: 3.1.4
130
+ signing_key:
117
131
  specification_version: 4
118
132
  summary: Checks that methods are ordered alphabetically.
119
133
  test_files: []