rubocop-sorbet 0.0.2

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: aceacd9544f68f9fa77c04eb851f4b9c950ae807bcd6acba40343930e1e09c60
4
+ data.tar.gz: 32568e235724593175627d5e27bf08fde7465bd0faaadf130f24928336ffa80c
5
+ SHA512:
6
+ metadata.gz: 7c373c8ab61cb4801f0e15acc68d959d4b5dcc517a958083067b30461f3d6d1095b4c57ebf65e6e0c84ae5c50c2bfa3f8a5dc3c43809020315a5f02c1e82107f
7
+ data.tar.gz: fc0e8c2beaae55bd7d041c26c18d4bb33fe12531e024ac99ade345bd4854c7196a3717af0f605408f4f9b50ab6dd77db368862c946547063b853980f4de5831b
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .rubocop-https--*
@@ -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"
@@ -0,0 +1,10 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 1.17.3
8
+ script:
9
+ - bundle exec rubocop --config .rubocop.yml
10
+ - bundle exec rspec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ufuk@paralaus.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
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
9
+
10
+ # Specify your gem's dependencies in rubocop-sorbet.gemspec
11
+ gemspec
@@ -0,0 +1,97 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rubocop-sorbet (0.0.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
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)
85
+
86
+ PLATFORMS
87
+ ruby
88
+
89
+ DEPENDENCIES
90
+ package_cloud (~> 0.3.05)
91
+ rspec (~> 3.7)
92
+ rubocop (~> 0.57)
93
+ rubocop-sorbet!
94
+ unparser (~> 0.4.2)
95
+
96
+ BUNDLED WITH
97
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Ufuk Kayserilioglu
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,36 @@
1
+ # Rubocop-Sorbet
2
+
3
+ A collection of Rubocop rules for Sorbet.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rubocop-sorbet'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ And add this to your `.rubocop.yml` file:
20
+
21
+ ```yaml
22
+ require:
23
+ - rubocop-sorbet
24
+ ```
25
+
26
+ ## Contributing
27
+
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.
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
33
+
34
+ ## Code of Conduct
35
+
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).
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('bundler/gem_tasks')
4
+
5
+ begin
6
+ require('rspec/core/rake_task')
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ rescue LoadError
9
+ end
10
+
11
+ task(default: :spec)
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'rubocop/sorbet'
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,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'))
@@ -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
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,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop_sorbet'
@@ -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,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'
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rubocop-sorbet'
8
+ spec.version = '0.0.2'
9
+ spec.authors = ['Ufuk Kayserilioglu']
10
+ spec.email = ['ufuk.kayserilioglu@shopify.com']
11
+
12
+ spec.summary = 'Automatic Sorbet code style checking tool.'
13
+ spec.homepage = 'https://github.com/shopify/rubocop-sorbet'
14
+ spec.license = 'MIT'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/shopify/rubocop-sorbet'
18
+
19
+ # Specify which files should be added to the gem when it is released.
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
+ %x(git ls-files -z).split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
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
+ end
@@ -0,0 +1 @@
1
+ # Default config
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-sorbet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ufuk Kayserilioglu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: unparser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.4.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.57'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.57'
55
+ description:
56
+ email:
57
+ - ufuk.kayserilioglu@shopify.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - ".shopify-build/VERSION"
65
+ - ".shopify-build/rubocop-sorbet.yml"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/rspec
75
+ - bin/setup
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
88
+ - rubocop-sorbet.gemspec
89
+ - shipit.yml
90
+ homepage: https://github.com/shopify/rubocop-sorbet
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ homepage_uri: https://github.com/shopify/rubocop-sorbet
95
+ source_code_uri: https://github.com/shopify/rubocop-sorbet
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.7.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Automatic Sorbet code style checking tool.
116
+ test_files: []