rubocop-on-rbs 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +186 -0
- data/config/default.yml +165 -0
- data/lib/rubocop/cop/rbs/layout/comment_indentation.rb +69 -0
- data/lib/rubocop/cop/rbs/layout/empty_lines_around_overloads.rb +49 -0
- data/lib/rubocop/cop/rbs/layout/end_alignment.rb +55 -0
- data/lib/rubocop/cop/rbs/layout/extra_spacing.rb +55 -0
- data/lib/rubocop/cop/rbs/layout/indentation_width.rb +64 -0
- data/lib/rubocop/cop/rbs/layout/overload_indentation.rb +69 -0
- data/lib/rubocop/cop/rbs/layout/space_around_arrow.rb +56 -0
- data/lib/rubocop/cop/rbs/layout/space_around_braces.rb +77 -0
- data/lib/rubocop/cop/rbs/layout/space_before_colon.rb +43 -0
- data/lib/rubocop/cop/rbs/layout/space_before_overload.rb +46 -0
- data/lib/rubocop/cop/rbs/layout/trailing_whitespace.rb +42 -0
- data/lib/rubocop/cop/rbs/lint/syntax.rb +18 -0
- data/lib/rubocop/cop/rbs/lint/type_params_arity.rb +170 -0
- data/lib/rubocop/cop/rbs/lint/useless_overload_type_params.rb +57 -0
- data/lib/rubocop/cop/rbs/lint/will_syntax_error.rb +205 -0
- data/lib/rubocop/cop/rbs/style/block_return_boolish.rb +35 -0
- data/lib/rubocop/cop/rbs/style/classic_type.rb +73 -0
- data/lib/rubocop/cop/rbs/style/duplicated_type.rb +61 -0
- data/lib/rubocop/cop/rbs/style/initialize_return_type.rb +40 -0
- data/lib/rubocop/cop/rbs/style/merge_untyped.rb +91 -0
- data/lib/rubocop/cop/rbs/style/optional_nil.rb +50 -0
- data/lib/rubocop/cop/rbs/style/true_false.rb +84 -0
- data/lib/rubocop/cop/rbs_cops.rb +28 -0
- data/lib/rubocop/rbs/cop_base.rb +91 -0
- data/lib/rubocop/rbs/inject.rb +20 -0
- data/lib/rubocop/rbs/processed_rbs_source.rb +29 -0
- data/lib/rubocop/rbs/version.rb +7 -0
- data/lib/rubocop/rbs.rb +15 -0
- data/lib/rubocop-on-rbs.rb +13 -0
- metadata +106 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rbs'
|
4
|
+
|
5
|
+
require_relative 'rbs/layout/comment_indentation'
|
6
|
+
require_relative 'rbs/layout/empty_lines_around_overloads'
|
7
|
+
require_relative 'rbs/layout/end_alignment'
|
8
|
+
require_relative 'rbs/layout/extra_spacing'
|
9
|
+
require_relative 'rbs/layout/indentation_width'
|
10
|
+
require_relative 'rbs/layout/overload_indentation'
|
11
|
+
require_relative 'rbs/layout/space_around_arrow'
|
12
|
+
require_relative 'rbs/layout/space_around_braces'
|
13
|
+
require_relative 'rbs/layout/space_before_colon'
|
14
|
+
require_relative 'rbs/layout/space_before_overload'
|
15
|
+
require_relative 'rbs/layout/trailing_whitespace'
|
16
|
+
|
17
|
+
require_relative 'rbs/lint/syntax'
|
18
|
+
require_relative 'rbs/lint/type_params_arity'
|
19
|
+
require_relative 'rbs/lint/useless_overload_type_params'
|
20
|
+
require_relative 'rbs/lint/will_syntax_error'
|
21
|
+
|
22
|
+
require_relative 'rbs/style/block_return_boolish'
|
23
|
+
require_relative 'rbs/style/true_false'
|
24
|
+
require_relative 'rbs/style/classic_type'
|
25
|
+
require_relative 'rbs/style/optional_nil'
|
26
|
+
require_relative 'rbs/style/duplicated_type'
|
27
|
+
require_relative 'rbs/style/initialize_return_type'
|
28
|
+
require_relative 'rbs/style/merge_untyped'
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module RBS
|
5
|
+
# Base class for cops that operate on RBS signatures.
|
6
|
+
class CopBase < RuboCop::Cop::Base
|
7
|
+
include RuboCop::Cop::RangeHelp
|
8
|
+
|
9
|
+
attr_reader :processed_rbs_source
|
10
|
+
|
11
|
+
exclude_from_registry
|
12
|
+
|
13
|
+
def on_new_investigation
|
14
|
+
# Called here when valid as Ruby
|
15
|
+
investigation_rbs()
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_other_file
|
19
|
+
investigation_rbs()
|
20
|
+
end
|
21
|
+
|
22
|
+
def investigation_rbs
|
23
|
+
return unless processed_source.buffer.name.then { |n| n.end_with?(".rbs") || n == "(string)" }
|
24
|
+
|
25
|
+
buffer = rbs_buffer()
|
26
|
+
@processed_rbs_source = RuboCop::RBS::ProcessedRBSSource.new(buffer)
|
27
|
+
|
28
|
+
if processed_rbs_source.error
|
29
|
+
on_rbs_parsing_error()
|
30
|
+
else
|
31
|
+
# HACK: Autocorrector needs to clear diagnostics
|
32
|
+
processed_source.diagnostics.clear
|
33
|
+
|
34
|
+
on_rbs_new_investigation()
|
35
|
+
|
36
|
+
processed_rbs_source.decls.each do |decl|
|
37
|
+
walk(decl)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def on_rbs_new_investigation; end
|
43
|
+
def on_rbs_parsing_error; end
|
44
|
+
|
45
|
+
# other on_* methods should sync with `#walk` method
|
46
|
+
def on_rbs_class(member); end
|
47
|
+
def on_rbs_module(member); end
|
48
|
+
def on_rbs_interface(member); end
|
49
|
+
def on_rbs_constant(const); end
|
50
|
+
def on_rbs_global(global); end
|
51
|
+
def on_rbs_type_alias(decl); end
|
52
|
+
def on_rbs_def(member); end
|
53
|
+
def on_rbs_attribute(member); end
|
54
|
+
|
55
|
+
def walk(decl)
|
56
|
+
case decl
|
57
|
+
when ::RBS::AST::Declarations::Module
|
58
|
+
on_rbs_module(decl)
|
59
|
+
decl.members.each { |member| walk(member) }
|
60
|
+
when ::RBS::AST::Declarations::Class
|
61
|
+
on_rbs_class(decl)
|
62
|
+
decl.members.each { |member| walk(member) }
|
63
|
+
when ::RBS::AST::Declarations::Interface
|
64
|
+
on_rbs_interface(decl)
|
65
|
+
decl.members.each { |member| walk(member) }
|
66
|
+
when ::RBS::AST::Declarations::Constant
|
67
|
+
on_rbs_constant(decl)
|
68
|
+
when ::RBS::AST::Declarations::Global
|
69
|
+
on_rbs_global(decl)
|
70
|
+
when ::RBS::AST::Declarations::TypeAlias
|
71
|
+
on_rbs_type_alias(decl)
|
72
|
+
when ::RBS::AST::Members::MethodDefinition
|
73
|
+
on_rbs_def(decl)
|
74
|
+
when ::RBS::AST::Members::Attribute
|
75
|
+
on_rbs_attribute(decl)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def rbs_buffer
|
80
|
+
::RBS::Buffer.new(
|
81
|
+
name: processed_source.buffer.name,
|
82
|
+
content: processed_source.raw_source
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
def location_to_range(location)
|
87
|
+
range_between(location.start_pos, location.end_pos)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The original code is from https://github.com/rubocop/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
|
4
|
+
# See https://github.com/rubocop/rubocop-rspec/blob/master/MIT-LICENSE.md
|
5
|
+
module RuboCop
|
6
|
+
module RBS
|
7
|
+
# Because RuboCop doesn't yet support plugins, we have to monkey patch in a
|
8
|
+
# bit of our configuration.
|
9
|
+
module Inject
|
10
|
+
def self.defaults!
|
11
|
+
path = CONFIG_DEFAULT.to_s
|
12
|
+
hash = ConfigLoader.send(:load_yaml_configuration, path)
|
13
|
+
config = Config.new(hash, path).tap(&:make_excludes_absolute)
|
14
|
+
puts "configuration from #{path}" if ConfigLoader.debug?
|
15
|
+
config = ConfigLoader.merge_with_default(config, path)
|
16
|
+
ConfigLoader.instance_variable_set(:@default_configuration, config)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module RBS
|
5
|
+
class ProcessedRBSSource
|
6
|
+
attr_reader :raw_source
|
7
|
+
attr_reader :source
|
8
|
+
attr_reader :buffer
|
9
|
+
attr_reader :directives
|
10
|
+
attr_reader :decls
|
11
|
+
attr_reader :error
|
12
|
+
|
13
|
+
def initialize(source)
|
14
|
+
@raw_source = source.content
|
15
|
+
@buffer, @directives, @decls = ::RBS::Parser.parse_signature(source)
|
16
|
+
@error = nil
|
17
|
+
@tokens = nil
|
18
|
+
rescue ::RBS::ParsingError => e
|
19
|
+
@error = e
|
20
|
+
end
|
21
|
+
|
22
|
+
def tokens
|
23
|
+
@tokens ||= begin
|
24
|
+
::RBS::Parser.lex(buffer).value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/rubocop/rbs.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'rbs/version'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module RBS
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
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,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rubocop'
|
4
|
+
|
5
|
+
require_relative 'rubocop/rbs'
|
6
|
+
require_relative 'rubocop/rbs/version'
|
7
|
+
require_relative 'rubocop/rbs/cop_base'
|
8
|
+
require_relative 'rubocop/rbs/inject'
|
9
|
+
require_relative 'rubocop/rbs/processed_rbs_source'
|
10
|
+
|
11
|
+
RuboCop::RBS::Inject.defaults!
|
12
|
+
|
13
|
+
require_relative 'rubocop/cop/rbs_cops'
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubocop-on-rbs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ksss
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-06-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rbs
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.41'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.41'
|
41
|
+
description: RuboCop extension for RBS file.
|
42
|
+
email:
|
43
|
+
- co000ri@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- CHANGELOG.md
|
49
|
+
- CODE_OF_CONDUCT.md
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- config/default.yml
|
53
|
+
- lib/rubocop-on-rbs.rb
|
54
|
+
- lib/rubocop/cop/rbs/layout/comment_indentation.rb
|
55
|
+
- lib/rubocop/cop/rbs/layout/empty_lines_around_overloads.rb
|
56
|
+
- lib/rubocop/cop/rbs/layout/end_alignment.rb
|
57
|
+
- lib/rubocop/cop/rbs/layout/extra_spacing.rb
|
58
|
+
- lib/rubocop/cop/rbs/layout/indentation_width.rb
|
59
|
+
- lib/rubocop/cop/rbs/layout/overload_indentation.rb
|
60
|
+
- lib/rubocop/cop/rbs/layout/space_around_arrow.rb
|
61
|
+
- lib/rubocop/cop/rbs/layout/space_around_braces.rb
|
62
|
+
- lib/rubocop/cop/rbs/layout/space_before_colon.rb
|
63
|
+
- lib/rubocop/cop/rbs/layout/space_before_overload.rb
|
64
|
+
- lib/rubocop/cop/rbs/layout/trailing_whitespace.rb
|
65
|
+
- lib/rubocop/cop/rbs/lint/syntax.rb
|
66
|
+
- lib/rubocop/cop/rbs/lint/type_params_arity.rb
|
67
|
+
- lib/rubocop/cop/rbs/lint/useless_overload_type_params.rb
|
68
|
+
- lib/rubocop/cop/rbs/lint/will_syntax_error.rb
|
69
|
+
- lib/rubocop/cop/rbs/style/block_return_boolish.rb
|
70
|
+
- lib/rubocop/cop/rbs/style/classic_type.rb
|
71
|
+
- lib/rubocop/cop/rbs/style/duplicated_type.rb
|
72
|
+
- lib/rubocop/cop/rbs/style/initialize_return_type.rb
|
73
|
+
- lib/rubocop/cop/rbs/style/merge_untyped.rb
|
74
|
+
- lib/rubocop/cop/rbs/style/optional_nil.rb
|
75
|
+
- lib/rubocop/cop/rbs/style/true_false.rb
|
76
|
+
- lib/rubocop/cop/rbs_cops.rb
|
77
|
+
- lib/rubocop/rbs.rb
|
78
|
+
- lib/rubocop/rbs/cop_base.rb
|
79
|
+
- lib/rubocop/rbs/inject.rb
|
80
|
+
- lib/rubocop/rbs/processed_rbs_source.rb
|
81
|
+
- lib/rubocop/rbs/version.rb
|
82
|
+
homepage: https://github.com/ksss/rubocop-on-rbs
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata:
|
86
|
+
rubygems_mfa_required: 'true'
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 3.1.0
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubygems_version: 3.5.9
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: RuboCop extension for RBS file.
|
106
|
+
test_files: []
|