rubocop-sorbet 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0a2efbcc17a1016f1b490abf983d6a68a20a8d0bc5652befbf1c0ba7c339f92
4
- data.tar.gz: d913baa821bc1146aeb2fa831689a25556447f86a60c6e5ee63c2c4ed1f5d387
3
+ metadata.gz: 9ad80ed7bb1d6485194f669d289efba935cecb7adff45f542ba8ada36171a57f
4
+ data.tar.gz: f2129cc057c628df84b40e4ba24d376624fa6ad3e3b7a27025a53e0f5862c422
5
5
  SHA512:
6
- metadata.gz: 9f928e323972868757bdc9e85ae4ccbd7ef63e763b02841375a2add43706d30b009d4370da43041ac8e4af8840dc825a2838bbe4a44438f5195cf936d370d542
7
- data.tar.gz: cdccd79f16c0a668c8b4aa91f015324b5c3599490daa7ab94089f1499b5fb0d300284a15fdf2bd220cb0b2bf6224c4479e4d94ac8ac037d87fe521dd342fec6a
6
+ metadata.gz: 4c493e9d0aa2718629cfba7c84a8c7ec530bb5b942c273d10b8893594ef7f2cb219227e7499a0981f400d7b10f0cef986adba8c0ad480c90d84ff8c14e5ceda1
7
+ data.tar.gz: 5b8d8d04a8b6a77feeb11929d6a4a55804dac5f7eeccf2ed47f0b7ed0dac1b30f08cefa030d59ea623e9fe907a01f0e48ab933f6476d86bf729b0597dcc7130c
data/.gitignore CHANGED
@@ -6,3 +6,5 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+
10
+ .rubocop-https--*
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ # This file strictly follows the rules defined in the Ruby style guide:
2
+ # http://shopify.github.io/ruby-style-guide/
3
+ inherit_from:
4
+ - https://shopify.github.io/ruby-style-guide/rubocop.yml
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.5
8
+ Exclude:
9
+ - lib/waffle_cone/rbi_compilers/yard_type_parser.rb
10
+ - vendor/**/*
11
+
12
+ Naming/AccessorMethodName:
13
+ Enabled: false
14
+
15
+ require:
16
+ - rubocop-sorbet
17
+
18
+ Naming/FileName:
19
+ Exclude:
20
+ - lib/rubocop-sorbet.rb
21
+
22
+ Style/StringLiterals:
23
+ EnforcedStyle: single_quotes
24
+
25
+ Lint/HandleExceptions:
26
+ Exclude:
27
+ - Rakefile
@@ -0,0 +1 @@
1
+ v1
@@ -0,0 +1,16 @@
1
+ containers:
2
+ default:
3
+ docker: "circleci/ruby:2.5.5"
4
+
5
+ steps:
6
+ - label: ":ruby: Specs"
7
+ dependencies:
8
+ - "bundler"
9
+ timeout: "5m"
10
+ run:
11
+ - "bundle exec rspec"
12
+ - label: "Rubocop"
13
+ dependencies:
14
+ - "bundler"
15
+ timeout: "5m"
16
+ run: "bundle exec rubocop"
data/.travis.yml CHANGED
@@ -5,3 +5,6 @@ cache: bundler
5
5
  rvm:
6
6
  - 2.6.2
7
7
  before_install: gem install bundler -v 1.17.3
8
+ script:
9
+ - bundle exec rubocop --config .rubocop.yml
10
+ - bundle exec rspec
data/Gemfile CHANGED
@@ -1,6 +1,11 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ source('https://rubygems.org')
4
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
5
+
6
+ group(:deployment) do
7
+ gem('package_cloud', '~> 0.3.05')
8
+ end
4
9
 
5
10
  # Specify your gem's dependencies in rubocop-sorbet.gemspec
6
11
  gemspec
data/Gemfile.lock CHANGED
@@ -1,22 +1,97 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-sorbet (0.1.0)
4
+ rubocop-sorbet (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- minitest (5.11.3)
10
- rake (10.5.0)
9
+ abstract_type (0.0.7)
10
+ adamantium (0.2.0)
11
+ ice_nine (~> 0.11.0)
12
+ memoizable (~> 0.4.0)
13
+ ast (2.4.0)
14
+ concord (0.1.5)
15
+ adamantium (~> 0.2.0)
16
+ equalizer (~> 0.0.9)
17
+ diff-lcs (1.3)
18
+ domain_name (0.5.20190701)
19
+ unf (>= 0.0.5, < 1.0.0)
20
+ equalizer (0.0.11)
21
+ highline (1.6.20)
22
+ http-cookie (1.0.3)
23
+ domain_name (~> 0.5)
24
+ ice_nine (0.11.2)
25
+ jaro_winkler (1.5.3)
26
+ json_pure (1.8.1)
27
+ memoizable (0.4.2)
28
+ thread_safe (~> 0.3, >= 0.3.1)
29
+ mime-types (3.2.2)
30
+ mime-types-data (~> 3.2015)
31
+ mime-types-data (3.2019.0331)
32
+ netrc (0.11.0)
33
+ package_cloud (0.3.05)
34
+ highline (= 1.6.20)
35
+ json_pure (= 1.8.1)
36
+ rainbow (= 2.2.2)
37
+ rest-client (~> 2.0)
38
+ thor (~> 0.18)
39
+ parallel (1.17.0)
40
+ parser (2.6.3.0)
41
+ ast (~> 2.4.0)
42
+ procto (0.0.3)
43
+ rainbow (2.2.2)
44
+ rake
45
+ rake (12.3.2)
46
+ rest-client (2.0.2)
47
+ http-cookie (>= 1.0.2, < 2.0)
48
+ mime-types (>= 1.16, < 4.0)
49
+ netrc (~> 0.8)
50
+ rspec (3.8.0)
51
+ rspec-core (~> 3.8.0)
52
+ rspec-expectations (~> 3.8.0)
53
+ rspec-mocks (~> 3.8.0)
54
+ rspec-core (3.8.2)
55
+ rspec-support (~> 3.8.0)
56
+ rspec-expectations (3.8.4)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.8.0)
59
+ rspec-mocks (3.8.1)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.8.0)
62
+ rspec-support (3.8.2)
63
+ rubocop (0.72.0)
64
+ jaro_winkler (~> 1.5.1)
65
+ parallel (~> 1.10)
66
+ parser (>= 2.6)
67
+ rainbow (>= 2.2.2, < 4.0)
68
+ ruby-progressbar (~> 1.7)
69
+ unicode-display_width (>= 1.4.0, < 1.7)
70
+ ruby-progressbar (1.10.1)
71
+ thor (0.20.3)
72
+ thread_safe (0.3.6)
73
+ unf (0.1.4)
74
+ unf_ext
75
+ unf_ext (0.0.7.6)
76
+ unicode-display_width (1.6.0)
77
+ unparser (0.4.5)
78
+ abstract_type (~> 0.0.7)
79
+ adamantium (~> 0.2.0)
80
+ concord (~> 0.1.5)
81
+ diff-lcs (~> 1.3)
82
+ equalizer (~> 0.0.9)
83
+ parser (~> 2.6.3)
84
+ procto (~> 0.0.2)
11
85
 
12
86
  PLATFORMS
13
87
  ruby
14
88
 
15
89
  DEPENDENCIES
16
- bundler (~> 1.17)
17
- minitest (~> 5.0)
18
- rake (~> 10.0)
90
+ package_cloud (~> 0.3.05)
91
+ rspec (~> 3.7)
92
+ rubocop (~> 0.57)
19
93
  rubocop-sorbet!
94
+ unparser (~> 0.4.2)
20
95
 
21
96
  BUNDLED WITH
22
97
  1.17.3
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Rubocop::Sorbet
1
+ # Rubocop-Sorbet
2
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/sorbet`. 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
3
+ A collection of Rubocop rules for Sorbet.
6
4
 
7
5
  ## Installation
8
6
 
@@ -16,23 +14,18 @@ And then execute:
16
14
 
17
15
  $ bundle
18
16
 
19
- Or install it yourself as:
20
-
21
- $ gem install rubocop-sorbet
22
-
23
17
  ## Usage
24
18
 
25
- TODO: Write usage instructions here
19
+ And add this to your `.rubocop.yml` file:
26
20
 
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
21
+ ```yaml
22
+ require:
23
+ - rubocop-sorbet
24
+ ```
32
25
 
33
26
  ## Contributing
34
27
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/paracycle/rubocop-sorbet. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
28
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/rubocop-sorbet. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
29
 
37
30
  ## License
38
31
 
@@ -40,4 +33,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
33
 
41
34
  ## Code of Conduct
42
35
 
43
- Everyone interacting in the Rubocop::Sorbet project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/paracycle/rubocop-sorbet/blob/master/CODE_OF_CONDUCT.md).
36
+ Everyone interacting in the Rubocop::Sorbet project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Shopify/rubocop-sorbet/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,10 +1,11 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
1
+ # frozen_string_literal: true
3
2
 
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList["test/**/*_test.rb"]
3
+ require('bundler/gem_tasks')
4
+
5
+ begin
6
+ require('rspec/core/rake_task')
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ rescue LoadError
8
9
  end
9
10
 
10
- task :default => :test
11
+ task(default: :spec)
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "rubocop/sorbet"
4
+ require 'bundler/setup'
5
+ require 'rubocop/sorbet'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "rubocop/sorbet"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path('../bundle', __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
+
29
+ load(Gem.bin_path('rspec-core', 'rspec'))
data/dev.yml ADDED
@@ -0,0 +1,10 @@
1
+ name: "rubocop-sorbet"
2
+
3
+ type: "ruby"
4
+
5
+ up:
6
+ - ruby: "2.5.5"
7
+ - "bundler"
8
+
9
+ test:
10
+ run: "bin/rspec"
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Sorbet
8
+ # This cop disallows using `.override(allow_incompatible: true)`.
9
+ # Using `allow_incompatible` suggests a violation of the Liskov
10
+ # Substitution Principle, meaning that a subclass is not a valid
11
+ # subtype of it's superclass. This Cop prevents these design smells
12
+ # from occurring.
13
+ #
14
+ # @example
15
+ #
16
+ # # bad
17
+ # sig.override(allow_incompatible: true)
18
+ #
19
+ # # good
20
+ # sig.override
21
+ class AllowIncompatibleOverride < RuboCop::Cop::Cop
22
+ def_node_search(:sig?, <<-PATTERN)
23
+ (
24
+ send
25
+ nil?
26
+ :sig
27
+ ...
28
+ )
29
+ PATTERN
30
+
31
+ def not_nil?(node)
32
+ !node.nil?
33
+ end
34
+
35
+ def_node_search(:allow_incompatible?, <<-PATTERN)
36
+ (pair (sym :allow_incompatible) (true))
37
+ PATTERN
38
+
39
+ def_node_matcher(:allow_incompatible_override?, <<-PATTERN)
40
+ (
41
+ send
42
+ [#not_nil? #sig?]
43
+ :override
44
+ [#not_nil? #allow_incompatible?]
45
+ )
46
+ PATTERN
47
+
48
+ def on_send(node)
49
+ return unless allow_incompatible_override?(node)
50
+ add_offense(
51
+ node.children[2],
52
+ message: 'Usage of `allow_incompatible` suggests a violation of the Liskov Substitution Principle. '\
53
+ 'Instead, strive to write interfaces which respect subtyping principles and remove `allow_incompatible`',
54
+ )
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Sorbet
8
+ # This cop disallows binding the return value of `T.any`, `T.all`, `T.enum`
9
+ # to a constant directly. To bind the value, one must use `T.type_alias`.
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ # FooOrBar = T.any(Foo, Bar)
15
+ #
16
+ # # good
17
+ # FooOrBar = T.type_alias(T.any(Foo, Bar))
18
+ class BindingConstantWithoutTypeAlias < RuboCop::Cop::Cop
19
+ def_node_matcher(:binding_unaliased_type?, <<-PATTERN)
20
+ (casgn _ _ [#not_nil? #not_t_let? #method_needing_aliasing_on_t?])
21
+ PATTERN
22
+
23
+ def_node_matcher(:using_type_alias?, <<-PATTERN)
24
+ (
25
+ send
26
+ (const nil? :T)
27
+ :type_alias
28
+ _
29
+ )
30
+ PATTERN
31
+
32
+ def_node_matcher(:t_let?, <<-PATTERN)
33
+ (
34
+ send
35
+ (const nil? :T)
36
+ :let
37
+ _
38
+ _
39
+ )
40
+ PATTERN
41
+
42
+ def_node_search(:method_needing_aliasing_on_t?, <<-PATTERN)
43
+ (
44
+ send
45
+ (const nil? :T)
46
+ {:any :all :noreturn :class_of :untyped :nilable :self_type :enum :proc}
47
+ ...
48
+ )
49
+ PATTERN
50
+
51
+ def not_t_let?(node)
52
+ !t_let?(node)
53
+ end
54
+
55
+ def not_nil?(node)
56
+ !node.nil?
57
+ end
58
+
59
+ def on_casgn(node)
60
+ return unless binding_unaliased_type?(node) && !using_type_alias?(node.children[2])
61
+ add_offense(
62
+ node.children[2],
63
+ message: "It looks like you're trying to bind a type to a constant. " \
64
+ 'To do this, you must alias the type using `T.type_alias`.'
65
+ )
66
+ end
67
+
68
+ def autocorrect(node)
69
+ lambda do |corrector|
70
+ corrector.replace(
71
+ node.source_range,
72
+ "T.type_alias(#{node.source})"
73
+ )
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Sorbet
8
+ # This cop disallows the usage of `checked(true)`. This usage could cause
9
+ # confusion; it could lead some people to believe that a method would be checked
10
+ # even if runtime checks have not been enabled on the class or globally.
11
+ # Additionally, in the event where checks are enabled, `checked(true)` would
12
+ # be redundant; only `checked(false)` or `soft` would change the behaviour.
13
+ #
14
+ # @example
15
+ #
16
+ # # bad
17
+ # sig { void.checked(true) }
18
+ #
19
+ # # good
20
+ # sig { void }
21
+ class CheckedTrueInSignature < RuboCop::Cop::Cop
22
+ include(RuboCop::Cop::RangeHelp)
23
+
24
+ def_node_matcher(:signature?, <<~PATTERN)
25
+ (block (send nil? :sig) (args) ...)
26
+ PATTERN
27
+
28
+ def_node_search(:offending_node, <<~PATTERN)
29
+ (send _ :checked (true))
30
+ PATTERN
31
+
32
+ MESSAGE =
33
+ 'Using `checked(true)` in a method signature definition is not allowed. ' \
34
+ '`checked(true)` is the default behavior for modules/classes with runtime checks enabled. ' \
35
+ 'To enable typechecking at runtime for this module, regardless of global settings, ' \
36
+ '`include(WaffleCone::RuntimeChecks)` to this module and set other methods to `checked(false)`.'
37
+ private_constant(:MESSAGE)
38
+
39
+ def on_block(node)
40
+ return unless signature?(node)
41
+
42
+ error = offending_node(node).first
43
+ return unless error
44
+
45
+ add_offense(
46
+ error,
47
+ location: source_range(
48
+ processed_source.buffer,
49
+ error.location.line,
50
+ (error.location.selector.begin_pos)..(error.location.end.begin_pos),
51
+ ),
52
+ message: MESSAGE
53
+ )
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Sorbet
8
+ # This cop disallows the calls that are used to get constants fom Strings
9
+ # such as +constantize+, +const_get+, and +constants+.
10
+ #
11
+ # The goal of this cop is to make the code easier to statically analyze,
12
+ # more IDE-friendly, and more predictable. It leads to code that clearly
13
+ # expresses which values the constant can have.
14
+ #
15
+ # @example
16
+ #
17
+ # # bad
18
+ # class_name.constantize
19
+ #
20
+ # # bad
21
+ # constants.detect { |c| c.name == "User" }
22
+ #
23
+ # # bad
24
+ # const_get(class_name)
25
+ #
26
+ # # good
27
+ # case class_name
28
+ # when "User"
29
+ # User
30
+ # else
31
+ # raise ArgumentError
32
+ # end
33
+ #
34
+ # # good
35
+ # { "User" => User }.fetch(class_name)
36
+ class ConstantsFromStrings < ::RuboCop::Cop::Cop
37
+ def_node_matcher(:constant_from_string?, <<-PATTERN)
38
+ (send _ {:constantize :constants :const_get} ...)
39
+ PATTERN
40
+
41
+ def on_send(node)
42
+ return unless constant_from_string?(node)
43
+ add_offense(
44
+ node,
45
+ location: :selector,
46
+ message: "Don't use `#{node.method_name}`, it make the code harder to understand, less editor-friendly " \
47
+ "and impossible to analyze. Replace `#{node.method_name}` with a case/when or a hash."
48
+ )
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'rubocop'
5
+
6
+ # Correct `send` expressions in include statements by constant literals.
7
+ #
8
+ # Sorbet, the static checker, is not (yet) able to support constructs on the
9
+ # following form:
10
+ #
11
+ # ```ruby
12
+ # class MyClass
13
+ # include send_expr
14
+ # end
15
+ # ```
16
+ #
17
+ # This cop replaces them by:
18
+ #
19
+ # ```ruby
20
+ # class MyClass
21
+ # MyClassInclude = send_expr
22
+ # include MyClassInclude
23
+ # end
24
+ # ```
25
+ #
26
+ # Multiple occurences of this can be found in Shopify's code base like:
27
+ #
28
+ # ```ruby
29
+ # include Rails.application.routes.url_helpers
30
+ # ```
31
+ # or
32
+ # ```ruby
33
+ # include Polaris::Engine.helpers
34
+ # ```
35
+ module RuboCop
36
+ module Cop
37
+ module Sorbet
38
+ class ForbidIncludeConstLiteral < RuboCop::Cop::Cop
39
+ MSG = 'Includes must only contain constant literals'
40
+
41
+ attr_accessor :used_names
42
+
43
+ def_node_matcher :not_lit_const_include?, <<-PATTERN
44
+ (send nil? {:include :extend :prepend}
45
+ $_
46
+ )
47
+ PATTERN
48
+
49
+ def initialize(*)
50
+ super
51
+ self.used_names = Set.new
52
+ end
53
+
54
+ def on_send(node)
55
+ return unless not_lit_const_include?(node) do |send_argument|
56
+ ![:const, :self].include?(send_argument.type)
57
+ end
58
+ parent = node.parent
59
+ return unless parent
60
+ parent = parent.parent if [:begin, :block].include?(parent.type)
61
+ return unless [:module, :class, :sclass].include?(parent.type)
62
+ add_offense(node)
63
+ end
64
+
65
+ def autocorrect(node)
66
+ lambda do |corrector|
67
+ # Find parent class node
68
+ parent = node.parent
69
+ parent = parent.parent if parent.type == :begin
70
+
71
+ # Build include variable name
72
+ class_name = (parent.child_nodes.first.const_name || 'Anon').split('::').last
73
+ include_name = find_free_name("#{class_name}Include")
74
+ used_names << include_name
75
+
76
+ # Apply fix
77
+ indent = ' ' * node.loc.column
78
+ fix = "#{include_name} = #{node.child_nodes.first.source}\n#{indent}"
79
+ corrector.insert_before(node.loc.expression, fix)
80
+ corrector.replace(node.child_nodes.first.loc.expression, include_name)
81
+ end
82
+ end
83
+
84
+ # Find a free local variable name
85
+ #
86
+ # Since each include uses its own local variable to store the send result,
87
+ # we need to ensure that we don't use the same name twice in the same
88
+ # module.
89
+ def find_free_name(base_name)
90
+ return base_name unless used_names.include?(base_name)
91
+ i = 2
92
+ i += 1 while used_names.include?("#{base_name}#{i}")
93
+ "#{base_name}#{i}"
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'rubocop'
5
+
6
+ # Correct superclass `send` expressions by constant literals.
7
+ #
8
+ # Sorbet, the static checker, is not (yet) able to support constructs on the
9
+ # following form:
10
+ #
11
+ # ```ruby
12
+ # class Foo < send_expr; end
13
+ # ```
14
+ #
15
+ # This cop replaces them by:
16
+ #
17
+ # ```ruby
18
+ # FooParent = send_expr
19
+ # class Foo < FooParent; end
20
+ # ```
21
+ #
22
+ # Multiple occurences of this can be found in Shopify's code base like:
23
+ #
24
+ # ```ruby
25
+ # class ShopScope < Component::TrustedIdScope[ShopIdentity::ShopId]
26
+ # ```
27
+ # or
28
+ # ```ruby
29
+ # class ApiClientEligibility < Struct.new(:api_client, :match_results, :shop)
30
+ # ```
31
+ module RuboCop
32
+ module Cop
33
+ module Sorbet
34
+ class ForbidSuperclassConstLiteral < RuboCop::Cop::Cop
35
+ MSG = 'Superclasses must only contain constant literals'
36
+
37
+ def_node_matcher :not_lit_const_superclass?, <<-PATTERN
38
+ (class
39
+ (const ...)
40
+ (send ...)
41
+ ...
42
+ )
43
+ PATTERN
44
+
45
+ def on_class(node)
46
+ return unless not_lit_const_superclass?(node)
47
+ add_offense(node.child_nodes[1])
48
+ end
49
+
50
+ def autocorrect(node)
51
+ lambda do |corrector|
52
+ class_name = node.parent.child_nodes.first.const_name
53
+ parent_name = "#{class_name}Parent"
54
+ indent = ' ' * node.parent.loc.column
55
+ corrector.insert_before(node.parent.loc.expression, "#{parent_name} = #{node.source}\n#{indent}")
56
+ corrector.replace(node.loc.expression, parent_name)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Sorbet
8
+ # This cop checks for the ordering of keyword arguments required by
9
+ # sorbet-runtime. The ordering requires that all keyword arguments
10
+ # are at the end of the parameters list, and all keyword arguments
11
+ # with a default value must be after those without default values.
12
+ #
13
+ # @example
14
+ #
15
+ # # bad
16
+ # sig { params(a: Integer, b: String).void }
17
+ # def foo(a: 1, b:); end
18
+ #
19
+ # # good
20
+ # sig { params(b: String, a: Integer).void }
21
+ # def foo(b:, a: 1); end
22
+ class KeywordArgumentOrdering < RuboCop::Cop::Cop
23
+ def_node_matcher(:signature?, <<-PATTERN)
24
+ (block (send nil? :sig) (args) ...)
25
+ PATTERN
26
+
27
+ def on_block(node)
28
+ return unless signature?(node)
29
+
30
+ method_node = node.parent.children[node.sibling_index + 1]
31
+ return if method_node.nil?
32
+ method_parameters = method_node.arguments
33
+
34
+ check_order_for_kwoptargs(method_parameters)
35
+ end
36
+
37
+ private
38
+
39
+ def check_order_for_kwoptargs(parameters)
40
+ out_of_kwoptarg = false
41
+
42
+ parameters.reverse.each do |param|
43
+ out_of_kwoptarg = true unless param.type == :kwoptarg || param.type == :blockarg
44
+
45
+ next unless param.type == :kwoptarg && out_of_kwoptarg
46
+
47
+ add_offense(
48
+ param,
49
+ message: 'Optional keyword arguments must be at the end of the parameter list.'
50
+ )
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Sorbet
8
+ # This cop checks for inconsistent ordering of parameters between the
9
+ # signature and the method definition. The sorbet-runtime gem raises
10
+ # when such inconsistency occurs.
11
+ #
12
+ # @example
13
+ #
14
+ # # bad
15
+ # sig { params(a: Integer, b: String).void }
16
+ # def foo(b:, a:); end
17
+ #
18
+ # # good
19
+ # sig { params(a: Integer, b: String).void }
20
+ # def foo(a:, b:); end
21
+ class ParametersOrderingInSignature < RuboCop::Cop::Cop
22
+ def_node_matcher(:signature?, <<-PATTERN)
23
+ (block (send nil? :sig) (args) ...)
24
+ PATTERN
25
+
26
+ def_node_search(:signature_params, <<-PATTERN)
27
+ (send _ :params ...)
28
+ PATTERN
29
+
30
+ def on_block(node)
31
+ return unless signature?(node)
32
+
33
+ sig_params = signature_params(node).first
34
+ sig_params_order =
35
+ if sig_params.nil?
36
+ []
37
+ else
38
+ sig_params.arguments.first.keys.map(&:value)
39
+ end
40
+
41
+ method_node = node.parent.children[node.sibling_index + 1]
42
+ return if method_node.nil? || method_node.type != :def
43
+ method_parameters = method_node.arguments
44
+
45
+ check_for_inconsistent_param_ordering(sig_params_order, method_parameters)
46
+ end
47
+
48
+ private
49
+
50
+ def check_for_inconsistent_param_ordering(sig_params_order, parameters)
51
+ parameters.each_with_index do |param, index|
52
+ param_name = param.children[0]
53
+ sig_param_name = sig_params_order[index]
54
+
55
+ next if param_name == sig_param_name
56
+
57
+ add_offense(
58
+ param,
59
+ message: "Inconsistent ordering of arguments at index #{index}. " \
60
+ "Expected `#{sig_param_name}` from sig above."
61
+ )
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ begin
6
+ require 'unparser'
7
+ rescue LoadError
8
+ nil
9
+ end
10
+
11
+ module RuboCop
12
+ module Cop
13
+ module Sorbet
14
+ class SignatureBuildOrder < RuboCop::Cop::Cop
15
+ ORDER =
16
+ [
17
+ :type_parameters,
18
+ :params,
19
+ :returns,
20
+ :void,
21
+ :abstract,
22
+ :implementation,
23
+ :override,
24
+ :overridable,
25
+ :soft,
26
+ :checked,
27
+ ].each_with_index.to_h.freeze
28
+
29
+ def_node_matcher(:signature?, <<~PATTERN)
30
+ (block (send nil? :sig) (args) ...)
31
+ PATTERN
32
+
33
+ def_node_search(:root_call, <<~PATTERN)
34
+ (send nil? {#{ORDER.keys.map(&:inspect).join(' ')}} ...)
35
+ PATTERN
36
+
37
+ def on_block(node)
38
+ return unless signature?(node)
39
+
40
+ calls = call_chain(node.children[2]).map(&:method_name)
41
+ return unless calls.any?
42
+
43
+ expected_order = calls.sort_by { |call| ORDER[call] }
44
+ return if expected_order == calls
45
+
46
+ message = "Sig builders must be invoked in the following order: #{expected_order.join(', ')}."
47
+
48
+ unless can_autocorrect?
49
+ message += ' For autocorrection, add the `unparser` gem to your project.'
50
+ end
51
+
52
+ add_offense(
53
+ node.children[2],
54
+ message: message,
55
+ )
56
+ node
57
+ end
58
+
59
+ def autocorrect(node)
60
+ return nil unless can_autocorrect?
61
+
62
+ lambda do |corrector|
63
+ nodes = call_chain(node).sort_by { |call| ORDER[call.method_name] }
64
+ tree =
65
+ nodes.reduce(nil) do |receiver, caller|
66
+ caller.updated(nil, [receiver] + caller.children.drop(1))
67
+ end
68
+
69
+ corrector.replace(
70
+ node.source_range,
71
+ Unparser.unparse(tree),
72
+ )
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def can_autocorrect?
79
+ defined?(::Unparser)
80
+ end
81
+
82
+ def call_chain(sig_child_node)
83
+ call_node = root_call(sig_child_node).first
84
+ return [] unless call_node
85
+
86
+ calls = []
87
+ while call_node != sig_child_node
88
+ calls << call_node
89
+ call_node = call_node.parent
90
+ end
91
+
92
+ calls << sig_child_node
93
+
94
+ calls
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop_sorbet'
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rubocop/cop/sorbet/allow_incompatible_override'
4
+ require_relative 'rubocop/cop/sorbet/binding_constants_without_type_alias'
5
+ require_relative 'rubocop/cop/sorbet/checked_true_in_signature'
6
+ require_relative 'rubocop/cop/sorbet/constants_from_strings'
7
+ require_relative 'rubocop/cop/sorbet/signature_build_order'
8
+ require_relative 'rubocop/cop/sorbet/forbid_superclass_const_literal'
9
+ require_relative 'rubocop/cop/sorbet/forbid_include_const_literal'
10
+ require_relative 'rubocop/cop/sorbet/parameters_ordering_in_signature'
11
+ require_relative 'rubocop/cop/sorbet/keyword_argument_ordering'
@@ -1,31 +1,31 @@
1
+ # frozen_string_literal: true
1
2
 
2
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "rubocop/sorbet/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "rubocop-sorbet"
8
- spec.version = Rubocop::Sorbet::VERSION
9
- spec.authors = ["Ufuk Kayserilioglu"]
10
- spec.email = ["ufuk.kayserilioglu@shopify.com"]
7
+ spec.name = 'rubocop-sorbet'
8
+ spec.version = '0.2.0'
9
+ spec.authors = ['Ufuk Kayserilioglu']
10
+ spec.email = ['ufuk.kayserilioglu@shopify.com']
11
11
 
12
- spec.summary = %q{Automatic Sorbet code style checking tool.}
13
- spec.homepage = "https://github.com/shopify/rubocop-sorbet"
14
- spec.license = "MIT"
12
+ spec.summary = 'Automatic Sorbet code style checking tool.'
13
+ spec.homepage = 'https://github.com/shopify/rubocop-sorbet'
14
+ spec.license = 'MIT'
15
15
 
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = "https://github.com/shopify/rubocop-sorbet"
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/shopify/rubocop-sorbet'
18
18
 
19
19
  # Specify which files should be added to the gem when it is released.
20
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ %x(git ls-files -z).split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
23
  end
24
- spec.bindir = "exe"
24
+ spec.bindir = 'exe'
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
- spec.require_paths = ["lib"]
26
+ spec.require_paths = ['lib']
27
27
 
28
- spec.add_development_dependency "bundler", "~> 1.17"
29
- spec.add_development_dependency "rake", "~> 10.0"
30
- spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency('rspec', '~> 3.7')
29
+ spec.add_development_dependency('unparser', '~> 0.4.2')
30
+ spec.add_development_dependency('rubocop', '~> 0.57')
31
31
  end
data/shipit.yml ADDED
@@ -0,0 +1 @@
1
+ # Default config
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-sorbet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ufuk Kayserilioglu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-04 00:00:00.000000000 Z
11
+ date: 2019-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.17'
19
+ version: '3.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.17'
26
+ version: '3.7'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: unparser
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 0.4.2
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 0.4.2
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest
42
+ name: rubocop
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '5.0'
47
+ version: '0.57'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '5.0'
54
+ version: '0.57'
55
55
  description:
56
56
  email:
57
57
  - ufuk.kayserilioglu@shopify.com
@@ -60,6 +60,9 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - ".shopify-build/VERSION"
65
+ - ".shopify-build/rubocop-sorbet.yml"
63
66
  - ".travis.yml"
64
67
  - CODE_OF_CONDUCT.md
65
68
  - Gemfile
@@ -68,10 +71,22 @@ files:
68
71
  - README.md
69
72
  - Rakefile
70
73
  - bin/console
74
+ - bin/rspec
71
75
  - bin/setup
72
- - lib/rubocop/sorbet.rb
73
- - lib/rubocop/sorbet/version.rb
76
+ - dev.yml
77
+ - lib/rubocop-sorbet.rb
78
+ - lib/rubocop/cop/sorbet/allow_incompatible_override.rb
79
+ - lib/rubocop/cop/sorbet/binding_constants_without_type_alias.rb
80
+ - lib/rubocop/cop/sorbet/checked_true_in_signature.rb
81
+ - lib/rubocop/cop/sorbet/constants_from_strings.rb
82
+ - lib/rubocop/cop/sorbet/forbid_include_const_literal.rb
83
+ - lib/rubocop/cop/sorbet/forbid_superclass_const_literal.rb
84
+ - lib/rubocop/cop/sorbet/keyword_argument_ordering.rb
85
+ - lib/rubocop/cop/sorbet/parameters_ordering_in_signature.rb
86
+ - lib/rubocop/cop/sorbet/signature_build_order.rb
87
+ - lib/rubocop_sorbet.rb
74
88
  - rubocop-sorbet.gemspec
89
+ - shipit.yml
75
90
  homepage: https://github.com/shopify/rubocop-sorbet
76
91
  licenses:
77
92
  - MIT
@@ -93,7 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
108
  - !ruby/object:Gem::Version
94
109
  version: '0'
95
110
  requirements: []
96
- rubygems_version: 3.0.3
111
+ rubyforge_project:
112
+ rubygems_version: 2.7.6
97
113
  signing_key:
98
114
  specification_version: 4
99
115
  summary: Automatic Sorbet code style checking tool.
@@ -1,5 +0,0 @@
1
- module Rubocop
2
- module Sorbet
3
- VERSION = "0.1.0"
4
- end
5
- end
@@ -1,8 +0,0 @@
1
- require "rubocop/sorbet/version"
2
-
3
- module Rubocop
4
- module Sorbet
5
- class Error < StandardError; end
6
- # Your code goes here...
7
- end
8
- end