rubocop-ipepe 0.2.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 88e1adbe08b2ef30c8afacb029a5240dcb26fb73cb7462951a5e48b42a2c011e
4
+ data.tar.gz: f09e9d131d92b567b163af95a17399ea251d0c04cb73de37bacf763b5762c305
5
+ SHA512:
6
+ metadata.gz: b43f6237752ad1a9f250c9645c1fbdcbb547c2b5c2f76ed12246d8c3bdc83a8df660f6980ba0b59caa69dd87be9b61c5d56e3fc47f8c85b721eb618231c57863
7
+ data.tar.gz: f4ea5b113609baa4371c244272c00daa21d912436fb4d474c51ef1a1977370a9935222b62e534c1d306153ff01bd571e4459761e6e2143ce617cb3df67103568
@@ -0,0 +1,41 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ "main" ]
13
+ pull_request:
14
+ branches: [ "main" ]
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ jobs:
20
+ test:
21
+
22
+ runs-on: ubuntu-latest
23
+ strategy:
24
+ matrix:
25
+ ruby-version: ['2.4', '2.5','2.6', '2.7', '3.0', '3.1']
26
+
27
+ steps:
28
+ - uses: actions/checkout@v3
29
+ - run: rm -rf Gemfile.lock
30
+ - name: Set up Ruby
31
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
32
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
33
+ # uses: ruby/setup-ruby@v1
34
+ uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
35
+ with:
36
+ ruby-version: ${{ matrix.ruby-version }}
37
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
38
+ - name: Run tests
39
+ run: bundle exec rspec
40
+ - name: Check coverage
41
+ run: ruby bin/coveragecheck
@@ -0,0 +1,38 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+
6
+ # pulled from repo
7
+ name: "Rubocop"
8
+
9
+ on:
10
+ pull_request:
11
+ branches: [ "main" ]
12
+
13
+ permissions:
14
+ contents: read
15
+
16
+ jobs:
17
+ rubocop:
18
+ runs-on: ubuntu-latest
19
+
20
+ steps:
21
+ - uses: actions/checkout@v3
22
+ - name: Set up Ruby
23
+ uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
24
+ with:
25
+ ruby-version: 2.7
26
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
27
+
28
+ - name: Rubocop run
29
+ run: |
30
+ bash -c "
31
+ bundle exec rubocop -P --require code_scanning --format CodeScanning::SarifFormatter -o rubocop.sarif
32
+ [[ $? -ne 2 ]]
33
+ "
34
+
35
+ - name: Upload Sarif output
36
+ uses: github/codeql-action/upload-sarif@v2
37
+ with:
38
+ sarif_file: rubocop.sarif
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,71 @@
1
+ AllCops:
2
+ DisabledByDefault: false
3
+ SuggestExtensions: false
4
+ NewCops: enable
5
+ TargetRubyVersion: 2.4
6
+
7
+ Naming/FileName:
8
+ Exclude:
9
+ - lib/rubocop-ipepe.rb
10
+
11
+ Style/MixinUsage:
12
+ Exclude:
13
+ - 'bin/*'
14
+ Style/FetchEnvVar:
15
+ Enabled: false
16
+ Style/RedundantConstantBase:
17
+ Enabled: false
18
+ Style/HashSyntax:
19
+ Enabled: true
20
+ EnforcedShorthandSyntax: never
21
+ Style/NumericPredicate:
22
+ EnforcedStyle: comparison
23
+ Style/ZeroLengthPredicate:
24
+ Enabled: false
25
+ Style/StringLiterals:
26
+ Enabled: true
27
+ EnforcedStyle: double_quotes
28
+ Style/FrozenStringLiteralComment:
29
+ Enabled: true
30
+ EnforcedStyle: never
31
+ Style/Documentation:
32
+ Enabled: false
33
+ Layout/LineLength:
34
+ Max: 100
35
+ Metrics/BlockLength:
36
+ Exclude:
37
+ - "*.rake"
38
+ - "spec/**/*.rb"
39
+ Metrics/AbcSize:
40
+ Max: 25
41
+ Metrics/CyclomaticComplexity:
42
+ Max: 15
43
+ Metrics/ParameterLists:
44
+ Max: 6
45
+ Lint/RaiseException:
46
+ Enabled: true
47
+ Lint/StructNewOverride:
48
+ Enabled: true
49
+ Style/HashEachMethods:
50
+ Enabled: true
51
+ Style/HashTransformKeys:
52
+ Enabled: true
53
+ Style/HashTransformValues:
54
+ Enabled: true
55
+ Style/MultilineBlockChain:
56
+ Enabled: false
57
+ Style/NegatedIf:
58
+ EnforcedStyle: postfix
59
+ Layout/DotPosition:
60
+ EnforcedStyle: trailing
61
+ Enabled: true
62
+ Metrics/MethodLength:
63
+ Max: 20
64
+ Style/DoubleNegation:
65
+ Enabled: false
66
+ Style/WordArray:
67
+ EnforcedStyle: brackets
68
+ Style/SymbolArray:
69
+ EnforcedStyle: brackets
70
+ Style/CommentedKeyword:
71
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.8
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rubocop-ipepe.gemspec
4
+ gemspec
5
+
6
+ gem "code-scanning-rubocop"
7
+ gem "rake", "~> 12.0"
8
+ gem "rspec"
9
+ gem "rubocop"
10
+ gem "simplecov"
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubocop-ipepe (0.2.0)
5
+ rubocop (~> 1.41)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ base64 (0.1.1)
12
+ code-scanning-rubocop (0.6.1)
13
+ rubocop (~> 1.0)
14
+ diff-lcs (1.5.0)
15
+ docile (1.4.0)
16
+ json (2.6.3)
17
+ language_server-protocol (3.17.0.3)
18
+ parallel (1.23.0)
19
+ parser (3.2.2.3)
20
+ ast (~> 2.4.1)
21
+ racc
22
+ racc (1.7.1)
23
+ rainbow (3.1.1)
24
+ rake (12.3.3)
25
+ regexp_parser (2.8.1)
26
+ rexml (3.2.6)
27
+ rspec (3.12.0)
28
+ rspec-core (~> 3.12.0)
29
+ rspec-expectations (~> 3.12.0)
30
+ rspec-mocks (~> 3.12.0)
31
+ rspec-core (3.12.1)
32
+ rspec-support (~> 3.12.0)
33
+ rspec-expectations (3.12.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.12.0)
36
+ rspec-mocks (3.12.3)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.12.0)
39
+ rspec-support (3.12.0)
40
+ rubocop (1.56.3)
41
+ base64 (~> 0.1.1)
42
+ json (~> 2.3)
43
+ language_server-protocol (>= 3.17.0)
44
+ parallel (~> 1.10)
45
+ parser (>= 3.2.2.3)
46
+ rainbow (>= 2.2.2, < 4.0)
47
+ regexp_parser (>= 1.8, < 3.0)
48
+ rexml (>= 3.2.5, < 4.0)
49
+ rubocop-ast (>= 1.28.1, < 2.0)
50
+ ruby-progressbar (~> 1.7)
51
+ unicode-display_width (>= 2.4.0, < 3.0)
52
+ rubocop-ast (1.29.0)
53
+ parser (>= 3.2.1.0)
54
+ ruby-progressbar (1.13.0)
55
+ simplecov (0.22.0)
56
+ docile (~> 1.1)
57
+ simplecov-html (~> 0.11)
58
+ simplecov_json_formatter (~> 0.1)
59
+ simplecov-html (0.12.3)
60
+ simplecov_json_formatter (0.1.4)
61
+ unicode-display_width (2.4.2)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ code-scanning-rubocop
68
+ rake (~> 12.0)
69
+ rspec
70
+ rubocop
71
+ rubocop-ipepe!
72
+ simplecov
73
+
74
+ BUNDLED WITH
75
+ 2.1.4
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Patryk Ptasinski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # RuboCop Ipepe
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/rubocop-ipepe.svg)](https://rubygems.org/gems/rubocop-ipepe)
4
+ ![CI](https://github.com/ipepe-oss/rubocop-ipepe/workflows/CI/badge.svg)
5
+
6
+ [RuboCop](https://github.com/rubocop/rubocop).
7
+
8
+ ## Installation
9
+
10
+ Just install the `rubocop-ipepe` gem
11
+
12
+ ```bash
13
+ gem install rubocop-ipepe
14
+ ```
15
+
16
+ or if you use bundler put this in your `Gemfile`
17
+
18
+ ```ruby
19
+ gem 'rubocop-ipepe', require: false
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ You need to tell RuboCop to load the Ipepe extension. There are three
25
+ ways to do this:
26
+
27
+ ### RuboCop configuration file
28
+
29
+ Put this into your `.rubocop.yml`.
30
+
31
+ ```yaml
32
+ require: rubocop-ipepe
33
+ ```
34
+
35
+ Alternatively, use the following array notation when specifying multiple extensions.
36
+
37
+ ```yaml
38
+ require:
39
+ - rubocop-other-extension
40
+ - rubocop-ipepe
41
+ ```
42
+
43
+ Now you can run `rubocop` and it will automatically load the RuboCop Ipepe
44
+ cops together with the standard cops.
45
+
46
+ ### Command line
47
+
48
+ ```bash
49
+ rubocop --require rubocop-ipepe
50
+ ```
51
+
52
+ ### Rake task
53
+
54
+ ```ruby
55
+ RuboCop::RakeTask.new do |task|
56
+ task.requires << 'rubocop-ipepe'
57
+ end
58
+ ```
59
+
60
+ ## The Cops
61
+
62
+ All cops are located under
63
+ [`lib/rubocop/cop/ipepe`](lib/rubocop/cop/ipepe), and contain
64
+ examples/documentation.
65
+
66
+ In your `.rubocop.yml`, you may treat the Ipepe cops just like any other
67
+ cop. For example:
68
+
69
+ ```yaml
70
+ Ipepe/SpecificMatcher:
71
+ Exclude:
72
+ - spec/my_spec.rb
73
+ ```
74
+
75
+ ### Ipepe/MultipleConditionUnless
76
+
77
+ Checks for multiple conditions in `unless` statement.
78
+
79
+ ```ruby
80
+ # bad
81
+ unless foo && bar
82
+ do_something
83
+ end
84
+
85
+ # good
86
+ if foo || bar
87
+ do_something
88
+ end
89
+ ```
90
+
91
+ ### Ipepe/TernaryOperator
92
+
93
+ Prohibits any use of ternary operator.
94
+
95
+ ```ruby
96
+ # bad
97
+ foo ? bar : baz
98
+
99
+ # good
100
+ if foo
101
+ bar
102
+ else
103
+ baz
104
+ end
105
+ ```
106
+
107
+ ## Development
108
+
109
+ ### Adding a new cop
110
+ `bundle exec rake 'new_cop[Ipepe/TestOperator]'`
111
+
112
+ ## License
113
+
114
+ `rubocop-ipepe` is MIT licensed. [See the accompanying file](LICENSE.md) for
115
+ the full text.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task default: :test
11
+
12
+ require "rspec/core/rake_task"
13
+
14
+ RSpec::Core::RakeTask.new(:spec) do |spec|
15
+ spec.pattern = FileList["spec/**/*_spec.rb"]
16
+ end
17
+
18
+ desc "Generate a new cop with a template"
19
+ task :new_cop, [:cop] do |_task, args|
20
+ require "rubocop"
21
+
22
+ cop_name = args.fetch(:cop) do
23
+ warn "usage: bundle exec rake new_cop[Department/Name]"
24
+ exit!
25
+ end
26
+
27
+ generator = RuboCop::Cop::Generator.new(cop_name)
28
+
29
+ generator.write_source
30
+ generator.write_spec
31
+ generator.inject_require(root_file_path: "lib/rubocop/cop/ipepe_cops.rb")
32
+ generator.inject_config(config_file_path: "config/default.yml")
33
+
34
+ puts generator.todo
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubocop/ipepe"
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__)
data/bin/coveragecheck ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "json"
4
+
5
+ File.read("coverage/.last_run.json").tap do |json|
6
+ data = JSON.parse(json)
7
+ coverage = [
8
+ data.dig("result", "covered_percent").to_f,
9
+ data.dig("result", "line").to_f
10
+ ].max
11
+ puts "Coverage: #{coverage}%"
12
+ if coverage < 100
13
+ puts "Coverage is below 100%! Exiting with error."
14
+ exit 1
15
+ end
16
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ Ipepe:
2
+ Enabled: true
3
+ DocumentationBaseURL: "https://github.com/ipepe-oss/rubocop-ipepe"
4
+
5
+ Ipepe/TernaryOperator:
6
+ Enabled: true
7
+ Description: "Prefer explicit if over ternary operator"
8
+ VersionAdded: '0.1.0'
9
+
10
+ Ipepe/MultipleConditionUnless:
11
+ Description: "`unless` with multiple conditions is not allowed"
12
+ Enabled: true
13
+ VersionAdded: '0.1.0'
14
+ # TODO: implement
15
+ # EnforcedStyle: 'allow_single_condition'
16
+ # SupportedStyles:
17
+ # - 'allow_single_condition'
18
+ # - 'never_unless'
19
+
20
+ Ipepe/AlphabeticalArrayOfStrings:
21
+ Description: 'Ensure that strings in array are in alphabetical order'
22
+ Enabled: true
23
+ VersionAdded: '0.2.0'
24
+
25
+ Ipepe/AlphabeticalHashKeys:
26
+ Description: 'Ensure that hash keys are in alphabetical order'
27
+ Enabled: true
28
+ VersionAdded: '0.2.0'
29
+ # TODO: implement
30
+ # EnforcedStyle: 'symbols_only'
31
+ # SupportedStyles:
32
+ # - 'symbols_only'
33
+ # - 'strings_only'
34
+ # - 'symbols_and_strings'
@@ -0,0 +1,83 @@
1
+ require:
2
+ - rubocop-performance
3
+ AllCops:
4
+ DisabledByDefault: false
5
+ SuggestExtensions: false
6
+ NewCops: enable
7
+ Exclude:
8
+ - 'db/schema.rb'
9
+ Style/MixinUsage:
10
+ Exclude:
11
+ - 'bin/*'
12
+ Style/FetchEnvVar:
13
+ Enabled: false
14
+ Style/RedundantConstantBase:
15
+ Enabled: false
16
+ Style/HashSyntax:
17
+ Enabled: true
18
+ EnforcedShorthandSyntax: never
19
+ Style/NumericPredicate:
20
+ EnforcedStyle: comparison
21
+ Style/ZeroLengthPredicate:
22
+ Enabled: false
23
+ Style/StringLiterals:
24
+ Enabled: true
25
+ EnforcedStyle: double_quotes
26
+ Style/FrozenStringLiteralComment:
27
+ Enabled: true
28
+ EnforcedStyle: never
29
+ Style/Documentation:
30
+ Enabled: false
31
+ Layout/LineLength:
32
+ Max: 100
33
+ Exclude:
34
+ - 'db/seeds.rb'
35
+ Metrics/BlockLength:
36
+ Exclude:
37
+ - db/seeds.rb
38
+ - app/admin/**/*.rb
39
+ - spec/**/*_spec.rb
40
+ - config/routes.rb
41
+ - config/deploy.rb
42
+ - app/admin/**/*.rb
43
+ - lib/tasks/**/*.rake
44
+ Metrics/AbcSize:
45
+ Exclude:
46
+ - db/migrate/*.rb
47
+ Max: 25
48
+ Metrics/CyclomaticComplexity:
49
+ Max: 15
50
+ Metrics/ParameterLists:
51
+ Max: 6
52
+ Lint/RaiseException:
53
+ Enabled: true
54
+ Lint/StructNewOverride:
55
+ Enabled: true
56
+ Style/HashEachMethods:
57
+ Enabled: true
58
+ Style/HashTransformKeys:
59
+ Enabled: true
60
+ Style/HashTransformValues:
61
+ Enabled: true
62
+ Style/MultilineBlockChain:
63
+ Enabled: false
64
+ Style/NegatedIf:
65
+ EnforcedStyle: postfix
66
+ Layout/DotPosition:
67
+ EnforcedStyle: trailing
68
+ Enabled: true
69
+ Metrics/MethodLength:
70
+ Max: 20
71
+ Exclude:
72
+ - 'db/**/*.rb'
73
+ Style/DoubleNegation:
74
+ Enabled: false
75
+ Style/WordArray:
76
+ EnforcedStyle: brackets
77
+ Style/SymbolArray:
78
+ EnforcedStyle: brackets
79
+ Style/CommentedKeyword:
80
+ Enabled: false
81
+ Naming/PredicateName:
82
+ Exclude:
83
+ - 'app/serializers/**/*.rb'
@@ -0,0 +1,64 @@
1
+ AllCops:
2
+ DisabledByDefault: false
3
+ SuggestExtensions: false
4
+ NewCops: enable
5
+ Style/MixinUsage:
6
+ Exclude:
7
+ - 'bin/*'
8
+ Style/FetchEnvVar:
9
+ Enabled: false
10
+ Style/RedundantConstantBase:
11
+ Enabled: false
12
+ Style/HashSyntax:
13
+ Enabled: true
14
+ EnforcedShorthandSyntax: never
15
+ Style/NumericPredicate:
16
+ EnforcedStyle: comparison
17
+ Style/ZeroLengthPredicate:
18
+ Enabled: false
19
+ Style/StringLiterals:
20
+ Enabled: true
21
+ EnforcedStyle: double_quotes
22
+ Style/FrozenStringLiteralComment:
23
+ Enabled: true
24
+ EnforcedStyle: never
25
+ Style/Documentation:
26
+ Enabled: false
27
+ Layout/LineLength:
28
+ Max: 100
29
+ Metrics/BlockLength:
30
+ Exclude:
31
+ - "*.rake"
32
+ Metrics/AbcSize:
33
+ Max: 25
34
+ Metrics/CyclomaticComplexity:
35
+ Max: 15
36
+ Metrics/ParameterLists:
37
+ Max: 6
38
+ Lint/RaiseException:
39
+ Enabled: true
40
+ Lint/StructNewOverride:
41
+ Enabled: true
42
+ Style/HashEachMethods:
43
+ Enabled: true
44
+ Style/HashTransformKeys:
45
+ Enabled: true
46
+ Style/HashTransformValues:
47
+ Enabled: true
48
+ Style/MultilineBlockChain:
49
+ Enabled: false
50
+ Style/NegatedIf:
51
+ EnforcedStyle: postfix
52
+ Layout/DotPosition:
53
+ EnforcedStyle: trailing
54
+ Enabled: true
55
+ Metrics/MethodLength:
56
+ Max: 20
57
+ Style/DoubleNegation:
58
+ Enabled: false
59
+ Style/WordArray:
60
+ EnforcedStyle: brackets
61
+ Style/SymbolArray:
62
+ EnforcedStyle: brackets
63
+ Style/CommentedKeyword:
64
+ Enabled: false
@@ -0,0 +1,29 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Ipepe
4
+ class AlphabeticalArrayOfStrings < ::RuboCop::Cop::Base
5
+ extend AutoCorrector
6
+ MSG = "Ensure that strings in array are in alphabetical order".freeze
7
+
8
+ def on_array(node)
9
+ str_type_hash = {}
10
+ node.children.each do |n|
11
+ str_type_hash[n.str_type?] ||= 0
12
+ str_type_hash[n.str_type?] += 1
13
+ end
14
+
15
+ return if str_type_hash.size != 1 || str_type_hash[true].nil?
16
+
17
+ strings = node.children.map(&:value)
18
+ sorted_strings = strings.sort
19
+
20
+ return if strings == sorted_strings
21
+
22
+ add_offense(node) do |corrector|
23
+ corrector.replace(node, "[#{sorted_strings.map { |s| "\"#{s}\"" }.join(', ')}]")
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,50 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Ipepe
4
+ class AlphabeticalHashKeys < ::RuboCop::Cop::Base
5
+ extend AutoCorrector
6
+ MSG = "Ensure that keys in hash are in alphabetical order".freeze
7
+
8
+ def on_hash(node)
9
+ keys = node.children.select(&:pair_type?).map(&:key)
10
+ sorted_keys = keys.sort_by(&:value)
11
+ return if cop_not_applicable?(keys, sorted_keys)
12
+
13
+ add_offense(node) do |corrector|
14
+ join_keys_with = " "
15
+ join_keys_with = "\n " if node.source.include?("\n")
16
+
17
+ corrector.replace(
18
+ node,
19
+ [
20
+ "{",
21
+ sorted_keypairs(node, sorted_keys).join(",#{join_keys_with}"),
22
+ "}"
23
+ ].join(join_keys_with)
24
+ )
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def cop_not_applicable?(keys, sorted_keys)
31
+ keys.empty? ||
32
+ keys.one? ||
33
+ (keys.none?(&:str_type?) && keys.none?(&:sym_type?)) ||
34
+ keys == sorted_keys
35
+ end
36
+
37
+ def sorted_keypairs(node, sorted_keys)
38
+ sorted_keys.map do |k|
39
+ keypair = node.children.find { |n| n.key == k }
40
+ if keypair.key.str_type?
41
+ "#{keypair.key.source} => #{keypair.value.source}"
42
+ elsif keypair.key.sym_type?
43
+ "#{keypair.key.source}: #{keypair.value.source}"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,20 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Ipepe
4
+ class MultipleConditionUnless < ::RuboCop::Cop::Base
5
+ extend AutoCorrector
6
+ MSG = "Use only one condition in unless or change to if".freeze
7
+
8
+ def on_if(node)
9
+ return unless node.unless?
10
+ return unless node.condition.and_type?
11
+
12
+ add_offense(node) do |corrector|
13
+ # change `unless` to `if !(condition)`
14
+ corrector.replace(node, "if !(#{node.condition.source})\n#{node.if_branch.source}\nend")
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Ipepe
4
+ class TernaryOperator < ::RuboCop::Cop::Base
5
+ extend AutoCorrector
6
+
7
+ MSG = "Use `if` instead of ternary operator.".freeze
8
+
9
+ def on_if(node)
10
+ return unless node.ternary?
11
+
12
+ add_offense(node) do |corrector|
13
+ corrector.replace(
14
+ node,
15
+ [
16
+ "if #{node.condition.source}",
17
+ node.if_branch.source,
18
+ "else",
19
+ node.else_branch.source,
20
+ "end"
21
+ ].join("\n")
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "ipepe/alphabetical_array_of_strings"
2
+ require_relative "ipepe/alphabetical_hash_keys"
3
+ require_relative "ipepe/multiple_condition_unless"
4
+ require_relative "ipepe/ternary_operator"
@@ -0,0 +1,18 @@
1
+ # The original code is from https://github.com/rubocop/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
2
+ # See https://github.com/rubocop/rubocop-rspec/blob/master/MIT-LICENSE.md
3
+ module RuboCop
4
+ module Ipepe
5
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
6
+ # bit of our configuration.
7
+ module Inject
8
+ def self.defaults!
9
+ path = CONFIG_DEFAULT.to_s
10
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
11
+ config = Config.new(hash, path).tap(&:make_excludes_absolute)
12
+ puts "configuration from #{path}" if ConfigLoader.debug?
13
+ config = ConfigLoader.merge_with_default(config, path)
14
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module RuboCop
2
+ module Ipepe
3
+ VERSION = "0.2.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require "rubocop/ipepe/version"
2
+
3
+ module RuboCop
4
+ module Ipepe
5
+ class Error < StandardError; end
6
+ # Your code goes here...
7
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
8
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
9
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
10
+
11
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ require "rubocop"
2
+
3
+ require_relative "rubocop/ipepe"
4
+ require_relative "rubocop/ipepe/version"
5
+ require_relative "rubocop/ipepe/inject"
6
+
7
+ RuboCop::Ipepe::Inject.defaults!
8
+
9
+ require_relative "rubocop/cop/ipepe_cops"
@@ -0,0 +1,32 @@
1
+ require_relative "lib/rubocop/ipepe/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rubocop-ipepe"
5
+ spec.version = RuboCop::Ipepe::VERSION
6
+ spec.authors = ["Patryk Ptasinski"]
7
+ spec.email = ["patryk@ipepe.pl"]
8
+
9
+ spec.summary = "Opinionated RuboCop configuration used by ipepe."
10
+ spec.description = "This gem is a collection of RuboCop cops used by ipepe."
11
+ spec.homepage = "https://github.com/ipepe-oss/rubocop-ipepe"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}#changelog"
19
+ spec.licenses = ["MIT"]
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 { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "rubocop", "~> 1.41"
31
+ spec.metadata["rubygems_mfa_required"] = "true"
32
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-ipepe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Patryk Ptasinski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-21 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.41'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.41'
27
+ description: This gem is a collection of RuboCop cops used by ipepe.
28
+ email:
29
+ - patryk@ipepe.pl
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".github/workflows/rspec.yml"
35
+ - ".github/workflows/rubocop.yml"
36
+ - ".gitignore"
37
+ - ".rubocop.yml"
38
+ - ".ruby-version"
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.md
42
+ - README.md
43
+ - Rakefile
44
+ - bin/console
45
+ - bin/coveragecheck
46
+ - bin/setup
47
+ - config/default.yml
48
+ - config/rails.default.yml
49
+ - config/ruby.default.yml
50
+ - lib/rubocop-ipepe.rb
51
+ - lib/rubocop/cop/ipepe/alphabetical_array_of_strings.rb
52
+ - lib/rubocop/cop/ipepe/alphabetical_hash_keys.rb
53
+ - lib/rubocop/cop/ipepe/multiple_condition_unless.rb
54
+ - lib/rubocop/cop/ipepe/ternary_operator.rb
55
+ - lib/rubocop/cop/ipepe_cops.rb
56
+ - lib/rubocop/ipepe.rb
57
+ - lib/rubocop/ipepe/inject.rb
58
+ - lib/rubocop/ipepe/version.rb
59
+ - rubocop-ipepe.gemspec
60
+ homepage: https://github.com/ipepe-oss/rubocop-ipepe
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ allowed_push_host: https://rubygems.org
65
+ homepage_uri: https://github.com/ipepe-oss/rubocop-ipepe
66
+ source_code_uri: https://github.com/ipepe-oss/rubocop-ipepe
67
+ changelog_uri: https://github.com/ipepe-oss/rubocop-ipepe#changelog
68
+ rubygems_mfa_required: 'true'
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.4.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.1.6
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Opinionated RuboCop configuration used by ipepe.
88
+ test_files: []