rubocop-discourse 1.0.2 → 2.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: ce133465e4ca5d05edcf743e5e38d4ad089c385db83920b53e2d517bee97e837
4
- data.tar.gz: ead5716e3ad77ae268eb91424ff663ed6f060f59fbcf9ca08925d3925e091b9a
3
+ metadata.gz: '09c74d2dcf1477ce0ae014f1f22d5d0683963a963ebd4e326eb870fb492398e9'
4
+ data.tar.gz: 0aec169456ac388da0e57f01c3f941f8604978d32150d00ccd35ed04c0f45237
5
5
  SHA512:
6
- metadata.gz: f4ea1ecd294f15ea3972f5b9aa385a74be53fe8f1dc146dd335e4487a4075e81ba26a9db64c5ffe3162a28600bfde330d52981c456fb1945e9956c56738e7fbb
7
- data.tar.gz: 45c3e852541f4cd2eed5b7d7e32b3cdeac7984eebdd636aa3d9349e4cdbac7d394f0444118f3df00d060ed09a61cb4f7085444c9ed67e2a6e2da2936583fd451
6
+ metadata.gz: c93f323c3161d55ad1f8aba8f545904757683f0628184b7414171c0713e24fc54c76df328a15ec02969689273649ef804eaa9ed363a51442c5cc7486786934fa
7
+ data.tar.gz: 9d68351058a663411c9a860fcdf19a3a3406663143316b20586ba32be835e82ff68ff35b43aecc1adf21d9b53289612e246a6a570a9822c9d8df905c4b952db6
@@ -0,0 +1,44 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches:
7
+ - master
8
+ tags:
9
+ - v*
10
+
11
+ jobs:
12
+ build:
13
+ runs-on: ubuntu-latest
14
+
15
+ steps:
16
+ - uses: actions/checkout@v1
17
+
18
+ - name: Setup ruby
19
+ uses: actions/setup-ruby@v1
20
+ with:
21
+ ruby-version: 2.6
22
+ architecture: 'x64'
23
+
24
+ - name: Setup bundler
25
+ run: gem install bundler
26
+
27
+ - name: Setup gems
28
+ run: bundle install
29
+
30
+ - name: Rubocop
31
+ run: bundle exec rubocop
32
+
33
+ publish:
34
+ if: contains(github.ref, 'refs/tags/v')
35
+ needs: build
36
+ runs-on: ubuntu-latest
37
+
38
+ steps:
39
+ - uses: actions/checkout@v2
40
+
41
+ - name: Release Gem
42
+ uses: CvX/publish-rubygems-action@master
43
+ env:
44
+ RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
@@ -0,0 +1,2 @@
1
+ .rubocop-http*
2
+ Gemfile.lock
@@ -0,0 +1,4 @@
1
+ inherit_from: https://raw.githubusercontent.com/discourse/discourse/master/.rubocop.yml
2
+
3
+ Discourse:
4
+ Enabled: true
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify dependencies in gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Civilized Discourse Construction Kit, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,14 @@
1
+ Discourse/NoChdir:
2
+ Enabled: true
3
+
4
+ Discourse/NoDirectMultisiteManipulation:
5
+ Enabled: true
6
+
7
+ Discourse/NoTimeNewWithoutArgs:
8
+ Enabled: true
9
+
10
+ Discourse/NoUriEscapeEncode:
11
+ Enabled: true
12
+
13
+ Discourse/TimeEqMatcher:
14
+ Enabled: true
@@ -1,3 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "rubocop"
4
+ require_relative "rubocop/discourse"
5
+ require_relative "rubocop/discourse/inject"
6
+
7
+ RuboCop::Discourse::Inject.defaults!
8
+
3
9
  require_relative "rubocop/cop/discourse_cops"
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Discourse
6
+ # Avoid using chdir - it is not thread safe.
7
+ #
8
+ # Instead, you may be able to use:
9
+ # Discourse::Utils.execute_command(chdir: "test") do |runner|
10
+ # runner.exec("pwd")
11
+ # end
12
+ #
13
+ # @example
14
+ # # bad
15
+ # Dir.chdir("test")
16
+ class NoChdir < Cop
17
+ MSG = "Chdir is not thread safe."
18
+
19
+ def_node_matcher :using_dir_chdir?, <<-MATCHER
20
+ (send (const nil? :Dir) :chdir ...)
21
+ MATCHER
22
+
23
+ def_node_matcher :using_fileutils_cd?, <<-MATCHER
24
+ (send (const nil? :FileUtils) :cd ...)
25
+ MATCHER
26
+
27
+ def on_send(node)
28
+ return if !(using_dir_chdir?(node) || using_fileutils_cd?(node))
29
+ add_offense(node, message: MSG)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Discourse
6
+ # Use `type: :multisite` example setting instead of modifying `Rails.configuration.multisite`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # it "works" do
11
+ # Rails.configuration.multisite = true
12
+ # do_something
13
+ # end
14
+ #
15
+ # # good
16
+ # it "works", type: :multisite do
17
+ # do_something
18
+ # end
19
+ class NoDirectMultisiteManipulation < Cop
20
+ MSG = "Use `type: :multisite` example setting instead of modifying `Rails.configuration.multisite`."
21
+
22
+ def_node_matcher :multisite_setter?, <<-MATCHER
23
+ (send
24
+ (send
25
+ (const nil? :Rails) :configuration) :multisite=
26
+ _)
27
+ MATCHER
28
+
29
+ def on_send(node)
30
+ return unless multisite_setter?(node)
31
+
32
+ add_offense(node, message: MSG)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Discourse
6
+ # Use `Time.zone.now` instead of `Time.new` without arguments.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # now = Time.new
11
+ #
12
+ # # good
13
+ # now = Time.zone.now
14
+ class NoTimeNewWithoutArgs < Cop
15
+ MSG = "Use `Time.zone.now` instead of `Time.new` without arguments."
16
+
17
+ def_node_matcher :time_new_without_args?, <<-MATCHER
18
+ (send (const nil? :Time) :new)
19
+ MATCHER
20
+
21
+ def on_send(node)
22
+ return unless time_new_without_args?(node)
23
+
24
+ add_offense(node, message: MSG)
25
+ end
26
+
27
+ def autocorrect(node)
28
+ lambda do |corrector|
29
+ corrector.replace(node.loc.expression, "Time.zone.now")
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Discourse
6
+ # Do not use URI.escape and its ilk, they are deprecated
7
+ # with a warning in the ruby source. Instead use
8
+ # Addressable::URI, which has encode, encode_component,
9
+ # and unencode methods. UrlHelper has helper methods for this.
10
+ #
11
+ # # @example
12
+ # # bad
13
+ # URI.encode("https://a%20a.com?a='a%22")
14
+ #
15
+ # # good
16
+ # UrlHelper.encode("https://a%20a.com?a='a%22")
17
+ # Addressable::URI.encode("https://a%20a.com?a='a%22")
18
+ class NoURIEscapeEncode < Cop
19
+ MSG = "URI.escape, URI.encode, URI.unescape, URI.decode are deprecated and should not be used."
20
+
21
+ def_node_matcher :using_uri_escape?, <<-MATCHER
22
+ (send (const nil? :URI) :escape ...)
23
+ MATCHER
24
+
25
+ def_node_matcher :using_uri_encode?, <<-MATCHER
26
+ (send (const nil? :URI) :encode ...)
27
+ MATCHER
28
+
29
+ def_node_matcher :using_uri_unescape?, <<-MATCHER
30
+ (send (const nil? :URI) :unescape ...)
31
+ MATCHER
32
+
33
+ def_node_matcher :using_uri_decode?, <<-MATCHER
34
+ (send (const nil? :URI) :decode ...)
35
+ MATCHER
36
+
37
+ def on_send(node)
38
+ return if [
39
+ using_uri_escape?(node),
40
+ using_uri_encode?(node),
41
+ using_uri_unescape?(node),
42
+ using_uri_decode?(node)
43
+ ].none?
44
+ add_offense(node, message: MSG)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Discourse
6
+ # Use `time_eq` matcher with timestamps in specs.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # expect(user.created_at).to eq(Time.zone.now)
11
+ #
12
+ # # good
13
+ # expect(user.created_at).to eq_time(Time.zone.now)
14
+ class TimeEqMatcher < Cop
15
+ MSG = "Use time_eq when testing timestamps"
16
+
17
+ def_node_matcher :using_eq_matcher_with_timestamp?, <<-MATCHER
18
+ (send
19
+ (send nil? :expect
20
+ (send ... #timestamp_suffix?))
21
+ {:to :not_to :to_not}
22
+ (send nil? :eq #not_nil))
23
+ MATCHER
24
+
25
+ def on_send(node)
26
+ return unless using_eq_matcher_with_timestamp?(node)
27
+
28
+ add_offense(node, message: MSG)
29
+ end
30
+
31
+ def autocorrect(node)
32
+ lambda do |corrector|
33
+ corrector.replace(node.children.last.loc.selector, "eq_time")
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def timestamp_suffix?(property)
40
+ property =~ /_at$/
41
+ end
42
+
43
+ def not_nil(expression)
44
+ expression != [s(:nil)]
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,76 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module RuboCop
4
- module Cop
5
- module DiscourseCops
6
- # Avoid using chdir - it is not thread safe.
7
- #
8
- # Instead, you may be able to use:
9
- # Discourse::Utils.execute_command(chdir: 'test') do |runner|
10
- # runner.exec('pwd')
11
- # end
12
- #
13
- # @example
14
- # # bad
15
- # Dir.chdir('test')
16
- class NoChdir < Cop
17
- MSG = 'Chdir is not thread safe.'
18
-
19
- def_node_matcher :using_dir_chdir?, <<-MATCHER
20
- (send (const nil? :Dir) :chdir ...)
21
- MATCHER
22
-
23
- def_node_matcher :using_fileutils_cd?, <<-MATCHER
24
- (send (const nil? :FileUtils) :cd ...)
25
- MATCHER
26
-
27
- def on_send(node)
28
- return if !(using_dir_chdir?(node) || using_fileutils_cd?(node))
29
- add_offense(node, message: MSG)
30
- end
31
- end
32
-
33
- # Do not use URI.escape and its ilk, they are deprecated
34
- # with a warning in the ruby source. Instead use
35
- # Addressable::URI, which has encode, encode_component,
36
- # and unencode methods. UrlHelper has helper methods for this.
37
- #
38
- # # @example
39
- # # bad
40
- # URI.encode("https://a%20a.com?a='a%22")
41
- #
42
- # # good
43
- # UrlHelper.encode("https://a%20a.com?a='a%22")
44
- # Addressable::URI.encode("https://a%20a.com?a='a%22")
45
- class NoURIEscapeEncode < Cop
46
- MSG = 'URI.escape, URI.encode, URI.unescape, URI.decode are deprecated and should not be used.'
47
-
48
- def_node_matcher :using_uri_escape?, <<-MATCHER
49
- (send (const nil? :URI) :escape ...)
50
- MATCHER
51
-
52
- def_node_matcher :using_uri_encode?, <<-MATCHER
53
- (send (const nil? :URI) :encode ...)
54
- MATCHER
55
-
56
- def_node_matcher :using_uri_unescape?, <<-MATCHER
57
- (send (const nil? :URI) :unescape ...)
58
- MATCHER
59
-
60
- def_node_matcher :using_uri_decode?, <<-MATCHER
61
- (send (const nil? :URI) :decode ...)
62
- MATCHER
63
-
64
- def on_send(node)
65
- return if [
66
- using_uri_escape?(node),
67
- using_uri_encode?(node),
68
- using_uri_unescape?(node),
69
- using_uri_decode?(node)
70
- ].none?
71
- add_offense(node, message: MSG)
72
- end
73
- end
74
- end
75
- end
76
- end
3
+ path = File.join(__dir__, "discourse", "*.rb")
4
+ Dir[path].each { |file| require file }
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # RuboCop Discourse project namespace
5
+ module Discourse
6
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
7
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
8
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
9
+
10
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Discourse
5
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
6
+ # bit of our configuration.
7
+ module Inject
8
+ def self.defaults!
9
+ path = CONFIG_DEFAULT.to_s
10
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
11
+ config = Config.new(hash, path)
12
+ puts "configuration from #{path}" if ConfigLoader.debug?
13
+ config = ConfigLoader.merge_with_default(config, path)
14
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "rubocop-discourse"
5
+ s.version = "2.0.0"
6
+ s.summary = "Custom rubocop cops used by Discourse"
7
+ s.authors = ["David Taylor"]
8
+ s.files = `git ls-files`.split($/)
9
+ s.license = "MIT"
10
+ s.require_paths = ["lib"]
11
+
12
+ s.add_runtime_dependency "rubocop", ">= 0.69.0"
13
+
14
+ s.add_development_dependency "rake", "~> 13.0"
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-discourse
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Taylor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-12 00:00:00.000000000 Z
11
+ date: 2020-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -24,14 +24,43 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.69.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
27
41
  description:
28
42
  email:
29
43
  executables: []
30
44
  extensions: []
31
45
  extra_rdoc_files: []
32
46
  files:
47
+ - ".github/workflows/ci.yml"
48
+ - ".gitignore"
49
+ - ".rubocop.yml"
50
+ - Gemfile
51
+ - LICENSE
52
+ - Rakefile
53
+ - config/default.yml
33
54
  - lib/rubocop-discourse.rb
55
+ - lib/rubocop/cop/discourse/no_chdir.rb
56
+ - lib/rubocop/cop/discourse/no_direct_multisite_manipulation.rb
57
+ - lib/rubocop/cop/discourse/no_time_new_without_args.rb
58
+ - lib/rubocop/cop/discourse/no_uri_escape_encode.rb
59
+ - lib/rubocop/cop/discourse/time_eq_matcher.rb
34
60
  - lib/rubocop/cop/discourse_cops.rb
61
+ - lib/rubocop/discourse.rb
62
+ - lib/rubocop/discourse/inject.rb
63
+ - rubocop-discourse.gemspec
35
64
  homepage:
36
65
  licenses:
37
66
  - MIT
@@ -51,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
80
  - !ruby/object:Gem::Version
52
81
  version: '0'
53
82
  requirements: []
54
- rubygems_version: 3.0.6
83
+ rubygems_version: 3.0.3
55
84
  signing_key:
56
85
  specification_version: 4
57
86
  summary: Custom rubocop cops used by Discourse