steep-kit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +18 -0
- data/LICENSE.md +21 -0
- data/README.md +396 -0
- data/exe/steep-kit +12 -0
- data/lib/steep_kit/alias_skip.rb +41 -0
- data/lib/steep_kit/check.rb +62 -0
- data/lib/steep_kit/cli.rb +110 -0
- data/lib/steep_kit/config.rb +139 -0
- data/lib/steep_kit/constant_literal_inference.rb +33 -0
- data/lib/steep_kit/constant_table.rb +152 -0
- data/lib/steep_kit/conventions.rb +175 -0
- data/lib/steep_kit/dynamic_superclasses.rb +26 -0
- data/lib/steep_kit/error.rb +9 -0
- data/lib/steep_kit/fast.rb +113 -0
- data/lib/steep_kit/generate.rb +55 -0
- data/lib/steep_kit/lint_constant_access.rb +181 -0
- data/lib/steep_kit/lint_inline.rb +177 -0
- data/lib/steep_kit/literal_inference.rb +143 -0
- data/lib/steep_kit/mixin_inference/installer.rb +153 -0
- data/lib/steep_kit/mixin_inference.rb +149 -0
- data/lib/steep_kit/output_plan.rb +181 -0
- data/lib/steep_kit/passes.rb +282 -0
- data/lib/steep_kit/patch_guard.rb +18 -0
- data/lib/steep_kit/paths.rb +17 -0
- data/lib/steep_kit/project.rb +44 -0
- data/lib/steep_kit/project_types.rb +72 -0
- data/lib/steep_kit/rbs_builder.rb +141 -0
- data/lib/steep_kit/rbs_collection.rb +61 -0
- data/lib/steep_kit/rbs_rails_postprocess.rb +98 -0
- data/lib/steep_kit/rbs_rails_regen.rb +39 -0
- data/lib/steep_kit/rbs_rails_scratch.rb +169 -0
- data/lib/steep_kit/rbs_rails_tasks.rb +20 -0
- data/lib/steep_kit/runtime.rb +344 -0
- data/lib/steep_kit/settings.rb +96 -0
- data/lib/steep_kit/signature_index.rb +116 -0
- data/lib/steep_kit/signature_kinds.rb +68 -0
- data/lib/steep_kit/signature_merger.rb +149 -0
- data/lib/steep_kit/signature_only.rb +33 -0
- data/lib/steep_kit/source_analysis.rb +227 -0
- data/lib/steep_kit/source_cache.rb +92 -0
- data/lib/steep_kit/source_files.rb +66 -0
- data/lib/steep_kit/source_set.rb +62 -0
- data/lib/steep_kit/steep.rb +35 -0
- data/lib/steep_kit/type_name_resolver.rb +47 -0
- data/lib/steep_kit/value_classes.rb +89 -0
- data/lib/steep_kit/version.rb +3 -0
- data/lib/steep_kit/watch.rb +64 -0
- data/lib/steep_kit/workspace_graph.rb +90 -0
- data/lib/steep_kit.rb +31 -0
- metadata +137 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
require_relative 'error'
|
|
2
|
+
|
|
3
|
+
module SteepKit
|
|
4
|
+
module Settings
|
|
5
|
+
def self.parse_section(name, raw, defaults, strings: [])
|
|
6
|
+
return defaults if raw.nil?
|
|
7
|
+
raise Error, "#{name} must be a mapping" unless raw.is_a?(Hash)
|
|
8
|
+
|
|
9
|
+
members = defaults.class.members
|
|
10
|
+
unknown = raw.keys - members
|
|
11
|
+
raise Error, "#{name} has unknown keys: #{unknown.join(', ')}" if unknown.any?
|
|
12
|
+
|
|
13
|
+
overrides = raw.to_h { |key, value| [key, validate_value(name, key, value, strings:)] }
|
|
14
|
+
defaults.with(**overrides)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.validate_value(section, key, value, strings:)
|
|
18
|
+
return validate_string(section, key, value) if strings.include?(key)
|
|
19
|
+
return value if [true, false].include?(value)
|
|
20
|
+
|
|
21
|
+
raise Error, "#{section}.#{key} must be a boolean"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.validate_string(section, key, value)
|
|
25
|
+
return nil if value.nil?
|
|
26
|
+
raise Error, "#{section}.#{key} must be a non-empty string" unless value.is_a?(String) && !value.empty?
|
|
27
|
+
|
|
28
|
+
value.freeze
|
|
29
|
+
end
|
|
30
|
+
private_class_method :validate_value, :validate_string
|
|
31
|
+
|
|
32
|
+
Projections = Data.define(:data_structs, :aliases, :attr_json) do
|
|
33
|
+
def self.default
|
|
34
|
+
new(data_structs: true, aliases: true, attr_json: false)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.parse(raw)
|
|
38
|
+
Settings.parse_section(:projections, raw, default)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
Checks = Data.define(:rbs_collection, :lint_inline, :lint_constant_access, :projected_freshness) do
|
|
43
|
+
def self.default
|
|
44
|
+
new(rbs_collection: true, lint_inline: true, lint_constant_access: true, projected_freshness: true)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.parse(raw)
|
|
48
|
+
Settings.parse_section(:checks, raw, default)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
Steep = Data.define(
|
|
53
|
+
:literal_constants,
|
|
54
|
+
:value_classes,
|
|
55
|
+
:alias_skip,
|
|
56
|
+
:signature_only_donors,
|
|
57
|
+
:index_signatures,
|
|
58
|
+
:compact_completed_typing,
|
|
59
|
+
:fast_group
|
|
60
|
+
) do
|
|
61
|
+
def self.default
|
|
62
|
+
new(
|
|
63
|
+
literal_constants: true,
|
|
64
|
+
value_classes: true,
|
|
65
|
+
alias_skip: true,
|
|
66
|
+
signature_only_donors: true,
|
|
67
|
+
index_signatures: true,
|
|
68
|
+
compact_completed_typing: true,
|
|
69
|
+
fast_group: nil
|
|
70
|
+
)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.parse(raw)
|
|
74
|
+
Settings.parse_section(:steep, raw, default, strings: %i[fast_group])
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
RbsRails = Data.define(:postgres_arrays, :custom_type_rbs_type, :pure_nilable_readers, :scratch_suffix) do
|
|
79
|
+
def self.default
|
|
80
|
+
new(
|
|
81
|
+
postgres_arrays: true,
|
|
82
|
+
custom_type_rbs_type: true,
|
|
83
|
+
pure_nilable_readers: true,
|
|
84
|
+
scratch_suffix: '_rbs_scratch'
|
|
85
|
+
)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def self.parse(raw)
|
|
89
|
+
parsed = Settings.parse_section(:rbs_rails, raw, default, strings: %i[scratch_suffix])
|
|
90
|
+
raise Error, 'rbs_rails.scratch_suffix must be a non-empty string' if parsed.scratch_suffix.nil?
|
|
91
|
+
|
|
92
|
+
parsed
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require 'pathname'
|
|
2
|
+
require_relative 'config'
|
|
3
|
+
require_relative 'patch_guard'
|
|
4
|
+
|
|
5
|
+
module SteepKit
|
|
6
|
+
class SignatureIndex
|
|
7
|
+
class ByPath
|
|
8
|
+
def initialize(entries)
|
|
9
|
+
@entries = entries.to_a
|
|
10
|
+
@positions = {}
|
|
11
|
+
@entries.each_with_index do |entry, position|
|
|
12
|
+
path = yield entry
|
|
13
|
+
next unless path
|
|
14
|
+
|
|
15
|
+
(@positions[path] ||= []) << position
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def select(paths)
|
|
20
|
+
positions = paths.flat_map { |path| @positions.fetch(path, []) }.sort
|
|
21
|
+
positions.map { |position| @entries.fetch(position) }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.install!(stderr: $stderr)
|
|
26
|
+
return false unless defined?(::Steep::Services::SignatureService) && defined?(::RBS::Environment)
|
|
27
|
+
|
|
28
|
+
env = PatchGuard.prepend_if_compatible?(
|
|
29
|
+
::RBS::Environment,
|
|
30
|
+
EnvironmentPatch,
|
|
31
|
+
required_methods: %i[add_source initialize_copy],
|
|
32
|
+
label: 'index_signatures(environment)',
|
|
33
|
+
stderr:
|
|
34
|
+
)
|
|
35
|
+
service = PatchGuard.prepend_if_compatible?(
|
|
36
|
+
::Steep::Services::SignatureService,
|
|
37
|
+
ServicePatch,
|
|
38
|
+
required_methods: %i[type_names const_decls global_decls],
|
|
39
|
+
label: 'index_signatures(service)',
|
|
40
|
+
stderr:
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
env && service
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def initialize(environment)
|
|
47
|
+
sources = [*environment.each_rbs_source, *environment.each_ruby_source]
|
|
48
|
+
@sources = ByPath.new(sources) { |source| source.buffer.name }
|
|
49
|
+
@constants = index_declarations(environment.constant_decls)
|
|
50
|
+
@globals = index_declarations(environment.global_decls)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def type_names(paths)
|
|
54
|
+
names = Set.new
|
|
55
|
+
@sources.select(paths).each do |source|
|
|
56
|
+
source.each_type_name { |name| names << name }
|
|
57
|
+
end
|
|
58
|
+
names
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def constants(paths)
|
|
62
|
+
@constants.select(paths).to_h
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def globals(paths)
|
|
66
|
+
@globals.select(paths).to_h
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def index_declarations(declarations)
|
|
72
|
+
ByPath.new(declarations) do |_, entry|
|
|
73
|
+
location = entry.decl.location
|
|
74
|
+
Pathname(location.buffer.name) if location
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
module EnvironmentPatch
|
|
79
|
+
def steep_kit_signature_index
|
|
80
|
+
@steep_kit_signature_index ||= SignatureIndex.new(self)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def add_source(...)
|
|
84
|
+
@steep_kit_signature_index = nil
|
|
85
|
+
|
|
86
|
+
super
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def initialize_copy(...)
|
|
90
|
+
super
|
|
91
|
+
|
|
92
|
+
@steep_kit_signature_index = nil
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
module ServicePatch
|
|
97
|
+
def type_names(paths:, env:)
|
|
98
|
+
return super unless Config.steep_settings.index_signatures
|
|
99
|
+
|
|
100
|
+
env.steep_kit_signature_index.type_names(paths)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def const_decls(paths:, env:)
|
|
104
|
+
return super unless Config.steep_settings.index_signatures
|
|
105
|
+
|
|
106
|
+
env.steep_kit_signature_index.constants(paths)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def global_decls(paths:, env: latest_env)
|
|
110
|
+
return super unless Config.steep_settings.index_signatures
|
|
111
|
+
|
|
112
|
+
env.steep_kit_signature_index.globals(paths)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require 'rbs'
|
|
2
|
+
require_relative 'config'
|
|
3
|
+
require_relative 'error'
|
|
4
|
+
require_relative 'paths'
|
|
5
|
+
|
|
6
|
+
module SteepKit
|
|
7
|
+
# Namespace kinds the host's own RBS declares: every sig/ tree except the
|
|
8
|
+
# projected output tree. Project#kinds consults them for compact-path parents
|
|
9
|
+
# the Ruby sources leave undeclared (such as a gem's class stubbed by the host).
|
|
10
|
+
class SignatureKinds
|
|
11
|
+
def initialize(root:, projected_root: Config::DEFAULT_PROJECTED_ROOT)
|
|
12
|
+
@root = Pathname(root).expand_path
|
|
13
|
+
@projected_root = projected_root
|
|
14
|
+
@cache = {}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Qualified name => :class | :module; a class declaration wins over module reopenings.
|
|
18
|
+
def call
|
|
19
|
+
current_paths = paths
|
|
20
|
+
expanded = current_paths.to_set
|
|
21
|
+
@cache.keep_if { |path, _| expanded.include?(path) }
|
|
22
|
+
|
|
23
|
+
current_paths.each_with_object({}) do |path, kinds|
|
|
24
|
+
kinds.merge!(kinds_for(path)) { |_, left, right| left == :class ? left : right }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def paths
|
|
31
|
+
generated = @root.join(@projected_root)
|
|
32
|
+
@root.glob('sig/**/*.rbs').sort.reject { |path| Paths.within?(path, generated) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def kinds_for(path)
|
|
36
|
+
content = path.binread.force_encoding(Encoding::UTF_8)
|
|
37
|
+
cached = @cache[path]
|
|
38
|
+
return cached[:kinds] if cached && cached[:content] == content
|
|
39
|
+
|
|
40
|
+
kinds = {}
|
|
41
|
+
declarations(path, content).each { |declaration| collect(declaration, RBS::Namespace.root, kinds) }
|
|
42
|
+
@cache[path] = {
|
|
43
|
+
content:,
|
|
44
|
+
kinds:
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
kinds
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def declarations(path, content)
|
|
51
|
+
raise SourceError.new(path.relative_path_from(@root).to_s, 'not valid UTF-8') unless content.valid_encoding?
|
|
52
|
+
|
|
53
|
+
RBS::Parser.parse_signature(RBS::Buffer.new(name: path.relative_path_from(@root).to_s, content:))[2]
|
|
54
|
+
rescue RBS::ParsingError => e
|
|
55
|
+
raise Error, "invalid RBS: #{e.message}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def collect(declaration, namespace, kinds)
|
|
59
|
+
class_decl = declaration.is_a?(RBS::AST::Declarations::Class)
|
|
60
|
+
return unless class_decl || declaration.is_a?(RBS::AST::Declarations::Module)
|
|
61
|
+
|
|
62
|
+
name = declaration.name.with_prefix(namespace)
|
|
63
|
+
qualified = name.relative!.to_s
|
|
64
|
+
kinds[qualified] = class_decl ? :class : kinds.fetch(qualified, :module)
|
|
65
|
+
declaration.members.each { |member| collect(member, name.to_namespace, kinds) }
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
require 'rbs'
|
|
2
|
+
require_relative 'rbs_builder'
|
|
3
|
+
|
|
4
|
+
module SteepKit
|
|
5
|
+
# Merges projection passes into one declaration per namespace. Typed members
|
|
6
|
+
# beat untyped definitions, otherwise the first wins. Absolute names reset
|
|
7
|
+
# nesting to the root; relative paths keep their context.
|
|
8
|
+
class SignatureMerger
|
|
9
|
+
Namespace = Struct.new(
|
|
10
|
+
:kind, :opened, :anchor, :super_class, :self_types, :type_params, :member_table, :children,
|
|
11
|
+
keyword_init: true
|
|
12
|
+
) do
|
|
13
|
+
def self.blank
|
|
14
|
+
new(
|
|
15
|
+
kind: nil, opened: false, anchor: nil, super_class: nil, self_types: {}, type_params: [],
|
|
16
|
+
member_table: {}, children: {}
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(kinds: {})
|
|
22
|
+
@kinds = kinds
|
|
23
|
+
@root = Namespace.blank
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def absorb(declarations)
|
|
27
|
+
declarations.each { |declaration| absorb_declaration([], declaration) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Namespaces nothing opens are declared on their own, empty, outer first.
|
|
31
|
+
def declarations
|
|
32
|
+
opened, unopened = all_namespaces.partition { |_, node| node.opened }
|
|
33
|
+
@anchored = opened.group_by { |_, node| node.anchor }
|
|
34
|
+
unopened.filter_map { |path, node| parent_declaration(path, node) } + render_scope([]) + @root.member_table.values.map(&:first)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def absorb_declaration(context, declaration)
|
|
40
|
+
return absorb_into(@root, declaration) unless namespace?(declaration)
|
|
41
|
+
|
|
42
|
+
path = segments(context, declaration.name)
|
|
43
|
+
node = dig(path)
|
|
44
|
+
merge_namespace_attributes(node, declaration)
|
|
45
|
+
node.anchor ||= declaration.name.namespace.absolute? ? [] : context if node.opened && opens?(declaration)
|
|
46
|
+
declaration.members.each do |member|
|
|
47
|
+
namespace?(member) ? absorb_declaration(path, member) : absorb_into(node, member)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def namespace?(declaration)
|
|
52
|
+
declaration.is_a?(RBS::AST::Declarations::Class) || declaration.is_a?(RBS::AST::Declarations::Module)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def absorb_into(node, member)
|
|
56
|
+
store(node.member_table, member_key(member), member)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def merge_namespace_attributes(node, declaration)
|
|
60
|
+
class_decl = declaration.is_a?(RBS::AST::Declarations::Class)
|
|
61
|
+
node.kind = class_decl ? :class : node.kind || :module
|
|
62
|
+
node.opened ||= opens?(declaration)
|
|
63
|
+
node.type_params = declaration.type_params if node.type_params.empty? && declaration.type_params.any?
|
|
64
|
+
return merge_super_class(node, declaration) if class_decl
|
|
65
|
+
|
|
66
|
+
declaration.self_types.each { |self_type| node.self_types[self_type.to_s] ||= self_type }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def opens?(declaration)
|
|
70
|
+
declaration.members.any? || declaration.type_params.any? ||
|
|
71
|
+
(declaration.is_a?(RBS::AST::Declarations::Class) ? !declaration.super_class.nil? : declaration.self_types.any?)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# A superclass with explicit type args beats a bare one.
|
|
75
|
+
def merge_super_class(node, declaration)
|
|
76
|
+
super_class = declaration.super_class or return
|
|
77
|
+
|
|
78
|
+
node.super_class = super_class if node.super_class.nil? || (node.super_class.args.empty? && super_class.args.any?)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def segments(context, type_name)
|
|
82
|
+
path = type_name.namespace.path.map(&:to_s) + [type_name.name.to_s]
|
|
83
|
+
type_name.namespace.absolute? ? path : context + path
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def dig(path)
|
|
87
|
+
path.reduce(@root) { |node, name| node.children[name] ||= Namespace.blank }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def store(table, key, member)
|
|
91
|
+
current = table[key]
|
|
92
|
+
typed = !untyped_def?(member)
|
|
93
|
+
return if current && (current.last || !typed)
|
|
94
|
+
|
|
95
|
+
table[key] = [member, typed]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def member_key(member)
|
|
99
|
+
case member
|
|
100
|
+
when RBS::AST::Members::MethodDefinition then [:def, member.kind, member.name]
|
|
101
|
+
when RBS::AST::Members::Alias then [:def, member.kind, member.new_name]
|
|
102
|
+
when RBS::AST::Members::AttrReader, RBS::AST::Members::AttrWriter, RBS::AST::Members::AttrAccessor
|
|
103
|
+
[:attr, member.class.name, member.kind, member.name]
|
|
104
|
+
else member
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def untyped_def?(member)
|
|
109
|
+
return false unless member.is_a?(RBS::AST::Members::MethodDefinition)
|
|
110
|
+
|
|
111
|
+
member.overloads.all? do |overload|
|
|
112
|
+
function = overload.method_type.type
|
|
113
|
+
next true if function.is_a?(RBS::Types::UntypedFunction)
|
|
114
|
+
|
|
115
|
+
function.each_param.all? { |param| param.type.is_a?(RBS::Types::Bases::Any) } &&
|
|
116
|
+
function.return_type.is_a?(RBS::Types::Bases::Any) && overload.method_type.block.nil?
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def all_namespaces(node = @root, path = [], namespaces = [])
|
|
121
|
+
node.children.each do |name, child|
|
|
122
|
+
namespaces << [path + [name], child]
|
|
123
|
+
all_namespaces(child, path + [name], namespaces)
|
|
124
|
+
end
|
|
125
|
+
namespaces
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def render_scope(scope)
|
|
129
|
+
@anchored.fetch(scope, []).map do |path, node|
|
|
130
|
+
render(path.drop(scope.size).join('::'), node, node.member_table.values.map(&:first) + render_scope(path))
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def render(name, node, members)
|
|
135
|
+
if node.kind == :class
|
|
136
|
+
RbsBuilder.class_decl(name, members, super_class: node.super_class, type_params: node.type_params)
|
|
137
|
+
else
|
|
138
|
+
RbsBuilder.module_decl(name, members, type_params: node.type_params, self_types: node.self_types.values)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def parent_declaration(path, node)
|
|
143
|
+
qualified = path.join('::')
|
|
144
|
+
kind = node.kind || @kinds[qualified] or return
|
|
145
|
+
|
|
146
|
+
RbsBuilder.namespace_decl("::#{qualified}", kind, [])
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require_relative 'config'
|
|
2
|
+
require_relative 'patch_guard'
|
|
3
|
+
|
|
4
|
+
module SteepKit
|
|
5
|
+
module SignatureOnly
|
|
6
|
+
def self.install!(stderr: $stderr)
|
|
7
|
+
return unless defined?(::Steep::Drivers::Check)
|
|
8
|
+
|
|
9
|
+
PatchGuard.prepend_if_compatible?(
|
|
10
|
+
::Steep::Drivers::Check,
|
|
11
|
+
self,
|
|
12
|
+
required_methods: %i[build_typecheck_params],
|
|
13
|
+
label: 'signature_only_donors',
|
|
14
|
+
stderr:
|
|
15
|
+
)
|
|
16
|
+
nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def build_typecheck_params(project)
|
|
20
|
+
params = super
|
|
21
|
+
return params unless Config.steep_settings.signature_only_donors
|
|
22
|
+
|
|
23
|
+
signatures, bodies = params[:inline_paths].partition do |_, path|
|
|
24
|
+
group = project.group_for_inline_source_path(Pathname(path))
|
|
25
|
+
group && group.code_diagnostics_config == ::Steep::Diagnostic::Ruby.silent
|
|
26
|
+
end
|
|
27
|
+
params[:inline_paths] = bodies
|
|
28
|
+
params[:signature_paths].concat(signatures)
|
|
29
|
+
|
|
30
|
+
params
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require 'rbs'
|
|
3
|
+
require_relative 'alias_skip'
|
|
4
|
+
require_relative 'constant_literal_inference'
|
|
5
|
+
require_relative 'constant_table'
|
|
6
|
+
require_relative 'dynamic_superclasses'
|
|
7
|
+
require_relative 'error'
|
|
8
|
+
require_relative 'literal_inference'
|
|
9
|
+
require_relative 'paths'
|
|
10
|
+
require_relative 'value_classes'
|
|
11
|
+
|
|
12
|
+
module SteepKit
|
|
13
|
+
# Shared per-file parse for projection passes: the native RBS inline view
|
|
14
|
+
# plus the raw Prism AST for constructs the inline parser does not model.
|
|
15
|
+
class SourceAnalysis
|
|
16
|
+
Segment = Data.define(:name, :kind) # kind: :class | :module, nil for a compact-path parent
|
|
17
|
+
|
|
18
|
+
InlineNamespace = Data.define(:segments, :decl)
|
|
19
|
+
PrismNamespace = Data.define(:segments, :node)
|
|
20
|
+
|
|
21
|
+
# Diagnostics the generators tolerate (lint still reports them all): the
|
|
22
|
+
# parser drops the construct and the signature is merely incomplete.
|
|
23
|
+
IGNORABLE_INLINE_DIAGNOSTICS = [
|
|
24
|
+
RBS::InlineParser::Diagnostic::NotImplementedYet,
|
|
25
|
+
RBS::InlineParser::Diagnostic::UnusedInlineAnnotation,
|
|
26
|
+
RBS::InlineParser::Diagnostic::NonConstantModuleName,
|
|
27
|
+
RBS::InlineParser::Diagnostic::NonConstantClassName,
|
|
28
|
+
RBS::InlineParser::Diagnostic::NonConstantSuperClassName
|
|
29
|
+
].freeze
|
|
30
|
+
|
|
31
|
+
SKIPPABLE = [Prism::ClassNode, Prism::ModuleNode, Prism::DefNode, Prism::ConstantWriteNode, Prism::ConstantPathWriteNode].freeze
|
|
32
|
+
CLOSED = [Prism::DefNode, Prism::ConstantWriteNode, Prism::ConstantPathWriteNode].freeze
|
|
33
|
+
DECLARING_CALLS = %i[include extend prepend attr_reader attr_writer attr_accessor].freeze
|
|
34
|
+
|
|
35
|
+
attr_reader :path
|
|
36
|
+
|
|
37
|
+
# Depth-first walk over a Prism subtree, the node itself included.
|
|
38
|
+
def self.each_node(node, prune: [], &block)
|
|
39
|
+
return if prune.any? { |klass| node.is_a?(klass) }
|
|
40
|
+
|
|
41
|
+
yield node
|
|
42
|
+
node.compact_child_nodes.each { |child| each_node(child, prune:, &block) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.qualified_name(segments)
|
|
46
|
+
segments.map(&:name).join('::')
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def initialize(root:, path:)
|
|
50
|
+
@root = Pathname(root).expand_path
|
|
51
|
+
@path = Pathname(path).cleanpath.to_s
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def source
|
|
55
|
+
@source ||= File.read(absolute_path, encoding: 'UTF-8')
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def prism
|
|
59
|
+
@prism ||= Prism.parse(source).tap do |result|
|
|
60
|
+
raise_parse_errors!(result) unless result.success?
|
|
61
|
+
raise_invalid_comments!(result)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def constant_table
|
|
66
|
+
@constant_table ||= ConstantTable.new(prism)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Raw native parse, diagnostics included — the lint reports these.
|
|
70
|
+
def inline_result
|
|
71
|
+
@inline_result ||= begin
|
|
72
|
+
buffer = RBS::Buffer.new(name: @path, content: source)
|
|
73
|
+
ConstantLiteralInference::TABLES[buffer] = constant_table
|
|
74
|
+
RBS::InlineParser.parse(buffer, prism)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Validated view for generators: raises on any non-ignorable diagnostic.
|
|
79
|
+
def inline
|
|
80
|
+
@inline ||= inline_result.tap do |result|
|
|
81
|
+
result.diagnostics.each do |diagnostic|
|
|
82
|
+
next if ignorable_inline_diagnostic?(diagnostic)
|
|
83
|
+
|
|
84
|
+
raise SourceError.new(@path, "inline RBS: #{diagnostic.message}")
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def rbs_source
|
|
90
|
+
result = inline
|
|
91
|
+
RBS::Source::Ruby.new(result.buffer, result.prism_result, result.declarations, result.diagnostics)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Flattened namespaces from the native inline parser, outer-first.
|
|
95
|
+
def inline_namespaces
|
|
96
|
+
@inline_namespaces ||= [].tap do |namespaces|
|
|
97
|
+
inline.declarations.each { |declaration| walk_inline(declaration, [], namespaces) }
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def constant_decls
|
|
102
|
+
@constant_decls ||= [*inline.declarations, *inline_namespaces.flat_map { |namespace| namespace.decl.members }]
|
|
103
|
+
.grep(RBS::AST::Ruby::Declarations::ConstantDecl)
|
|
104
|
+
.sort_by { |declaration| declaration.node.location.start_offset }
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Flattened namespaces from the Prism AST, outer-first.
|
|
108
|
+
# `# @rbs skip` drops a subtree, matching the native parser.
|
|
109
|
+
def prism_namespaces
|
|
110
|
+
walk_prism_once
|
|
111
|
+
@prism_namespaces
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Skipped namespaces included (a skip hides members, not the kind), plus value classes.
|
|
115
|
+
def declared_kinds
|
|
116
|
+
walk_prism_once
|
|
117
|
+
@declared_kinds
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# `# @rbs skip` in the leading comment block of a Prism node.
|
|
121
|
+
def skipped?(node)
|
|
122
|
+
return false unless source.include?('skip')
|
|
123
|
+
|
|
124
|
+
block = comment_association.leading_block(node)&.block
|
|
125
|
+
block ? block.each_paragraph([]).any?(RBS::AST::Ruby::Annotations::SkipAnnotation) : false
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Depth-first over the Prism nodes the native inline parser visits.
|
|
129
|
+
def each_parsed_node(node = prism.value, &block)
|
|
130
|
+
return if SKIPPABLE.any? { |klass| node.is_a?(klass) } && skipped?(node)
|
|
131
|
+
|
|
132
|
+
yield node
|
|
133
|
+
return if CLOSED.any? { |klass| node.is_a?(klass) }
|
|
134
|
+
return if node.is_a?(Prism::CallNode) && (node.receiver || DECLARING_CALLS.include?(node.name))
|
|
135
|
+
|
|
136
|
+
node.compact_child_nodes.each { |child| each_parsed_node(child, &block) }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def body_statements(node)
|
|
140
|
+
ValueClasses.statements(node)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
private
|
|
144
|
+
|
|
145
|
+
def comment_association
|
|
146
|
+
@comment_association ||= RBS::InlineParser::CommentAssociation.build(RBS::Buffer.new(name: @path, content: source), prism)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def walk_inline(declaration, parent_segments, namespaces)
|
|
150
|
+
case declaration
|
|
151
|
+
when RBS::AST::Ruby::Declarations::ClassDecl
|
|
152
|
+
segments = nested_segments(parent_segments, declaration.class_name, :class)
|
|
153
|
+
when RBS::AST::Ruby::Declarations::ModuleDecl
|
|
154
|
+
segments = nested_segments(parent_segments, declaration.module_name, :module)
|
|
155
|
+
else
|
|
156
|
+
return
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
namespaces << InlineNamespace.new(segments:, decl: declaration)
|
|
160
|
+
declaration.members.each { |member| walk_inline(member, segments, namespaces) }
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def nested_segments(parent_segments, type_name, kind)
|
|
164
|
+
*parents, leaf = [*type_name.namespace.path, type_name.name].map(&:to_s)
|
|
165
|
+
segments = parents.map { |name| Segment.new(name:, kind: nil) } + [Segment.new(name: leaf, kind:)]
|
|
166
|
+
type_name.absolute? ? segments : parent_segments + segments
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def walk_prism_once
|
|
170
|
+
return if @prism_namespaces
|
|
171
|
+
|
|
172
|
+
@walked_namespaces = []
|
|
173
|
+
@walked_kinds = []
|
|
174
|
+
walk_prism(prism.value, [], skipped: false)
|
|
175
|
+
@declared_kinds = @walked_kinds
|
|
176
|
+
@prism_namespaces = @walked_namespaces
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def walk_prism(node, parent_segments, skipped:)
|
|
180
|
+
case node
|
|
181
|
+
when Prism::ClassNode, Prism::ModuleNode then walk_namespace(node, parent_segments, skipped:)
|
|
182
|
+
when Prism::ConstantWriteNode then record_value_class(node, parent_segments) unless skipped
|
|
183
|
+
when Prism::ProgramNode
|
|
184
|
+
body_statements(node).each { |child| walk_prism(child, parent_segments, skipped:) }
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def walk_namespace(node, parent_segments, skipped:)
|
|
189
|
+
type_name = LiteralInference.constant_type_name(node.constant_path) or return
|
|
190
|
+
|
|
191
|
+
segments = nested_segments(parent_segments, type_name, node.is_a?(Prism::ClassNode) ? :class : :module)
|
|
192
|
+
skipped ||= skipped?(node)
|
|
193
|
+
@walked_kinds << [SourceAnalysis.qualified_name(segments), segments.last.kind]
|
|
194
|
+
@walked_namespaces << PrismNamespace.new(segments:, node:) unless skipped
|
|
195
|
+
body_statements(node).each { |child| walk_prism(child, segments, skipped:) }
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def record_value_class(node, parent_segments)
|
|
199
|
+
return unless parent_segments.any? && ValueClasses.assignment_kind(node)
|
|
200
|
+
|
|
201
|
+
@walked_kinds << ["#{SourceAnalysis.qualified_name(parent_segments)}::#{node.name}", :class]
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def ignorable_inline_diagnostic?(diagnostic)
|
|
205
|
+
IGNORABLE_INLINE_DIAGNOSTICS.any? { |diagnostic_class| diagnostic.is_a?(diagnostic_class) }
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def absolute_path
|
|
209
|
+
candidate = @root.join(@path).cleanpath
|
|
210
|
+
raise SourceError.new(@path, 'source path escapes the project root') unless Paths.within?(candidate, @root)
|
|
211
|
+
|
|
212
|
+
candidate
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# RBS fails on a non-UTF-8 comment; non-UTF-8 literals are fine.
|
|
216
|
+
def raise_invalid_comments!(result)
|
|
217
|
+
comment = result.comments.find { |candidate| !candidate.slice.valid_encoding? } or return
|
|
218
|
+
|
|
219
|
+
raise SourceError.new(@path, "comment on line #{comment.location.start_line} is not valid UTF-8")
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def raise_parse_errors!(result)
|
|
223
|
+
messages = result.errors.map { |error| "line #{error.location.start_line}: #{error.message}" }
|
|
224
|
+
raise SourceError.new(@path, "parse error: #{messages.join('; ')}")
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|