sigmold 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 +19 -0
- data/LICENSE +21 -0
- data/README.ja.md +136 -0
- data/README.md +128 -0
- data/exe/sigmold +174 -0
- data/lib/sigmold/calls.rb +75 -0
- data/lib/sigmold/calls_recorder.rb +65 -0
- data/lib/sigmold/check.rb +78 -0
- data/lib/sigmold/deps.rb +60 -0
- data/lib/sigmold/explore.rb +443 -0
- data/lib/sigmold/hole.rb +30 -0
- data/lib/sigmold/holes.rb +85 -0
- data/lib/sigmold/pour.rb +100 -0
- data/lib/sigmold/review.rb +127 -0
- data/lib/sigmold/signature.rb +151 -0
- data/lib/sigmold/strict.rb +159 -0
- data/lib/sigmold/surface.rb +54 -0
- data/lib/sigmold/surface_check.rb +59 -0
- data/lib/sigmold/typecheck.rb +88 -0
- data/lib/sigmold/version.rb +6 -0
- data/lib/sigmold.rb +308 -0
- data/sig/generated/sigmold/calls.rbs +47 -0
- data/sig/generated/sigmold/calls_recorder.rbs +22 -0
- data/sig/generated/sigmold/check.rbs +39 -0
- data/sig/generated/sigmold/deps.rbs +30 -0
- data/sig/generated/sigmold/explore.rbs +181 -0
- data/sig/generated/sigmold/hole.rbs +24 -0
- data/sig/generated/sigmold/holes.rbs +56 -0
- data/sig/generated/sigmold/pour.rbs +60 -0
- data/sig/generated/sigmold/review.rbs +58 -0
- data/sig/generated/sigmold/signature.rbs +105 -0
- data/sig/generated/sigmold/strict.rbs +96 -0
- data/sig/generated/sigmold/surface.rbs +20 -0
- data/sig/generated/sigmold/surface_check.rbs +36 -0
- data/sig/generated/sigmold/typecheck.rbs +66 -0
- data/sig/generated/sigmold/version.rbs +5 -0
- data/sig/generated/sigmold.rbs +135 -0
- metadata +80 -0
data/lib/sigmold/pour.rb
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "pathname"
|
|
6
|
+
require_relative "surface"
|
|
7
|
+
|
|
8
|
+
module Sigmold
|
|
9
|
+
# Has an agent write a file again from the mold alone: the checkout
|
|
10
|
+
# without the file, its signature cut to the public surface, and the
|
|
11
|
+
# tests. Each copy is a rewrite `gaps` can compare with the original.
|
|
12
|
+
module Pour
|
|
13
|
+
Rewrite = Data.define(
|
|
14
|
+
:label, #: String
|
|
15
|
+
:dir, #: String
|
|
16
|
+
:path, #: String
|
|
17
|
+
:written, #: bool
|
|
18
|
+
:tests, #: bool?
|
|
19
|
+
:kept #: bool
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
# Copies +files+ from the current folder into +into+, leaves +file+
|
|
23
|
+
# out, and cuts +sig+ to its public surface.
|
|
24
|
+
#
|
|
25
|
+
# @rbs file: String
|
|
26
|
+
# @rbs sig: String
|
|
27
|
+
# @rbs into: String
|
|
28
|
+
# @rbs files: Array[String]
|
|
29
|
+
def self.hide(file, sig, into, files:) #: void
|
|
30
|
+
raise ArgumentError, "#{into} exists" if File.exist?(into)
|
|
31
|
+
|
|
32
|
+
file = clean(file)
|
|
33
|
+
sig = clean(sig)
|
|
34
|
+
|
|
35
|
+
FileUtils.mkdir_p(into)
|
|
36
|
+
files.each do |f|
|
|
37
|
+
next if clean(f) == file || !File.file?(f)
|
|
38
|
+
|
|
39
|
+
FileUtils.mkdir_p(File.join(into, File.dirname(f)))
|
|
40
|
+
FileUtils.cp(f, File.join(into, f))
|
|
41
|
+
end
|
|
42
|
+
FileUtils.mkdir_p(File.join(into, File.dirname(file))) # the agent writes the file where it was
|
|
43
|
+
File.write(File.join(into, sig), Surface.public_only(File.read(sig)))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Runs +agent+, a shell command, once per rewrite in a folder of its
|
|
47
|
+
# own under +out+. The brief goes to its standard input and to
|
|
48
|
+
# SIGMOLD_BRIEF. +test+, a shell command, runs after it in that folder.
|
|
49
|
+
#
|
|
50
|
+
# @rbs file: String
|
|
51
|
+
# @rbs sig: String
|
|
52
|
+
# @rbs agent: String
|
|
53
|
+
# @rbs out: String
|
|
54
|
+
# @rbs files: Array[String]
|
|
55
|
+
# @rbs times: Integer
|
|
56
|
+
# @rbs test: String?
|
|
57
|
+
# @rbs return: Array[Rewrite]
|
|
58
|
+
def self.run(file:, sig:, agent:, out:, files:, times: 2, test: nil)
|
|
59
|
+
file = clean(file) # `./lib/x.rb` names the same file git lists as `lib/x.rb`
|
|
60
|
+
sig = clean(sig)
|
|
61
|
+
(1..times).map do |n|
|
|
62
|
+
label = "rewrite#{n}"
|
|
63
|
+
dir = File.join(out, label)
|
|
64
|
+
hide(file, sig, dir, files:)
|
|
65
|
+
brief = Surface.brief(file, sig, dir)
|
|
66
|
+
IO.popen({ "SIGMOLD_BRIEF" => brief }, ["sh", "-c", agent], "w", chdir: dir) do |stdin|
|
|
67
|
+
stdin.write(brief)
|
|
68
|
+
rescue Errno::EPIPE
|
|
69
|
+
nil # an agent that reads the brief from SIGMOLD_BRIEF may close its input
|
|
70
|
+
end
|
|
71
|
+
path = File.join(dir, file)
|
|
72
|
+
written = File.file?(path)
|
|
73
|
+
tests = test && written ? system("sh", "-c", test, chdir: dir, out: File::NULL, err: File::NULL) == true : nil
|
|
74
|
+
# The agent may declare its private parts in the signature; the
|
|
75
|
+
# public part is the mold and stays as it was.
|
|
76
|
+
kept = Surface.public_only(File.read(File.join(dir, sig))) == Surface.public_only(File.read(sig))
|
|
77
|
+
Rewrite.new(label:, dir:, path:, written:, tests:, kept:)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# @rbs path: String
|
|
82
|
+
# @rbs return: String
|
|
83
|
+
def self.clean(path)
|
|
84
|
+
Pathname(path).cleanpath.to_s
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# @rbs rewrites: Array[Rewrite]
|
|
88
|
+
# @rbs return: String
|
|
89
|
+
def self.report(rewrites)
|
|
90
|
+
rewrites.map do |r|
|
|
91
|
+
state = if !r.written then "wrote nothing"
|
|
92
|
+
elsif !r.kept then "changed the public signature"
|
|
93
|
+
elsif r.tests.nil? then "written"
|
|
94
|
+
else r.tests ? "written, tests pass" : "written, tests fail"
|
|
95
|
+
end
|
|
96
|
+
"#{r.label}: #{state} (#{r.path})\n"
|
|
97
|
+
end.join
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "tmpdir"
|
|
6
|
+
require_relative "surface"
|
|
7
|
+
|
|
8
|
+
module Sigmold
|
|
9
|
+
# Shows what a person reviews between two commits: the public part of the
|
|
10
|
+
# signatures and the tests. The code, and the private parts an agent
|
|
11
|
+
# declared for it, are named but not shown, because `check` and `pour`
|
|
12
|
+
# hold them to the mold.
|
|
13
|
+
module Review
|
|
14
|
+
# @rbs base: String
|
|
15
|
+
# @rbs head: String? -- nil reads the working tree
|
|
16
|
+
# @rbs sig: Array[String]
|
|
17
|
+
# @rbs tests: Array[String]
|
|
18
|
+
# @rbs lib: String
|
|
19
|
+
# @rbs return: String
|
|
20
|
+
def self.report(base:, head: nil, sig: ["sig"], tests: %w[test spec], lib: "lib")
|
|
21
|
+
before_sig = files(base, sig)
|
|
22
|
+
after_sig = files(head, sig)
|
|
23
|
+
public_diff = diff(before_sig.transform_values { Surface.public_only(_1) }, after_sig.transform_values { Surface.public_only(_1) })
|
|
24
|
+
tests_diff = diff(files(base, tests), files(head, tests))
|
|
25
|
+
private_only = (before_sig.keys | after_sig.keys).select do |path|
|
|
26
|
+
before_sig[path] != after_sig[path] &&
|
|
27
|
+
Surface.public_only(before_sig[path].to_s) == Surface.public_only(after_sig[path].to_s)
|
|
28
|
+
end
|
|
29
|
+
code = changed(files(base, [lib]), files(head, [lib]))
|
|
30
|
+
[
|
|
31
|
+
"review #{base}..#{head || 'the working tree'}",
|
|
32
|
+
section("sig/, the public part", public_diff),
|
|
33
|
+
section("tests", tests_diff),
|
|
34
|
+
section("what the public sig/ names", uses(before_sig, after_sig)),
|
|
35
|
+
"== not for review (sigmold check holds them to the mold)",
|
|
36
|
+
*private_only.sort.map { "#{_1}: only its private part changed" },
|
|
37
|
+
*code.map { "#{_1}: changed" },
|
|
38
|
+
("nothing" if private_only.empty? && code.empty?)
|
|
39
|
+
].compact.join("\n") + "\n"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The dependencies the public signatures name that came or went, as
|
|
43
|
+
# `+ A -> B`, so a reviewer sees a new edge before the code that uses it.
|
|
44
|
+
#
|
|
45
|
+
# @rbs before: Hash[String, String]
|
|
46
|
+
# @rbs after: Hash[String, String]
|
|
47
|
+
# @rbs return: String
|
|
48
|
+
def self.uses(before, after)
|
|
49
|
+
require_relative "deps"
|
|
50
|
+
old = edges(before)
|
|
51
|
+
new = edges(after)
|
|
52
|
+
gone = (old - new).map { "- #{_1}" }
|
|
53
|
+
came = (new - old).map { "+ #{_1}" }
|
|
54
|
+
[*gone, *came].map { "#{_1}\n" }.join
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @rbs sig: Hash[String, String]
|
|
58
|
+
# @rbs return: Array[String]
|
|
59
|
+
def self.edges(sig)
|
|
60
|
+
return [] if sig.empty?
|
|
61
|
+
|
|
62
|
+
Dir.mktmpdir("sigmold-review-sig") do |dir|
|
|
63
|
+
sig.each { |path, text| File.write(File.join(dir, path.tr("/", "_")), Surface.public_only(text)) }
|
|
64
|
+
Deps.edges(Signature.new([dir])).flat_map { |owner, uses| uses.map { "#{owner} -> #{_1}" } }.sort
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @rbs title: String
|
|
69
|
+
# @rbs text: String
|
|
70
|
+
# @rbs return: String
|
|
71
|
+
def self.section(title, text)
|
|
72
|
+
"== #{title}\n#{text.empty? ? "no change\n" : text}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The text of each file under +dirs+ at +rev+, or in the working tree.
|
|
76
|
+
#
|
|
77
|
+
# @rbs rev: String?
|
|
78
|
+
# @rbs dirs: Array[String]
|
|
79
|
+
# @rbs return: Hash[String, String]
|
|
80
|
+
def self.files(rev, dirs)
|
|
81
|
+
if rev
|
|
82
|
+
paths = git("ls-tree", "-r", "--name-only", rev, "--", *dirs).split("\n")
|
|
83
|
+
paths.to_h { [_1, git("show", "#{rev}:#{_1}")] }
|
|
84
|
+
else
|
|
85
|
+
dirs.flat_map { Dir.glob(File.join(_1, "**/*")) }.select { File.file?(_1) }.sort.to_h { [_1, File.read(_1)] }
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# @rbs before: Hash[String, String]
|
|
90
|
+
# @rbs after: Hash[String, String]
|
|
91
|
+
# @rbs return: Array[String]
|
|
92
|
+
def self.changed(before, after)
|
|
93
|
+
(before.keys | after.keys).sort.reject { before[_1] == after[_1] }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# A unified diff between two sets of files, with paths as the project
|
|
97
|
+
# names them.
|
|
98
|
+
#
|
|
99
|
+
# @rbs before: Hash[String, String]
|
|
100
|
+
# @rbs after: Hash[String, String]
|
|
101
|
+
# @rbs return: String
|
|
102
|
+
def self.diff(before, after)
|
|
103
|
+
return "" if changed(before, after).empty?
|
|
104
|
+
|
|
105
|
+
Dir.mktmpdir("sigmold-review") do |dir|
|
|
106
|
+
{ "a" => before, "b" => after }.each do |side, contents|
|
|
107
|
+
FileUtils.mkdir_p(File.join(dir, side))
|
|
108
|
+
contents.each do |path, text|
|
|
109
|
+
FileUtils.mkdir_p(File.join(dir, side, File.dirname(path)))
|
|
110
|
+
File.write(File.join(dir, side, path), text)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
out = IO.popen(%w[git diff --no-index --no-color a b], chdir: dir, &:read).to_s
|
|
114
|
+
out.gsub(%r{(?<=\s)([ab])/[ab]/}, '\1/').gsub(%r{(?<=^--- |^\+\+\+ )([ab])/[ab]/}, '\1/')
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# @rbs args: String
|
|
119
|
+
# @rbs return: String
|
|
120
|
+
def self.git(*args)
|
|
121
|
+
out = IO.popen(["git", *args], err: File::NULL, &:read).to_s
|
|
122
|
+
raise ArgumentError, "sigmold review: git #{args.first} failed for #{args.inspect}" unless $?&.success?
|
|
123
|
+
|
|
124
|
+
out
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require "pathname"
|
|
5
|
+
|
|
6
|
+
module Sigmold
|
|
7
|
+
# Reads the public surface of a namespace from its RBS files: the methods a
|
|
8
|
+
# reader of sig/ sees, each with its method types. `surface`, `calls`,
|
|
9
|
+
# `strict`, and the explorer that `gaps` builds all start from this one
|
|
10
|
+
# reading, so they agree on what "declared" means. It loads the `rbs` gem
|
|
11
|
+
# when called, because sigmold has no runtime dependency.
|
|
12
|
+
class Signature
|
|
13
|
+
# One public method a signature declares. +id+ is `A::B#name` for an
|
|
14
|
+
# instance method and `A::B.name` for a singleton method.
|
|
15
|
+
Entry = Data.define(
|
|
16
|
+
:id, #: String
|
|
17
|
+
:owner, #: String
|
|
18
|
+
:name, #: Symbol
|
|
19
|
+
:singleton, #: bool
|
|
20
|
+
:types, #: Array[untyped]
|
|
21
|
+
:annotations, #: Array[String]
|
|
22
|
+
:location #: String?
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
attr_reader :env #: untyped
|
|
26
|
+
attr_reader :builder #: untyped
|
|
27
|
+
|
|
28
|
+
# @rbs dirs: Array[String]
|
|
29
|
+
def initialize(dirs) #: void
|
|
30
|
+
require "rbs"
|
|
31
|
+
rbs = Object.const_get(:RBS) #: untyped
|
|
32
|
+
loader = rbs::EnvironmentLoader.new
|
|
33
|
+
dirs.each { loader.add(path: Pathname(_1)) }
|
|
34
|
+
@dirs = dirs
|
|
35
|
+
@env = rbs::Environment.from_loader(loader).resolve_type_names
|
|
36
|
+
@builder = rbs::DefinitionBuilder.new(env: @env)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The modules and classes the signatures declare under +namespace+,
|
|
40
|
+
# the namespace included, as names without the leading `::`.
|
|
41
|
+
#
|
|
42
|
+
# @rbs namespace: String
|
|
43
|
+
# @rbs return: Array[String]
|
|
44
|
+
def modules(namespace)
|
|
45
|
+
@env.class_decls.keys.map { _1.to_s.delete_prefix("::") }
|
|
46
|
+
.select { _1 == namespace || _1.start_with?("#{namespace}::") }.sort
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The modules and classes declared in the given folders themselves, not
|
|
50
|
+
# in the core library the loader adds.
|
|
51
|
+
#
|
|
52
|
+
# @rbs return: Array[String]
|
|
53
|
+
def own_modules
|
|
54
|
+
dirs = @dirs.map { File.expand_path(_1) }
|
|
55
|
+
@env.class_decls.filter_map do |name, entry|
|
|
56
|
+
files = decls(entry).map { _1.location&.buffer&.name.to_s }
|
|
57
|
+
name.to_s.delete_prefix("::") if files.any? { |f| dirs.any? { File.expand_path(f).start_with?("#{_1}/") } }
|
|
58
|
+
end.sort
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Whether a method is declared in the given folders, not in the core
|
|
62
|
+
# library a project's signature reopens.
|
|
63
|
+
#
|
|
64
|
+
# @rbs entry: Entry
|
|
65
|
+
def own?(entry) #: bool
|
|
66
|
+
file = entry.location.to_s.sub(/:\d+\z/, "")
|
|
67
|
+
@dirs.any? { |dir| (full = File.expand_path(dir)) == File.expand_path(file) || File.expand_path(file).start_with?("#{full}/") }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The annotations written above a class or module, in every file
|
|
71
|
+
# that opens it.
|
|
72
|
+
#
|
|
73
|
+
# @rbs name: String
|
|
74
|
+
# @rbs return: Array[String]
|
|
75
|
+
def annotations_of(name)
|
|
76
|
+
decls(@env.class_decls.fetch(type_name(name))).flat_map { _1.annotations.map(&:string) }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# @rbs name: String
|
|
80
|
+
def class_named?(name) #: bool
|
|
81
|
+
class?(@env.class_decls.fetch(type_name(name)))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# @rbs name: String
|
|
85
|
+
def declared?(name) #: bool
|
|
86
|
+
@env.class_decls.key?(type_name(name))
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Every public method declared under +namespace+. `new` counts as
|
|
90
|
+
# declared unless the signature makes it private, because every class
|
|
91
|
+
# answers it.
|
|
92
|
+
#
|
|
93
|
+
# @rbs namespace: String
|
|
94
|
+
# @rbs return: Array[Entry]
|
|
95
|
+
def surface(namespace)
|
|
96
|
+
modules(namespace).flat_map { entries(_1) }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @rbs owner: String
|
|
100
|
+
# @rbs return: Array[Entry]
|
|
101
|
+
def entries(owner)
|
|
102
|
+
name = type_name(owner)
|
|
103
|
+
kind = @env.class_decls.fetch(name)
|
|
104
|
+
sides = [[false, @builder.build_instance(name)], [true, @builder.build_singleton(name)]]
|
|
105
|
+
sides.flat_map do |singleton, definition|
|
|
106
|
+
definition.methods.filter_map do |method, m|
|
|
107
|
+
next unless m.public?
|
|
108
|
+
next unless m.defined_in == name || (singleton && method == :new && class?(kind))
|
|
109
|
+
|
|
110
|
+
Entry.new(
|
|
111
|
+
id: "#{owner}#{singleton ? '.' : '#'}#{method}", owner:, name: method, singleton:,
|
|
112
|
+
types: m.method_types, annotations: m.annotations.map(&:string),
|
|
113
|
+
location: m.defs.first&.type&.location&.then { "#{_1.buffer.name}:#{_1.start_line}" }
|
|
114
|
+
)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Methods the signature declares private, under the same ids.
|
|
120
|
+
#
|
|
121
|
+
# @rbs namespace: String
|
|
122
|
+
# @rbs return: Array[String]
|
|
123
|
+
def private_ids(namespace)
|
|
124
|
+
modules(namespace).flat_map do |owner|
|
|
125
|
+
name = type_name(owner)
|
|
126
|
+
[["#", @builder.build_instance(name)], [".", @builder.build_singleton(name)]].flat_map do |mark, definition|
|
|
127
|
+
definition.methods.filter_map { |method, m| "#{owner}#{mark}#{method}" if !m.public? && m.defined_in == name }
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
private
|
|
133
|
+
|
|
134
|
+
# @rbs name: String
|
|
135
|
+
# @rbs return: untyped
|
|
136
|
+
def type_name(name)
|
|
137
|
+
Object.const_get(:RBS)::TypeName.parse("::#{name}")
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# @rbs entry: untyped
|
|
141
|
+
# @rbs return: Array[untyped]
|
|
142
|
+
def decls(entry)
|
|
143
|
+
entry.respond_to?(:each_decl) ? entry.each_decl.to_a : entry.decls.map(&:decl) # rbs 3 keeps them in decls
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# @rbs kind: untyped
|
|
147
|
+
def class?(kind) #: bool
|
|
148
|
+
kind.class.name.to_s.end_with?("ClassEntry")
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require_relative "typecheck"
|
|
5
|
+
require_relative "signature"
|
|
6
|
+
require_relative "holes"
|
|
7
|
+
|
|
8
|
+
module Sigmold
|
|
9
|
+
# Finds the ways code can pass Steep without meeting its signature, so a
|
|
10
|
+
# clean check means the types hold. A reviewer who reads only sig/ and the
|
|
11
|
+
# tests relies on that.
|
|
12
|
+
#
|
|
13
|
+
# - Every Steep diagnostic counts, at any level. A Steepfile's defaults
|
|
14
|
+
# show `case/in` (UnsupportedSyntax) and a node Steep gave up on
|
|
15
|
+
# (UnexpectedError) only as hints, and their bodies go unchecked.
|
|
16
|
+
# - A type comment that says `untyped`, or a `steep:ignore`, turns the
|
|
17
|
+
# check off where it stands.
|
|
18
|
+
# - `untyped` in a public signature lets any value through. One the owner
|
|
19
|
+
# chose carries `%a{sigmold:untyped}`, so the choice is visible in sig/.
|
|
20
|
+
#
|
|
21
|
+
# A diagnostic on a hole belongs to `sigmold holes`, not here.
|
|
22
|
+
module Strict
|
|
23
|
+
Finding = Data.define(
|
|
24
|
+
:path, #: String
|
|
25
|
+
:line, #: Integer
|
|
26
|
+
:rule, #: String
|
|
27
|
+
:message #: String
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
ALLOW = "sigmold:untyped"
|
|
31
|
+
|
|
32
|
+
# @rbs root: String
|
|
33
|
+
# @rbs return: Array[Finding]
|
|
34
|
+
def self.find(root)
|
|
35
|
+
within(Typecheck.new(root))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @rbs check: Typecheck
|
|
39
|
+
# @rbs return: Array[Finding]
|
|
40
|
+
def self.within(check)
|
|
41
|
+
in_code = check.sources.flat_map { diagnostics(_1) + comments(_1) }
|
|
42
|
+
dirs = check.signature_dirs.reject { _1.start_with?("..") }.map { File.join(check.root, _1) }
|
|
43
|
+
(in_code + signatures(dirs, check.root)).sort_by { [_1.path, _1.line, _1.rule] }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @rbs checked: Typecheck::Checked
|
|
47
|
+
# @rbs return: Array[Finding]
|
|
48
|
+
def self.diagnostics(checked)
|
|
49
|
+
holes = Nodes.each(checked.typing.source.node).select { Holes.hole_call?(_1) }.map { _1.location.expression }
|
|
50
|
+
checked.typing.errors.filter_map do |error|
|
|
51
|
+
next if hole_made?(error, holes) || attribute?(error)
|
|
52
|
+
|
|
53
|
+
location = error.location
|
|
54
|
+
line = (location.respond_to?(:start_line) ? location.start_line : location&.line) || 1
|
|
55
|
+
Finding.new(path: checked.path, line:, rule: error.class.name.to_s.split("::").last.to_s,
|
|
56
|
+
message: error.header_line.to_s)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# A diagnostic belongs to a hole when it stands inside the hole call,
|
|
61
|
+
# or when the value it judges is the hole itself: the body a hole ends,
|
|
62
|
+
# an argument it fills, a method called on it. A diagnostic that only
|
|
63
|
+
# encloses a hole, such as a body whose other branch has a wrong type,
|
|
64
|
+
# still counts.
|
|
65
|
+
#
|
|
66
|
+
# @rbs error: untyped
|
|
67
|
+
# @rbs holes: Array[untyped]
|
|
68
|
+
def self.hole_made?(error, holes) #: bool
|
|
69
|
+
node = error.respond_to?(:node) && error.node
|
|
70
|
+
range = node && node.location.expression
|
|
71
|
+
return true if range && holes.any? { |h| h.begin_pos <= range.begin_pos && range.end_pos <= h.end_pos }
|
|
72
|
+
|
|
73
|
+
judged = %i[actual type].filter_map { error.respond_to?(_1) ? error.public_send(_1) : nil }
|
|
74
|
+
judged.any? { _1.to_s == HOLE }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
HOLE = "::Sigmold::Hole"
|
|
78
|
+
|
|
79
|
+
ATTRIBUTES = { attr_reader: [""], attr_accessor: ["", "="], attr_writer: ["="] }.freeze #: Hash[Symbol, Array[String]]
|
|
80
|
+
|
|
81
|
+
# Steep does not count `attr_reader :name` in Ruby as the method an RBS
|
|
82
|
+
# `attr_reader name: T` declares, and says the method is missing.
|
|
83
|
+
#
|
|
84
|
+
# @rbs error: untyped
|
|
85
|
+
def self.attribute?(error) #: bool
|
|
86
|
+
return false unless error.class.name.to_s.end_with?("::MethodDefinitionMissing") && error.kind == :instance
|
|
87
|
+
|
|
88
|
+
attributes(error.node).include?(error.missing_method.to_s)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# The methods the attr_ calls directly in a class body define, not
|
|
92
|
+
# those of a class nested in it. Steep's own nodes, such as a type
|
|
93
|
+
# assertion, sit among the parser's and are passed over.
|
|
94
|
+
#
|
|
95
|
+
# @rbs node: untyped
|
|
96
|
+
# @rbs top: bool
|
|
97
|
+
# @rbs return: Array[String]
|
|
98
|
+
def self.attributes(node, top: true)
|
|
99
|
+
return [] unless node.is_a?(Object.const_get("Parser::AST::Node")) && (top || !%i[class module sclass].include?(node.type))
|
|
100
|
+
|
|
101
|
+
if node.type == :send && node.children[0].nil? && (suffixes = ATTRIBUTES[node.children[1]])
|
|
102
|
+
names = node.children.drop(2).select { %i[sym str].include?(_1.type) }.map { _1.children[0].to_s }
|
|
103
|
+
return names.product(suffixes).map(&:join)
|
|
104
|
+
end
|
|
105
|
+
node.children.flat_map { attributes(_1, top: false) }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# @rbs checked: Typecheck::Checked
|
|
109
|
+
# @rbs return: Array[Finding]
|
|
110
|
+
def self.comments(checked)
|
|
111
|
+
require "prism"
|
|
112
|
+
Prism.parse_comments(checked.text).filter_map do |comment|
|
|
113
|
+
text = comment.slice
|
|
114
|
+
line = comment.location.start_line
|
|
115
|
+
if text.match?(/steep:ignore/)
|
|
116
|
+
Finding.new(path: checked.path, line:, rule: "steep:ignore", message: "turns Steep off on this line")
|
|
117
|
+
elsif text.match?(/\A#\s*(:|@rbs\b|@type\b).*\buntyped\b/)
|
|
118
|
+
Finding.new(path: checked.path, line:, rule: "untyped comment", message: text.strip)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# @rbs dirs: Array[String]
|
|
124
|
+
# @rbs root: String
|
|
125
|
+
# @rbs return: Array[Finding]
|
|
126
|
+
def self.signatures(dirs, root)
|
|
127
|
+
return [] if dirs.empty?
|
|
128
|
+
|
|
129
|
+
signature = Signature.new(dirs)
|
|
130
|
+
signature.own_modules.flat_map { signature.entries(_1) }.select { signature.own?(_1) }.flat_map do |entry|
|
|
131
|
+
next [] if entry.annotations.include?(ALLOW)
|
|
132
|
+
|
|
133
|
+
loose = entry.types.map(&:to_s).grep(/\buntyped\b/)
|
|
134
|
+
next [] if loose.empty?
|
|
135
|
+
|
|
136
|
+
path, line = (entry.location || "?:1").split(":")
|
|
137
|
+
[Finding.new(path: relative(path.to_s, root), line: line.to_i, rule: "untyped in sig",
|
|
138
|
+
message: "#{entry.id}: #{loose.join(' | ')} (mark it %a{#{ALLOW}} when it is meant)")]
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# @rbs path: String
|
|
143
|
+
# @rbs root: String
|
|
144
|
+
# @rbs return: String
|
|
145
|
+
def self.relative(path, root)
|
|
146
|
+
full = File.expand_path(path, root)
|
|
147
|
+
base = File.expand_path(root)
|
|
148
|
+
full.start_with?("#{base}/") ? full.delete_prefix("#{base}/") : path
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# @rbs found: Array[Finding]
|
|
152
|
+
# @rbs return: String
|
|
153
|
+
def self.report(found)
|
|
154
|
+
return "strict: nothing lets code past its types\n" if found.empty?
|
|
155
|
+
|
|
156
|
+
found.map { "#{_1.path}:#{_1.line} [#{_1.rule}] #{_1.message}\n" }.join
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
module Sigmold
|
|
5
|
+
# Cuts an RBS file down to what callers see, so the rewriting agent does
|
|
6
|
+
# not inherit the original's private shape, and writes its brief.
|
|
7
|
+
module Surface
|
|
8
|
+
# Drops instance variable declarations and everything after a bare
|
|
9
|
+
# `private` line in each class body. A `private def` line goes too.
|
|
10
|
+
#
|
|
11
|
+
# @rbs rbs: String
|
|
12
|
+
# @rbs return: String
|
|
13
|
+
def self.public_only(rbs)
|
|
14
|
+
out = [] #: Array[String]
|
|
15
|
+
hidden = nil #: Integer?
|
|
16
|
+
rbs.each_line do |line|
|
|
17
|
+
indent = line[/\A */].to_s.size
|
|
18
|
+
if hidden
|
|
19
|
+
next unless line.strip == "end" && indent < hidden
|
|
20
|
+
|
|
21
|
+
hidden = nil
|
|
22
|
+
end
|
|
23
|
+
if line.strip == "private"
|
|
24
|
+
hidden = indent
|
|
25
|
+
next
|
|
26
|
+
end
|
|
27
|
+
next if line =~ /\A\s*@\w+: / || line =~ /\A\s*private def /
|
|
28
|
+
|
|
29
|
+
out << line
|
|
30
|
+
end
|
|
31
|
+
out.join.gsub(/\n{3,}/, "\n\n").gsub(/\n\n(\s*end\n)/, "\n\\1").gsub(/^(\s*(?:class|module) [^\n]*\n)\n+/, "\\1")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @rbs file: String
|
|
35
|
+
# @rbs sig: String
|
|
36
|
+
# @rbs into: String
|
|
37
|
+
# @rbs return: String
|
|
38
|
+
def self.brief(file, sig, into)
|
|
39
|
+
<<~BRIEF
|
|
40
|
+
Work only inside #{into}. #{file} was removed. Write it again.
|
|
41
|
+
Read only the public signature #{sig}, the tests, and the other files of lib/.
|
|
42
|
+
Do not read README, CHANGELOG, docs, skills, or anything outside #{into}.
|
|
43
|
+
Do not edit the tests. Do not change what #{sig} declares. Your code's private
|
|
44
|
+
parts, its instance variables, constants, and private methods, may be
|
|
45
|
+
declared in #{sig} after a `private` line at the end of the class or module;
|
|
46
|
+
a private singleton method is written `private def self.name: ...`, because
|
|
47
|
+
RBS keeps `private` for instance methods. Write no `untyped` and no
|
|
48
|
+
`steep:ignore`. Run the tests that cover #{file}, and `steep check` when there
|
|
49
|
+
is a Steepfile.
|
|
50
|
+
Report which behavior the signature and tests left open, and what you chose.
|
|
51
|
+
BRIEF
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require_relative "signature"
|
|
5
|
+
|
|
6
|
+
module Sigmold
|
|
7
|
+
# Compares the public methods a loaded namespace has with the public
|
|
8
|
+
# methods its signatures declare, so a reader of sig/ sees every entry
|
|
9
|
+
# point and only those. A method counts on the Ruby side only when its
|
|
10
|
+
# source is under +lib+, which leaves out what Struct and Data define.
|
|
11
|
+
module SurfaceCheck
|
|
12
|
+
Gap = Data.define(
|
|
13
|
+
:method, #: String
|
|
14
|
+
:where #: :only_in_lib | :only_in_sig
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
# @rbs namespace: Module
|
|
18
|
+
# @rbs sig: Array[String]
|
|
19
|
+
# @rbs lib: String
|
|
20
|
+
# @rbs return: Array[Gap]
|
|
21
|
+
def self.gaps(namespace, sig:, lib:)
|
|
22
|
+
signature = Signature.new(sig)
|
|
23
|
+
declared = signature.surface(namespace.name.to_s).group_by(&:owner)
|
|
24
|
+
lib = File.expand_path(lib)
|
|
25
|
+
modules(namespace).flat_map do |mod|
|
|
26
|
+
owner = mod.name.to_s
|
|
27
|
+
next [Gap.new(method: owner, where: :only_in_lib)] unless signature.declared?(owner)
|
|
28
|
+
|
|
29
|
+
mine = declared.fetch(owner, [])
|
|
30
|
+
sides = [["#", mod, mine.reject(&:singleton)], [".", mod.singleton_class, mine.select(&:singleton)]] #: Array[[String, Module, Array[Signature::Entry]]]
|
|
31
|
+
sides.flat_map do |mark, side, entries|
|
|
32
|
+
ruby = side.public_instance_methods(false).select { (side.instance_method(_1).source_location&.first || "").start_with?(lib) }
|
|
33
|
+
names = entries.map(&:name)
|
|
34
|
+
hidden = names.select { side.private_method_defined?(_1) || side.protected_method_defined?(_1) }
|
|
35
|
+
(ruby - names - [:initialize]).map { Gap.new(method: "#{owner}#{mark}#{_1}", where: :only_in_lib) } +
|
|
36
|
+
hidden.map { Gap.new(method: "#{owner}#{mark}#{_1}", where: :only_in_sig) }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @rbs gaps: Array[Gap]
|
|
42
|
+
# @rbs return: String
|
|
43
|
+
def self.report(gaps)
|
|
44
|
+
return "surface: sig/ shows every public method and only those\n" if gaps.empty?
|
|
45
|
+
|
|
46
|
+
gaps.map { "#{_1.method}: #{_1.where == :only_in_lib ? 'public in lib/, not in sig/' : 'public in sig/, private in lib/'}\n" }.join
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @rbs mod: Module
|
|
50
|
+
# @rbs return: Array[Module]
|
|
51
|
+
def self.modules(mod, seen = Set.new) #: Array[Module]
|
|
52
|
+
return [] if seen.include?(mod)
|
|
53
|
+
|
|
54
|
+
seen << mod
|
|
55
|
+
inner = mod.constants(false).filter_map { mod.const_get(_1) rescue nil }.grep(Module) #: Array[Module]
|
|
56
|
+
[mod, *inner.select { _1.name&.start_with?("#{mod.name}::") }.flat_map { modules(_1, seen) }]
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|