rubocop-ordered_methods 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ade6d192050ac40f268177e3ede23b40f57d569041c6191baa855bfcef87a2c2
4
+ data.tar.gz: b39ded0724baf7c35b2d48506bebbe0924a04be6fe0c300a75090bf7e9deb7af
5
+ SHA512:
6
+ metadata.gz: 79368459e3709585a1741e6d5374b5e9d5176562f494a344ceed58208c512bf8c9f324ef8200bd9de9645b95a2d6a05d4bc584af97329997328bad44db2426ff
7
+ data.tar.gz: 1c3dcbf99709710ad10fec4e0085f1fd8caf9094082d27cb8a8b77fc6e081d0942bbfd41953b2bd715d184d3430a92ee463665913c51fcfcdac483c141a69f89
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ require: rubocop-ordered_methods
2
+
3
+ Metrics/BlockLength:
4
+ Exclude:
5
+ - spec/**/*
6
+
7
+ Naming/FileName:
8
+ Exclude:
9
+ - lib/rubocop-ordered_methods.rb
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.2
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
10
+ - 2.6
11
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at shane@shanecav.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rubocop-ordered_methods.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubocop-ordered_methods (0.1)
5
+ rubocop (~> 0.6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ diff-lcs (1.3)
12
+ jaro_winkler (1.5.2)
13
+ parallel (1.13.0)
14
+ parser (2.6.0.0)
15
+ ast (~> 2.4.0)
16
+ powerpack (0.1.2)
17
+ rainbow (3.0.0)
18
+ rake (10.5.0)
19
+ rspec (3.8.0)
20
+ rspec-core (~> 3.8.0)
21
+ rspec-expectations (~> 3.8.0)
22
+ rspec-mocks (~> 3.8.0)
23
+ rspec-core (3.8.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-expectations (3.8.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-mocks (3.8.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-support (3.8.0)
32
+ rubocop (0.64.0)
33
+ jaro_winkler (~> 1.5.1)
34
+ parallel (~> 1.10)
35
+ parser (>= 2.5, != 2.5.1.1)
36
+ powerpack (~> 0.1)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (~> 1.4.0)
40
+ ruby-progressbar (1.10.0)
41
+ unicode-display_width (1.4.1)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler (~> 1.17)
48
+ rake (~> 10.0)
49
+ rspec (~> 3.0)
50
+ rubocop-ordered_methods!
51
+
52
+ BUNDLED WITH
53
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Shane Cavanaugh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # RuboCop OrderedMethods
2
+
3
+ Check that methods are defined alphabetically.
4
+
5
+ ```ruby
6
+ # bad
7
+ def self.b; end
8
+ def self.a; end
9
+
10
+ def b; end
11
+ def a; end
12
+
13
+ private
14
+
15
+ def d; end
16
+ def c; end
17
+
18
+ # good
19
+ def self.a; end
20
+ def self.b; end
21
+
22
+ def a; end
23
+ def b; end
24
+
25
+ private
26
+
27
+ def c; end
28
+ def d; end
29
+ ```
30
+
31
+ ## Installation
32
+
33
+ Add this line to your application's Gemfile:
34
+
35
+ ```ruby
36
+ gem 'rubocop-ordered_methods'
37
+ ```
38
+
39
+ And then execute:
40
+
41
+ $ bundle
42
+
43
+ Or install it yourself as:
44
+
45
+ $ gem install rubocop-ordered_methods
46
+
47
+ ## Usage
48
+
49
+ You need to tell RuboCop to load the OrderedMethods extension. There are three
50
+ ways to do this:
51
+
52
+ ### RuboCop configuration file
53
+
54
+ Put this into your `.rubocop.yml`.
55
+
56
+ ```
57
+ require: rubocop-ordered_methods
58
+ ```
59
+
60
+ Now you can run `rubocop` and it will automatically load the RuboCop OrderedMethods
61
+ cops together with the standard cops.
62
+
63
+ ### Command line
64
+
65
+ ```bash
66
+ rubocop --require rubocop-ordered_methods
67
+ ```
68
+
69
+ ### Configurable attributes
70
+
71
+ Name | Default value | Configurable values
72
+ --- | --- | ---
73
+ IgnoredMethods | `initialize` | Array
74
+
75
+ ## Development
76
+
77
+ ### Setup
78
+
79
+ ```bash
80
+ bundle install
81
+ bundle exec rake
82
+ ```
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shanecav84/rubocop-ordered_methods. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
91
+
92
+ ## Code of Conduct
93
+
94
+ Everyone interacting in the RuboCop OrderedMethods project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/shanecav84/rubocop-ordered_methods/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RuboCop::RakeTask.new
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: %i[rubocop spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'rubocop/ordered_methods'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Layout
6
+ # Check that methods are defined alphabetically.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # def self.b; end
11
+ # def self.a; end
12
+ #
13
+ # def b; end
14
+ # def a; end
15
+ #
16
+ # private
17
+ #
18
+ # def d; end
19
+ # def c; end
20
+ #
21
+ # # good
22
+ # def self.a; end
23
+ # def self.b; end
24
+ #
25
+ # def a; end
26
+ # def b; end
27
+ #
28
+ # private
29
+ #
30
+ # def c; end
31
+ # def d; end
32
+ class OrderedMethods < Cop
33
+ include IgnoredMethods
34
+ include RangeHelp
35
+
36
+ MSG = 'Methods should be sorted alphabetically.'.freeze
37
+ VISIBILITY_MODIFIERS = %i[
38
+ module_function
39
+ private
40
+ protected
41
+ public
42
+ ].freeze
43
+
44
+ def_node_matcher :class_def?, 'defs'
45
+ def_node_matcher :instance_def?, 'def'
46
+ def_node_matcher :visibility_modifier?, <<-PATTERN
47
+ (send nil? { #{VISIBILITY_MODIFIERS.map(&:inspect).join(' ')} })
48
+ PATTERN
49
+
50
+ def on_begin(node)
51
+ consecutive_methods(node.children) do |previous, current|
52
+ add_offense(current) unless ordered?(previous, current)
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def consecutive_methods(ast)
59
+ filtered_and_grouped(ast).each do |method_group|
60
+ method_group.each_cons(2) do |left_method, right_method|
61
+ yield left_method, right_method
62
+ end
63
+ end
64
+ end
65
+
66
+ def filter_relevant_nodes(nodes)
67
+ nodes.select do |node|
68
+ (
69
+ class_def?(node) ||
70
+ instance_def?(node) ||
71
+ visibility_modifier?(node)
72
+ ) && !ignored_method?(node.method_name)
73
+ end
74
+ end
75
+
76
+ def filtered_and_grouped(ast)
77
+ group_methods_by_visiblity_block(filter_relevant_nodes(ast))
78
+ end
79
+
80
+ # Group methods by the visiblity block they are declared in. Multiple
81
+ # blocks of the same visiblity will have their methods grouped
82
+ # separately; for example, the following would be separated into two
83
+ # groups:
84
+ # private
85
+ # def a; end
86
+ # private
87
+ # def b; end
88
+ # rubocop:disable Metrics/MethodLength
89
+ def group_methods_by_visiblity_block(nodes)
90
+ is_class_method_block = false
91
+ nodes.each_with_object([[]]) do |node, grouped_methods|
92
+ if new_visiblity_block?(node, is_class_method_block)
93
+ grouped_methods << []
94
+ end
95
+
96
+ is_class_method_block = true if class_def?(node)
97
+ is_class_method_block = false if instance_def?(node)
98
+
99
+ if visibility_modifier?(node)
100
+ is_class_method_block = false
101
+ next
102
+ end
103
+
104
+ grouped_methods.last << node
105
+ end
106
+ end
107
+ # rubocop:enable Metrics/MethodLength
108
+
109
+ def new_visiblity_block?(node, is_class_method_block)
110
+ (class_def?(node) && !is_class_method_block) ||
111
+ (instance_def?(node) && is_class_method_block) ||
112
+ visibility_modifier?(node)
113
+ end
114
+
115
+ def ordered?(left_method, right_method)
116
+ (left_method.method_name <=> right_method.method_name) != 1
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,2 @@
1
+ require 'rubocop'
2
+ require_relative 'rubocop/cop/layout/ordered_methods'
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'rubocop-ordered_methods'
6
+ spec.version = '0.1'
7
+ spec.authors = ['Shane Cavanaugh']
8
+ spec.email = ['shane@shanecav.net']
9
+
10
+ spec.summary = 'Checks that methods are ordered alphabetically.'
11
+ spec.homepage = 'https://github.com/shanecav84/rubocop-ordered_methods'
12
+ spec.license = 'MIT'
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
16
+ # into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_runtime_dependency 'rubocop', '~> 0.6'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.17'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-ordered_methods
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Shane Cavanaugh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - shane@shanecav.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - lib/rubocop-ordered_methods.rb
87
+ - lib/rubocop/cop/layout/ordered_methods.rb
88
+ - rubocop-ordered_methods.gemspec
89
+ homepage: https://github.com/shanecav84/rubocop-ordered_methods
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.0.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Checks that methods are ordered alphabetically.
112
+ test_files: []