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,92 @@
|
|
|
1
|
+
require_relative 'config'
|
|
2
|
+
require_relative 'patch_guard'
|
|
3
|
+
|
|
4
|
+
module SteepKit
|
|
5
|
+
module SourceCache
|
|
6
|
+
MODE_ENV = 'STEEP_KIT_STEEP_MODE'.freeze
|
|
7
|
+
|
|
8
|
+
class Summary
|
|
9
|
+
attr_reader :source_index, :call_counts
|
|
10
|
+
|
|
11
|
+
def initialize(typing, editor:)
|
|
12
|
+
@source_index = typing.source_index if editor
|
|
13
|
+
@call_counts = typing.method_calls.each_value.with_object(Hash.new(0)) do |call, counts|
|
|
14
|
+
counts[call.class] += 1
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def stats(target, path)
|
|
19
|
+
calls = ::Steep::TypeInference::MethodCall
|
|
20
|
+
::Steep::Services::StatsCalculator::SuccessStats.new(
|
|
21
|
+
target:,
|
|
22
|
+
path:,
|
|
23
|
+
typed_calls_count: call_counts[calls::Typed],
|
|
24
|
+
untyped_calls_count: call_counts[calls::Untyped],
|
|
25
|
+
error_calls_count: call_counts[calls::Error] + call_counts[calls::NoMethodError]
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
module ServicePatch
|
|
31
|
+
def typecheck_source(path:, target:)
|
|
32
|
+
diagnostics = super
|
|
33
|
+
return diagnostics unless Config.steep_settings.compact_completed_typing
|
|
34
|
+
|
|
35
|
+
file = source_files[path]
|
|
36
|
+
return diagnostics unless file&.typing.is_a?(::Steep::Typing)
|
|
37
|
+
|
|
38
|
+
source_files[path] = compact_source(file)
|
|
39
|
+
diagnostics
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def compact_source(file)
|
|
45
|
+
summary = Summary.new(file.typing, editor: ENV[MODE_ENV] != 'check')
|
|
46
|
+
self.class::SourceFile.new(
|
|
47
|
+
path: file.path,
|
|
48
|
+
content: file.content,
|
|
49
|
+
node: false,
|
|
50
|
+
ignores: nil,
|
|
51
|
+
typing: summary,
|
|
52
|
+
errors: file.diagnostics
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
module StatsPatch
|
|
58
|
+
def calc_stats(target, file:)
|
|
59
|
+
return file.typing.stats(target, file.path) if file.typing.is_a?(Summary)
|
|
60
|
+
|
|
61
|
+
super
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
module CheckPatch
|
|
66
|
+
def with_local_server(project, &)
|
|
67
|
+
return super unless Config.steep_settings.compact_completed_typing
|
|
68
|
+
|
|
69
|
+
previous = ENV.fetch(MODE_ENV, nil)
|
|
70
|
+
ENV[MODE_ENV] = 'check'
|
|
71
|
+
|
|
72
|
+
super
|
|
73
|
+
ensure
|
|
74
|
+
ENV[MODE_ENV] = previous if Config.steep_settings.compact_completed_typing
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def self.install!(stderr: $stderr)
|
|
79
|
+
return unless defined?(::Steep::Services::TypeCheckService) &&
|
|
80
|
+
defined?(::Steep::Services::StatsCalculator) &&
|
|
81
|
+
defined?(::Steep::Drivers::Check)
|
|
82
|
+
|
|
83
|
+
patches = [
|
|
84
|
+
[::Steep::Services::TypeCheckService, ServicePatch, %i[typecheck_source source_files], 'compact_completed_typing(service)'],
|
|
85
|
+
[::Steep::Services::StatsCalculator, StatsPatch, %i[calc_stats], 'compact_completed_typing(stats)'],
|
|
86
|
+
[::Steep::Drivers::Check, CheckPatch, %i[with_local_server], 'compact_completed_typing(check)']
|
|
87
|
+
]
|
|
88
|
+
patches.each { |target, patch, required_methods, label| PatchGuard.prepend_if_compatible?(target, patch, required_methods:, label:, stderr:) }
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'pathname'
|
|
2
|
+
require_relative 'error'
|
|
3
|
+
require_relative 'paths'
|
|
4
|
+
require_relative 'source_analysis'
|
|
5
|
+
|
|
6
|
+
module SteepKit
|
|
7
|
+
# Expands configured source paths (files or directories) into sorted,
|
|
8
|
+
# repo-relative Ruby file paths, validating containment in the repo root.
|
|
9
|
+
class SourceFiles
|
|
10
|
+
def initialize(root:)
|
|
11
|
+
@root = Pathname(root).expand_path
|
|
12
|
+
@real_root = @root.realpath
|
|
13
|
+
@real_directories = {}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def expand(paths)
|
|
17
|
+
files = paths.flat_map { |path| expand_path(path) }
|
|
18
|
+
prefix = "#{@root}#{File::SEPARATOR}"
|
|
19
|
+
files.map { |file| file.to_s.delete_prefix(prefix) }.uniq.sort.freeze
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def analyses(paths)
|
|
23
|
+
expand(paths).map { |path| SourceAnalysis.new(root: @root, path:) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def expand_path(path)
|
|
29
|
+
candidate = validate_existing_path(path)
|
|
30
|
+
return ruby_files_in(candidate) if candidate.directory?
|
|
31
|
+
|
|
32
|
+
raise Error, "source path is not a file or directory: #{path}" unless candidate.file?
|
|
33
|
+
raise Error, "source is not a Ruby file: #{path}" unless candidate.extname == '.rb'
|
|
34
|
+
|
|
35
|
+
[candidate]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def ruby_files_in(directory)
|
|
39
|
+
directory.glob('**/*.rb').select(&:file?).each { |file| ensure_resolved_within_root!(file, file) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def validate_existing_path(path)
|
|
43
|
+
path = Pathname(path.to_s)
|
|
44
|
+
candidate = path.absolute? ? path.expand_path : @root.join(path).expand_path
|
|
45
|
+
raise Error, "source path is outside the repository root: #{path}" unless Paths.within?(candidate, @root)
|
|
46
|
+
raise Error, "source path does not exist: #{path}" unless candidate.exist?
|
|
47
|
+
|
|
48
|
+
ensure_resolved_within_root!(candidate, path)
|
|
49
|
+
candidate
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def ensure_resolved_within_root!(candidate, path)
|
|
53
|
+
return if Paths.within?(real_path(candidate), @real_root)
|
|
54
|
+
|
|
55
|
+
raise Error, "source path resolves outside the repository root: #{path}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# realpath(dir/name) == realpath(dir)/name unless name is a symlink.
|
|
59
|
+
def real_path(candidate)
|
|
60
|
+
return candidate.realpath if candidate.symlink?
|
|
61
|
+
|
|
62
|
+
directory = candidate.dirname
|
|
63
|
+
(@real_directories[directory.to_s] ||= directory.realpath).join(candidate.basename)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require_relative 'source_files'
|
|
2
|
+
|
|
3
|
+
module SteepKit
|
|
4
|
+
class SourceSet
|
|
5
|
+
def initialize(config:)
|
|
6
|
+
@config = config
|
|
7
|
+
@cache = {}
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def analyses
|
|
11
|
+
@cache.values
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def refresh(paths: nil)
|
|
15
|
+
return reload if paths.nil?
|
|
16
|
+
|
|
17
|
+
previous = paths.map { |path| @cache[path]&.declared_kinds }
|
|
18
|
+
changed = paths.filter_map { |path| refresh_analysis(path) }
|
|
19
|
+
return reload unless previous == changed.map(&:declared_kinds)
|
|
20
|
+
|
|
21
|
+
affected_paths(paths, changed)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def reload
|
|
27
|
+
paths = SourceFiles.new(root: @config.root).expand(@config.signature_paths)
|
|
28
|
+
@cache = paths.to_h { |path| [path, cached_analysis(path)] }
|
|
29
|
+
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def refresh_analysis(path)
|
|
34
|
+
unless @config.root.join(path).file?
|
|
35
|
+
@cache.delete(path)
|
|
36
|
+
return
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
SourceFiles.new(root: @config.root).expand([path])
|
|
40
|
+
analysis = cached_analysis(path)
|
|
41
|
+
analysis.inline
|
|
42
|
+
|
|
43
|
+
analysis
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def affected_paths(paths, changed)
|
|
47
|
+
names = changed.flat_map(&:declared_kinds).to_set(&:first)
|
|
48
|
+
affected = analyses.filter_map do |analysis|
|
|
49
|
+
analysis.path if analysis.declared_kinds.any? { |name, _| names.include?(name) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
paths | affected
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def cached_analysis(path)
|
|
56
|
+
cached = @cache[path]
|
|
57
|
+
return cached if cached && File.binread(@config.root.join(path)) == cached.source.b
|
|
58
|
+
|
|
59
|
+
@cache[path] = SourceAnalysis.new(root: @config.root, path:)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Steepfile entry point: `require 'steep_kit/steep'` followed by
|
|
2
|
+
# `SteepKit::Steep.setup(root: __dir__)` pins UTF-8, loads config, installs
|
|
3
|
+
# conventions, and applies only the enabled RBS and Steep runtime patches.
|
|
4
|
+
Encoding.default_external = Encoding::UTF_8
|
|
5
|
+
|
|
6
|
+
require_relative 'alias_skip'
|
|
7
|
+
require_relative 'config'
|
|
8
|
+
require_relative 'constant_literal_inference'
|
|
9
|
+
require_relative 'dynamic_superclasses'
|
|
10
|
+
require_relative 'mixin_inference'
|
|
11
|
+
require_relative 'signature_index'
|
|
12
|
+
require_relative 'signature_only'
|
|
13
|
+
require_relative 'source_cache'
|
|
14
|
+
require_relative 'type_name_resolver'
|
|
15
|
+
require_relative 'value_classes'
|
|
16
|
+
|
|
17
|
+
module SteepKit
|
|
18
|
+
module Steep
|
|
19
|
+
def self.setup(root:, path: nil, stderr: $stderr)
|
|
20
|
+
Encoding.default_external = Encoding::UTF_8
|
|
21
|
+
config = Config.load(root:, path:)
|
|
22
|
+
install_patches!(config.steep, stderr:)
|
|
23
|
+
config
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.install_patches!(settings = Config.steep_settings, stderr: $stderr)
|
|
27
|
+
SignatureOnly.install!(stderr:) if settings.signature_only_donors
|
|
28
|
+
if settings.index_signatures
|
|
29
|
+
TypeNameResolver.install!(stderr:)
|
|
30
|
+
SignatureIndex.install!(stderr:)
|
|
31
|
+
end
|
|
32
|
+
SourceCache.install!(stderr:) if settings.compact_completed_typing
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require_relative 'config'
|
|
2
|
+
require_relative 'patch_guard'
|
|
3
|
+
|
|
4
|
+
module SteepKit
|
|
5
|
+
module TypeNameResolver
|
|
6
|
+
def self.install!(stderr: $stderr)
|
|
7
|
+
return false unless defined?(::Steep::Signature::Validator) && defined?(::Steep::AST::Node::TypeAssertion)
|
|
8
|
+
|
|
9
|
+
validator = PatchGuard.prepend_if_compatible?(
|
|
10
|
+
::Steep::Signature::Validator,
|
|
11
|
+
ValidatorPatch,
|
|
12
|
+
required_methods: %i[type_name_resolver factory],
|
|
13
|
+
label: 'index_signatures(validator)',
|
|
14
|
+
stderr:
|
|
15
|
+
)
|
|
16
|
+
assertion = PatchGuard.prepend_if_compatible?(
|
|
17
|
+
::Steep::AST::Node::TypeAssertion,
|
|
18
|
+
TypeAssertionPatch,
|
|
19
|
+
required_methods: %i[rbs_type type_location],
|
|
20
|
+
label: 'index_signatures(type_assertion)',
|
|
21
|
+
stderr:
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
validator && assertion
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
module ValidatorPatch
|
|
28
|
+
def type_name_resolver
|
|
29
|
+
return super unless Config.steep_settings.index_signatures
|
|
30
|
+
|
|
31
|
+
factory.type_name_resolver
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
module TypeAssertionPatch
|
|
36
|
+
def rbs_type(context, subtyping, type_vars)
|
|
37
|
+
return super unless Config.steep_settings.index_signatures
|
|
38
|
+
|
|
39
|
+
type = RBS::Parser.parse_type(type_location.buffer, range: type_location.range, variables: type_vars, require_eof: true)
|
|
40
|
+
return unless type
|
|
41
|
+
|
|
42
|
+
resolver = subtyping.factory.type_name_resolver
|
|
43
|
+
type.map_type_name { |name| resolver.resolve(name, context:) || name.absolute! }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
require 'prism'
|
|
2
|
+
require 'rbs'
|
|
3
|
+
require_relative 'config'
|
|
4
|
+
require_relative 'literal_inference'
|
|
5
|
+
|
|
6
|
+
module SteepKit
|
|
7
|
+
# Value classes built by `Data.define(...)` / `Struct.new(...)`: detection
|
|
8
|
+
# shared by the projection and the lint, plus the prepend-patch on the
|
|
9
|
+
# native inline parser that (1) reads `class CONST < Data.define(...)` as
|
|
10
|
+
# `< ::Data` and `class CONST < Struct.new(...)` as `< ::Struct[untyped]`,
|
|
11
|
+
# and (2) declares no constant for `CONST = Data.define(...)` directly inside
|
|
12
|
+
# a class/module, because the DataStructs pass projects that CONST as a class.
|
|
13
|
+
module ValueClasses
|
|
14
|
+
SUPERCLASSES = {
|
|
15
|
+
data: '::Data',
|
|
16
|
+
struct: '::Struct[untyped]'
|
|
17
|
+
}.freeze
|
|
18
|
+
SUPER_TYPES = SUPERCLASSES.transform_values { |type| RBS::Parser.parse_type(type) }.freeze
|
|
19
|
+
|
|
20
|
+
def self.install!
|
|
21
|
+
target = RBS::InlineParser::Parser
|
|
22
|
+
target.prepend(InlineParserPatch) unless target.ancestors.include?(InlineParserPatch)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# :data / :struct for a `Data.define(...)` / `Struct.new(...)` call (frozen or not), else nil.
|
|
26
|
+
def self.kind(node)
|
|
27
|
+
call = LiteralInference.unwrap_freeze(node)
|
|
28
|
+
return unless call.is_a?(Prism::CallNode)
|
|
29
|
+
|
|
30
|
+
case [LiteralInference.constant_path_names(call.receiver), call.name]
|
|
31
|
+
in [['Data'], :define] then :data
|
|
32
|
+
in [['Struct'], :new] then :struct
|
|
33
|
+
else nil
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# :data / :struct for a `CONST = Data.define(...)` statement, else nil.
|
|
38
|
+
def self.assignment_kind(statement)
|
|
39
|
+
kind(statement.value) if statement.is_a?(Prism::ConstantWriteNode)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Immediate body statements: the positions DataStructs projects from.
|
|
43
|
+
def self.statements(node)
|
|
44
|
+
body = node.is_a?(Prism::ProgramNode) ? node.statements : node.body
|
|
45
|
+
body.is_a?(Prism::StatementsNode) ? body.body : []
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
module InlineParserPatch
|
|
49
|
+
def parse_super_class(super_class_expr, inheritance_operator_loc)
|
|
50
|
+
return super unless Config.steep_settings.value_classes
|
|
51
|
+
|
|
52
|
+
kind = ValueClasses.kind(super_class_expr) or return super
|
|
53
|
+
|
|
54
|
+
type = ValueClasses::SUPER_TYPES.fetch(kind)
|
|
55
|
+
location = rbs_location(super_class_expr.location)
|
|
56
|
+
RBS::AST::Ruby::Declarations::ClassDecl::SuperClass.new(
|
|
57
|
+
location, rbs_location(inheritance_operator_loc), type.name, type_application(location, type.args)
|
|
58
|
+
)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Where DataStructs projects nothing, the assignment stays a constant.
|
|
62
|
+
def parse_constant_declaration(node)
|
|
63
|
+
return if Config.steep_settings.value_classes && ValueClasses.assignment_kind(node) && projected_position?(node)
|
|
64
|
+
|
|
65
|
+
super
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def projected_position?(node)
|
|
71
|
+
return false if module_nesting.empty?
|
|
72
|
+
|
|
73
|
+
chain = [result.prism_result.value, *module_nesting.map(&:node), node]
|
|
74
|
+
chain.each_cons(2).all? { |parent, child| ValueClasses.statements(parent).any? { |statement| statement.equal?(child) } }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The `[untyped]` of `::Struct[untyped]`, anchored on the superclass expression.
|
|
78
|
+
def type_application(location, type_args)
|
|
79
|
+
return if type_args.empty?
|
|
80
|
+
|
|
81
|
+
RBS::AST::Ruby::Annotations::TypeApplicationAnnotation.new(
|
|
82
|
+
location:, prefix_location: location, type_args:, close_bracket_location: location, comma_locations: []
|
|
83
|
+
)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
SteepKit::ValueClasses.install!
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require_relative 'config'
|
|
2
|
+
require_relative 'error'
|
|
3
|
+
require_relative 'generate'
|
|
4
|
+
require_relative 'paths'
|
|
5
|
+
|
|
6
|
+
module SteepKit
|
|
7
|
+
# `steep-kit watch`: incrementally regenerates projected RBS files on source
|
|
8
|
+
# or signature changes. Requires the host's `listen` gem.
|
|
9
|
+
class Watch
|
|
10
|
+
LATENCY = 0.2
|
|
11
|
+
|
|
12
|
+
def initialize(config:, stdout: $stdout, stderr: $stderr)
|
|
13
|
+
@config = config
|
|
14
|
+
@stdout = stdout
|
|
15
|
+
@stderr = stderr
|
|
16
|
+
@generator = Generate.new(config: @config)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def run
|
|
20
|
+
directories = watched_directories
|
|
21
|
+
listener = build_listener(directories)
|
|
22
|
+
cycle
|
|
23
|
+
listener.start
|
|
24
|
+
@stdout.puts("watch: watching #{directories.map { |directory| Paths.relative_within(directory, @config.root) }.join(', ')}")
|
|
25
|
+
sleep
|
|
26
|
+
rescue Interrupt
|
|
27
|
+
# Ctrl-C is the normal way to stop; exit quietly.
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def cycle(changes: nil)
|
|
31
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
32
|
+
summary = @generator.call(changes:)
|
|
33
|
+
@stdout.puts("watch: #{@config.projected_root} #{summary} (#{(Process.clock_gettime(Process::CLOCK_MONOTONIC) - started).round(1)}s)")
|
|
34
|
+
rescue Error, SystemCallError => e
|
|
35
|
+
@stderr.puts("watch: #{e.message} (waiting for the next change)")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def build_listener(directories)
|
|
41
|
+
require 'listen'
|
|
42
|
+
projected = @config.root.join(@config.projected_root)
|
|
43
|
+
Listen.to(*directories, only: /\.(?:rb|rbs)\z/, latency: LATENCY) do |modified, added, removed|
|
|
44
|
+
changes = (modified + added + removed).reject { |path| Paths.within?(path, projected) }
|
|
45
|
+
cycle(changes:) unless changes.empty?
|
|
46
|
+
end
|
|
47
|
+
rescue LoadError
|
|
48
|
+
raise Error, 'the watch command needs the `listen` gem in the host bundle'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def watched_directories
|
|
52
|
+
directories = @config.signature_paths.map do |path|
|
|
53
|
+
absolute = @config.root.join(path)
|
|
54
|
+
raise Error, "signature path does not exist: #{path}" unless absolute.exist?
|
|
55
|
+
|
|
56
|
+
(absolute.directory? ? absolute : absolute.dirname).to_s
|
|
57
|
+
end
|
|
58
|
+
signatures = @config.root.join('sig')
|
|
59
|
+
signatures.mkpath
|
|
60
|
+
directories << signatures.to_s
|
|
61
|
+
directories.uniq
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require 'find'
|
|
2
|
+
require 'open3'
|
|
3
|
+
require 'pathname'
|
|
4
|
+
require 'rubydex'
|
|
5
|
+
require_relative 'error'
|
|
6
|
+
|
|
7
|
+
module SteepKit
|
|
8
|
+
# One indexed and resolved rubydex graph of the Ruby workspace.
|
|
9
|
+
module WorkspaceGraph
|
|
10
|
+
EXCLUDED_ROOT_DIRECTORIES = %w[
|
|
11
|
+
.bundle
|
|
12
|
+
.claude
|
|
13
|
+
.gem_rbs_collection
|
|
14
|
+
.git
|
|
15
|
+
.github
|
|
16
|
+
.idea
|
|
17
|
+
.ruby-lsp
|
|
18
|
+
.vscode
|
|
19
|
+
coverage
|
|
20
|
+
log
|
|
21
|
+
node_modules
|
|
22
|
+
public
|
|
23
|
+
storage
|
|
24
|
+
tmp
|
|
25
|
+
vendor
|
|
26
|
+
].freeze
|
|
27
|
+
INDEXABLE_EXTENSIONS = %w[.rb .rake .ru].freeze
|
|
28
|
+
GIT_LS_FILES = [
|
|
29
|
+
'git', 'ls-files', '-z', '--cached', '--others', '--exclude-standard', '--',
|
|
30
|
+
*INDEXABLE_EXTENSIONS.map { |extension| "*#{extension}" },
|
|
31
|
+
*EXCLUDED_ROOT_DIRECTORIES.map { |name| ":(exclude)#{name}" }
|
|
32
|
+
].freeze
|
|
33
|
+
# Unset: git hooks export the first three, and pathspec switches narrow `*.rb`.
|
|
34
|
+
GIT_ENV = %w[
|
|
35
|
+
GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
|
|
36
|
+
GIT_LITERAL_PATHSPECS GIT_GLOB_PATHSPECS GIT_NOGLOB_PATHSPECS GIT_ICASE_PATHSPECS
|
|
37
|
+
].to_h { |name| [name, nil] }.freeze
|
|
38
|
+
|
|
39
|
+
def self.build(root)
|
|
40
|
+
root = Pathname(root).realpath
|
|
41
|
+
graph = Rubydex::Graph.configure_for_workspace(root.to_s)
|
|
42
|
+
graph.index_all(ruby_files(root))
|
|
43
|
+
graph.resolve
|
|
44
|
+
graph
|
|
45
|
+
rescue StandardError => e
|
|
46
|
+
raise Error, "rubydex could not index the workspace: #{e.class}: #{e.message}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.ruby_files(root)
|
|
50
|
+
(git_ruby_files(root) || walked_ruby_files(root)).sort
|
|
51
|
+
end
|
|
52
|
+
private_class_method :ruby_files
|
|
53
|
+
|
|
54
|
+
def self.git_ruby_files(root)
|
|
55
|
+
return unless root.join('.git').exist?
|
|
56
|
+
|
|
57
|
+
listing, status = Open3.capture2(GIT_ENV, *GIT_LS_FILES, chdir: root.to_s, err: File::NULL)
|
|
58
|
+
return unless status.success?
|
|
59
|
+
|
|
60
|
+
listing.split("\0").uniq.map { |path| root.join(path).to_s }.select { |path| indexable_file?(path) }
|
|
61
|
+
rescue SystemCallError
|
|
62
|
+
nil
|
|
63
|
+
end
|
|
64
|
+
private_class_method :git_ruby_files
|
|
65
|
+
|
|
66
|
+
def self.walked_ruby_files(root)
|
|
67
|
+
files = []
|
|
68
|
+
Find.find(root.to_s) do |path|
|
|
69
|
+
if excluded_root_directory?(root, path)
|
|
70
|
+
Find.prune
|
|
71
|
+
elsif indexable_file?(path)
|
|
72
|
+
files << path
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
files
|
|
76
|
+
end
|
|
77
|
+
private_class_method :walked_ruby_files
|
|
78
|
+
|
|
79
|
+
def self.excluded_root_directory?(root, path)
|
|
80
|
+
File.directory?(path) && File.dirname(path) == root.to_s &&
|
|
81
|
+
EXCLUDED_ROOT_DIRECTORIES.include?(File.basename(path))
|
|
82
|
+
end
|
|
83
|
+
private_class_method :excluded_root_directory?
|
|
84
|
+
|
|
85
|
+
def self.indexable_file?(path)
|
|
86
|
+
File.file?(path) && INDEXABLE_EXTENSIONS.include?(File.extname(path))
|
|
87
|
+
end
|
|
88
|
+
private_class_method :indexable_file?
|
|
89
|
+
end
|
|
90
|
+
end
|
data/lib/steep_kit.rb
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'pathname'
|
|
2
|
+
|
|
3
|
+
require_relative 'steep_kit/error'
|
|
4
|
+
require_relative 'steep_kit/paths'
|
|
5
|
+
require_relative 'steep_kit/settings'
|
|
6
|
+
require_relative 'steep_kit/conventions'
|
|
7
|
+
require_relative 'steep_kit/config'
|
|
8
|
+
require_relative 'steep_kit/output_plan'
|
|
9
|
+
require_relative 'steep_kit/rbs_builder'
|
|
10
|
+
require_relative 'steep_kit/alias_skip'
|
|
11
|
+
require_relative 'steep_kit/dynamic_superclasses'
|
|
12
|
+
require_relative 'steep_kit/source_analysis'
|
|
13
|
+
require_relative 'steep_kit/source_files'
|
|
14
|
+
require_relative 'steep_kit/literal_inference'
|
|
15
|
+
require_relative 'steep_kit/constant_table'
|
|
16
|
+
require_relative 'steep_kit/mixin_inference'
|
|
17
|
+
require_relative 'steep_kit/passes'
|
|
18
|
+
require_relative 'steep_kit/signature_kinds'
|
|
19
|
+
require_relative 'steep_kit/project'
|
|
20
|
+
require_relative 'steep_kit/project_types'
|
|
21
|
+
require_relative 'steep_kit/workspace_graph'
|
|
22
|
+
require_relative 'steep_kit/signature_merger'
|
|
23
|
+
require_relative 'steep_kit/lint_constant_access'
|
|
24
|
+
require_relative 'steep_kit/lint_inline'
|
|
25
|
+
require_relative 'steep_kit/rbs_collection'
|
|
26
|
+
require_relative 'steep_kit/generate'
|
|
27
|
+
require_relative 'steep_kit/check'
|
|
28
|
+
require_relative 'steep_kit/watch'
|
|
29
|
+
require_relative 'steep_kit/fast'
|
|
30
|
+
require_relative 'steep_kit/cli'
|
|
31
|
+
require_relative 'steep_kit/version'
|