workitcop 0.5.0 → 1.0.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 +4 -4
- data/CHANGELOG.md +13 -7
- data/README.md +2 -2
- data/config/default.yml +4 -29
- data/lib/rubocop/cop/workit/redundant_boolean_conditional.rb +66 -0
- data/lib/rubocop/cop/workit/rspec_redundant_http_status.rb +2 -2
- data/lib/workitcop/plugin.rb +29 -0
- data/lib/workitcop/version.rb +6 -2
- data/lib/workitcop.rb +4 -21
- metadata +31 -21
- data/lib/rubocop/cop/workit/comittee_assert_schema_confirm.rb +0 -16
- data/lib/rubocop/cop/workit/committee_expected_response_status_code.rb +0 -55
- data/lib/rubocop/cop/workit/mixin/rspec_explicit_help.rb +0 -110
- data/lib/rubocop/cop/workit/mixin/rspec_inflected_help.rb +0 -106
- data/lib/rubocop/cop/workit/mixin/rspec_predicate_matcher_base.rb +0 -54
- data/lib/rubocop/cop/workit/rspec_capybara_match_style.rb +0 -58
- data/lib/rubocop/cop/workit/rspec_capybara_predicate_matcher.rb +0 -83
- data/lib/rubocop/cop/workit/rspec_minitest_assertions.rb +0 -57
- data/lib/workitcop/inject.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8499ea6e1ca8f31565ebd4d21f96ac0ff2fb80452d1feab9e2e5450ccf62982
|
4
|
+
data.tar.gz: 2b78f6f4dd8468ad0088bc267d36a8c07960e520ecfe45836119c433ec46eddc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e06ec8780d78c301fdf242cc07832b2703ce82e2d9b4446a2d547418151b50c7f63a6b871f6e68b1a67c5360b9a2113ebc3c7123573f49bb48f7bc3208b1382
|
7
|
+
data.tar.gz: d07f8193e57c565313fc78fda0f263a309992d25a14d62ebee4f49a5dc1d14be6fe3ca99717296820381f40f975dfe943ac0c82a5f7c28dda34be4ac7ad934c3
|
data/CHANGELOG.md
CHANGED
@@ -2,38 +2,44 @@
|
|
2
2
|
|
3
3
|
## Unreleased
|
4
4
|
|
5
|
-
## 0.
|
5
|
+
## 1.0.0 (2025-08-14)
|
6
|
+
|
7
|
+
- Remove deprecated cops. ([@ydah])
|
8
|
+
- Pluginfy Workitcop. ([@ydah])
|
9
|
+
- Add new `Workit/RedundantBooleanConditional` cop. ([@ydah])
|
10
|
+
|
11
|
+
## 0.5.0 (2022-12-27)
|
6
12
|
|
7
13
|
- Deprecate `Workit/ComitteeAssertSchemaConfirm`. ([@ydah])
|
8
14
|
- Add new `Workit/CommitteeExpectedResponseStatusCode` and `Workit/RSpecRedundantHttpStatus`. ([@ydah])
|
9
15
|
|
10
|
-
## 0.4.2
|
16
|
+
## 0.4.2 (2022-12-12)
|
11
17
|
|
12
18
|
- Fix a false negative for `Workit/ComitteeAssertSchemaConfirm`. ([@ydah])
|
13
19
|
|
14
|
-
## 0.4.1
|
20
|
+
## 0.4.1 (2022-12-12)
|
15
21
|
|
16
22
|
- Add documentation. ([@ydah])
|
17
23
|
|
18
|
-
## 0.4.0
|
24
|
+
## 0.4.0 (2022-12-12)
|
19
25
|
|
20
26
|
- Add new `Workit/RSpecCapybaraPredicateMatcher` cop. ([@ydah])
|
21
27
|
- Add new `Workit/RSpecMinitestAssertions` cop. ([@ydah])
|
22
28
|
- Add new `Workit/RSpecCapybaraMatchStyle` cop. ([@ydah])
|
23
29
|
|
24
|
-
## 0.3.0
|
30
|
+
## 0.3.0 (2022-12-08)
|
25
31
|
|
26
32
|
- Fix an error for `Workit/RestrictOnSend` when not in class. ([@ydah])
|
27
33
|
- Add new `Workit/ComitteeAssertSchemaConfirm` cop. ([@ydah])
|
28
34
|
- Remove VersionAdded for default.yml. ([@ydah])
|
29
35
|
- Change default to `Enabled: false`. ([@ydah])
|
30
36
|
|
31
|
-
## 0.2.0
|
37
|
+
## 0.2.0 (2022-11-07)
|
32
38
|
|
33
39
|
- Add new `Workit/ActionArgs` cop. ([@ydah])
|
34
40
|
- Add new `Workit/NoopRescue` cop. ([@ydah])
|
35
41
|
|
36
|
-
## 0.1.0
|
42
|
+
## 0.1.0 (2022-07-04)
|
37
43
|
|
38
44
|
- Initial release
|
39
45
|
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Workitcop
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/workitcop) [](https://github.com/ydah/workitcop/actions/workflows/ci.yml)
|
4
4
|
|
5
|
-
|
5
|
+
A custom cop collection of working toolkits.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
data/config/default.yml
CHANGED
@@ -17,44 +17,19 @@ Workit/ActionArgs:
|
|
17
17
|
- cancel
|
18
18
|
- destroy
|
19
19
|
|
20
|
-
Workit/ComitteeAssertSchemaConfirm:
|
21
|
-
Description: |
|
22
|
-
Check for not pass expected response status code to check it against the corresponding schema explicitly.
|
23
|
-
Enabled: false
|
24
|
-
|
25
|
-
Workit/CommitteeExpectedResponseStatusCode:
|
26
|
-
Description: |
|
27
|
-
Check if the status code is specified as an argument to the method of the Committee
|
28
|
-
where the expected response status code is required.
|
29
|
-
Enabled: false
|
30
|
-
|
31
20
|
Workit/NoopRescue:
|
32
21
|
Description: "Check for suppress or ignore checked exception."
|
33
22
|
Enabled: false
|
34
23
|
|
24
|
+
Workit/RedundantBooleanConditional:
|
25
|
+
Description: "Checks for redundant boolean conditions."
|
26
|
+
Enabled: false
|
27
|
+
|
35
28
|
Workit/RestrictOnSend:
|
36
29
|
Description: |
|
37
30
|
Check for `RESTRICT_ON_SEND` is defined if `on_send` or `after_send` are defined.
|
38
31
|
Enabled: false
|
39
32
|
|
40
|
-
Workit/RSpecCapybaraMatchStyle:
|
41
|
-
Description: Checks for usage of deprecated style methods.
|
42
|
-
Enabled: false
|
43
|
-
|
44
|
-
Workit/RSpecCapybaraPredicateMatcher:
|
45
|
-
Description: Prefer using predicate matcher over using predicate method directly.
|
46
|
-
Enabled: false
|
47
|
-
Strict: true
|
48
|
-
EnforcedStyle: inflected
|
49
|
-
AllowedExplicitMatchers: []
|
50
|
-
SupportedStyles:
|
51
|
-
- inflected
|
52
|
-
- explicit
|
53
|
-
|
54
|
-
Workit/RSpecMinitestAssertions:
|
55
|
-
Description: Check if using Minitest matchers.
|
56
|
-
Enabled: false
|
57
|
-
|
58
33
|
Workit/RSpecRedundantHttpStatus:
|
59
34
|
Description: Check for validation of redundant response HTTP status codes.
|
60
35
|
Enabled: false
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Workit
|
6
|
+
# Checks for redundant boolean conditions.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# true if x == y
|
11
|
+
#
|
12
|
+
# # good
|
13
|
+
# x == y
|
14
|
+
#
|
15
|
+
class RedundantBooleanConditional < Base
|
16
|
+
extend AutoCorrector
|
17
|
+
|
18
|
+
MSG = "This conditional expression can just be replaced by `%<replaced>s`."
|
19
|
+
|
20
|
+
# @!method true_or_false?(node)
|
21
|
+
def_node_matcher :true_or_false?, <<~RUBY
|
22
|
+
({:true :false})
|
23
|
+
RUBY
|
24
|
+
|
25
|
+
def on_if(node)
|
26
|
+
return unless redundant?(node)
|
27
|
+
|
28
|
+
add_offense(node, message: offense_message(node)) do |corrector|
|
29
|
+
corrector.replace(node, replacement_condition(node))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def offense_message(node)
|
36
|
+
replacement = replacement_condition(node)
|
37
|
+
replaced = node.elsif? ? "\n#{replacement}" : replacement
|
38
|
+
|
39
|
+
format(MSG, replaced: replaced)
|
40
|
+
end
|
41
|
+
|
42
|
+
def redundant?(node)
|
43
|
+
return false if node.else? || node.elsif? || node.elsif_conditional? || node.ternary?
|
44
|
+
|
45
|
+
node.if_branch.true_type? || node.if_branch.false_type? # rubocop:disable InternalAffairs/NodeTypeMultiplePredicates
|
46
|
+
end
|
47
|
+
|
48
|
+
def replacement_condition(node)
|
49
|
+
condition = node.condition.source
|
50
|
+
expression = invert_expression?(node) ? "!(#{condition})" : condition
|
51
|
+
|
52
|
+
node.elsif? ? indented_else_node(expression, node) : expression
|
53
|
+
end
|
54
|
+
|
55
|
+
def invert_expression?(node)
|
56
|
+
(!node.elsif_conditional? && node.if? && node.if_branch.false_type?) ||
|
57
|
+
(node.unless? && node.if_branch.true_type?)
|
58
|
+
end
|
59
|
+
|
60
|
+
def indented_else_node(expression, node)
|
61
|
+
"else\n#{indentation(node)}#{expression}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -31,11 +31,11 @@ module RuboCop
|
|
31
31
|
$(send nil? :have_http_status (:int _))
|
32
32
|
PATTERN
|
33
33
|
|
34
|
-
def on_send(node)
|
34
|
+
def on_send(node) # rubocop:disable InternalAffairs/OnSendWithoutOnCSend
|
35
35
|
return if node.first_argument.nil?
|
36
36
|
|
37
37
|
have_http_status(node.parent) do |http_node|
|
38
|
-
return autocorrect(node, http_node.parent.
|
38
|
+
return autocorrect(node, http_node.parent.source_range)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "lint_roller"
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Workitcop
|
7
|
+
# A plugin that integrates Workitcop with RuboCop's plugin system.
|
8
|
+
class Plugin < LintRoller::Plugin
|
9
|
+
def about
|
10
|
+
LintRoller::About.new(
|
11
|
+
name: "workitcop",
|
12
|
+
version: Version::STRING,
|
13
|
+
homepage: "https://github.com/ydah/workitcop",
|
14
|
+
description: "A custom collection of working toolkits."
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def supported?(context)
|
19
|
+
context.engine == :rubocop
|
20
|
+
end
|
21
|
+
|
22
|
+
def rules(_context)
|
23
|
+
project_root = Pathname.new(__dir__).join("../..")
|
24
|
+
|
25
|
+
LintRoller::Rules.new(type: :path, config_format: :rubocop, value: project_root.join("config", "default.yml"))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/workitcop/version.rb
CHANGED
data/lib/workitcop.rb
CHANGED
@@ -1,29 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require_relative "workitcop/version"
|
3
|
+
require "rubocop"
|
5
4
|
|
6
|
-
require_relative "
|
7
|
-
require_relative "
|
8
|
-
require_relative "rubocop/cop/workit/mixin/rspec_predicate_matcher_base"
|
5
|
+
require_relative "workitcop/version"
|
6
|
+
require_relative "workitcop/plugin"
|
9
7
|
|
10
8
|
require_relative "rubocop/cop/workit/action_args"
|
11
|
-
require_relative "rubocop/cop/workit/committee_expected_response_status_code"
|
12
9
|
require_relative "rubocop/cop/workit/noop_rescue"
|
10
|
+
require_relative "rubocop/cop/workit/redundant_boolean_conditional"
|
13
11
|
require_relative "rubocop/cop/workit/restrict_on_send"
|
14
|
-
require_relative "rubocop/cop/workit/rspec_capybara_match_style"
|
15
|
-
require_relative "rubocop/cop/workit/rspec_capybara_predicate_matcher"
|
16
|
-
require_relative "rubocop/cop/workit/rspec_minitest_assertions"
|
17
12
|
require_relative "rubocop/cop/workit/rspec_redundant_http_status"
|
18
|
-
|
19
|
-
# deprecated
|
20
|
-
require_relative "rubocop/cop/workit/comittee_assert_schema_confirm"
|
21
|
-
|
22
|
-
module Workitcop
|
23
|
-
PROJECT_ROOT = ::Pathname.new(__dir__).parent.expand_path.freeze
|
24
|
-
CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
|
25
|
-
CONFIG = ::YAML.safe_load(CONFIG_DEFAULT.read).freeze
|
26
|
-
private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
|
27
|
-
end
|
28
|
-
|
29
|
-
Workitcop::Inject.defaults!
|
metadata
CHANGED
@@ -1,30 +1,48 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: workitcop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ydah
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
13
|
+
name: lint_roller
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
16
15
|
requirements:
|
17
16
|
- - "~>"
|
18
17
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
18
|
+
version: '1.1'
|
20
19
|
type: :runtime
|
21
20
|
prerelease: false
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
23
22
|
requirements:
|
24
23
|
- - "~>"
|
25
24
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
27
|
-
|
25
|
+
version: '1.1'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rubocop
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.72.1
|
33
|
+
- - "<"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '2.0'
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 1.72.1
|
43
|
+
- - "<"
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.0'
|
28
46
|
email:
|
29
47
|
- t.yudai92@gmail.com
|
30
48
|
executables: []
|
@@ -39,19 +57,12 @@ files:
|
|
39
57
|
- README.md
|
40
58
|
- config/default.yml
|
41
59
|
- lib/rubocop/cop/workit/action_args.rb
|
42
|
-
- lib/rubocop/cop/workit/comittee_assert_schema_confirm.rb
|
43
|
-
- lib/rubocop/cop/workit/committee_expected_response_status_code.rb
|
44
|
-
- lib/rubocop/cop/workit/mixin/rspec_explicit_help.rb
|
45
|
-
- lib/rubocop/cop/workit/mixin/rspec_inflected_help.rb
|
46
|
-
- lib/rubocop/cop/workit/mixin/rspec_predicate_matcher_base.rb
|
47
60
|
- lib/rubocop/cop/workit/noop_rescue.rb
|
61
|
+
- lib/rubocop/cop/workit/redundant_boolean_conditional.rb
|
48
62
|
- lib/rubocop/cop/workit/restrict_on_send.rb
|
49
|
-
- lib/rubocop/cop/workit/rspec_capybara_match_style.rb
|
50
|
-
- lib/rubocop/cop/workit/rspec_capybara_predicate_matcher.rb
|
51
|
-
- lib/rubocop/cop/workit/rspec_minitest_assertions.rb
|
52
63
|
- lib/rubocop/cop/workit/rspec_redundant_http_status.rb
|
53
64
|
- lib/workitcop.rb
|
54
|
-
- lib/workitcop/
|
65
|
+
- lib/workitcop/plugin.rb
|
55
66
|
- lib/workitcop/version.rb
|
56
67
|
homepage: https://github.com/ydah/workitcop
|
57
68
|
licenses:
|
@@ -61,7 +72,7 @@ metadata:
|
|
61
72
|
source_code_uri: https://github.com/ydah/workitcop
|
62
73
|
changelog_uri: https://github.com/ydah/workitcop/blob/main/CHANGELOG.md
|
63
74
|
rubygems_mfa_required: 'true'
|
64
|
-
|
75
|
+
default_lint_roller_plugin: RuboCop::Workitcop::Plugin
|
65
76
|
rdoc_options: []
|
66
77
|
require_paths:
|
67
78
|
- lib
|
@@ -69,15 +80,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
80
|
requirements:
|
70
81
|
- - ">="
|
71
82
|
- !ruby/object:Gem::Version
|
72
|
-
version: 2.
|
83
|
+
version: 2.7.0
|
73
84
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
85
|
requirements:
|
75
86
|
- - ">="
|
76
87
|
- !ruby/object:Gem::Version
|
77
88
|
version: '0'
|
78
89
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
80
|
-
signing_key:
|
90
|
+
rubygems_version: 3.6.9
|
81
91
|
specification_version: 4
|
82
|
-
summary:
|
92
|
+
summary: A custom cop collection of working toolkits.
|
83
93
|
test_files: []
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Workit
|
6
|
-
# Check for not pass expected response status code to check it against the corresponding schema explicitly.
|
7
|
-
#
|
8
|
-
# This cop is deprecated.
|
9
|
-
#
|
10
|
-
# @see https://www.rubydoc.info/gems/workitcop/RuboCop/Cop/Workit/CommitteeExpectedResponseStatusCode
|
11
|
-
#
|
12
|
-
class ComitteeAssertSchemaConfirm < CommitteeExpectedResponseStatusCode
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Workit
|
6
|
-
# Check if the status code is specified as an argument to the method of the Committee
|
7
|
-
# where the expected response status code is required.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# # bad
|
11
|
-
# it 'something' do
|
12
|
-
# subject
|
13
|
-
# expect(response).to have_http_status 400
|
14
|
-
# assert_schema_conform
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# # good
|
18
|
-
# it 'something' do
|
19
|
-
# subject
|
20
|
-
# assert_schema_conform(400)
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
class CommitteeExpectedResponseStatusCode < Base
|
24
|
-
include RangeHelp
|
25
|
-
extend AutoCorrector
|
26
|
-
|
27
|
-
MSG = "Specify the HTTP status code of the expected response as an argument."
|
28
|
-
RESTRICT_ON_SEND = %i[assert_schema_conform assert_response_schema_confirm].freeze
|
29
|
-
|
30
|
-
# @!method have_http_status(node)
|
31
|
-
def_node_search :have_http_status, <<~PATTERN
|
32
|
-
(send nil? :have_http_status (:int $_))
|
33
|
-
PATTERN
|
34
|
-
|
35
|
-
def on_send(node)
|
36
|
-
return if node.arguments?
|
37
|
-
|
38
|
-
have_http_status(node.parent) do |status|
|
39
|
-
return autocorrect(node, status)
|
40
|
-
end
|
41
|
-
|
42
|
-
add_offense(node)
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
def autocorrect(node, status)
|
48
|
-
add_offense(node) do |corrector|
|
49
|
-
corrector.insert_after(node, "(#{status})")
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Workit
|
6
|
-
# A helper for `explicit` style
|
7
|
-
module RSpecExplicitHelp
|
8
|
-
include RuboCop::RSpec::Language
|
9
|
-
extend NodePattern::Macros
|
10
|
-
|
11
|
-
MSG_EXPLICIT = "Prefer using `%<predicate_name>s` over " \
|
12
|
-
"`%<matcher_name>s` matcher."
|
13
|
-
BUILT_IN_MATCHERS = %w[
|
14
|
-
be_truthy be_falsey be_falsy
|
15
|
-
have_attributes have_received
|
16
|
-
be_between be_within
|
17
|
-
].freeze
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def allowed_explicit_matchers
|
22
|
-
cop_config.fetch("AllowedExplicitMatchers", []) + BUILT_IN_MATCHERS
|
23
|
-
end
|
24
|
-
|
25
|
-
def check_explicit(node)
|
26
|
-
predicate_matcher_block?(node) do |actual, matcher|
|
27
|
-
add_offense(node, message: message_explicit(matcher)) do |corrector|
|
28
|
-
to_node = node.send_node
|
29
|
-
corrector_explicit(corrector, to_node, actual, matcher, to_node)
|
30
|
-
end
|
31
|
-
ignore_node(node.children.first)
|
32
|
-
return
|
33
|
-
end
|
34
|
-
|
35
|
-
return if part_of_ignored_node?(node)
|
36
|
-
|
37
|
-
predicate_matcher?(node) do |actual, matcher|
|
38
|
-
add_offense(node, message: message_explicit(matcher)) do |corrector|
|
39
|
-
corrector_explicit(corrector, node, actual, matcher, matcher)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
# @!method predicate_matcher?(node)
|
45
|
-
def_node_matcher :predicate_matcher?, <<-PATTERN
|
46
|
-
(send
|
47
|
-
(send nil? :expect $!nil?)
|
48
|
-
#Runners.all
|
49
|
-
{$(send nil? #predicate_matcher_name? ...)
|
50
|
-
(block $(send nil? #predicate_matcher_name? ...) ...)})
|
51
|
-
PATTERN
|
52
|
-
|
53
|
-
# @!method predicate_matcher_block?(node)
|
54
|
-
def_node_matcher :predicate_matcher_block?, <<-PATTERN
|
55
|
-
(block
|
56
|
-
(send
|
57
|
-
(send nil? :expect $!nil?)
|
58
|
-
#Runners.all
|
59
|
-
$(send nil? #predicate_matcher_name?))
|
60
|
-
...)
|
61
|
-
PATTERN
|
62
|
-
|
63
|
-
def predicate_matcher_name?(name)
|
64
|
-
raise ::NotImplementedError
|
65
|
-
end
|
66
|
-
|
67
|
-
def message_explicit(matcher)
|
68
|
-
format(MSG_EXPLICIT,
|
69
|
-
predicate_name: to_predicate_method(matcher.method_name),
|
70
|
-
matcher_name: matcher.method_name)
|
71
|
-
end
|
72
|
-
|
73
|
-
def corrector_explicit(corrector, to_node, actual, matcher, block_child)
|
74
|
-
replacement_matcher = replacement_matcher(to_node)
|
75
|
-
corrector.replace(matcher.loc.expression, replacement_matcher)
|
76
|
-
move_predicate(corrector, actual, matcher, block_child)
|
77
|
-
corrector.replace(to_node.loc.selector, "to")
|
78
|
-
end
|
79
|
-
|
80
|
-
def move_predicate(corrector, actual, matcher, block_child)
|
81
|
-
predicate = to_predicate_method(matcher.method_name)
|
82
|
-
args = args_loc(matcher).source
|
83
|
-
block_loc = block_loc(block_child)
|
84
|
-
block = block_loc ? block_loc.source : ""
|
85
|
-
|
86
|
-
corrector.remove(block_loc) if block_loc
|
87
|
-
corrector.insert_after(actual.loc.expression,
|
88
|
-
".#{predicate}" + args + block)
|
89
|
-
end
|
90
|
-
|
91
|
-
def to_predicate_method(matcher)
|
92
|
-
raise ::NotImplementedError
|
93
|
-
end
|
94
|
-
|
95
|
-
def replacement_matcher(node)
|
96
|
-
case [cop_config["Strict"], node.method?(:to)]
|
97
|
-
when [true, true]
|
98
|
-
"be(true)"
|
99
|
-
when [true, false]
|
100
|
-
"be(false)"
|
101
|
-
when [false, true]
|
102
|
-
"be_truthy"
|
103
|
-
when [false, false]
|
104
|
-
"be_falsey"
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
@@ -1,106 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Workit
|
6
|
-
# A helper for `inflected` style
|
7
|
-
module RSpecInflectedHelp
|
8
|
-
include RuboCop::RSpec::Language
|
9
|
-
extend NodePattern::Macros
|
10
|
-
|
11
|
-
MSG_INFLECTED = "Prefer using `%<matcher_name>s` matcher over " \
|
12
|
-
"`%<predicate_name>s`."
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
def check_inflected(node)
|
17
|
-
predicate_in_actual?(node) do |predicate, to, matcher|
|
18
|
-
msg = message_inflected(predicate)
|
19
|
-
add_offense(node, message: msg) do |corrector|
|
20
|
-
remove_predicate(corrector, predicate)
|
21
|
-
corrector.replace(node.loc.selector,
|
22
|
-
true?(to, matcher) ? "to" : "not_to")
|
23
|
-
rewrite_matcher(corrector, predicate, matcher)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# @!method predicate_in_actual?(node)
|
29
|
-
def_node_matcher :predicate_in_actual?, <<-PATTERN
|
30
|
-
(send
|
31
|
-
(send nil? :expect {
|
32
|
-
(block $(send !nil? #predicate? ...) ...)
|
33
|
-
$(send !nil? #predicate? ...)})
|
34
|
-
$#Runners.all
|
35
|
-
$#boolean_matcher?)
|
36
|
-
PATTERN
|
37
|
-
|
38
|
-
# @!method be_bool?(node)
|
39
|
-
def_node_matcher :be_bool?, <<-PATTERN
|
40
|
-
(send nil? {:be :eq :eql :equal} {true false})
|
41
|
-
PATTERN
|
42
|
-
|
43
|
-
# @!method be_boolthy?(node)
|
44
|
-
def_node_matcher :be_boolthy?, <<-PATTERN
|
45
|
-
(send nil? {:be_truthy :be_falsey :be_falsy :a_truthy_value :a_falsey_value :a_falsy_value})
|
46
|
-
PATTERN
|
47
|
-
|
48
|
-
def boolean_matcher?(node)
|
49
|
-
if cop_config["Strict"]
|
50
|
-
be_boolthy?(node)
|
51
|
-
else
|
52
|
-
be_bool?(node) || be_boolthy?(node)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def predicate?(sym)
|
57
|
-
raise ::NotImplementedError
|
58
|
-
end
|
59
|
-
|
60
|
-
def message_inflected(predicate)
|
61
|
-
format(MSG_INFLECTED,
|
62
|
-
predicate_name: predicate.method_name,
|
63
|
-
matcher_name: to_predicate_matcher(predicate.method_name))
|
64
|
-
end
|
65
|
-
|
66
|
-
def to_predicate_matcher(name)
|
67
|
-
raise ::NotImplementedError
|
68
|
-
end
|
69
|
-
|
70
|
-
def remove_predicate(corrector, predicate)
|
71
|
-
range = predicate.loc.dot.with(
|
72
|
-
end_pos: predicate.loc.expression.end_pos
|
73
|
-
)
|
74
|
-
|
75
|
-
corrector.remove(range)
|
76
|
-
|
77
|
-
block_range = block_loc(predicate)
|
78
|
-
corrector.remove(block_range) if block_range
|
79
|
-
end
|
80
|
-
|
81
|
-
def rewrite_matcher(corrector, predicate, matcher)
|
82
|
-
args = args_loc(predicate).source
|
83
|
-
block_loc = block_loc(predicate)
|
84
|
-
block = block_loc ? block_loc.source : ""
|
85
|
-
|
86
|
-
corrector.replace(
|
87
|
-
matcher.loc.expression,
|
88
|
-
to_predicate_matcher(predicate.method_name) + args + block
|
89
|
-
)
|
90
|
-
end
|
91
|
-
|
92
|
-
def true?(to_symbol, matcher)
|
93
|
-
result = case matcher.method_name
|
94
|
-
when :be, :eq
|
95
|
-
matcher.first_argument.true_type?
|
96
|
-
when :be_truthy, :a_truthy_value
|
97
|
-
true
|
98
|
-
when :be_falsey, :be_falsy, :a_falsey_value, :a_falsy_value
|
99
|
-
false
|
100
|
-
end
|
101
|
-
to_symbol == :to ? result : !result
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Workit
|
6
|
-
# Helper methods for Predicate matcher.
|
7
|
-
module RSpecPredicateMatcherBase
|
8
|
-
include ConfigurableEnforcedStyle
|
9
|
-
include RSpecInflectedHelp
|
10
|
-
include RSpecExplicitHelp
|
11
|
-
|
12
|
-
RESTRICT_ON_SEND = %i[to to_not not_to].freeze
|
13
|
-
|
14
|
-
def on_send(node)
|
15
|
-
case style
|
16
|
-
when :inflected
|
17
|
-
check_inflected(node)
|
18
|
-
when :explicit
|
19
|
-
check_explicit(node)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
24
|
-
check_explicit(node) if style == :explicit
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
# returns args location with whitespace
|
30
|
-
# @example
|
31
|
-
# foo 1, 2
|
32
|
-
# ^^^^^
|
33
|
-
def args_loc(send_node)
|
34
|
-
send_node.loc.selector.end.with(
|
35
|
-
end_pos: send_node.loc.expression.end_pos
|
36
|
-
)
|
37
|
-
end
|
38
|
-
|
39
|
-
# returns block location with whitespace
|
40
|
-
# @example
|
41
|
-
# foo { bar }
|
42
|
-
# ^^^^^^^^
|
43
|
-
def block_loc(send_node)
|
44
|
-
parent = send_node.parent
|
45
|
-
return unless parent.block_type?
|
46
|
-
|
47
|
-
send_node.loc.expression.end.with(
|
48
|
-
end_pos: parent.loc.expression.end_pos
|
49
|
-
)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Workit
|
6
|
-
# Checks for usage of deprecated style methods.
|
7
|
-
#
|
8
|
-
# @example when using `assert_style`
|
9
|
-
# # bad
|
10
|
-
# page.find(:css, '#first').assert_style(display: 'block')
|
11
|
-
#
|
12
|
-
# # good
|
13
|
-
# page.find(:css, '#first').assert_matches_style(display: 'block')
|
14
|
-
#
|
15
|
-
# @example when using `has_style?`
|
16
|
-
# # bad
|
17
|
-
# expect(page.find(:css, 'first')
|
18
|
-
# .has_style?(display: 'block')).to be true
|
19
|
-
#
|
20
|
-
# # good
|
21
|
-
# expect(page.find(:css, 'first')
|
22
|
-
# .matches_style?(display: 'block')).to be true
|
23
|
-
#
|
24
|
-
# @example when using `have_style`
|
25
|
-
# # bad
|
26
|
-
# expect(page).to have_style(display: 'block')
|
27
|
-
#
|
28
|
-
# # good
|
29
|
-
# expect(page).to match_style(display: 'block')
|
30
|
-
#
|
31
|
-
class RSpecCapybaraMatchStyle < Base
|
32
|
-
extend AutoCorrector
|
33
|
-
|
34
|
-
MSG = "Use `%<good>s` instead of `%<bad>s`."
|
35
|
-
RESTRICT_ON_SEND = %i[assert_style has_style? have_style].freeze
|
36
|
-
PREFERRED_METHOD = {
|
37
|
-
"assert_style" => "assert_matches_style",
|
38
|
-
"has_style?" => "matches_style?",
|
39
|
-
"have_style" => "match_style"
|
40
|
-
}.freeze
|
41
|
-
|
42
|
-
def on_send(node)
|
43
|
-
method_node = node.loc.selector
|
44
|
-
add_offense(method_node) do |corrector|
|
45
|
-
corrector.replace(method_node,
|
46
|
-
PREFERRED_METHOD[method_node.source])
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
def message(node)
|
53
|
-
format(MSG, good: PREFERRED_METHOD[node.source], bad: node.source)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
@@ -1,83 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Workit
|
6
|
-
# Prefer using predicate matcher over using predicate method directly.
|
7
|
-
#
|
8
|
-
# Capybara defines magic matchers for predicate methods.
|
9
|
-
# This cop recommends to use the predicate matcher instead of using
|
10
|
-
# predicate method directly.
|
11
|
-
#
|
12
|
-
# @example Strict: true, EnforcedStyle: inflected (default)
|
13
|
-
# # bad
|
14
|
-
# expect(foo.matches_css?(bar: 'baz')).to be_truthy
|
15
|
-
# expect(foo.matches_selector?(bar: 'baz')).to be_truthy
|
16
|
-
# expect(foo.matches_style?(bar: 'baz')).to be_truthy
|
17
|
-
# expect(foo.matches_xpath?(bar: 'baz')).to be_truthy
|
18
|
-
#
|
19
|
-
# # good
|
20
|
-
# expect(foo).to match_css(bar: 'baz')
|
21
|
-
# expect(foo).to match_selector(bar: 'baz')
|
22
|
-
# expect(foo).to match_style(bar: 'baz')
|
23
|
-
# expect(foo).to match_xpath(bar: 'baz')
|
24
|
-
#
|
25
|
-
# # also good - It checks "true" strictly.
|
26
|
-
# expect(foo.matches_style?(bar: 'baz')).to be(true)
|
27
|
-
#
|
28
|
-
# @example Strict: false, EnforcedStyle: inflected
|
29
|
-
# # bad
|
30
|
-
# expect(foo.matches_style?(bar: 'baz')).to be_truthy
|
31
|
-
# expect(foo.matches_style?(bar: 'baz')).to be(true)
|
32
|
-
#
|
33
|
-
# # good
|
34
|
-
# expect(foo).to match_style(bar: 'baz')
|
35
|
-
#
|
36
|
-
# @example Strict: true, EnforcedStyle: explicit
|
37
|
-
# # bad
|
38
|
-
# expect(foo).to match_style(bar: 'baz')
|
39
|
-
#
|
40
|
-
# # good - the above code is rewritten to it by this cop
|
41
|
-
# expect(foo.matches_style?(bar: 'baz')).to be(true)
|
42
|
-
#
|
43
|
-
# @example Strict: false, EnforcedStyle: explicit
|
44
|
-
# # bad
|
45
|
-
# expect(foo).to match_style(bar: 'baz')
|
46
|
-
#
|
47
|
-
# # good - the above code is rewritten to it by this cop
|
48
|
-
# expect(foo.matches_style?(bar: 'baz')).to be_truthy
|
49
|
-
#
|
50
|
-
class RSpecCapybaraPredicateMatcher < Base
|
51
|
-
extend AutoCorrector
|
52
|
-
include RuboCop::Cop::Workit::RSpecPredicateMatcherBase
|
53
|
-
|
54
|
-
MATCHER_SUFFIX = %w[css selector style xpath].freeze
|
55
|
-
INFLECTED_MATCHER = MATCHER_SUFFIX.each.map do |suffix|
|
56
|
-
"match_#{suffix}"
|
57
|
-
end.freeze
|
58
|
-
EXPLICIT_MATCHER = MATCHER_SUFFIX.each.map do |suffix|
|
59
|
-
"matches_#{suffix}?"
|
60
|
-
end.freeze
|
61
|
-
|
62
|
-
def predicate_matcher_name?(name)
|
63
|
-
name = name.to_s
|
64
|
-
return false if allowed_explicit_matchers.include?(name)
|
65
|
-
|
66
|
-
INFLECTED_MATCHER.include?(name)
|
67
|
-
end
|
68
|
-
|
69
|
-
def to_predicate_matcher(name)
|
70
|
-
name.to_s.sub("matches_", "match_")[0..-2]
|
71
|
-
end
|
72
|
-
|
73
|
-
def predicate?(sym)
|
74
|
-
EXPLICIT_MATCHER.include?(sym.to_s)
|
75
|
-
end
|
76
|
-
|
77
|
-
def to_predicate_method(matcher)
|
78
|
-
"#{matcher.to_s.sub("match_", "matches_")}?"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Workit
|
6
|
-
# Check if using Minitest matchers.
|
7
|
-
#
|
8
|
-
# @example
|
9
|
-
# # bad
|
10
|
-
# assert_equal(a, b)
|
11
|
-
# assert_equal a, b, "must be equal"
|
12
|
-
# refute_equal(a, b)
|
13
|
-
#
|
14
|
-
# # good
|
15
|
-
# expect(a).to eq(b)
|
16
|
-
# expect(a).to(eq(b), "must be equal")
|
17
|
-
# expect(a).not_to eq(b)
|
18
|
-
#
|
19
|
-
class RSpecMinitestAssertions < Base
|
20
|
-
extend AutoCorrector
|
21
|
-
|
22
|
-
MSG = "Use `%<prefer>s`."
|
23
|
-
RESTRICT_ON_SEND = %i[assert_equal refute_equal].freeze
|
24
|
-
|
25
|
-
# @!method minitest_assertion(node)
|
26
|
-
def_node_matcher :minitest_assertion, <<-PATTERN
|
27
|
-
(send nil? {:assert_equal :refute_equal} $_ $_ $_?)
|
28
|
-
PATTERN
|
29
|
-
|
30
|
-
def on_send(node)
|
31
|
-
minitest_assertion(node) do |expected, actual, failure_message|
|
32
|
-
prefer = replacement(node, expected, actual, failure_message.first)
|
33
|
-
add_offense(node, message: message(prefer)) do |corrector|
|
34
|
-
corrector.replace(node, prefer)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
def replacement(node, expected, actual, failure_message)
|
42
|
-
runner = node.method?(:assert_equal) ? "to" : "not_to"
|
43
|
-
if failure_message.nil?
|
44
|
-
"expect(#{expected.source}).#{runner} eq(#{actual.source})"
|
45
|
-
else
|
46
|
-
"expect(#{expected.source}).#{runner}(eq(#{actual.source}), " \
|
47
|
-
"#{failure_message.source})"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def message(prefer)
|
52
|
-
format(MSG, prefer: prefer)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
data/lib/workitcop/inject.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "rubocop"
|
4
|
-
require "rubocop-rspec"
|
5
|
-
|
6
|
-
module Workitcop
|
7
|
-
module Inject
|
8
|
-
def self.defaults!
|
9
|
-
path = CONFIG_DEFAULT.to_s
|
10
|
-
hash = ::RuboCop::ConfigLoader.send(:load_yaml_configuration, path)
|
11
|
-
config = ::RuboCop::Config.new(hash, path).tap(&:make_excludes_absolute)
|
12
|
-
puts "configuration from #{path}" if ::RuboCop::ConfigLoader.debug?
|
13
|
-
config = ::RuboCop::ConfigLoader.merge_with_default(config, path)
|
14
|
-
::RuboCop::ConfigLoader.instance_variable_set(:@default_configuration, config)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|