rubocop-iotventure 0.1.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: 13ab895efc82ad4adbf3fda57d05c8107eed7353ac606468e9e2d433b96500a9
4
+ data.tar.gz: c69374ca50a8adb21fc33d70d4cd74aab6f16beee4de10e54c415fd6f59d8bc5
5
+ SHA512:
6
+ metadata.gz: 6f61f1ffa728a317032a3a7031501d8454b47a6ea6e68688951e8f6bbfffe517c930dee650645b0240262fa2bfac29da659d2a9be37adf5b2646bcf7799c93fc
7
+ data.tar.gz: ff51238471e4c1c69342670135f87d840393a8d4bb7a8a72bad7d0444ea3452de05cb5865fcfa4a97608f5b64d73f219be459ff42775f47b7f7191b4b5557154
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Layout/LineLength:
6
+ Max: 120
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - '**/spec/**/*'
11
+
12
+ Naming/FileName:
13
+ Exclude:
14
+ - lib/rubocop-iotventure.rb
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-04-18
4
+
5
+ - Initial release
6
+
7
+ - Add DuplicateResponseCode cop
8
+
9
+ - Add SaveRequestExample cop
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at f.starke@iot-venture.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rubocop-iotventure.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubocop-iotventure (0.1.0)
5
+ rubocop (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.5.0)
13
+ docile (1.4.0)
14
+ method_source (1.0.0)
15
+ parallel (1.22.1)
16
+ parser (3.1.2.0)
17
+ ast (~> 2.4.1)
18
+ pry (0.14.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.3.0)
24
+ rexml (3.2.5)
25
+ rspec (3.11.0)
26
+ rspec-core (~> 3.11.0)
27
+ rspec-expectations (~> 3.11.0)
28
+ rspec-mocks (~> 3.11.0)
29
+ rspec-core (3.11.0)
30
+ rspec-support (~> 3.11.0)
31
+ rspec-expectations (3.11.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.11.0)
34
+ rspec-mocks (3.11.1)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.11.0)
37
+ rspec-support (3.11.0)
38
+ rubocop (1.27.0)
39
+ parallel (~> 1.10)
40
+ parser (>= 3.1.0.0)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8, < 3.0)
43
+ rexml
44
+ rubocop-ast (>= 1.16.0, < 2.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 3.0)
47
+ rubocop-ast (1.17.0)
48
+ parser (>= 3.1.1.0)
49
+ ruby-progressbar (1.11.0)
50
+ simplecov (0.21.2)
51
+ docile (~> 1.1)
52
+ simplecov-html (~> 0.11)
53
+ simplecov_json_formatter (~> 0.1)
54
+ simplecov-html (0.12.3)
55
+ simplecov_json_formatter (0.1.4)
56
+ unicode-display_width (2.1.0)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ bundler
63
+ pry
64
+ rake (~> 13.0)
65
+ rspec
66
+ rubocop (~> 1.21)
67
+ rubocop-iotventure!
68
+ simplecov
69
+
70
+ BUNDLED WITH
71
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 IoT Venture GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Rubocop::Iotventure
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rubocop/iotventure`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rubocop-iotventure', require: false
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rubocop-iotventure
22
+
23
+ ## Usage
24
+
25
+ ### Rswag/DuplicateResponseCode
26
+
27
+ ```ruby
28
+ # bad
29
+ response 400, 'response description 1' {}
30
+ response 400, 'response description 2' {}
31
+
32
+ # good
33
+ response 400, 'response description 1' {}
34
+ response 401, 'response description 2' {}
35
+ ```
36
+
37
+ This cop prevents duplicated response code blocks. Those would overwrite each other when generating the `swagger.yaml` file.
38
+
39
+ ### Rswag/SaveRequestExample
40
+
41
+ ```ruby
42
+ # bad
43
+ parameter name: :param1, in: :body
44
+ response 200, 'response description' {}
45
+
46
+ # good
47
+ parameter name: :param1, in: :body
48
+ response 200, 'response description', save_request_example: :param1 {}
49
+ ```
50
+
51
+ This cop enforces usage of the `save_request_example` parameter in `api` files (saves the body parameter). This should only be enabled when there is custom logic reacting to this parameter.
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on Bitbucket at https://bitbucket.org/iotventure/rubocop-iotventure. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://bitbucket.org/iotventure/rubocop-iotventure/src/master/CODE_OF_CONDUCT.md).
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
66
+
67
+ ## Code of Conduct
68
+
69
+ Everyone interacting in the Rubocop::Iotventure project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://bitbucket.org/iotventure/rubocop-iotventure/src/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
9
+
10
+ require 'rspec/core/rake_task'
11
+
12
+ RSpec::Core::RakeTask.new(:spec) do |spec|
13
+ spec.pattern = FileList['spec/**/*_spec.rb']
14
+ end
15
+
16
+ desc 'Generate a new cop with a template'
17
+ task :new_cop, [:cop] do |_task, args|
18
+ require 'rubocop'
19
+
20
+ cop_name = args.fetch(:cop) do
21
+ warn 'usage: bundle exec rake new_cop[Department/Name]'
22
+ exit!
23
+ end
24
+
25
+ generator = RuboCop::Cop::Generator.new(cop_name)
26
+
27
+ generator.write_source
28
+ generator.write_spec
29
+ generator.inject_require(root_file_path: 'lib/rubocop/cop/iotventure_cops.rb')
30
+ generator.inject_config(config_file_path: 'config/default.yml')
31
+
32
+ puts generator.todo
33
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "rubocop/iotventure"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
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,15 @@
1
+ Iotventure/DuplicateResponseCode:
2
+ Description: 'Finds duplicated response code blocks in swagger files.'
3
+ Enabled: true
4
+ VersionAdded: '0.1.0'
5
+ Include:
6
+ - 'spec/requests/api/**/*'
7
+ - 'spec/acceptance/api/**/*'
8
+
9
+ Iotventure/SaveRequestExample:
10
+ Description: 'Checks whether request example (body parameter) is saved.'
11
+ Enabled: true
12
+ VersionAdded: '0.1.0'
13
+ Include:
14
+ - 'spec/requests/api/**/*'
15
+ - 'spec/acceptance/api/**/*'
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Iotventure
6
+ # This cop checks for duplicated response code example definitions.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ #
12
+ # response 400, 'response description 1' {}
13
+ # response 400, 'response description 2' {}
14
+ #
15
+ #
16
+ # @example
17
+ #
18
+ # # bad
19
+ #
20
+ # context 'context 1' do
21
+ # response 400, 'response description 1' {}
22
+ # end
23
+ # context 'context 2' do
24
+ # response 400, 'response description 2' {}
25
+ # end
26
+ #
27
+ # @example
28
+ #
29
+ # # bad
30
+ #
31
+ # 2.times do
32
+ # response 400, 'response description' {}
33
+ # end
34
+ #
35
+ #
36
+ # @example
37
+ #
38
+ # # good
39
+ #
40
+ # response 400, 'response description' do
41
+ # context 'context 1' {}
42
+ # context 'context 2' {}
43
+ # end
44
+ #
45
+ #
46
+ # @example
47
+ #
48
+ # # good
49
+ #
50
+ # response 400, 'response description 1' {}
51
+ # response 401, 'response description 2' {}
52
+ #
53
+ class DuplicateResponseCode < Base
54
+ DUPLICATE_MSG = 'Response Code `%<response_code>s` is defined at both %<other>s and %<current>s.'
55
+ IN_LOOP_MSG = 'Response definition at %<current>s is defined inside loop at %<loop>s'
56
+
57
+ # @!method response_code_definition?(node)
58
+ def_node_matcher :response_code_definition, <<~PATTERN
59
+ (block
60
+ (send nil? :response
61
+ (int $_)
62
+ ...)
63
+ ...
64
+ )
65
+ PATTERN
66
+
67
+ # @!method route_definition?(node)
68
+ def_node_matcher :route_definition, <<~PATTERN
69
+ (block
70
+ (send nil? #route_verb? (str _))
71
+ ...
72
+ )
73
+ PATTERN
74
+
75
+ def initialize(config = nil, options = nil)
76
+ super
77
+ @definitions = {}
78
+ end
79
+
80
+ def on_block(node)
81
+ code = response_code_definition(node)
82
+ return unless code
83
+
84
+ check_for_duplicate_response_code(node, code)
85
+ check_for_definition_inside_loop(node)
86
+ end
87
+
88
+ private
89
+
90
+ def check_for_duplicate_response_code(node, response_code)
91
+ route_definition_ancestor = route_definition_ancestor(node)
92
+ return unless route_definition_ancestor
93
+
94
+ route_definition_ancestor_line = route_definition_ancestor.first_line
95
+
96
+ if (other_node = @definitions[route_definition_ancestor_line]&.[](response_code))
97
+ message = message_for_dup(node, other_node, response_code)
98
+
99
+ add_offense(node.loc.expression, message: message)
100
+ else
101
+ @definitions[route_definition_ancestor_line] ||= {}
102
+ @definitions[route_definition_ancestor_line][response_code] = node
103
+ end
104
+ end
105
+
106
+ def message_for_dup(node, other_node, response_code)
107
+ format(DUPLICATE_MSG, response_code: response_code, other: source_location(other_node),
108
+ current: source_location(node))
109
+ end
110
+
111
+ def route_definition_ancestor(node)
112
+ ancestor = node.parent
113
+ ancestor = ancestor.parent until ancestor.nil? || route_definition(ancestor)
114
+ ancestor
115
+ end
116
+
117
+ def route_verb?(word)
118
+ %i[get post put patch delete].include? word.to_sym
119
+ end
120
+
121
+ def check_for_definition_inside_loop(node)
122
+ ancestor = node
123
+ until route_definition(ancestor)
124
+ ancestor = ancestor.parent
125
+
126
+ return if ancestor.nil?
127
+
128
+ next unless loop_method?(ancestor)
129
+
130
+ message = message_for_loop(node, ancestor)
131
+ return add_offense(node.loc.expression, message: message)
132
+ end
133
+ end
134
+
135
+ def loop_method?(node)
136
+ return false unless node.block_type?
137
+
138
+ send_node = node.send_node
139
+ send_node.enumerable_method? || send_node.enumerator_method? || send_node.method?(:loop)
140
+ end
141
+
142
+ def message_for_loop(node, loop_node)
143
+ format(IN_LOOP_MSG, current: source_location(node), loop: source_location(loop_node))
144
+ end
145
+
146
+ def source_location(node)
147
+ range = node.location.expression
148
+ path = smart_path(range.source_buffer.name)
149
+ "#{path}:#{range.line}"
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Iotventure
6
+ # This cop checks for missing request examples
7
+ # (only needed when params in query).
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ #
13
+ # parameter name: :param1, in: :body
14
+ # response 200, 'response description' {}
15
+ #
16
+ # @example
17
+ #
18
+ # # bad
19
+ #
20
+ # parameter name: :param1, in: :body
21
+ # response 200, 'response description', save_request_example: :param2 {}
22
+ #
23
+ #
24
+ # @example
25
+ #
26
+ # # good
27
+ #
28
+ # parameter name: :param1, in: :body
29
+ # response 200, 'response description', save_request_example: :param1 {}
30
+ #
31
+ #
32
+ # @example
33
+ #
34
+ # # good
35
+ #
36
+ # response 200, 'response description' {}
37
+ #
38
+ #
39
+ class SaveRequestExample < Base
40
+ MISSING_MSG = 'Request example `:%<param_name>s` is missing at %<current>s.'
41
+ MISNAMED_MSG = 'Request example `:%<wrong_param_name>s` is misnamed at %<current>s.'\
42
+ 'It should be `:%<correct_param_name>s`.'
43
+
44
+ # @!method body_param_name?(node)
45
+ def_node_matcher :body_param_name, <<~PATTERN
46
+ (begin
47
+ <(send nil? :parameter
48
+ (hash
49
+ <(pair
50
+ (sym :name)
51
+ (sym $_)
52
+ )
53
+ (pair
54
+ (sym :in)
55
+ (sym :body)
56
+ )... >
57
+ )) ... >
58
+ )
59
+ PATTERN
60
+
61
+ # @!method success_response(node)
62
+ def_node_matcher :success_response, <<~PATTERN
63
+ (block
64
+ (send nil? :response
65
+ (:int #success_response_code?)
66
+ (:str _)
67
+ $...
68
+ )
69
+ ...
70
+ )
71
+ PATTERN
72
+
73
+ # @!method save_request_example_name(node)
74
+ def_node_matcher :save_request_example_name, <<~PATTERN
75
+ (hash
76
+ <
77
+ (pair
78
+ (sym :save_request_example)
79
+ (sym $_)
80
+ )
81
+ ...
82
+ >
83
+ )
84
+ PATTERN
85
+
86
+ def on_begin(node)
87
+ body_param_name = body_param_name(node)
88
+ return unless body_param_name
89
+
90
+ check_for_wrong_save_request_example(node, body_param_name)
91
+ end
92
+
93
+ private
94
+
95
+ def check_for_wrong_save_request_example(node, body_param_name)
96
+ success_node, arguments_hash_capture = node.children.each do |child|
97
+ capture = success_response(child)
98
+
99
+ break child, capture if capture
100
+ end
101
+
102
+ return add_missing_offense(success_node, body_param_name) if arguments_hash_capture.empty?
103
+
104
+ check_arguments_hash(success_node, body_param_name, arguments_hash_capture.first)
105
+ end
106
+
107
+ def check_arguments_hash(success_node, body_param_name, arguments_hash)
108
+ save_request_example_name = save_request_example_name(arguments_hash)
109
+
110
+ return add_missing_offense(success_node, body_param_name) if save_request_example_name.nil?
111
+
112
+ return if save_request_example_name == body_param_name
113
+
114
+ add_misnamed_offense(arguments_hash, body_param_name, save_request_example_name)
115
+ end
116
+
117
+ def success_response_code?(value)
118
+ value.between?(200, 299)
119
+ end
120
+
121
+ def add_missing_offense(node, body_param_name)
122
+ message = format(MISSING_MSG, param_name: body_param_name, current: source_location(node))
123
+ add_offense(node.loc.expression, message: message)
124
+ end
125
+
126
+ def add_misnamed_offense(node, body_param_name, save_request_example_name)
127
+ message = format(MISNAMED_MSG, wrong_param_name: save_request_example_name,
128
+ correct_param_name: body_param_name,
129
+ current: source_location(node))
130
+ add_offense(node.loc.expression, message: message)
131
+ end
132
+
133
+ def source_location(node)
134
+ range = node.location.expression
135
+ path = smart_path(range.source_buffer.name)
136
+ "#{path}:#{range.line}"
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'iotventure/duplicate_response_code'
4
+ require_relative 'iotventure/save_request_example'
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The original code is from https://github.com/rubocop/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
4
+ # See https://github.com/rubocop/rubocop-rspec/blob/master/MIT-LICENSE.md
5
+ module RuboCop
6
+ module Iotventure
7
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
8
+ # bit of our configuration.
9
+ module Inject
10
+ def self.defaults!
11
+ path = CONFIG_DEFAULT.to_s
12
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
13
+ config = Config.new(hash, path).tap(&:make_excludes_absolute)
14
+ puts "configuration from #{path}" if ConfigLoader.debug?
15
+ config = ConfigLoader.merge_with_default(config, path)
16
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Iotventure
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "iotventure/version"
4
+
5
+ module RuboCop
6
+ module Iotventure
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
10
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
11
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
12
+
13
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
14
+ end
15
+ end
16
+
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ require_relative 'rubocop/iotventure'
6
+ require_relative 'rubocop/iotventure/version'
7
+ require_relative 'rubocop/iotventure/inject'
8
+
9
+ RuboCop::Iotventure::Inject.defaults!
10
+
11
+ require_relative 'rubocop/cop/iotventure_cops'
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rubocop/iotventure/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rubocop-iotventure"
7
+ spec.version = RuboCop::Iotventure::VERSION
8
+ spec.authors = ["Fynn Starke"]
9
+ spec.email = ["f.starke@iot-venture.com"]
10
+
11
+ spec.summary = 'Rswag cops.'
12
+ spec.description = 'Adds some helpful internal cops'
13
+ spec.homepage = 'https://rubygems.org/gems/rubocop-iotventure'
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["homepage_uri"] = spec.homepage
18
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+
38
+ spec.add_dependency 'rubocop', '~> 1.0'
39
+
40
+ spec.add_development_dependency 'bundler'
41
+ spec.add_development_dependency 'pry'
42
+ spec.add_development_dependency 'rake'
43
+ spec.add_development_dependency 'rspec'
44
+ spec.add_development_dependency 'simplecov'
45
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-iotventure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fynn Starke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-18 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.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Adds some helpful internal cops
98
+ email:
99
+ - f.starke@iot-venture.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".rspec"
105
+ - ".rubocop.yml"
106
+ - CHANGELOG.md
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - config/default.yml
116
+ - lib/rubocop-iotventure.rb
117
+ - lib/rubocop/cop/iotventure/duplicate_response_code.rb
118
+ - lib/rubocop/cop/iotventure/save_request_example.rb
119
+ - lib/rubocop/cop/iotventure_cops.rb
120
+ - lib/rubocop/iotventure.rb
121
+ - lib/rubocop/iotventure/inject.rb
122
+ - lib/rubocop/iotventure/version.rb
123
+ - rubocop-iotventure.gemspec
124
+ homepage: https://rubygems.org/gems/rubocop-iotventure
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 2.6.0
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.2.32
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Rswag cops.
147
+ test_files: []