workitcop 0.4.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79371e45e2687e40b8c948ff054876d913874e37afec5976a0a87321b8d366fd
4
- data.tar.gz: 1b7d4eb18afc0e5aacb18858b5fde8528afd27e37c06d12153ce44da043772f5
3
+ metadata.gz: e8499ea6e1ca8f31565ebd4d21f96ac0ff2fb80452d1feab9e2e5450ccf62982
4
+ data.tar.gz: 2b78f6f4dd8468ad0088bc267d36a8c07960e520ecfe45836119c433ec46eddc
5
5
  SHA512:
6
- metadata.gz: abf0954f07328af9294526a90d474ece1b2a385e43424daabc6e975c2c3ff9199a3d86666cdbc71224ed9da878d71958cf1123e053b469c19f8f3b9b04c8609d
7
- data.tar.gz: fc02dae95dda1045deaa1fe766a43249435a8f3afe4234714aa80622cacfc28f6eb4e288b80a55f0d55fad9451d0ec5858619f26b6cb3ede6f147265585663e7
6
+ metadata.gz: 6e06ec8780d78c301fdf242cc07832b2703ce82e2d9b4446a2d547418151b50c7f63a6b871f6e68b1a67c5360b9a2113ebc3c7123573f49bb48f7bc3208b1382
7
+ data.tar.gz: d07f8193e57c565313fc78fda0f263a309992d25a14d62ebee4f49a5dc1d14be6fe3ca99717296820381f40f975dfe943ac0c82a5f7c28dda34be4ac7ad934c3
data/CHANGELOG.md CHANGED
@@ -2,33 +2,44 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
- ## 0.4.2 - 2022-12-12
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)
12
+
13
+ - Deprecate `Workit/ComitteeAssertSchemaConfirm`. ([@ydah])
14
+ - Add new `Workit/CommitteeExpectedResponseStatusCode` and `Workit/RSpecRedundantHttpStatus`. ([@ydah])
15
+
16
+ ## 0.4.2 (2022-12-12)
6
17
 
7
18
  - Fix a false negative for `Workit/ComitteeAssertSchemaConfirm`. ([@ydah])
8
19
 
9
- ## 0.4.1 - 2022-12-12
20
+ ## 0.4.1 (2022-12-12)
10
21
 
11
22
  - Add documentation. ([@ydah])
12
23
 
13
- ## 0.4.0 - 2022-12-12
24
+ ## 0.4.0 (2022-12-12)
14
25
 
15
26
  - Add new `Workit/RSpecCapybaraPredicateMatcher` cop. ([@ydah])
16
27
  - Add new `Workit/RSpecMinitestAssertions` cop. ([@ydah])
17
28
  - Add new `Workit/RSpecCapybaraMatchStyle` cop. ([@ydah])
18
29
 
19
- ## 0.3.0 - 2022-12-08
30
+ ## 0.3.0 (2022-12-08)
20
31
 
21
32
  - Fix an error for `Workit/RestrictOnSend` when not in class. ([@ydah])
22
33
  - Add new `Workit/ComitteeAssertSchemaConfirm` cop. ([@ydah])
23
34
  - Remove VersionAdded for default.yml. ([@ydah])
24
35
  - Change default to `Enabled: false`. ([@ydah])
25
36
 
26
- ## 0.2.0 - 2022-11-07
37
+ ## 0.2.0 (2022-11-07)
27
38
 
28
39
  - Add new `Workit/ActionArgs` cop. ([@ydah])
29
40
  - Add new `Workit/NoopRescue` cop. ([@ydah])
30
41
 
31
- ## 0.1.0 - 2022-07-04
42
+ ## 0.1.0 (2022-07-04)
32
43
 
33
44
  - Initial release
34
45
 
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
- # Workitcop [![Gem Version](https://badge.fury.io/rb/workitcop.svg)](https://badge.fury.io/rb/workitcop) [![CI](https://github.com/ydah/workitcop/actions/workflows/ci.yml/badge.svg)](https://github.com/ydah/workitcop/actions/workflows/ci.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/77fd345a1f0e8ab706ed/maintainability)](https://codeclimate.com/github/ydah/workitcop/maintainability)
1
+ # Workitcop
2
2
 
3
- Custom cops for [RuboCop](https://github.com/rubocop/rubocop).
3
+ [![Gem Version](https://badge.fury.io/rb/workitcop.svg)](https://badge.fury.io/rb/workitcop) [![CI](https://github.com/ydah/workitcop/actions/workflows/ci.yml/badge.svg)](https://github.com/ydah/workitcop/actions/workflows/ci.yml)
4
+
5
+ A custom cop collection of working toolkits.
4
6
 
5
7
  ## Installation
6
8
 
@@ -16,28 +18,13 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
18
  gem install workitcop
17
19
  ```
18
20
 
19
- ## Usage
20
-
21
- You need to tell RuboCop to load the Workitcop extension.
21
+ ## Documentation
22
22
 
23
- ### RuboCop configuration file
23
+ You can read a lot more about Workitcop in its [official docs](https://ydah.github.io/docs.wkt).
24
24
 
25
- Put this into your `.rubocop.yml`.
26
-
27
- ```yaml
28
- require: workitcop
29
- ```
30
-
31
- Alternatively, use the following array notation when specifying multiple extensions.
32
-
33
- ```yaml
34
- require:
35
- - rubocop-other-extension
36
- - workitcop
37
- ```
25
+ ## Changelog
38
26
 
39
- Now you can run `rubocop` and it will automatically load the Workitcop
40
- cops together with the standard cops.
27
+ Workitcop's changelog is available [here](https://github.com/ydah/workitcop/blob/main/CHANGELOG.md).
41
28
 
42
29
  ## License
43
30
 
data/config/default.yml CHANGED
@@ -8,7 +8,7 @@ Workit/ActionArgs:
8
8
  Check for controller action must be using `action_args`.
9
9
  Enabled: false
10
10
  Include:
11
- - 'controllers/**/*'
11
+ - "controllers/**/*"
12
12
  ControllerMethods:
13
13
  - index
14
14
  - show
@@ -17,13 +17,12 @@ 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.
20
+ Workit/NoopRescue:
21
+ Description: "Check for suppress or ignore checked exception."
23
22
  Enabled: false
24
23
 
25
- Workit/NoopRescue:
26
- Description: 'Check for suppress or ignore checked exception.'
24
+ Workit/RedundantBooleanConditional:
25
+ Description: "Checks for redundant boolean conditions."
27
26
  Enabled: false
28
27
 
29
28
  Workit/RestrictOnSend:
@@ -31,20 +30,6 @@ Workit/RestrictOnSend:
31
30
  Check for `RESTRICT_ON_SEND` is defined if `on_send` or `after_send` are defined.
32
31
  Enabled: false
33
32
 
34
- Workit/RSpecCapybaraMatchStyle:
35
- Description: Checks for usage of deprecated style methods.
36
- Enabled: false
37
-
38
- Workit/RSpecCapybaraPredicateMatcher:
39
- Description: Prefer using predicate matcher over using predicate method directly.
40
- Enabled: false
41
- Strict: true
42
- EnforcedStyle: inflected
43
- AllowedExplicitMatchers: []
44
- SupportedStyles:
45
- - inflected
46
- - explicit
47
-
48
- Workit/RSpecMinitestAssertions:
49
- Description: Check if using Minitest matchers.
33
+ Workit/RSpecRedundantHttpStatus:
34
+ Description: Check for validation of redundant response HTTP status codes.
50
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
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Workit
6
+ # Check for validation of redundant response HTTP status codes.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # it 'something' do
11
+ # subject
12
+ # expect(response).to have_http_status 400
13
+ # assert_schema_conform(400)
14
+ # end
15
+ #
16
+ # # good
17
+ # it 'something' do
18
+ # subject
19
+ # assert_schema_conform(400)
20
+ # end
21
+ #
22
+ class RSpecRedundantHttpStatus < Base
23
+ include RangeHelp
24
+ extend AutoCorrector
25
+
26
+ MSG = "Remove redundant HTTP response status code validation."
27
+ RESTRICT_ON_SEND = %i[assert_schema_conform assert_response_schema_confirm].freeze
28
+
29
+ # @!method have_http_status(node)
30
+ def_node_search :have_http_status, <<~PATTERN
31
+ $(send nil? :have_http_status (:int _))
32
+ PATTERN
33
+
34
+ def on_send(node) # rubocop:disable InternalAffairs/OnSendWithoutOnCSend
35
+ return if node.first_argument.nil?
36
+
37
+ have_http_status(node.parent) do |http_node|
38
+ return autocorrect(node, http_node.parent.source_range)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def autocorrect(_node, bad_range)
45
+ add_offense(bad_range) do |corrector|
46
+ corrector.remove(range_by_whole_lines(bad_range, include_final_newline: true))
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -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
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Workitcop
4
- VERSION = "0.4.2"
3
+ module Rubocop
4
+ module Workitcop
5
+ module Version
6
+ STRING = "1.0.0"
7
+ end
8
+ end
5
9
  end
data/lib/workitcop.rb CHANGED
@@ -1,25 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "workitcop/inject"
4
- require_relative "workitcop/version"
3
+ require "rubocop"
5
4
 
6
- require_relative "rubocop/cop/workit/mixin/rspec_explicit_help"
7
- require_relative "rubocop/cop/workit/mixin/rspec_inflected_help"
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/comittee_assert_schema_confirm"
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
-
18
- module Workitcop
19
- PROJECT_ROOT = ::Pathname.new(__dir__).parent.expand_path.freeze
20
- CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
21
- CONFIG = ::YAML.safe_load(CONFIG_DEFAULT.read).freeze
22
- private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
23
- end
24
-
25
- Workitcop::Inject.defaults!
12
+ require_relative "rubocop/cop/workit/rspec_redundant_http_status"
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.2
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: 2022-12-12 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: rubocop
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.31'
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.31'
27
- description:
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,17 +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/mixin/rspec_explicit_help.rb
44
- - lib/rubocop/cop/workit/mixin/rspec_inflected_help.rb
45
- - lib/rubocop/cop/workit/mixin/rspec_predicate_matcher_base.rb
46
60
  - lib/rubocop/cop/workit/noop_rescue.rb
61
+ - lib/rubocop/cop/workit/redundant_boolean_conditional.rb
47
62
  - lib/rubocop/cop/workit/restrict_on_send.rb
48
- - lib/rubocop/cop/workit/rspec_capybara_match_style.rb
49
- - lib/rubocop/cop/workit/rspec_capybara_predicate_matcher.rb
50
- - lib/rubocop/cop/workit/rspec_minitest_assertions.rb
63
+ - lib/rubocop/cop/workit/rspec_redundant_http_status.rb
51
64
  - lib/workitcop.rb
52
- - lib/workitcop/inject.rb
65
+ - lib/workitcop/plugin.rb
53
66
  - lib/workitcop/version.rb
54
67
  homepage: https://github.com/ydah/workitcop
55
68
  licenses:
@@ -59,7 +72,7 @@ metadata:
59
72
  source_code_uri: https://github.com/ydah/workitcop
60
73
  changelog_uri: https://github.com/ydah/workitcop/blob/main/CHANGELOG.md
61
74
  rubygems_mfa_required: 'true'
62
- post_install_message:
75
+ default_lint_roller_plugin: RuboCop::Workitcop::Plugin
63
76
  rdoc_options: []
64
77
  require_paths:
65
78
  - lib
@@ -67,15 +80,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
80
  requirements:
68
81
  - - ">="
69
82
  - !ruby/object:Gem::Version
70
- version: 2.6.0
83
+ version: 2.7.0
71
84
  required_rubygems_version: !ruby/object:Gem::Requirement
72
85
  requirements:
73
86
  - - ">="
74
87
  - !ruby/object:Gem::Version
75
88
  version: '0'
76
89
  requirements: []
77
- rubygems_version: 3.3.3
78
- signing_key:
90
+ rubygems_version: 3.6.9
79
91
  specification_version: 4
80
- summary: Custom cops for `RuboCop`.
92
+ summary: A custom cop collection of working toolkits.
81
93
  test_files: []
@@ -1,57 +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
- # @example
9
- # # bad
10
- # it 'something' do
11
- # subject
12
- # expect(response).to have_http_status 400
13
- # do_something
14
- # assert_schema_conform
15
- # end
16
- #
17
- # # good
18
- # it 'something' do
19
- # subject
20
- # do_something
21
- # assert_schema_conform(400)
22
- # end
23
- #
24
- class ComitteeAssertSchemaConfirm < Base
25
- include RangeHelp
26
- extend AutoCorrector
27
-
28
- MSG = "Pass expected response status code to check it against the corresponding schema explicitly."
29
- RESTRICT_ON_SEND = %i[assert_schema_conform assert_response_schema_confirm].freeze
30
-
31
- # @!method have_http_status(node)
32
- def_node_search :have_http_status, <<~PATTERN
33
- $(send nil? :have_http_status (:int $_))
34
- PATTERN
35
-
36
- def on_send(node)
37
- return if node.arguments?
38
-
39
- have_http_status(node.parent) do |child_node, value|
40
- return autocorrect(node, child_node, value)
41
- end
42
-
43
- add_offense(node)
44
- end
45
-
46
- private
47
-
48
- def autocorrect(node, child_node, value)
49
- add_offense(node) do |corrector|
50
- corrector.remove(range_by_whole_lines(child_node.parent.loc.expression, include_final_newline: true))
51
- corrector.insert_after(node, "(#{value})")
52
- end
53
- end
54
- end
55
- end
56
- end
57
- 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
@@ -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