rubocop-avoid_unless 0.1.3
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 +7 -0
- data/LICENSE.txt +21 -0
- data/config/default.yml +4 -0
- data/lib/rubocop/avoid_unless/plugin.rb +29 -0
- data/lib/rubocop/avoid_unless/version.rb +7 -0
- data/lib/rubocop/cop/style/avoid_unless.rb +129 -0
- data/lib/rubocop-avoid_unless.rb +6 -0
- metadata +73 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: cd4984b811f306fa9d68491d5db64f41e611dea914b38ab7d2540a54ecb2f241
|
|
4
|
+
data.tar.gz: 24c0639d10125868c066613d0265d2f737f800b1143e3bafe5fbe33a35bb2bf1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ec8b3927b1b4f0f754252930dcefcf32c69eaa80e735c5c5f6ff01ff596cd544ecb763abe81d209fa5a6d44bcaded018d2bb6b5ecd80b5371fdd6fe0daf910c2
|
|
7
|
+
data.tar.gz: b6e42242b8de5506a2a8a0e8e943c22a1d5134be7afeb5f8d62f4dcdad6c6e9d8c1353f70c55dc28b09e04d7cfae8eae763673d2c7e1fc87d91e6067923d631c
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Endoze
|
|
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/config/default.yml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lint_roller"
|
|
4
|
+
|
|
5
|
+
module RuboCop
|
|
6
|
+
module AvoidUnless
|
|
7
|
+
class Plugin < LintRoller::Plugin
|
|
8
|
+
def about
|
|
9
|
+
LintRoller::About.new(
|
|
10
|
+
name: "rubocop-avoid_unless",
|
|
11
|
+
version: VERSION,
|
|
12
|
+
homepage: "https://github.com/endoze/rubocop-avoid_unless"
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def supported?(context)
|
|
17
|
+
context.engine == :rubocop
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def rules(context)
|
|
21
|
+
LintRoller::Rules.new(
|
|
22
|
+
type: :path,
|
|
23
|
+
config_format: :rubocop,
|
|
24
|
+
value: File.join(__dir__, "..", "..", "..", "config", "default.yml")
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Style
|
|
6
|
+
# Disallows the use of `unless` in all forms.
|
|
7
|
+
# Use `if` with an inverse or negated condition instead.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# # bad
|
|
11
|
+
# do_something unless items.any?
|
|
12
|
+
# return unless value.present?
|
|
13
|
+
# return unless condition
|
|
14
|
+
#
|
|
15
|
+
# # good
|
|
16
|
+
# do_something if items.none?
|
|
17
|
+
# return if value.blank?
|
|
18
|
+
# return if !condition
|
|
19
|
+
class AvoidUnless < Base
|
|
20
|
+
extend AutoCorrector
|
|
21
|
+
|
|
22
|
+
MSG = "Avoid `unless`. Use `if` with an inverse or negated condition instead."
|
|
23
|
+
|
|
24
|
+
INVERSE_METHODS = {
|
|
25
|
+
any?: :none?,
|
|
26
|
+
none?: :any?,
|
|
27
|
+
present?: :blank?,
|
|
28
|
+
blank?: :present?,
|
|
29
|
+
empty?: :present?,
|
|
30
|
+
even?: :odd?,
|
|
31
|
+
odd?: :even?,
|
|
32
|
+
zero?: :nonzero?,
|
|
33
|
+
nonzero?: :zero?,
|
|
34
|
+
true?: :false?,
|
|
35
|
+
false?: :true?
|
|
36
|
+
}.freeze
|
|
37
|
+
|
|
38
|
+
INVERSE_METHODS_WITH_ARGS = {
|
|
39
|
+
include?: :exclude?,
|
|
40
|
+
exclude?: :include?
|
|
41
|
+
}.freeze
|
|
42
|
+
|
|
43
|
+
INVERSE_OPERATORS = {
|
|
44
|
+
:== => :!=,
|
|
45
|
+
:!= => :==,
|
|
46
|
+
:> => :<=,
|
|
47
|
+
:< => :>=,
|
|
48
|
+
:>= => :<,
|
|
49
|
+
:<= => :>,
|
|
50
|
+
:=~ => :!~,
|
|
51
|
+
:!~ => :=~
|
|
52
|
+
}.freeze
|
|
53
|
+
|
|
54
|
+
def on_if(node)
|
|
55
|
+
return if !node.unless?
|
|
56
|
+
|
|
57
|
+
add_offense(node) do |corrector|
|
|
58
|
+
condition = node.condition
|
|
59
|
+
replacement = inverted_condition(condition)
|
|
60
|
+
corrector.replace(node.loc.keyword, "if")
|
|
61
|
+
corrector.replace(condition, replacement)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def inverted_condition(node)
|
|
68
|
+
case node.type
|
|
69
|
+
when :send
|
|
70
|
+
invert_send(node)
|
|
71
|
+
when :begin
|
|
72
|
+
# Parenthesized expression: (expr)
|
|
73
|
+
inner = node.children.first
|
|
74
|
+
"(#{inverted_condition(inner)})"
|
|
75
|
+
when :or
|
|
76
|
+
# De Morgan: !(a || b) => !a && !b
|
|
77
|
+
lhs, rhs = *node
|
|
78
|
+
"#{inverted_condition(lhs)} && #{inverted_condition(rhs)}"
|
|
79
|
+
when :and
|
|
80
|
+
# De Morgan: !(a && b) => !a || !b
|
|
81
|
+
lhs, rhs = *node
|
|
82
|
+
"#{inverted_condition(lhs)} || #{inverted_condition(rhs)}"
|
|
83
|
+
else
|
|
84
|
+
negate(node)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def invert_send(node)
|
|
89
|
+
receiver, method_name, *args = *node
|
|
90
|
+
|
|
91
|
+
if method_name == :!
|
|
92
|
+
# unless !x => if x
|
|
93
|
+
return receiver.source
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
if (inverse = INVERSE_OPERATORS[method_name]) && args.size == 1
|
|
97
|
+
return "#{receiver.source} #{inverse} #{args.first.source}"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
if (inverse = INVERSE_METHODS[method_name]) && args.empty?
|
|
101
|
+
receiver_source = receiver ? "#{receiver.source}." : ""
|
|
102
|
+
return "#{receiver_source}#{inverse}"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
if (inverse = INVERSE_METHODS_WITH_ARGS[method_name]) && args.any?
|
|
106
|
+
receiver_source = receiver ? "#{receiver.source}." : ""
|
|
107
|
+
args_source = args.map(&:source).join(", ")
|
|
108
|
+
|
|
109
|
+
return "#{receiver_source}#{inverse}(#{args_source})"
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
negate(node)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def negate(node)
|
|
116
|
+
if requires_parens_for_negation?(node)
|
|
117
|
+
"!(#{node.source})"
|
|
118
|
+
else
|
|
119
|
+
"!#{node.source}"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def requires_parens_for_negation?(node)
|
|
124
|
+
node.or_type? || node.and_type?
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rubocop-avoid_unless
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.3
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Endoze
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rubocop
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '1.72'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '1.72'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: lint_roller
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1.1'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1.1'
|
|
40
|
+
description: A RuboCop extension that provides a Style/AvoidUnless cop. It flags all
|
|
41
|
+
uses of `unless` and auto-corrects them to `if` with an inverse or negated condition.
|
|
42
|
+
executables: []
|
|
43
|
+
extensions: []
|
|
44
|
+
extra_rdoc_files: []
|
|
45
|
+
files:
|
|
46
|
+
- LICENSE.txt
|
|
47
|
+
- config/default.yml
|
|
48
|
+
- lib/rubocop-avoid_unless.rb
|
|
49
|
+
- lib/rubocop/avoid_unless/plugin.rb
|
|
50
|
+
- lib/rubocop/avoid_unless/version.rb
|
|
51
|
+
- lib/rubocop/cop/style/avoid_unless.rb
|
|
52
|
+
homepage: https://github.com/endoze/rubocop-avoid_unless
|
|
53
|
+
licenses:
|
|
54
|
+
- MIT
|
|
55
|
+
metadata: {}
|
|
56
|
+
rdoc_options: []
|
|
57
|
+
require_paths:
|
|
58
|
+
- lib
|
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '3.1'
|
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
requirements: []
|
|
70
|
+
rubygems_version: 3.6.9
|
|
71
|
+
specification_version: 4
|
|
72
|
+
summary: RuboCop cop that disallows `unless` in favor of `if` with inverted conditions
|
|
73
|
+
test_files: []
|