rubocop-dir_methods 0.1.0

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: 2597ca4e84265eb115ab05db871b55b11564a208c4222d2713bbcb179d0bee59
4
+ data.tar.gz: b17f217c171dd434c89943ff656832358072a6d69063e16f52a47b2b2e1703ba
5
+ SHA512:
6
+ metadata.gz: 2376dfbd471fc7fe629e5d4a98741a2635b4214cda686b9f6251a4f1935c85d05a5b0ea72750b457e1246f0f040ec2d27a8da26bab883375ce8a37e37169d405
7
+ data.tar.gz: aef64d3b3e4d984807d20c9cddb0f077f6329b356672704ea9124403849cd0a9b9cd2c89b1f086e67aceb267906feaf3322a37b3fd73ecbbbc76517c6801619a
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - "spec/dummy_app/**/*"
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
8
+ Style/WordArray:
9
+ Enabled: false
10
+ Style/SymbolArray:
11
+ Enabled: false
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Style/FrozenStringLiteralComment:
16
+ Exclude:
17
+ - "bin/console"
18
+
19
+ Naming/FileName:
20
+ Exclude:
21
+ - "lib/rubocop-dir_methods.rb"
22
+
23
+ Metrics/BlockLength:
24
+ Exclude:
25
+ - "spec/**/*"
26
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake", "~> 12.0"
8
+ gem "rspec"
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubocop-dir_methods (0.1.0)
5
+ rubocop
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.5.0)
12
+ json (2.6.3)
13
+ parallel (1.23.0)
14
+ parser (3.2.2.3)
15
+ ast (~> 2.4.1)
16
+ racc
17
+ racc (1.7.1)
18
+ rainbow (3.1.1)
19
+ rake (12.3.3)
20
+ regexp_parser (2.8.1)
21
+ rexml (3.2.5)
22
+ rspec (3.12.0)
23
+ rspec-core (~> 3.12.0)
24
+ rspec-expectations (~> 3.12.0)
25
+ rspec-mocks (~> 3.12.0)
26
+ rspec-core (3.12.2)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-expectations (3.12.3)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-mocks (3.12.5)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-support (3.12.0)
35
+ rubocop (1.52.1)
36
+ json (~> 2.3)
37
+ parallel (~> 1.10)
38
+ parser (>= 3.2.2.3)
39
+ rainbow (>= 2.2.2, < 4.0)
40
+ regexp_parser (>= 1.8, < 3.0)
41
+ rexml (>= 3.2.5, < 4.0)
42
+ rubocop-ast (>= 1.28.0, < 2.0)
43
+ ruby-progressbar (~> 1.7)
44
+ unicode-display_width (>= 2.4.0, < 3.0)
45
+ rubocop-ast (1.29.0)
46
+ parser (>= 3.2.1.0)
47
+ ruby-progressbar (1.13.0)
48
+ unicode-display_width (2.4.2)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ rake (~> 12.0)
55
+ rspec
56
+ rubocop-dir_methods!
57
+
58
+ BUNDLED WITH
59
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 GeorgeGorbanev
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,84 @@
1
+ # Rubocop::DirMethods
2
+
3
+ This gem is a tool designed to extend the functionality of RuboCop. It provides single RuboCop/Cop with capability to define and enforce custom naming conventions for public methods within specific directories in your Ruby projects. With this gem, you can maintain consistent coding practices and preserve the integrity of your project's codebase.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rubocop-dir_methods', require: false
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rubocop-dir_methods
20
+
21
+ ## Usage
22
+
23
+ Imagine you have a Rails application with a classic `controllers` directory:
24
+ ```
25
+ - sample_rails_app
26
+ - app
27
+ - controllers
28
+ - application_controller.rb
29
+ - sample_controller.rb
30
+ ...
31
+ ...
32
+ ```
33
+ And you wish to enforce RESTful method naming conventions to help developers think in terms of REST. You can configure your project to allow only RESTful method names within the `controllers` directory:
34
+ ```
35
+ # .rubocop.yml
36
+ require:
37
+ - rubocop-dir_methods
38
+
39
+ Naming/DirMethods:
40
+ dirs:
41
+ app/controllers:
42
+ allowed_methods:
43
+ - new
44
+ - create
45
+ - show
46
+ - edit
47
+ - update
48
+ - destroy
49
+ reason: "We want to follow RESTful design in controllers"
50
+
51
+ ```
52
+
53
+ If a developer creates a method that does not adhere to these conventions, the linter will promptly notify them, ensuring compliance with the established guidelines before the code review process:
54
+ ```ruby
55
+ # app/controllers/sample_controller.rb
56
+ class SampleController < ApplicationController
57
+ def sample_public_method
58
+ sample_implementation
59
+ end
60
+ end
61
+ ```
62
+ After running `rubocop`:
63
+ ```sh
64
+ rubocop
65
+ ...
66
+ app/controllers/sample_controller.rb:3:3: C: Naming/DirMethods: Invalid public method name: sample_public_method (reason: We want to follow RESTful design in controllers)
67
+ def sample_public_method ...
68
+ ^^^^^^^^^^^^^^^^^^^^^^^^
69
+ ```
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`.
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/GeorgeGorbanev/rubocop-dir_methods.
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList["spec/**/*_spec.rb"]
9
+ end
10
+ RuboCop::RakeTask.new
11
+
12
+ task(:default).clear.enhance(["spec", "rubocop"])
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubocop/dir_methods"
5
+ require "irb"
6
+
7
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "naming/dir_methods"
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Naming
6
+ class DirMethods < Base
7
+ include RuboCop::Cop::DefNode
8
+
9
+ MODIFIERS = [:public, :protected, :private].freeze
10
+
11
+ def on_def(node)
12
+ node_visibility_modifier = visibility_modifier(node)
13
+ return if node_visibility_modifier != :public
14
+
15
+ file_path = current_file_path(node)
16
+ _, dir_config = dir_config(file_path)
17
+ return if dir_config.nil? || dir_config["allowed_methods"].include?(node.method_name.to_s)
18
+
19
+ add_offense(node, message: offense_message(node, dir_config))
20
+ end
21
+
22
+ private
23
+
24
+ def current_file_path(node)
25
+ src_path = node.location.expression.source_buffer.name
26
+
27
+ if src_path.start_with?("/")
28
+ project_path = Pathname.new(File.expand_path(".")).realpath
29
+ Pathname.new(src_path).relative_path_from(project_path).to_s
30
+ else
31
+ src_path
32
+ end
33
+ end
34
+
35
+ def dir_config(file_path)
36
+ cop_config.fetch("dirs", {}).find do |config|
37
+ file_path.start_with?(config.first)
38
+ end
39
+ end
40
+
41
+ def visibility_modifier(node)
42
+ node.left_siblings.reverse.each do |sibling_node|
43
+ if inline_modifier?(sibling_node)
44
+ return sibling_node
45
+ elsif parent_modifier?(sibling_node)
46
+ return sibling_node.method_name
47
+ end
48
+ end
49
+
50
+ :public
51
+ end
52
+
53
+ def inline_modifier?(node)
54
+ MODIFIERS.include?(node)
55
+ end
56
+
57
+ def parent_modifier?(node)
58
+ node.respond_to?(:method_name) && MODIFIERS.include?(node.method_name)
59
+ end
60
+
61
+ def offense_message(node, dir_config)
62
+ message = "Invalid public method name: #{node.method_name}"
63
+ message += " (reason: #{dir_config['reason']})" if dir_config["reason"]
64
+ message
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module DirMethods
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop/dir_methods/version"
4
+
5
+ module RuboCop
6
+ module DirMethods
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ require_relative "rubocop/dir_methods"
6
+ require_relative "rubocop/dir_methods/version"
7
+ require_relative "rubocop/cop/dir_methods_cops"
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rubocop/dir_methods/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rubocop-dir_methods"
7
+ spec.version = RuboCop::DirMethods::VERSION
8
+ spec.authors = ["GeorgeGorbanev"]
9
+ spec.email = ["georgegorbanev@gmail.com"]
10
+
11
+ spec.summary = "Rubocop plugin for enforcing method names in dirs."
12
+ spec.description = "This gem is a tool designed to extend the functionality of RuboCop." \
13
+ "It provides single RuboCop/Cop with capability to define and enforce custom naming " \
14
+ "conventions for public methods within specific directories in your Ruby projects. " \
15
+ "With this gem, you can maintain consistent coding practices and preserve the " \
16
+ "integrity of your project's codebase."
17
+ spec.homepage = "https://github.com/GeorgeGorbanev/rubocop-dir_methods"
18
+ spec.license = "MIT"
19
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = spec.homepage
23
+
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_runtime_dependency "rubocop"
32
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-dir_methods
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - GeorgeGorbanev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-02 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This gem is a tool designed to extend the functionality of RuboCop.It
28
+ provides single RuboCop/Cop with capability to define and enforce custom naming
29
+ conventions for public methods within specific directories in your Ruby projects.
30
+ With this gem, you can maintain consistent coding practices and preserve the integrity
31
+ of your project's codebase.
32
+ email:
33
+ - georgegorbanev@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - ".gitignore"
39
+ - ".rspec"
40
+ - ".rubocop.yml"
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - bin/console
47
+ - bin/setup
48
+ - lib/rubocop-dir_methods.rb
49
+ - lib/rubocop/cop/dir_methods_cops.rb
50
+ - lib/rubocop/cop/naming/dir_methods.rb
51
+ - lib/rubocop/dir_methods.rb
52
+ - lib/rubocop/dir_methods/version.rb
53
+ - rubocop-dir_methods.gemspec
54
+ homepage: https://github.com/GeorgeGorbanev/rubocop-dir_methods
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://github.com/GeorgeGorbanev/rubocop-dir_methods
59
+ source_code_uri: https://github.com/GeorgeGorbanev/rubocop-dir_methods
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.5.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.1.4
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Rubocop plugin for enforcing method names in dirs.
79
+ test_files: []