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,139 @@
|
|
|
1
|
+
require 'pathname'
|
|
2
|
+
require 'yaml'
|
|
3
|
+
require_relative 'conventions'
|
|
4
|
+
require_relative 'error'
|
|
5
|
+
require_relative 'paths'
|
|
6
|
+
require_relative 'settings'
|
|
7
|
+
|
|
8
|
+
module SteepKit
|
|
9
|
+
class Config
|
|
10
|
+
CONFIG_FILES = %w[.steep-kit.yml config/steep_kit.yml config/steep-kit.yml].freeze
|
|
11
|
+
DEFAULT_PROJECTED_ROOT = 'sig/projected'.freeze
|
|
12
|
+
REQUIRED_KEYS = %i[signature_paths].freeze
|
|
13
|
+
OPTIONAL_KEYS = %i[checked_paths projected_root projections checks steep mixins superclasses rbs_rails].freeze
|
|
14
|
+
OPTION_KEYS = %i[config_path projections checks steep mixins superclasses rbs_rails].freeze
|
|
15
|
+
|
|
16
|
+
attr_reader :root, :config_path, :signature_paths, :checked_paths, :donor_paths, :projected_root,
|
|
17
|
+
:projections, :checks, :steep, :rbs_rails, :conventions
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
attr_reader :current
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.install(config)
|
|
24
|
+
@current = config
|
|
25
|
+
Conventions.install(config ? config.conventions : Conventions.new)
|
|
26
|
+
config
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.steep_settings
|
|
30
|
+
@current ? @current.steep : Settings::Steep.default
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.rbs_rails_settings
|
|
34
|
+
@current ? @current.rbs_rails : Settings::RbsRails.default
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.locate(root:)
|
|
38
|
+
root = Pathname(root).expand_path
|
|
39
|
+
|
|
40
|
+
CONFIG_FILES.map { |relative| root.join(relative) }.find(&:file?)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.load(root:, path: nil)
|
|
44
|
+
root = Pathname(root).expand_path
|
|
45
|
+
file = resolve_config_file(root, path)
|
|
46
|
+
attributes = YAML.safe_load(file.read(encoding: 'UTF-8'), symbolize_names: true)
|
|
47
|
+
validate_document!(attributes, file)
|
|
48
|
+
|
|
49
|
+
config = new(root:, config_path: Paths.relative_within(file, root), **attributes)
|
|
50
|
+
install(config)
|
|
51
|
+
rescue Psych::Exception => e
|
|
52
|
+
raise Error, "invalid steep-kit config #{file}: #{e.message}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.resolve_config_file(root, path)
|
|
56
|
+
if path
|
|
57
|
+
candidate = Pathname(path).expand_path(root)
|
|
58
|
+
raise Error, "steep-kit config does not exist: #{candidate}" unless candidate.file?
|
|
59
|
+
|
|
60
|
+
return candidate
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
locate(root:) or raise Error, "steep-kit config does not exist under #{root} (looked for #{CONFIG_FILES.join(', ')})"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.validate_document!(attributes, path)
|
|
67
|
+
raise Error, "steep-kit config must contain a mapping: #{path}" unless attributes.is_a?(Hash)
|
|
68
|
+
|
|
69
|
+
missing = REQUIRED_KEYS - attributes.keys
|
|
70
|
+
unknown = attributes.keys - REQUIRED_KEYS - OPTIONAL_KEYS
|
|
71
|
+
raise Error, "steep-kit config is missing: #{missing.join(', ')}" if missing.any?
|
|
72
|
+
raise Error, "steep-kit config has unknown keys: #{unknown.join(', ')}" if unknown.any?
|
|
73
|
+
end
|
|
74
|
+
private_class_method :resolve_config_file, :validate_document!
|
|
75
|
+
|
|
76
|
+
def initialize(root:, signature_paths:, checked_paths: nil, projected_root: DEFAULT_PROJECTED_ROOT, **options)
|
|
77
|
+
@root = Pathname(root).expand_path
|
|
78
|
+
raise Error, "repository root does not exist: #{@root}" unless @root.directory?
|
|
79
|
+
|
|
80
|
+
assign_paths(signature_paths, checked_paths, projected_root)
|
|
81
|
+
assign_settings(options)
|
|
82
|
+
freeze
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def assign_paths(signature_paths, checked_paths, projected_root)
|
|
88
|
+
@signature_paths = normalize_paths(:signature_paths, signature_paths)
|
|
89
|
+
@checked_paths = checked_paths.nil? ? @signature_paths : normalize_paths(:checked_paths, checked_paths)
|
|
90
|
+
validate_checked_coverage!
|
|
91
|
+
@donor_paths = @signature_paths.reject { |path| covered_by?(@checked_paths, path) }.freeze
|
|
92
|
+
@projected_root = normalize_relative_path(:projected_root, projected_root)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def assign_settings(options)
|
|
96
|
+
validate_options!(options)
|
|
97
|
+
@config_path = options[:config_path]&.dup&.freeze
|
|
98
|
+
@projections = Settings::Projections.parse(options[:projections])
|
|
99
|
+
@checks = Settings::Checks.parse(options[:checks])
|
|
100
|
+
@steep = Settings::Steep.parse(options[:steep])
|
|
101
|
+
@rbs_rails = Settings::RbsRails.parse(options[:rbs_rails])
|
|
102
|
+
@conventions = Conventions.parse(mixins: options[:mixins], superclasses: options[:superclasses])
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def validate_options!(options)
|
|
106
|
+
unknown = options.keys - OPTION_KEYS
|
|
107
|
+
raise Error, "steep-kit config has unknown keys: #{unknown.join(', ')}" if unknown.any?
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def validate_checked_coverage!
|
|
111
|
+
uncovered = @checked_paths.reject { |path| covered_by?(@signature_paths, path) }
|
|
112
|
+
raise Error, "checked_paths entries are not covered by signature_paths: #{uncovered.join(', ')}" if uncovered.any?
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def normalize_paths(key, values)
|
|
116
|
+
raise Error, "#{key} must be an array" unless values.is_a?(Array)
|
|
117
|
+
|
|
118
|
+
values.map { |value| normalize_relative_path(key, value) }.uniq.freeze
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def normalize_relative_path(key, value)
|
|
122
|
+
raise Error, "#{key} entries must be strings" unless value.is_a?(String)
|
|
123
|
+
raise Error, "#{key} entries cannot be empty" if value.empty?
|
|
124
|
+
|
|
125
|
+
path = Pathname(value)
|
|
126
|
+
raise Error, "#{key} entries must be relative to the repository root: #{value}" if path.absolute?
|
|
127
|
+
|
|
128
|
+
clean = path.cleanpath.to_s
|
|
129
|
+
escapes_root = clean == '.' || clean == '..' || clean.start_with?("..#{File::SEPARATOR}")
|
|
130
|
+
raise Error, "#{key} entries must stay inside the repository root: #{value}" if escapes_root
|
|
131
|
+
|
|
132
|
+
clean.freeze
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def covered_by?(covering_paths, path)
|
|
136
|
+
covering_paths.any? { |covering| Paths.within?(path, covering) }
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require 'rbs'
|
|
3
|
+
require_relative 'config'
|
|
4
|
+
require_relative 'constant_table'
|
|
5
|
+
|
|
6
|
+
module SteepKit
|
|
7
|
+
# Prepend-patch upgrading ConstantDecl#type with literal inference.
|
|
8
|
+
# An explicit `#:` annotation always wins.
|
|
9
|
+
module ConstantLiteralInference
|
|
10
|
+
# Weak keys so long-lived langserver processes drop re-parsed buffers.
|
|
11
|
+
TABLES = ObjectSpace::WeakKeyMap.new
|
|
12
|
+
|
|
13
|
+
def self.table_for(buffer)
|
|
14
|
+
TABLES[buffer] ||= ConstantTable.new(Prism.parse(buffer.content))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.install!
|
|
18
|
+
target = RBS::AST::Ruby::Declarations::ConstantDecl
|
|
19
|
+
target.prepend(self) unless target.ancestors.include?(self)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def type
|
|
23
|
+
return super if type_annotation || !Config.steep_settings.literal_constants
|
|
24
|
+
|
|
25
|
+
@literal_inference_type ||= begin # rubocop:disable Naming/MemoizedInstanceVariableName -- namespaced ivar on a patched third-party class
|
|
26
|
+
inferred = ConstantLiteralInference.table_for(buffer).type_at(node)
|
|
27
|
+
(inferred && RBS::Parser.parse_type(inferred, require_eof: true)) || super
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
SteepKit::ConstantLiteralInference.install!
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require 'rbs'
|
|
3
|
+
require_relative 'literal_inference'
|
|
4
|
+
|
|
5
|
+
module SteepKit
|
|
6
|
+
# Types every constant assignment in one file: literal inference for
|
|
7
|
+
# pure-literal values, trailing `#:` annotations as seeds, and references
|
|
8
|
+
# to sibling constants resolved lexically (innermost namespace outward),
|
|
9
|
+
# iterated to a fixed point so chains work and cycles stay untyped.
|
|
10
|
+
class ConstantTable
|
|
11
|
+
# scope: enclosing names, compact paths split (`class A::B` -> A, B);
|
|
12
|
+
# nesting: headers as written (`A::B`), which rubydex resolves from.
|
|
13
|
+
Entry = Data.define(:qualified_name, :value_node, :scope, :nesting)
|
|
14
|
+
|
|
15
|
+
ANNOTATION = /\A#:\s+(?<type>.+)/
|
|
16
|
+
|
|
17
|
+
def initialize(prism_result)
|
|
18
|
+
@entries = {}
|
|
19
|
+
@entries_by_name = {}
|
|
20
|
+
@types = {}
|
|
21
|
+
@types_by_name = {}
|
|
22
|
+
collect(prism_result.value, [], [])
|
|
23
|
+
seed_annotations(prism_result)
|
|
24
|
+
resolve!
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def type_at(node)
|
|
28
|
+
@types[node.location.start_offset]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def entry_at(node)
|
|
32
|
+
@entries[node.location.start_offset]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The value assigned to `names` as seen from `scope`, innermost namespace
|
|
36
|
+
# outward — the walk Ruby takes for a constant reference — or nil.
|
|
37
|
+
def value_node_for(names, scope)
|
|
38
|
+
lookup(names, scope) { |qualified| @entries_by_name[qualified]&.value_node }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def collect(node, scope, nesting)
|
|
44
|
+
case node
|
|
45
|
+
when Prism::ModuleNode, Prism::ClassNode
|
|
46
|
+
path = qualify(node.constant_path, scope)
|
|
47
|
+
if path
|
|
48
|
+
collect_children(node.body, path, nesting + [node.constant_path.slice])
|
|
49
|
+
else
|
|
50
|
+
collect_children(node.body, scope, nesting)
|
|
51
|
+
end
|
|
52
|
+
when Prism::ProgramNode
|
|
53
|
+
collect_children(node.statements, scope, nesting)
|
|
54
|
+
when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode
|
|
55
|
+
path = qualify(node, scope)
|
|
56
|
+
record(node, path, scope, nesting) if path
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def collect_children(body, scope, nesting)
|
|
61
|
+
statements = body.is_a?(Prism::StatementsNode) ? body.body : []
|
|
62
|
+
statements.each { |child| collect(child, scope, nesting) }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def qualify(node, scope)
|
|
66
|
+
type_name = LiteralInference.constant_type_name(node) or return
|
|
67
|
+
names = [*type_name.namespace.path, type_name.name].map(&:to_s)
|
|
68
|
+
type_name.absolute? ? names : scope + names
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Ruby evaluates the value in the enclosing scope, even for an absolute name.
|
|
72
|
+
def record(node, path, scope, nesting)
|
|
73
|
+
entry = Entry.new(qualified_name: "::#{path.join('::')}", value_node: node.value, scope:, nesting:)
|
|
74
|
+
@entries[node.location.start_offset] = entry
|
|
75
|
+
@entries_by_name[entry.qualified_name] = entry
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Trailing `#: Type` annotations type their assignment even when the
|
|
79
|
+
# value is computed, so references to annotated constants resolve.
|
|
80
|
+
# Skips invalid bytes: Steep must not fail where upstream does not.
|
|
81
|
+
def seed_annotations(prism_result)
|
|
82
|
+
annotations = prism_result.comments.filter_map do |comment|
|
|
83
|
+
text = comment.slice
|
|
84
|
+
match = text.valid_encoding? && text.strip.match(ANNOTATION)
|
|
85
|
+
[comment.location.start_line, match[:type]] if match
|
|
86
|
+
end.to_h
|
|
87
|
+
|
|
88
|
+
@entries.each do |offset, entry|
|
|
89
|
+
type = seed_type(annotations[entry.value_node.location.end_line], entry)
|
|
90
|
+
store(offset, entry, type) if type
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Inside a namespace only absolute names: the type reaches referencing
|
|
95
|
+
# constants unresolved, where a relative name would read from the root.
|
|
96
|
+
def seed_type(text, entry)
|
|
97
|
+
type = text && RBS::Parser.parse_type(text, require_eof: true) or return
|
|
98
|
+
type.to_s unless entry.scope.any? && relative_names?(type)
|
|
99
|
+
rescue RBS::ParsingError, ArgumentError, EncodingError
|
|
100
|
+
nil
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def relative_names?(type)
|
|
104
|
+
names = []
|
|
105
|
+
type.map_type_name do |name, *|
|
|
106
|
+
names << name
|
|
107
|
+
name
|
|
108
|
+
end
|
|
109
|
+
names.any? { |name| !name.absolute? }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def resolve!
|
|
113
|
+
loop do
|
|
114
|
+
progressed = false
|
|
115
|
+
@entries.each do |offset, entry|
|
|
116
|
+
next if @types.key?(offset)
|
|
117
|
+
|
|
118
|
+
inference = LiteralInference.new(resolver: resolver_for(entry.scope))
|
|
119
|
+
type = inference.type_for(entry.value_node)
|
|
120
|
+
next unless type
|
|
121
|
+
|
|
122
|
+
store(offset, entry, type)
|
|
123
|
+
progressed = true
|
|
124
|
+
end
|
|
125
|
+
break unless progressed
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def store(offset, entry, type)
|
|
130
|
+
@types[offset] = type
|
|
131
|
+
@types_by_name[entry.qualified_name] = type
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def resolver_for(scope)
|
|
135
|
+
->(names, absolute:) do
|
|
136
|
+
next @types_by_name["::#{names.join('::')}"] if absolute
|
|
137
|
+
|
|
138
|
+
lookup(names, scope) { |qualified| @types_by_name[qualified] }
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Yields the qualified candidates for `names` from `scope` outward; the
|
|
143
|
+
# first truthy block result wins.
|
|
144
|
+
def lookup(names, scope)
|
|
145
|
+
scope.size.downto(0) do |depth|
|
|
146
|
+
hit = yield "::#{(scope.first(depth) + names).join('::')}"
|
|
147
|
+
return hit if hit
|
|
148
|
+
end
|
|
149
|
+
nil
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
require_relative 'error'
|
|
2
|
+
|
|
3
|
+
module SteepKit
|
|
4
|
+
class Conventions
|
|
5
|
+
CONSTANT_PATH = /\A(?:::)?[A-Z]\w*(?:::[A-Z]\w*)*\z/
|
|
6
|
+
CONSTANT_NAME = /\A[A-Z]\w*\z/
|
|
7
|
+
METHOD_NAME = /\A[a-z_]\w*[!?=]?\z/
|
|
8
|
+
MIXIN_KEYS = %i[class_methods aliases nested_classes].freeze
|
|
9
|
+
ALIAS_ALLOWED_KEYS = %i[on alias to require_typed].freeze
|
|
10
|
+
ALIAS_REQUIRED_KEYS = %i[on alias to].freeze
|
|
11
|
+
NESTED_CLASS_ALLOWED_KEYS = %i[on name superclass include extend].freeze
|
|
12
|
+
NESTED_CLASS_REQUIRED_KEYS = %i[on name].freeze
|
|
13
|
+
|
|
14
|
+
ClassMethodRule = Data.define(:mixin, :extension) do
|
|
15
|
+
def self.parse(entry)
|
|
16
|
+
case entry
|
|
17
|
+
when String
|
|
18
|
+
mixin = Conventions.absolute_name('mixins.class_methods', entry)
|
|
19
|
+
new(mixin:, extension: "#{mixin}::ClassMethods".freeze)
|
|
20
|
+
when Hash
|
|
21
|
+
parse_mapping(entry)
|
|
22
|
+
else
|
|
23
|
+
raise Error, 'mixins.class_methods entries must be strings or mappings'
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.parse_mapping(entry)
|
|
28
|
+
Conventions.validate_entry_keys!('mixins.class_methods', entry, required: %i[include extend], allowed: %i[include extend])
|
|
29
|
+
|
|
30
|
+
new(
|
|
31
|
+
mixin: Conventions.absolute_name('mixins.class_methods include', entry[:include]),
|
|
32
|
+
extension: Conventions.absolute_name('mixins.class_methods extend', entry[:extend])
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
private_class_method :parse_mapping
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
AliasRule = Data.define(:on, :alias_name, :target_name, :require_typed) do
|
|
39
|
+
def self.parse(entry)
|
|
40
|
+
Conventions.validate_entry_keys!('mixins.aliases', entry, required: ALIAS_REQUIRED_KEYS, allowed: ALIAS_ALLOWED_KEYS)
|
|
41
|
+
require_typed = entry.fetch(:require_typed, false)
|
|
42
|
+
raise Error, 'mixins.aliases require_typed must be a boolean' unless [true, false].include?(require_typed)
|
|
43
|
+
|
|
44
|
+
new(
|
|
45
|
+
on: Conventions.absolute_name('mixins.aliases on', entry[:on]),
|
|
46
|
+
alias_name: Conventions.method_name('mixins.aliases alias', entry[:alias]),
|
|
47
|
+
target_name: Conventions.method_name('mixins.aliases to', entry[:to]),
|
|
48
|
+
require_typed:
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
NestedClassRule = Data.define(:on, :name, :superclass, :includes, :extends) do
|
|
54
|
+
def self.parse(entry)
|
|
55
|
+
Conventions.validate_entry_keys!('mixins.nested_classes', entry, required: NESTED_CLASS_REQUIRED_KEYS, allowed: NESTED_CLASS_ALLOWED_KEYS)
|
|
56
|
+
|
|
57
|
+
new(
|
|
58
|
+
on: Conventions.absolute_name('mixins.nested_classes on', entry[:on]),
|
|
59
|
+
name: Conventions.constant_name('mixins.nested_classes name', entry[:name]),
|
|
60
|
+
superclass: (Conventions.absolute_name('mixins.nested_classes superclass', entry[:superclass]) if entry[:superclass]),
|
|
61
|
+
includes: parse_modules('include', entry[:include]),
|
|
62
|
+
extends: parse_modules('extend', entry[:extend])
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.parse_modules(key, value)
|
|
67
|
+
return [].freeze if value.nil?
|
|
68
|
+
raise Error, "mixins.nested_classes #{key} must be an array" unless value.is_a?(Array)
|
|
69
|
+
|
|
70
|
+
value.map { |name| Conventions.absolute_name("mixins.nested_classes #{key}", name) }.uniq.freeze
|
|
71
|
+
end
|
|
72
|
+
private_class_method :parse_modules
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
attr_reader :class_methods, :aliases, :nested_classes, :superclasses
|
|
76
|
+
|
|
77
|
+
def self.current
|
|
78
|
+
@current ||= new
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.install(conventions)
|
|
82
|
+
@current = conventions
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.parse(mixins: nil, superclasses: nil)
|
|
86
|
+
parsed_mixins = parse_mixins(mixins)
|
|
87
|
+
|
|
88
|
+
new(
|
|
89
|
+
class_methods: parsed_mixins[:class_methods],
|
|
90
|
+
aliases: parsed_mixins[:aliases],
|
|
91
|
+
nested_classes: parsed_mixins[:nested_classes],
|
|
92
|
+
superclasses: parse_superclasses(superclasses)
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def self.validate_entry_keys!(label, entry, required:, allowed:)
|
|
97
|
+
raise Error, "#{label} entries must be mappings" unless entry.is_a?(Hash)
|
|
98
|
+
|
|
99
|
+
unknown = entry.keys - allowed
|
|
100
|
+
missing = required - entry.keys
|
|
101
|
+
raise Error, "#{label} entries have unknown keys: #{unknown.join(', ')}" if unknown.any?
|
|
102
|
+
raise Error, "#{label} entries are missing: #{missing.join(', ')}" if missing.any?
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.absolute_name(key, value)
|
|
106
|
+
raise Error, "#{key} must be a constant path: #{value.inspect}" unless value.is_a?(String) && value.match?(CONSTANT_PATH)
|
|
107
|
+
|
|
108
|
+
"::#{value.delete_prefix('::')}".freeze
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def self.constant_name(key, value)
|
|
112
|
+
raise Error, "#{key} must be a constant name: #{value.inspect}" unless value.is_a?(String) && value.match?(CONSTANT_NAME)
|
|
113
|
+
|
|
114
|
+
value.to_sym
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def self.method_name(key, value)
|
|
118
|
+
text = value.is_a?(Symbol) ? value.to_s : value
|
|
119
|
+
raise Error, "#{key} must be a method name: #{value.inspect}" unless text.is_a?(String) && text.match?(METHOD_NAME)
|
|
120
|
+
|
|
121
|
+
text.to_sym
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def self.parse_mixins(mixins)
|
|
125
|
+
return { class_methods: [], aliases: [], nested_classes: [] } if mixins.nil?
|
|
126
|
+
raise Error, 'mixins must be a mapping' unless mixins.is_a?(Hash)
|
|
127
|
+
|
|
128
|
+
unknown = mixins.keys - MIXIN_KEYS
|
|
129
|
+
raise Error, "mixins has unknown keys: #{unknown.join(', ')}" if unknown.any?
|
|
130
|
+
|
|
131
|
+
{
|
|
132
|
+
class_methods: parse_list('mixins.class_methods', mixins[:class_methods], ClassMethodRule),
|
|
133
|
+
aliases: parse_list('mixins.aliases', mixins[:aliases], AliasRule),
|
|
134
|
+
nested_classes: parse_list('mixins.nested_classes', mixins[:nested_classes], NestedClassRule)
|
|
135
|
+
}
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def self.parse_list(label, entries, rule_class)
|
|
139
|
+
return [] if entries.nil?
|
|
140
|
+
raise Error, "#{label} must be an array" unless entries.is_a?(Array)
|
|
141
|
+
|
|
142
|
+
entries.map { |entry| rule_class.parse(entry) }
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def self.parse_superclasses(superclasses)
|
|
146
|
+
return {} if superclasses.nil?
|
|
147
|
+
raise Error, 'superclasses must be a mapping' unless superclasses.is_a?(Hash)
|
|
148
|
+
|
|
149
|
+
superclasses.to_h do |expression, superclass|
|
|
150
|
+
source = expression.to_s.delete_prefix('::')
|
|
151
|
+
raise Error, 'superclasses keys cannot be empty' if source.empty?
|
|
152
|
+
|
|
153
|
+
[source.freeze, absolute_name("superclasses #{source}", superclass)]
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
private_class_method :parse_mixins, :parse_list, :parse_superclasses
|
|
158
|
+
|
|
159
|
+
def initialize(class_methods: [], aliases: [], nested_classes: [], superclasses: {})
|
|
160
|
+
@class_methods = class_methods.freeze
|
|
161
|
+
@aliases = aliases.freeze
|
|
162
|
+
@nested_classes = nested_classes.freeze
|
|
163
|
+
@superclasses = superclasses.freeze
|
|
164
|
+
freeze
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def superclass_for(expression)
|
|
168
|
+
superclasses[expression.delete_prefix('::')]
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def empty_mixins?
|
|
172
|
+
class_methods.empty? && aliases.empty? && nested_classes.empty?
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require 'rbs'
|
|
3
|
+
require_relative 'conventions'
|
|
4
|
+
|
|
5
|
+
module SteepKit
|
|
6
|
+
module DynamicSuperclasses
|
|
7
|
+
def self.install!
|
|
8
|
+
target = RBS::InlineParser::Parser
|
|
9
|
+
target.prepend(InlineParserPatch) unless target.ancestors.include?(InlineParserPatch)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
module InlineParserPatch
|
|
13
|
+
def parse_super_class(super_class_expr, inheritance_operator_loc)
|
|
14
|
+
return super unless super_class_expr.is_a?(Prism::CallNode)
|
|
15
|
+
|
|
16
|
+
superclass = Conventions.current.superclass_for(super_class_expr.slice) or return super
|
|
17
|
+
|
|
18
|
+
RBS::AST::Ruby::Declarations::ClassDecl::SuperClass.new(
|
|
19
|
+
rbs_location(super_class_expr.location), rbs_location(inheritance_operator_loc), RBS::TypeName.parse(superclass), nil
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
SteepKit::DynamicSuperclasses.install!
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
require 'optparse'
|
|
2
|
+
require 'pathname'
|
|
3
|
+
require_relative 'config'
|
|
4
|
+
require_relative 'error'
|
|
5
|
+
|
|
6
|
+
module SteepKit
|
|
7
|
+
# `steep-kit fast [--group=NAME] [-j N] [path_or_glob ...]`:
|
|
8
|
+
# Runs `steep check --no-daemon --validate=skip` with strict file/glob
|
|
9
|
+
# validation so directory paths or empty globs fail loudly instead of
|
|
10
|
+
# succeeding with zero checked files.
|
|
11
|
+
class Fast
|
|
12
|
+
class InvalidPaths < StandardError; end
|
|
13
|
+
|
|
14
|
+
module CheckedPaths
|
|
15
|
+
def run
|
|
16
|
+
return super unless Fast.active?
|
|
17
|
+
|
|
18
|
+
super
|
|
19
|
+
rescue InvalidPaths => e
|
|
20
|
+
stderr.puts("steep-kit fast: #{e.message}")
|
|
21
|
+
1
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def build_typecheck_params(project)
|
|
25
|
+
return super unless Fast.active?
|
|
26
|
+
|
|
27
|
+
command_line_patterns.replace(command_line_patterns.flat_map { |pattern| expand_path(project, pattern) })
|
|
28
|
+
params = super
|
|
29
|
+
raise InvalidPaths, 'no Ruby bodies selected for checking; use `steep check` for signatures' if params.values_at(:code_paths, :inline_paths).all?(&:empty?)
|
|
30
|
+
|
|
31
|
+
params
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def expand_path(project, pattern)
|
|
37
|
+
path = project.absolute_path(Pathname(pattern))
|
|
38
|
+
raise InvalidPaths, "#{pattern} is a directory; use a file or a quoted glob such as '#{pattern}/**/*.rb'" if path.directory?
|
|
39
|
+
|
|
40
|
+
files = Dir.glob(path.to_s).select { |match| File.file?(match) }
|
|
41
|
+
raise InvalidPaths, "#{pattern} selects no files" if files.empty?
|
|
42
|
+
|
|
43
|
+
files
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.active?
|
|
48
|
+
Thread.current[:steep_kit_fast_active] == true
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.with_active
|
|
52
|
+
previous = Thread.current[:steep_kit_fast_active]
|
|
53
|
+
Thread.current[:steep_kit_fast_active] = true
|
|
54
|
+
yield
|
|
55
|
+
ensure
|
|
56
|
+
Thread.current[:steep_kit_fast_active] = previous
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def initialize(root:, config: nil, stdout: $stdout, stderr: $stderr, cli_runner: nil)
|
|
60
|
+
@root = Pathname(root).expand_path
|
|
61
|
+
@config = config
|
|
62
|
+
@stdout = stdout
|
|
63
|
+
@stderr = stderr
|
|
64
|
+
@cli_runner = cli_runner
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def run(argv)
|
|
68
|
+
options, patterns = parse_options(argv)
|
|
69
|
+
arguments = build_arguments(options, patterns)
|
|
70
|
+
return @cli_runner.call(arguments) if @cli_runner
|
|
71
|
+
|
|
72
|
+
load_steep!
|
|
73
|
+
Fast.with_active do
|
|
74
|
+
Dir.chdir(@root.to_s) do
|
|
75
|
+
::Steep::CLI.new(argv: arguments, stdout: @stdout, stderr: @stderr, stdin: $stdin).run
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def parse_options(argv)
|
|
83
|
+
options = {
|
|
84
|
+
group: @config&.steep&.fast_group,
|
|
85
|
+
jobs: ENV.fetch('STEEP_JOBS', '2')
|
|
86
|
+
}
|
|
87
|
+
remaining = argv.dup
|
|
88
|
+
parser = OptionParser.new do |opts|
|
|
89
|
+
opts.on('-g', '--group=NAME') { |value| options[:group] = value }
|
|
90
|
+
opts.on('-j', '--jobs=COUNT') { |value| options[:jobs] = value }
|
|
91
|
+
end
|
|
92
|
+
parser.parse!(remaining)
|
|
93
|
+
[options, remaining]
|
|
94
|
+
rescue OptionParser::ParseError => e
|
|
95
|
+
raise Error, e.message
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def build_arguments(options, patterns)
|
|
99
|
+
arguments = ['check', '--no-daemon', '--validate=skip', '-j', options[:jobs].to_s]
|
|
100
|
+
arguments << "--group=#{options[:group]}" if options[:group] && !options[:group].empty?
|
|
101
|
+
arguments.concat(patterns)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def load_steep!
|
|
105
|
+
require 'steep'
|
|
106
|
+
require 'steep/cli'
|
|
107
|
+
require_relative 'steep'
|
|
108
|
+
::Steep::Drivers::Check.prepend(CheckedPaths) unless ::Steep::Drivers::Check.ancestors.include?(CheckedPaths)
|
|
109
|
+
rescue LoadError
|
|
110
|
+
raise Error, 'the fast command needs the `steep` gem in the host bundle'
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|