rubocop-flexport 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a599585e57397ed31a361c5267c82ebe7ac4d7e0f673712e31369f135ec0fded
4
+ data.tar.gz: c9d92a894a7940d3cea6bec075b5e8934d0a79003e59fe58da9925caa74ab46c
5
+ SHA512:
6
+ metadata.gz: c596496c5e30b66d43f587a115d948ccea67549e116f4661337a101132a0776909be6329f2af13a5cf2ea115321787fbd589f00f6e20713cd012a48275d7db5e
7
+ data.tar.gz: 88aadf6198c2e703e2bf016d81059ec6e2309889ccaba9be6d10b9a458a8072a72baef8df08d0b0bbac66e2059a4a05b8af365439e76b30c8b2c498ad8c4815a
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Flexport Inc.
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.
@@ -0,0 +1,59 @@
1
+ # Rubocop::Flexport
2
+
3
+ This repo is for cops developed at Flexport that don't make sense to upstream
4
+ into any of the existing RuboCop repos. When possible, we prefer upstreaming.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rubocop-flexport'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rubocop-flexport
21
+
22
+ ## Usage
23
+
24
+ Put this into your .rubocop.yml:
25
+
26
+ ```
27
+ require:
28
+ - rubocop-flexport
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
34
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
35
+ prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`.
38
+
39
+ To test it locally against your main codebase, update your Gemfile to something
40
+ like below and then run `bundle install`:
41
+
42
+ ```
43
+ gem "rubocop-flexport", path: "/Users/<user>/rubocop-flexport"
44
+ ```
45
+
46
+ To release a new version, update the version number in `version.rb`, and then
47
+ run `bundle exec rake release`, which will create a git tag for the version,
48
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/flexport/rubocop-flexport.
53
+ This project is intended to be a safe, welcoming space for collaboration, and
54
+ contributors are expected to adhere to the
55
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'rubocop/flexport'
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__)
@@ -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,6 @@
1
+ Flexport/NewGlobalModel:
2
+ Description: 'Disallows addition of new global models to `app/models`. Prefer Rails Engines or namespaces.'
3
+ Enabled: true
4
+ VersionAdded: '0.1.0'
5
+ GlobalModelsPath: 'app/models/'
6
+ AllowNamespacedGlobalModels: true
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ require_relative 'rubocop/flexport'
6
+ require_relative 'rubocop/flexport/version'
7
+ require_relative 'rubocop/flexport/inject'
8
+
9
+ RuboCop::Flexport::Inject.defaults!
10
+
11
+ require_relative 'rubocop/cop/flexport_cops'
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Flexport
6
+ # This cop disallows adding new models to the `app/models` directory.
7
+ #
8
+ # The goal is to encourage developers to put new models inside Rails
9
+ # Engines (or at least namespaces), where they can be more modularly
10
+ # isolated and ownership is clear.
11
+ #
12
+ # Use RuboCop's standard `Exclude` file list parameter to exclude
13
+ # existing global model files from counting as violations for this cop.
14
+ #
15
+ # @example AllowNamespacedGlobalModels: true (default)
16
+ # # When `AllowNamespacedGlobalModels` is true, the cop only forbids
17
+ # # additions at the top-level directory.
18
+ #
19
+ # # bad
20
+ # # path: app/models/my_new_global_model.rb
21
+ # class MyNewGlobalModel < ApplicationRecord
22
+ # # ...
23
+ # end
24
+ #
25
+ # # good
26
+ # # path: app/models/my_namespace/my_new_global_model.rb
27
+ # class MyNamespace::MyNewGlobalModel < ApplicationRecord
28
+ # # ...
29
+ # end
30
+ #
31
+ # # good
32
+ # # path: engines/my_engine/app/models/my_engine/my_new_engine_model.rb
33
+ # class MyEngine::MyNewEngineModel < ApplicationRecord
34
+ # # ...
35
+ # end
36
+ #
37
+ # @example AllowNamespacedGlobalModels: false
38
+ # # When `AllowNamespacedGlobalModels` is false, the cop forbids all
39
+ # # new models in this directory and its descendants.
40
+ #
41
+ # # bad
42
+ # # path: app/models/my_new_global_model.rb
43
+ # class MyNewGlobalModel < ApplicationRecord
44
+ # # ...
45
+ # end
46
+ #
47
+ # # bad
48
+ # # path: app/models/my_namespace/my_new_global_model.rb
49
+ # class MyNamespace::MyNewGlobalModel < ApplicationRecord
50
+ # # ...
51
+ # end
52
+ #
53
+ # # good
54
+ # # path: engines/my_engine/app/models/my_engine/my_new_engine_model.rb
55
+ # class MyEngine::MyNewEngineModel < ApplicationRecord
56
+ # # ...
57
+ # end
58
+ class NewGlobalModel < Cop
59
+ ALLOW_NAMESPACES_MSG =
60
+ 'Do not add new top-level global models in `app/models`. ' \
61
+ 'Prefer namespaced models like `app/models/foo/bar.rb` or ' \
62
+ 'or models inside Rails Engines.'
63
+
64
+ DISALLOW_NAMESPACES_MSG =
65
+ 'Do not add new global models in `app/models`. ' \
66
+ 'Instead add new models to Rails Engines.'
67
+
68
+ def investigate(processed_source)
69
+ return if processed_source.blank?
70
+
71
+ path = processed_source.file_path
72
+ return unless global_rails_model?(path)
73
+
74
+ add_offense(processed_source.ast)
75
+ end
76
+
77
+ private
78
+
79
+ def message(_node)
80
+ return ALLOW_NAMESPACES_MSG if allow_namespaced_global_models
81
+
82
+ DISALLOW_NAMESPACES_MSG
83
+ end
84
+
85
+ def global_rails_model?(path)
86
+ return false unless path.include?(global_models_path)
87
+ return false if path.include?('/concerns/')
88
+ return false if in_engine?(path)
89
+ return false if allowed_namespace?(path)
90
+
91
+ true
92
+ end
93
+
94
+ def allowed_namespace?(path)
95
+ return false unless allow_namespaced_global_models
96
+
97
+ parts = path.split(global_models_path)
98
+ parts.last.split('/').length > 1
99
+ end
100
+
101
+ def in_engine?(path)
102
+ return true if path.include?('/engines/')
103
+
104
+ # Engines model dirs are structured like:
105
+ # my_engine/app/models/my_engine/my_model.rb.
106
+ # We detect models whose directory structure matches
107
+ # this pattern even if they aren't children of an
108
+ # "/engines/" directory.
109
+ parts = path.split(global_models_path)
110
+ potential_engine_name = parts.last.split('/').first
111
+ engine_models_path = File.join(
112
+ potential_engine_name,
113
+ global_models_path,
114
+ potential_engine_name
115
+ )
116
+ path.include?(engine_models_path)
117
+ end
118
+
119
+ def global_models_path
120
+ path = cop_config['GlobalModelsPath']
121
+ path += '/' unless path.end_with?('/')
122
+ path
123
+ end
124
+
125
+ def allow_namespaced_global_models
126
+ cop_config['AllowNamespacedGlobalModels']
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'flexport/new_global_model'
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop/flexport/version'
4
+
5
+ module RuboCop
6
+ # Namespace for Flexport-specific RuboCop cops.
7
+ module Flexport
8
+ class Error < StandardError; end
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
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The original code is from https://github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
4
+ # See https://github.com/rubocop-hq/rubocop-rspec/blob/master/MIT-LICENSE.md
5
+ module RuboCop
6
+ module Flexport
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)
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 Flexport
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-flexport
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Flexport Engineering
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-19 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: 0.70.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.70.0
27
+ description: ''
28
+ email:
29
+ - dev@flexport.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - bin/console
37
+ - bin/setup
38
+ - config/default.yml
39
+ - lib/rubocop-flexport.rb
40
+ - lib/rubocop/cop/flexport/new_global_model.rb
41
+ - lib/rubocop/cop/flexport_cops.rb
42
+ - lib/rubocop/flexport.rb
43
+ - lib/rubocop/flexport/inject.rb
44
+ - lib/rubocop/flexport/version.rb
45
+ homepage: https://github.com/flexport/rubocop-flexport
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.7.6
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: RuboCop cops used at Flexport.
69
+ test_files: []