sevencop 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: b59b9e87fae854412dd7555c8e0afb4d6c8c5e2a4a629ceb0b5840d94bfbd25c
4
+ data.tar.gz: d2a7a9cf5eb6b2722e44bb1f29f84ff3557e8be03fab8ecfd7441fb5723b64ec
5
+ SHA512:
6
+ metadata.gz: a8be2a642fcda52bb7b779032bb1714366ef300a44fc2b0d435333c0239d16b7aff7802e8a43206a65e105e328c6d42c400fbf132d2baa40eecadbf54e16b177
7
+ data.tar.gz: 8581bf84c297111f877db7d3e51eeddea2c1000af8ebaae49dbac164f229d9e73f7ef878a4cfe8520d8a40d1ec01954b04eaafe4370a3e7359e78f8573a45715
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ SuggestExtensions: false
4
+ TargetRubyVersion: 2.6
5
+
6
+ Layout/LineLength:
7
+ Enabled: false
8
+
9
+ Metrics:
10
+ Enabled: false
11
+
12
+ Naming/FileName:
13
+ Exclude:
14
+ - lib/sevencop.rb
15
+
16
+ Style/Documentation:
17
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ ## 0.1.0 - 2022-06-07
6
+
7
+ ### Added
8
+
9
+ - Initial release.
10
+ - Add `Sevencop/RedundantExistenceCheck` cop.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at r7kamura@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in sevencop.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'rubocop', '~> 1.21'
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sevencop (0.1.0)
5
+ rubocop (>= 1.27)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.5.0)
12
+ parallel (1.21.0)
13
+ parser (3.1.1.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.2.1)
18
+ rexml (3.2.5)
19
+ rspec (3.11.0)
20
+ rspec-core (~> 3.11.0)
21
+ rspec-expectations (~> 3.11.0)
22
+ rspec-mocks (~> 3.11.0)
23
+ rspec-core (3.11.0)
24
+ rspec-support (~> 3.11.0)
25
+ rspec-expectations (3.11.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.11.0)
28
+ rspec-mocks (3.11.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.11.0)
31
+ rspec-support (3.11.0)
32
+ rubocop (1.27.0)
33
+ parallel (~> 1.10)
34
+ parser (>= 3.1.0.0)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.8, < 3.0)
37
+ rexml
38
+ rubocop-ast (>= 1.16.0, < 2.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 3.0)
41
+ rubocop-ast (1.16.0)
42
+ parser (>= 3.1.1.0)
43
+ ruby-progressbar (1.11.0)
44
+ unicode-display_width (2.1.0)
45
+
46
+ PLATFORMS
47
+ x86_64-linux
48
+
49
+ DEPENDENCIES
50
+ rake (~> 13.0)
51
+ rspec (~> 3.0)
52
+ rubocop (~> 1.21)
53
+ sevencop!
54
+
55
+ BUNDLED WITH
56
+ 2.3.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ryo Nakamura
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,54 @@
1
+ # sevencop
2
+
3
+ [![test](https://github.com/r7kamura/sevencop/actions/workflows/test.yml/badge.svg)](https://github.com/r7kamura/sevencop/actions/workflows/test.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/sevencop.svg)](https://rubygems.org/gems/sevencop)
5
+
6
+ Custom cops for [RuboCop](https://github.com/rubocop/rubocop).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'sevencop', require: false
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```
19
+ bundle install
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```
25
+ gem install sevencop
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```yaml
31
+ # .rubocop.yml
32
+ require:
33
+ - sevencop
34
+ ```
35
+
36
+ ## Cops
37
+
38
+ ### `Sevencop/RedundantExistenceCheck`
39
+
40
+ Identifies redundant existent check before file operation.
41
+
42
+ ```ruby
43
+ # bad
44
+ FileUtils.mkdir(a) unless FileTest.exist?(a)
45
+
46
+ # good
47
+ FileUtils.mkdir(a)
48
+
49
+ # bad
50
+ FileUtils.rm(a) if FileTest.exist?(a)
51
+
52
+ # good
53
+ FileUtils.rm(a)
54
+ ```
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
13
+
14
+ desc 'Generate a new cop with a template'
15
+ task :new_cop, [:cop] do |_task, args|
16
+ require 'rubocop'
17
+
18
+ cop_name = args.fetch(:cop) do
19
+ warn 'usage: bundle exec rake new_cop[Department/Name]'
20
+ exit!
21
+ end
22
+
23
+ generator = RuboCop::Cop::Generator.new(cop_name)
24
+
25
+ generator.write_source
26
+ generator.write_spec
27
+ generator.inject_require(root_file_path: 'lib/rubocop/cop/rails_deprecation_cops.rb')
28
+ generator.inject_config(config_file_path: 'config/default.yml')
29
+
30
+ puts generator.todo
31
+ end
@@ -0,0 +1,5 @@
1
+ Sevencop/RedundantExistenceCheck:
2
+ Description: Avoid redundant existent check before file operation.
3
+ Enabled: true
4
+ Safe: false
5
+ VersionAdded: '0.1'
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Identifies redundant existent check before file operation.
7
+ #
8
+ # @safety
9
+ # This cop is unsafe because it can register a false positive
10
+ # where the check is truly needed.
11
+ #
12
+ # @example
13
+ #
14
+ # # bad
15
+ # FileUtils.mkdir(a) unless FileTest.exist?(a)
16
+ #
17
+ # # good
18
+ # FileUtils.mkdir(a)
19
+ #
20
+ # # bad
21
+ # FileUtils.rm(a) if FileTest.exist?(a)
22
+ #
23
+ # # good
24
+ # FileUtils.rm(a)
25
+ #
26
+ class RedundantExistenceCheck < Base
27
+ extend AutoCorrector
28
+
29
+ CLASS_NAMES_FOR_EXIST = ::Set[
30
+ :File,
31
+ :FileTest,
32
+ ]
33
+
34
+ METHOD_NAMES_FOR_MAKE = ::Set[
35
+ :makedirs,
36
+ :mkdir,
37
+ :mkdir_p,
38
+ :mkpath,
39
+ :touch,
40
+ ]
41
+
42
+ METHOD_NAMES_FOR_REMOVE = ::Set[
43
+ :remove,
44
+ :remove_dir,
45
+ :remove_entry,
46
+ :remove_entry_secure,
47
+ :remove_file,
48
+ :rm,
49
+ :rm_f,
50
+ :rm_r,
51
+ :rm_rf,
52
+ :rmdir,
53
+ :rmtree,
54
+ :safe_unlink,
55
+ ]
56
+
57
+ METHOD_NAMES_FOR_EXIST = ::Set[
58
+ :exist?,
59
+ :exists?,
60
+ ]
61
+
62
+ MSG = 'Avoid redundant existent check before file operation.'
63
+
64
+ def_node_matcher :make_unless_exist?, <<~PATTERN
65
+ (if
66
+ (send (const nil? CLASS_NAMES_FOR_EXIST) METHOD_NAMES_FOR_EXIST _)
67
+ nil?
68
+ (send (const nil? :FileUtils) METHOD_NAMES_FOR_MAKE ...)
69
+ )
70
+ PATTERN
71
+
72
+ def_node_matcher :remove_if_exist?, <<~PATTERN
73
+ (if
74
+ (send (const nil? CLASS_NAMES_FOR_EXIST) METHOD_NAMES_FOR_EXIST _)
75
+ (send (const nil? :FileUtils) METHOD_NAMES_FOR_REMOVE ...)
76
+ nil?
77
+ )
78
+ PATTERN
79
+
80
+ def on_if(node)
81
+ if redundant_on_if(node)
82
+ add_offense(node) do |rewriter|
83
+ remove_if(
84
+ node: node,
85
+ rewriter: rewriter
86
+ )
87
+ end
88
+ elsif redundant_on_unless(node)
89
+ add_offense(node) do |rewriter|
90
+ remove_unless(
91
+ node: node,
92
+ rewriter: rewriter
93
+ )
94
+ end
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ def redundant_on_if(node)
101
+ remove_if_exist?(node) && same_argument_on_if(node)
102
+ end
103
+
104
+ def redundant_on_unless(node)
105
+ make_unless_exist?(node) && same_argument_on_unless(node)
106
+ end
107
+
108
+ def remove_if(node:, rewriter:)
109
+ rewriter.replace(
110
+ node.location.expression,
111
+ node.children[1].source
112
+ )
113
+ end
114
+
115
+ def remove_unless(node:, rewriter:)
116
+ rewriter.replace(
117
+ node.location.expression,
118
+ node.children[2].source
119
+ )
120
+ end
121
+
122
+ def same_argument_on_if(node)
123
+ node.children[0].children[2] == node.children[1].children[2]
124
+ end
125
+
126
+ def same_argument_on_unless(node)
127
+ node.children[0].children[2] == node.children[2].children[2]
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ # The original code is from https://github.com/rubocop/rubocop-rspec/blob/main/lib/rubocop/rspec/inject.rb
6
+ # See https://github.com/rubocop/rubocop-rspec/blob/main/MIT-LICENSE.md
7
+ module Sevencop
8
+ module Inject
9
+ def self.defaults!
10
+ path = CONFIG_DEFAULT.to_s
11
+ hash = ::RuboCop::ConfigLoader.send(:load_yaml_configuration, path)
12
+ config = ::RuboCop::Config.new(hash, path).tap(&:make_excludes_absolute)
13
+ puts "configuration from #{path}" if ::RuboCop::ConfigLoader.debug?
14
+ config = ::RuboCop::ConfigLoader.merge_with_default(config, path)
15
+ ::RuboCop::ConfigLoader.instance_variable_set(:@default_configuration, config)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sevencop
4
+ VERSION = '0.1.0'
5
+ end
data/lib/sevencop.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require 'yaml'
5
+
6
+ require_relative 'sevencop/inject'
7
+ require_relative 'sevencop/version'
8
+
9
+ require_relative 'rubocop/cop/sevencop/redundant_existence_check'
10
+
11
+ module Sevencop
12
+ PROJECT_ROOT = ::Pathname.new(__dir__).parent.expand_path.freeze
13
+
14
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
15
+
16
+ CONFIG = ::YAML.safe_load(CONFIG_DEFAULT.read).freeze
17
+
18
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
19
+ end
20
+
21
+ Sevencop::Inject.defaults!
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/sevencop/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'sevencop'
7
+ spec.version = Sevencop::VERSION
8
+ spec.authors = ['Ryo Nakamura']
9
+ spec.email = ['r7kamura@gmail.com']
10
+
11
+ spec.summary = 'Custom cops for RuboCop.'
12
+ spec.homepage = 'https://github.com/r7kamura/sevencop'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.6.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+ spec.metadata['rubygems_mfa_required'] = 'true'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_runtime_dependency 'rubocop', '>= 1.27'
33
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sevencop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-06 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: '1.27'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.27'
27
+ description:
28
+ email:
29
+ - r7kamura@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - config/default.yml
44
+ - lib/rubocop/cop/sevencop/redundant_existence_check.rb
45
+ - lib/sevencop.rb
46
+ - lib/sevencop/inject.rb
47
+ - lib/sevencop/version.rb
48
+ - rubocop-rails_deprecation.gemspec
49
+ homepage: https://github.com/r7kamura/sevencop
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/r7kamura/sevencop
54
+ source_code_uri: https://github.com/r7kamura/sevencop
55
+ changelog_uri: https://github.com/r7kamura/sevencop/blob/main/CHANGELOG.md
56
+ rubygems_mfa_required: 'true'
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.6.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.3.7
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Custom cops for RuboCop.
76
+ test_files: []