rubocop-disable_syntax 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: 2043bf5a5a0901cb7335082d5536484f634272a0a3f9cc6d8146e75b2663a0ab
4
+ data.tar.gz: 9a87b08496dc0cd0b42c6ee5a373cc9b6d5f42166148f15f7f26939f3b8d1259
5
+ SHA512:
6
+ metadata.gz: 7501434582cfcae34bb8c03ea062cefa44201d3c51cd308a7ea1f33de68ef4d3410dde6c82221bcbee1867c0460b4edacd6ba12d8381c2ede6ba93b56255bbbf
7
+ data.tar.gz: 5ac9ed8490f18910f3b27a9157a143b2e7751dde4a81e921ec126567f5984d74d37a00c153f2f5811b593c99e24b8285e58c9b6f8f6fb09fc287d72e139f23fb
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## master (unreleased)
2
+
3
+ ## 0.1.0 (2023-09-26)
4
+
5
+ - First release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 fatkodima
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,80 @@
1
+ # rubocop-disable_syntax
2
+
3
+ [![Build Status](https://github.com/fatkodima/rubocop-disable_syntax/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fatkodima/rubocop-disable_syntax/actions/workflows/ci.yml)
4
+
5
+ `rubocop-disable_syntax` is a [RuboCop](https://github.com/rubocop/rubocop) plugin that allows to disable some unfavorite ruby syntax such as `unless`, safe navigation etc.
6
+
7
+ ## Requirements
8
+
9
+ - ruby 2.7+
10
+ - rubocop 1.50+
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'rubocop-disable_syntax', group: :development, require: false
18
+ ```
19
+
20
+ And then run:
21
+
22
+ ```sh
23
+ $ bundle install
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ You need to tell RuboCop to load the `rubocop-disable_syntax` extension.
29
+
30
+ Put this into your `.rubocop.yml`.
31
+
32
+ ```yaml
33
+ require:
34
+ - rubocop-disable_syntax
35
+ ```
36
+
37
+ All the ruby syntax features are enabled by default and so this gem acts as a no-op. You need to manually configure
38
+ which ruby features you want to disable:
39
+
40
+ ```yml
41
+ Style/DisableSyntax:
42
+ DisableSyntax:
43
+ - unless
44
+ - ternary
45
+ - safe_navigation
46
+ - endless_methods
47
+ - arguments_forwarding
48
+ - numbered_parameters
49
+ - pattern_matching
50
+ - shorthand_hash_syntax
51
+ - and_or_not
52
+ - until
53
+ - percent_literals
54
+ ```
55
+
56
+ * `unless` - no `unless` keyword
57
+ * `ternary` - no ternary operator (`condition ? foo : bar`)
58
+ * `safe_navigation` - no safe navigation operator (`&.`)
59
+ * `endless_methods` - no endless methods (`def foo = 1`)
60
+ * `arguments_forwarding` - no arguments forwarding (`foo(...)`, `foo(*)`, `foo(**)`, `foo(&)`)
61
+ * `numbered_parameters` - no numbered parameters (`foo.each { puts _1 }`)
62
+ * `pattern_matching` - no pattern matching
63
+ * `shorthand_hash_syntax` - no shorthand hash syntax (`{ x:, y: }`)
64
+ * `and_or_not` - no `and`/`or`/`not` keywords (should use `&&`/`||`/`!` instead)
65
+ * `until` - no `until` keyword
66
+ * `percent_literals` - no any `%` style literals (`%w[foo bar]`, `%i[foo bar]`, `%q("str")`, `%r{/regex/}`)
67
+
68
+ ## Development
69
+
70
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake` to run the linter and tests.
71
+
72
+ 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).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fatkodima/rubocop-disable_syntax.
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,17 @@
1
+ Style/DisableSyntax:
2
+ Description: 'Forbid some unfavorite ruby syntax, such as `unless`, safe navigation etc.'
3
+ Enabled: true
4
+ VersionAdded: '0.1'
5
+ SupportedDisableSyntax:
6
+ - unless
7
+ - ternary
8
+ - safe_navigation
9
+ - endless_methods
10
+ - arguments_forwarding
11
+ - numbered_parameters
12
+ - pattern_matching
13
+ - shorthand_hash_syntax
14
+ - and_or_not
15
+ - until
16
+ - percent_literals
17
+ DisableSyntax: []
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ class DisableSyntax < Base
7
+ extend AutoCorrector
8
+
9
+ def on_if(node)
10
+ if node.unless? && !unless_allowed?
11
+ add_offense(node, message: "Do not use `unless`.") do |corrector|
12
+ corrector.replace(node.loc.keyword, "if")
13
+ corrector.wrap(node.condition, "!(", ")")
14
+ end
15
+ elsif node.ternary? && !ternary_allowed?
16
+ add_offense(node, message: "Do not use ternary operator.")
17
+ end
18
+ end
19
+
20
+ def on_csend(node)
21
+ if !safe_navigation_allowed?
22
+ add_offense(node, message: "Do not use `&.`.")
23
+ end
24
+ on_send(node)
25
+ end
26
+
27
+ def on_send(node)
28
+ if !arguments_forwarding_allowed? && arguments_forwarding?(node)
29
+ add_offense(node, message: "Do not use arguments forwarding.")
30
+ elsif node.prefix_not? && !and_or_not_allowed?
31
+ add_offense(node, message: "Use `!` instead of `not`.") do |corrector|
32
+ corrector.replace(node.loc.selector, "!")
33
+ end
34
+ end
35
+ end
36
+
37
+ def on_def(node)
38
+ if node.endless? && !endless_methods_allowed?
39
+ add_offense(node, message: "Do not use endless methods.") do |corrector|
40
+ arguments = node.arguments.any? ? node.arguments.source : ""
41
+
42
+ corrector.replace(node, <<~RUBY.strip)
43
+ def #{node.method_name}#{arguments}
44
+ #{node.body.source}
45
+ end
46
+ RUBY
47
+ end
48
+ end
49
+ end
50
+ alias on_defs on_def
51
+
52
+ def on_numblock(node)
53
+ if !numbered_parameters_allowed?
54
+ add_offense(node, message: "Do not use numbered parameters.")
55
+ end
56
+ end
57
+
58
+ def on_case_match(node)
59
+ if !pattern_matching_allowed?
60
+ add_offense(node, message: "Do not use pattern matching.")
61
+ end
62
+ end
63
+
64
+ def on_hash(node)
65
+ return if shorthand_hash_syntax_allowed? || node.pairs.none?(&:value_omission?)
66
+
67
+ add_offense(node, message: "Do not use shorthand hash syntax.") do |corrector|
68
+ node.pairs.each do |pair|
69
+ if pair.value_omission?
70
+ hash_key_source = pair.key.source
71
+ corrector.replace(pair, "#{hash_key_source}: #{hash_key_source}")
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def on_and(node)
78
+ if node.semantic_operator? && !and_or_not_allowed?
79
+ add_offense(node, message: "Use `#{node.alternate_operator}` instead of `#{node.operator}`.") do |corrector|
80
+ corrector.replace(node.loc.operator, node.alternate_operator)
81
+ end
82
+ end
83
+ end
84
+ alias on_or on_and
85
+
86
+ def on_until(node)
87
+ return if until_allowed?
88
+
89
+ add_offense(node, message: "Do not use `until`.") do |corrector|
90
+ corrector.replace(node.loc.keyword, "while")
91
+ corrector.wrap(node.condition, "!(", ")")
92
+ end
93
+ end
94
+
95
+ def on_array(node)
96
+ if node.percent_literal? && !percent_literals_allowed?
97
+ add_offense(node, message: "Do not use `%` literals for arrays.")
98
+ end
99
+ end
100
+
101
+ def on_regexp(node)
102
+ if node.percent_r_literal? && !percent_literals_allowed?
103
+ add_offense(node, message: "Do not use `%` literals for regexes.")
104
+ end
105
+ end
106
+
107
+ def on_str(node)
108
+ if !percent_literals_allowed? && str_percent_literal?(node)
109
+ add_offense(node, message: "Do not use `%` literals for strings.")
110
+ end
111
+ end
112
+
113
+ private
114
+ def unless_allowed?
115
+ !disable_syntax.include?("unless")
116
+ end
117
+
118
+ def ternary_allowed?
119
+ !disable_syntax.include?("ternary")
120
+ end
121
+
122
+ def safe_navigation_allowed?
123
+ !disable_syntax.include?("safe_navigation")
124
+ end
125
+
126
+ def endless_methods_allowed?
127
+ !disable_syntax.include?("endless_methods")
128
+ end
129
+
130
+ def arguments_forwarding_allowed?
131
+ !disable_syntax.include?("arguments_forwarding")
132
+ end
133
+
134
+ def arguments_forwarding?(send_node)
135
+ send_node.arguments.any? do |arg|
136
+ (arg.block_pass_type? && arg.source == "&") || # foo(&)
137
+ arg.forwarded_args_type? || # foo(...)
138
+ arg.forwarded_restarg_type? || # foo(*)
139
+ (arg.hash_type? && arg.source == "**") # foo(**)
140
+ end
141
+ end
142
+
143
+ def numbered_parameters_allowed?
144
+ !disable_syntax.include?("numbered_parameters")
145
+ end
146
+
147
+ def pattern_matching_allowed?
148
+ !disable_syntax.include?("pattern_matching")
149
+ end
150
+
151
+ def shorthand_hash_syntax_allowed?
152
+ !disable_syntax.include?("shorthand_hash_syntax")
153
+ end
154
+
155
+ def and_or_not_allowed?
156
+ !disable_syntax.include?("and_or_not")
157
+ end
158
+
159
+ def until_allowed?
160
+ !disable_syntax.include?("until")
161
+ end
162
+
163
+ def percent_literals_allowed?
164
+ !disable_syntax.include?("percent_literals")
165
+ end
166
+
167
+ def str_percent_literal?(node)
168
+ if node.loc.respond_to?(:begin) && node.loc.begin
169
+ node.loc.begin.source.start_with?("%")
170
+ end
171
+ end
172
+
173
+ def disable_syntax
174
+ @disable_syntax ||= begin
175
+ supported_disable_syntax = cop_config.fetch("SupportedDisableSyntax", [])
176
+ disable_syntax = cop_config.fetch("DisableSyntax", [])
177
+ if (extra_syntax = disable_syntax - supported_disable_syntax).any?
178
+ raise "Unknown `DisableSyntax` value(s): #{extra_syntax.join(', ')}"
179
+ end
180
+
181
+ disable_syntax
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
@@ -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 DisableSyntax
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 DisableSyntax
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "disable_syntax/version"
4
+
5
+ module RuboCop
6
+ module DisableSyntax
7
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
8
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
9
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
10
+
11
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ require_relative "rubocop/disable_syntax"
6
+ require_relative "rubocop/disable_syntax/version"
7
+ require_relative "rubocop/disable_syntax/inject"
8
+
9
+ RuboCop::DisableSyntax::Inject.defaults!
10
+
11
+ require_relative "rubocop/cop/style/disable_syntax"
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-disable_syntax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - fatkodima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-26 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.50'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.50'
27
+ description:
28
+ email:
29
+ - fatkodima123@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - config/default.yml
38
+ - lib/rubocop-disable_syntax.rb
39
+ - lib/rubocop/cop/style/disable_syntax.rb
40
+ - lib/rubocop/disable_syntax.rb
41
+ - lib/rubocop/disable_syntax/inject.rb
42
+ - lib/rubocop/disable_syntax/version.rb
43
+ homepage: https://github.com/fatkodima/rubocop-disable_syntax
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/fatkodima/rubocop-disable_syntax
48
+ source_code_uri: https://github.com/fatkodima/rubocop-disable_syntax
49
+ changelog_uri: https://github.com/fatkodima/rubocop-disable_syntax/blob/master/CHANGELOG.md
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.7.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.4.10
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: A RuboCop plugin that allows to disable some unfavorite ruby syntax, such
69
+ as `unless`, safe navigation etc.
70
+ test_files: []