skyward-core 0.3.0 → 0.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 707a5a84c350a7f1277035bfa2f2671a7c8297f5bf620b5c8a34b5d4167a3005
4
- data.tar.gz: 42857fbc309d41e5dcf1d68be4962766f6022cd8f1429d41e46d763ce05eae58
3
+ metadata.gz: 90a38b5373fe97c93e3ce56ace2d25fd96953ab9ab5bfa7b0c412b275d279ee3
4
+ data.tar.gz: 4731d96668ceb7b7263805cac269d3991d5c8c9d1d42aa63c51b7fc036c1608b
5
5
  SHA512:
6
- metadata.gz: a4416139d3ffbd9649cc53e7a6040f865c61af299364b81739662e8356bfdd9fbebbb95cc9a76410d10973046c38eb30afb8047d153959626ddabc49c29839e4
7
- data.tar.gz: e4005e6ceccb54ba121a36c526fe653a866c2129d006f28fcf18ad13109e4ed8e4ba2a377a5d6ddd6f19a1d91b63ca25f8abcb0b979e199d1fc8dbbae30550c1
6
+ metadata.gz: ec5d6f85473cb330e80af6f3ca4766afd0f529181558916aa4f13ee644759aaae8c363b03bc62f19e9ad42b7ae73c6771020917d8bc69282950d62b679a1e155
7
+ data.tar.gz: 4b1b885bac1b45e2b36d0a89f28527e2753c86e214391221a59be20f7e03eae9f4434b274f2e45c8fc2059e4523a8d734115f7963a71d9df7c0817f2e1091c4b
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jake Bottrall
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -9,17 +9,18 @@ The plumbing that [Skyward API](../skyward-api/README.md) and [Skyward ORM](../s
9
9
 
10
10
  - **The `Skyward` namespace.** Skyward Core opens it, and every Skyward gem's Zeitwerk loader extends it. Each gem's names live in that gem's own namespace there: `Skyward::Core`, `Skyward::API` and `Skyward::ORM`.
11
11
  - **The `skyward` command.** It comes with Skyward Core, so it's there whichever Skyward gems you install. Each gem adds its own commands to it.
12
- - **Loading RBS signatures.** One loader finds your app's signatures and those of the gems it uses, for every Skyward gem.
12
+ - **Loading RBS signatures.** One loader finds your app's signatures and those of the gems it uses, for every Skyward gem. It reads them when your app boots, every boot, from the `sig/` you deployed, and loads each signatures directory once, for Skyward API and Skyward ORM to share. So `rbs` is a runtime dependency and your app ships its `sig/`: production doesn't boot from types compiled at deploy time. A process pays for the load once, and what boots is what you deployed.
13
13
  - **Keeping `sig/generated` current.** One function regenerates your app's `sig/generated` from its Ruby with rbs-inline, and the Skyward commands that read types call it first, so you never run rbs-inline by hand. `skyward check` only checks it's current, for CI.
14
14
  - **Reading RBS types.** One reader turns a signature into values that Skyward API and Skyward ORM each build their own mappings on, so a `Todo` read from the database and a `Todo` sent as JSON are the same thing.
15
15
  - **`Skyward::Core::BootError`**, which every Skyward gem raises when your app can't boot, saying why.
16
+ - **Pointing a boot error at its source.** A boot error about your Ruby or RBS shows an excerpt of the file, with a `path:line` header and carets under the problem, and a "Did you mean" suggestion for a misspelt name, rendered the same way by every Skyward gem.
16
17
 
17
18
  ## Adding commands
18
19
 
19
20
  The `skyward` executable finds each installed gem's `lib/skyward/<gem>/commands.rb` with `Gem.find_files` and loads it, and that file registers the gem's commands:
20
21
 
21
22
  ```ruby
22
- Skyward::Core::CLI.register("routes", "List every route and the method that serves it") do |args|
23
+ Skyward::Core::CLI.register('routes', 'List every route and the method that serves it') do |args|
23
24
  # ...
24
25
  end
25
26
  ```
@@ -35,7 +36,8 @@ Skyward supports every Ruby that isn't end-of-life (3.3 and later, as of Novembe
35
36
  This README is the spec, and this section lists each part of it that's built, as it lands. The gem opens the `Skyward` namespace and sets up its loader.
36
37
 
37
38
  - **The `skyward` command.** The executable loads each installed gem's `commands.rb`, `Skyward::Core::CLI.register` adds a command, and `skyward` lists them and runs one, as [Adding commands](#adding-commands) describes. For the commands to run other gems' executables, `Skyward::Core::CLI::Executable.find(name, feature: name)` returns the path of gem `name`'s executable `name` when `feature` can be required in the bundle `skyward` runs in, or `nil` otherwise, and `Skyward::Core::CLI::Executable::CAPTURE` runs a command with an environment in a directory and returns its output, errors included, and whether it succeeded.
38
- - **Loading RBS signatures.** `Skyward::Core::Signatures.load(directory)` returns an `RBS::DefinitionBuilder` over every `.rbs` file under `directory`, the `sig/` of each Skyward gem that has registered it with `Skyward::Core::Signatures.register(path)`, and the `date` and `time` libraries. Skyward Core registers its own. A directory that doesn't exist contributes nothing. A file that doesn't parse, a name declared twice, or a class whose declarations disagree on its type parameters raises `Skyward::Core::BootError` naming the file and line.
39
+ - **Loading RBS signatures.** `Skyward::Core::Signatures.load(directory)` returns an `RBS::DefinitionBuilder` over every `.rbs` file under `directory`, the `sig/` of each Skyward gem that has registered it with `Skyward::Core::Signatures.register(path)`, and the `date`, `time` and `bigdecimal` libraries. Skyward Core registers its own. A load of signatures that haven't changed, the same directory however it's spelled, the same registered gems' `sig/` and every `.rbs` file under them with the same path and contents, returns the builder still in use, so Skyward API and Skyward ORM share one environment. Anything else loads them again. The loader keeps only the builder it last returned, and weakly, so it's shared while a reader uses it and collected once none does, and then loads again. A directory that doesn't exist contributes nothing. A file that doesn't parse, a name declared twice, or a class whose declarations disagree on its type parameters raises `Skyward::Core::BootError` with an excerpt: carets under the token that doesn't parse (`Syntax error: <RBS's message>.`), under each declaration's name (`Todo is declared twice.`), or under the name of the declaration RBS names. A load that fails keeps nothing, so every load raises it until the signatures change.
39
40
  - **Keeping `sig/generated` current.** `Skyward::Core::Signatures::Inline.generate(directory, rbs_inline:, capture:)` runs the rbs-inline executable at `rbs_inline` through `capture`, in `directory` and in its default opt-in mode, which reads only files marked `# rbs_inline: enabled`, over every `.rb` file there outside `test/`, `spec/`, `vendor/`, `tmp/` and hidden directories, into a scratch directory, as `skyward check` does. It then writes each file whose content differs to `directory`'s `sig/generated`, leaving the others untouched, removes each file there that rbs-inline no longer generates, and any directory that leaves empty, and returns `nil`. When rbs-inline fails, it changes nothing and returns a message with rbs-inline's output, for the command to report. With no `rbs_inline`, as in a bundle without rbs-inline, it runs nothing and returns `nil`, so the app runs with `sig/generated` as committed.
40
- - **Reading RBS types.** `Skyward::Core::Types::Reader.new(builder)` takes the `RBS::DefinitionBuilder` that `Skyward::Core::Signatures.load` returns, and `read(type)` turns an RBS type into a frozen `Data` value from `Skyward::Core::Types`: a `Scalar` (`:integer`, `:float`, `:string`, `:bool`, `:date`, `:time` or `:untyped`), a `Literal` (a String, Integer, boolean or `nil`), an `Enum` of Symbols, a `Nullable`, an `ArrayOf`, a `HashOf` (a `Hash[String, T]`, of its values' type), a `Record` or `Named` class with `Field`s (a name, a type and whether it's required), or a `Union`. It expands aliases, generic ones included, reads `T?` and `T | nil` as `Nullable` and a union of symbol literals as one `Enum`, and flattens nested unions, dropping duplicates. A class is `Named` when its RBS declares public instance `attr_reader`s, rbs-inline's `Data.define` output included: its fields come from its own declarations, in written order, across every file that reopens it, skipping `private` sections. A type it can't read, such as an interface, a tuple, a proc, a `Hash` whose keys aren't `String`s or a class with no readers, comes back as an `Unreadable` naming the type, the class and reader it was found at (`Todo#tags`) and what to use instead, for the caller to raise `Skyward::Core::BootError` with its own context. A class with no loaded Ruby class, or a type that refers to itself, raises `Skyward::Core::BootError`. `type_name(klass)` finds a Ruby class's RBS declaration (none for an anonymous class), and `method_type(type_name, method)` a method's signature, raising `Skyward::Core::BootError` when RBS can't build the class.
41
+ - **Reading RBS types.** `Skyward::Core::Types::Reader.new(builder, refinements: {})` takes the `RBS::DefinitionBuilder` that `Skyward::Core::Signatures.load` returns, and `read(type)` turns an RBS type into a frozen `Data` value from `Skyward::Core::Types`: a `Scalar` (`:integer`, `:float`, `:string`, `:bool`, `:date`, `:time`, `:decimal` (a `BigDecimal`) or `:untyped`), a `Literal` (a String, Integer, boolean or `nil`), an `Enum` of Symbols, a `Nullable`, an `ArrayOf`, a `HashOf` (a `Hash[String, T]`, of its values' type), a `Record` or `Named` class with `Field`s (a name, a type and whether it's required), a `Union`, or a `Refined`. It expands aliases, generic ones included, reads `T?` and `T | nil` as `Nullable` and a union of symbol literals as one `Enum`, and flattens nested unions, dropping duplicates. `refinements:` maps an alias's full name (`"::Skyward::ORM::uuid"`) to a Symbol (`:uuid`), so a gem can tell which alias a type was written with: an alias in it reads as a `Refined` of what the alias expands to (`type`) and that Symbol (`as`), wherever it's reached, directly, through another alias of it, or inside a `T?` (a `Nullable` of the `Refined`), an `Array[T]` or a record. Every other alias, and every alias when `refinements:` is left out, expands to what it names and keeps nothing of it. A class is `Named` when its RBS declares public instance `attr_reader`s, rbs-inline's `Data.define` output included: its fields come from its own declarations, in written order, across every file that reopens it, skipping `private` sections. A type it can't read, such as an interface, a tuple, a proc, a `Hash` whose keys aren't `String`s or a class with no readers, comes back as an `Unreadable` naming the type, the class and reader it was found at (`Todo#tags`), what to use instead, and its `suggestion`: for a class name no loaded signature declares (`attr_reader list: Lisst`), the closest declared class name, and otherwise `nil`, for the caller to raise `Skyward::Core::BootError` with its own context. A class with no loaded Ruby class raises `Skyward::Core::BootError` with an excerpt of its declaration, carets under its name, and a type that refers to itself raises it naming the cycle. `type_name(klass)` finds a Ruby class's RBS declaration (none for an anonymous class), and `method_type(type_name, method)` a method's signature, raising `Skyward::Core::BootError` when RBS can't build the class. When RBS's error has a location, the message is RBS's without it, plus an excerpt, and a type name RBS can't find adds `Did you mean ...?` with the closest declared name of the same kind.
41
42
  - **`Skyward::Core::BootError`**, for any Skyward gem to raise when an app can't boot.
43
+ - **Pointing a boot error at its source.** A `Skyward::Core::Diagnostics::Spot` is a file, a line, that line's text, and optionally a range of character columns to put carets under. `Skyward::Core::Diagnostics::Spots.rbs(location)` makes one from an `RBS::Location`, from the buffer RBS keeps in memory. The rest read a Ruby file through Prism, and return `nil` when it can't be read or nothing on the line matches, as when it changed after it loaded. Given a `Method`, `keyword_parameter(method, name)`, `parameters(method)` and `method_name(method)` find its `def` and return a spot under one keyword parameter, the whole parameter list, or its name. Given a file and line, as from `caller_locations`, `keyword_key(path, line, call:, key:)` and `keyword_value(path, line, call:, key:)` find the call named `call` that starts there and return a spot under a keyword argument's key (`lsit_id:`) or value (`:show`), `symbol(path, line, call:, value:)` under a Symbol argument, and `string(path, line, call:, part:)` under a part of a String argument (`:id` in `"/:id"`). `reader_type(path, line)` returns a spot under the type in an `attr_reader`'s `#:` comment. `Skyward::Core::Diagnostics::Excerpt.render(spots)` renders spots, in order, as a block indented four spaces for a message to include, or `nil` with no spot. Each file's spots make a block starting with a `path:line` header naming the first line that has carets, else the first line, with the path relative to the current directory when it's inside it. Its lines follow in line order, with their common leading whitespace removed, `...` between two lines that aren't adjacent, and a line of `^` under each spot's columns. `Skyward::Core::Diagnostics::Suggestion.closest(name, names)` returns the closest of `names` to `name` from Ruby's `DidYouMean::SpellChecker`, or `nil` when none is close, and requires `did_you_mean` itself, so it works under `--disable-did_you_mean`.
data/exe/skyward CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "skyward/core"
4
+ require 'skyward/core'
5
5
 
6
- Skyward::Core::CLI.first_per_gem(Gem.find_files("skyward/*/commands.rb")).each { |file| load file }
6
+ Skyward::Core::CLI.first_per_gem(Gem.find_files('skyward/*/commands.rb')).each { |file| load file }
7
7
  exit Skyward::Core::CLI.run(ARGV, registry: Skyward::Core::CLI::REGISTRY, out: $stdout)
@@ -19,7 +19,7 @@ module Skyward::Core::CLI
19
19
  def run(argv, registry:, out:)
20
20
  name, *args = argv
21
21
  case name
22
- when nil, "help", "--help", "-h"
22
+ when nil, 'help', '--help', '-h'
23
23
  out.write(help(registry))
24
24
  0
25
25
  else
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skyward::Core::Diagnostics::Excerpt
4
+ extend self
5
+
6
+ INDENT = ' ' #: String
7
+
8
+ # @rbs spots: Array[Skyward::Core::Diagnostics::Spot]
9
+ # @rbs directory: String
10
+ # @rbs return: String?
11
+ def render(spots, directory: Dir.pwd)
12
+ return if spots.empty?
13
+
14
+ spots.group_by(&:path).map { |path, in_file| block(relative(path, directory), in_file) }.join("\n\n")
15
+ end
16
+
17
+ private
18
+
19
+ # @rbs path: String
20
+ # @rbs spots: Array[Skyward::Core::Diagnostics::Spot]
21
+ # @rbs return: String
22
+ def block(path, spots)
23
+ lines = spots.group_by(&:line).sort_by(&:first)
24
+ texts = lines.map { |(_, on_line)| on_line.fetch(0).text }
25
+ indent = texts.reject { |text| text.strip.empty? }.map { |text| text[/\A\s*/].to_s.size }.min || 0
26
+ header = lines.find { |(_, on_line)| on_line.any?(&:columns) } || lines.fetch(0)
27
+ body = lines.each_with_index.flat_map do |(line, on_line), index|
28
+ gap = '...' if index.positive? && lines.fetch(index - 1).first != line - 1
29
+ [gap, texts.fetch(index)[indent..].to_s, *carets(on_line, indent)].compact
30
+ end
31
+ ["#{path}:#{header.first}", *body].map { |row| "#{INDENT}#{row}".rstrip }.join("\n")
32
+ end
33
+
34
+ # @rbs spots: Array[Skyward::Core::Diagnostics::Spot]
35
+ # @rbs indent: Integer
36
+ # @rbs return: Array[String]
37
+ def carets(spots, indent)
38
+ columns = spots.flat_map { |spot| spot.columns.to_a }.map { |column| column - indent }
39
+ return [] if columns.empty?
40
+
41
+ [(0..columns.max.to_i).map { |column| columns.include?(column) ? '^' : ' ' }.join]
42
+ end
43
+
44
+ # @rbs path: String
45
+ # @rbs directory: String
46
+ # @rbs return: String
47
+ def relative(path, directory)
48
+ prefix = File.join(File.expand_path(directory), '')
49
+ expanded = File.expand_path(path, directory)
50
+ expanded.start_with?(prefix) ? expanded.delete_prefix(prefix) : path
51
+ end
52
+ end
@@ -0,0 +1,177 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'prism'
4
+
5
+ module Skyward::Core::Diagnostics::Spots
6
+ extend self
7
+
8
+ # @rbs location: RBS::Location[untyped, untyped]
9
+ # @rbs return: Skyward::Core::Diagnostics::Spot
10
+ def rbs(location)
11
+ line = location.start_line
12
+ text = location.buffer.lines.fetch(line - 1, '')
13
+ finish = location.end_line == line ? location.end_column : text.size
14
+ spot(location.buffer.name.to_s, line, text, location.start_column...finish)
15
+ end
16
+
17
+ # @rbs method: Method | UnboundMethod
18
+ # @rbs name: Symbol
19
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
20
+ def keyword_parameter(method, name)
21
+ definition(method) { |node| node.parameters&.keywords&.find { |keyword| keyword.name == name }&.location }
22
+ end
23
+
24
+ # @rbs method: Method | UnboundMethod
25
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
26
+ def parameters(method) = definition(method) { |node| node.parameters&.location }
27
+
28
+ # @rbs method: Method | UnboundMethod
29
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
30
+ def method_name(method) = definition(method, &:name_loc)
31
+
32
+ # @rbs path: String
33
+ # @rbs line: Integer
34
+ # @rbs call: Symbol
35
+ # @rbs key: Symbol
36
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
37
+ def keyword_key(path, line, call:, key:)
38
+ arguments(path, line, call) { |found| pair(found, key)&.key&.location }
39
+ end
40
+
41
+ # @rbs path: String
42
+ # @rbs line: Integer
43
+ # @rbs call: Symbol
44
+ # @rbs key: Symbol
45
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
46
+ def keyword_value(path, line, call:, key:)
47
+ arguments(path, line, call) { |found| pair(found, key)&.value&.location }
48
+ end
49
+
50
+ # @rbs path: String
51
+ # @rbs line: Integer
52
+ # @rbs call: Symbol
53
+ # @rbs value: Symbol
54
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
55
+ def symbol(path, line, call:, value:)
56
+ arguments(path, line, call) do |found|
57
+ found.find { |argument| symbol?(argument, value) }&.location
58
+ end
59
+ end
60
+
61
+ # @rbs path: String
62
+ # @rbs line: Integer
63
+ # @rbs call: Symbol
64
+ # @rbs part: String
65
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
66
+ def string(path, line, call:, part:)
67
+ arguments(path, line, call) do |found|
68
+ found.filter_map { |argument| within(argument.content_loc, part) if argument.is_a?(Prism::StringNode) }.first
69
+ end
70
+ end
71
+
72
+ # @rbs path: String
73
+ # @rbs line: Integer
74
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
75
+ def reader_type(path, line)
76
+ parse(path) do |result|
77
+ next unless call(result, line, :attr_reader)
78
+
79
+ comment = result.comments.find do |found|
80
+ found.location.start_line == line && found.location.slice.start_with?('#:')
81
+ end
82
+ within(comment.location, comment.location.slice.delete_prefix('#:').strip) if comment
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ # @rbs path: String
89
+ # @rbs line: Integer
90
+ # @rbs text: String
91
+ # @rbs columns: Range[Integer]
92
+ # @rbs return: Skyward::Core::Diagnostics::Spot
93
+ def spot(path, line, text, columns)
94
+ # A zero-width location, such as the end of a file, still gets a caret.
95
+ finish = [columns.end, columns.begin + 1].max
96
+ Skyward::Core::Diagnostics::Spot.new(path:, line:, text:, columns: columns.begin...finish)
97
+ end
98
+
99
+ # @rbs path: String
100
+ # @rbs &: (Prism::ParseResult) -> Prism::Location?
101
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
102
+ def parse(path)
103
+ return unless File.file?(path)
104
+
105
+ source = File.read(path)
106
+ location = yield Prism.parse(source, filepath: path)
107
+ return unless location
108
+
109
+ line = location.start_line
110
+ text = source.lines.fetch(line - 1, '').chomp
111
+ finish = location.end_line == line ? location.end_character_column : text.size
112
+ spot(path, line, text, location.start_character_column...finish)
113
+ end
114
+
115
+ # @rbs method: Method | UnboundMethod
116
+ # @rbs &: (Prism::DefNode) -> Prism::Location?
117
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
118
+ def definition(method)
119
+ path, line = method.source_location
120
+ return unless path && line
121
+
122
+ parse(path) do |result|
123
+ found = result.value.breadth_first_search do |node|
124
+ node.is_a?(Prism::DefNode) && node.name == method.original_name && node.location.start_line == line
125
+ end
126
+ yield found if found.is_a?(Prism::DefNode)
127
+ end
128
+ end
129
+
130
+ # @rbs path: String
131
+ # @rbs line: Integer
132
+ # @rbs name: Symbol
133
+ # @rbs &: (Array[Prism::node]) -> Prism::Location?
134
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
135
+ def arguments(path, line, name)
136
+ parse(path) do |result|
137
+ found = call(result, line, name)
138
+ yield found.arguments&.arguments || [] if found
139
+ end
140
+ end
141
+
142
+ # @rbs result: Prism::ParseResult
143
+ # @rbs line: Integer
144
+ # @rbs name: Symbol
145
+ # @rbs return: Prism::CallNode?
146
+ def call(result, line, name)
147
+ found = result.value.breadth_first_search do |node|
148
+ node.is_a?(Prism::CallNode) && node.name == name && node.location.start_line == line
149
+ end
150
+ found if found.is_a?(Prism::CallNode)
151
+ end
152
+
153
+ # @rbs arguments: Array[Prism::node]
154
+ # @rbs key: Symbol
155
+ # @rbs return: Prism::AssocNode?
156
+ def pair(arguments, key)
157
+ keywords = arguments.find { |argument| argument.is_a?(Prism::KeywordHashNode) }
158
+ return unless keywords.is_a?(Prism::KeywordHashNode)
159
+
160
+ found = keywords.elements.find { |element| element.is_a?(Prism::AssocNode) && symbol?(element.key, key) }
161
+ found if found.is_a?(Prism::AssocNode)
162
+ end
163
+
164
+ # @rbs node: Prism::node
165
+ # @rbs value: Symbol
166
+ # @rbs return: bool
167
+ def symbol?(node, value) = node.is_a?(Prism::SymbolNode) && node.unescaped == value.to_s
168
+
169
+ # Prism's locations count bytes, so a part is found by its byte offset.
170
+ # @rbs location: Prism::Location
171
+ # @rbs part: String
172
+ # @rbs return: Prism::Location?
173
+ def within(location, part)
174
+ offset = location.slice.byteindex(part)
175
+ location.copy(start_offset: location.start_offset + offset, length: part.bytesize) if offset
176
+ end
177
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ruby run with --disable-did_you_mean doesn't load it.
4
+ require 'did_you_mean'
5
+
6
+ module Skyward::Core::Diagnostics::Suggestion
7
+ extend self
8
+
9
+ # @rbs name: String
10
+ # @rbs names: Array[String]
11
+ # @rbs return: String?
12
+ def closest(name, names) = DidYouMean::SpellChecker.new(dictionary: names).correct(name).first
13
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skyward::Core::Diagnostics
4
+ # The line's text travels with it, so an excerpt never reads the file again.
5
+ Spot = Data.define(
6
+ :path, #: String
7
+ :line, #: Integer
8
+ :text, #: String
9
+ :columns #: Range[Integer]?
10
+ )
11
+ end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "fileutils"
4
- require "tmpdir"
3
+ require 'fileutils'
4
+ require 'tmpdir'
5
5
 
6
6
  module Skyward::Core::Signatures::Inline
7
7
  extend self
8
8
 
9
- OUTPUT = "sig/generated" #: String
9
+ OUTPUT = 'sig/generated' #: String
10
10
 
11
11
  # None holds the app's contract: its tests, its installed gems and scratch.
12
12
  UNCHECKED = %w[test spec vendor tmp].freeze #: Array[String]
@@ -35,7 +35,7 @@ module Skyward::Core::Signatures::Inline
35
35
  # @rbs capture: Skyward::Core::CLI::Executable::capture
36
36
  # @rbs return: Hash[String, String] | String
37
37
  def run(directory, rbs_inline:, capture:)
38
- Dir.mktmpdir("skyward-signatures") do |output|
38
+ Dir.mktmpdir('skyward-signatures') do |output|
39
39
  log, ok = capture.call({}, [Gem.ruby, rbs_inline, "--output=#{output}", *sources(directory)], directory)
40
40
  ok ? rbs_files(output) : log
41
41
  end
@@ -44,15 +44,15 @@ module Skyward::Core::Signatures::Inline
44
44
  # @rbs directory: String
45
45
  # @rbs return: Array[String]
46
46
  def sources(directory)
47
- roots = Dir.glob("*/", base: directory).map { |root| root.delete_suffix("/") } - UNCHECKED
48
- files = Dir.glob("*.rb", base: directory)
47
+ roots = Dir.glob('*/', base: directory).map { |root| root.delete_suffix('/') } - UNCHECKED
48
+ files = Dir.glob('*.rb', base: directory)
49
49
  [*files, *roots.reject { |root| Dir.glob("#{root}/**/*.rb", base: directory).empty? }].sort
50
50
  end
51
51
 
52
52
  # @rbs directory: String
53
53
  # @rbs return: Hash[String, String]
54
54
  def rbs_files(directory)
55
- Dir.glob("**/*.rbs", base: directory).to_h { |file| [file, File.read(File.join(directory, file))] }
55
+ Dir.glob('**/*.rbs', base: directory).to_h { |file| [file, File.read(File.join(directory, file))] }
56
56
  end
57
57
 
58
58
  private
@@ -69,7 +69,7 @@ module Skyward::Core::Signatures::Inline
69
69
  end
70
70
  (current.keys - files.keys).each { |file| File.delete(File.join(generated, file)) }
71
71
  # Deepest first, so a directory emptied of directories goes too.
72
- Dir.glob("**/*/", base: generated).sort.reverse_each do |dir|
72
+ Dir.glob('**/*/', base: generated).sort.reverse_each do |dir|
73
73
  path = File.join(generated, dir)
74
74
  Dir.rmdir(path) if Dir.empty?(path)
75
75
  end
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rbs"
3
+ require 'digest'
4
+ require 'rbs'
5
+ require 'weakref'
4
6
 
5
7
  class Skyward::Core::Signatures::Loader
6
8
  # @rbs @gems: Array[String]
9
+ # @rbs @key: Hash[String, Hash[String, String]]?
10
+ # @rbs @builder: WeakRef?
7
11
 
8
12
  # @rbs return: void
9
13
  def initialize
@@ -19,17 +23,57 @@ class Skyward::Core::Signatures::Loader
19
23
  # @rbs directory: String
20
24
  # @rbs return: RBS::DefinitionBuilder
21
25
  def load(directory)
26
+ key = contents(directory)
27
+ kept = @builder
28
+ return kept.__getobj__ if key == @key && kept&.weakref_alive?
29
+
22
30
  loader = RBS::EnvironmentLoader.new
23
31
  [directory, *@gems].each { |path| loader.add(path: Pathname(path)) }
24
- %w[date time].each { |library| loader.add(library:, version: nil) }
32
+ %w[date time bigdecimal].each { |library| loader.add(library:, version: nil) }
25
33
  env = RBS::Environment.from_loader(loader).resolve_type_names
26
34
  # RBS otherwise defers this conflict until a definition is built, past boot.
27
35
  env.validate_type_params
28
- RBS::DefinitionBuilder.new(env:)
36
+ builder = RBS::DefinitionBuilder.new(env:)
37
+ @key = key
38
+ @builder = WeakRef.new(builder)
39
+ builder
40
+ rescue RBS::ParsingError => e
41
+ raise Skyward::Core::BootError, failure("Syntax error: #{e.error_message}.", [e.location])
29
42
  rescue RBS::DuplicatedDeclarationError => e
30
- locations = e.decls.map { |decl| RBS::Location.to_string(decl.location) }
31
- raise Skyward::Core::BootError, "Can't load the RBS signatures: #{e.name} is declared at #{locations.join(" and ")}"
32
- rescue RBS::ParsingError, RBS::GenericParameterMismatchError => e
33
- raise Skyward::Core::BootError, "Can't load the RBS signatures: #{e.message}"
43
+ names = e.decls.filter_map { |decl| name_location(decl) }
44
+ raise Skyward::Core::BootError, failure("#{e.name.to_s.delete_prefix('::')} is declared twice.", names)
45
+ rescue RBS::GenericParameterMismatchError => e
46
+ location = e.decl.location
47
+ message = e.message.delete_prefix("#{RBS::Location.to_string(location)}: ")
48
+ raise Skyward::Core::BootError, failure(message, [location&.[](:name)].compact)
49
+ end
50
+
51
+ private
52
+
53
+ # @rbs directory: String
54
+ # @rbs return: Hash[String, Hash[String, String]]
55
+ def contents(directory)
56
+ [File.expand_path(directory), *@gems].to_h do |root|
57
+ files = RBS::FileFinder.each_file(Pathname(root), skip_hidden: false).map(&:to_s)
58
+ # By contents, since a file rewritten within the same tick keeps its modification time.
59
+ [root, files.to_h { |file| [file, Digest::SHA256.file(file).hexdigest] }]
60
+ end
61
+ end
62
+
63
+ # @rbs decl: RBS::DuplicatedDeclarationError::declaration
64
+ # @rbs return: RBS::Location[untyped, untyped]?
65
+ def name_location(decl)
66
+ case decl
67
+ when RBS::AST::Declarations::ClassAlias, RBS::AST::Declarations::ModuleAlias then decl.location&.[](:new_name)
68
+ else decl.location&.[](:name)
69
+ end
70
+ end
71
+
72
+ # @rbs message: String
73
+ # @rbs locations: Array[RBS::Location[untyped, untyped]]
74
+ # @rbs return: String
75
+ def failure(message, locations)
76
+ spots = locations.map { |location| Skyward::Core::Diagnostics::Spots.rbs(location) }
77
+ ["Can't load the RBS signatures: #{message}", Skyward::Core::Diagnostics::Excerpt.render(spots)].compact.join("\n\n")
34
78
  end
35
79
  end
@@ -2,21 +2,25 @@
2
2
 
3
3
  class Skyward::Core::Types::Reader
4
4
  # @rbs @builder: RBS::DefinitionBuilder
5
+ # @rbs @refinements: Hash[String, Symbol]
5
6
 
6
7
  SCALARS = {
7
- "::Integer" => :integer,
8
- "::Float" => :float,
9
- "::String" => :string,
10
- "::Date" => :date,
11
- "::Time" => :time
8
+ '::Integer' => :integer,
9
+ '::Float' => :float,
10
+ '::String' => :string,
11
+ '::Date' => :date,
12
+ '::Time' => :time,
13
+ '::BigDecimal' => :decimal
12
14
  }.freeze #: Hash[String, Skyward::Core::Types::scalar]
13
15
 
14
16
  NIL_LITERAL = Skyward::Core::Types::Literal.new(value: nil) #: Skyward::Core::Types::Literal
15
17
 
16
18
  # @rbs builder: RBS::DefinitionBuilder
19
+ # @rbs refinements: Hash[String, Symbol]
17
20
  # @rbs return: void
18
- def initialize(builder)
21
+ def initialize(builder, refinements: {})
19
22
  @builder = builder
23
+ @refinements = refinements
20
24
  end
21
25
 
22
26
  # @rbs type: RBS::Types::t
@@ -39,11 +43,45 @@ class Skyward::Core::Types::Reader
39
43
  def method_type(type_name, method_name)
40
44
  @builder.build_instance(type_name).methods[method_name]&.method_types&.first
41
45
  rescue RBS::DefinitionError => e
42
- raise Skyward::Core::BootError, "Can't read the RBS signatures: #{e.message}"
46
+ raise Skyward::Core::BootError, definition_failure(e)
43
47
  end
44
48
 
45
49
  private
46
50
 
51
+ # @rbs error: RBS::DefinitionError
52
+ # @rbs return: String
53
+ def definition_failure(error)
54
+ # Not every RBS error has a location, and those that do share no ancestor.
55
+ location = error.location if error.respond_to?(:location) # steep:ignore NoMethod
56
+ case location
57
+ when RBS::Location
58
+ message = error.message.delete_prefix("#{RBS::Location.to_string(location)}: ")
59
+ suggestion = closest(error.type_name) if error.is_a?(RBS::NoTypeFoundError)
60
+ explain("Can't read the RBS signatures: #{message}", location, suggestion)
61
+ else "Can't read the RBS signatures: #{error.message}"
62
+ end
63
+ end
64
+
65
+ # @rbs message: String
66
+ # @rbs location: RBS::Location[untyped, untyped]?
67
+ # @rbs suggestion: String?
68
+ # @rbs return: String
69
+ def explain(message, location, suggestion = nil)
70
+ spots = [location].compact.map { |found| Skyward::Core::Diagnostics::Spots.rbs(found) }
71
+ hint = "Did you mean #{suggestion}?" if suggestion
72
+ [[message, Skyward::Core::Diagnostics::Excerpt.render(spots)].compact.join("\n\n"), hint].compact.join("\n")
73
+ end
74
+
75
+ # Only a declared name of the same kind, as `String` and not `string` for `Strng`.
76
+ # @rbs type_name: RBS::TypeName
77
+ # @rbs return: String?
78
+ def closest(type_name)
79
+ env = @builder.env
80
+ names = [*env.class_decls.keys, *env.class_alias_decls.keys, *env.interface_decls.keys, *env.type_alias_decls.keys]
81
+ declared = names.select { |name| name.kind == type_name.kind }.map { |name| written(name) }
82
+ Skyward::Core::Diagnostics::Suggestion.closest(written(type_name), declared)
83
+ end
84
+
47
85
  # @rbs type: RBS::Types::t
48
86
  # @rbs path: Array[String]
49
87
  # @rbs return: Skyward::Core::Types::result
@@ -60,7 +98,7 @@ class Skyward::Core::Types::Reader
60
98
  when RBS::Types::Alias then alias_type(type, path)
61
99
  when RBS::Types::Record then record(type, path)
62
100
  when RBS::Types::ClassInstance then instance(type, path)
63
- when RBS::Types::Tuple then unreadable(type, "Array[T], or a record")
101
+ when RBS::Types::Tuple then unreadable(type, 'Array[T], or a record')
64
102
  else unreadable(type)
65
103
  end
66
104
  end
@@ -100,7 +138,14 @@ class Skyward::Core::Types::Reader
100
138
  # @rbs path: Array[String]
101
139
  # @rbs return: Skyward::Core::Types::result
102
140
  def alias_type(type, path)
103
- read_within(@builder.expand_alias2(type.name, type.args), enter(written(type), path))
141
+ expanded = read_within(@builder.expand_alias2(type.name, type.args), enter(written(type), path))
142
+ as = @refinements.fetch(type.name.to_s, nil)
143
+ return expanded unless as
144
+
145
+ case expanded
146
+ when Skyward::Core::Types::Unreadable then expanded
147
+ else Skyward::Core::Types::Refined.new(type: expanded, as:)
148
+ end
104
149
  end
105
150
 
106
151
  # @rbs type: RBS::Types::Record
@@ -119,12 +164,12 @@ class Skyward::Core::Types::Reader
119
164
  name = type.name.to_s
120
165
  scalar = SCALARS.fetch(name, nil)
121
166
  if scalar then Skyward::Core::Types::Scalar.new(name: scalar)
122
- elsif name == "::Array" && type.args.size != 1 then unreadable(type, "Array[T]")
123
- elsif name == "::Array"
167
+ elsif name == '::Array' && type.args.size != 1 then unreadable(type, 'Array[T]')
168
+ elsif name == '::Array'
124
169
  all_readable(type.args.map { |arg| read_within(arg, path) }) do |items|
125
170
  Skyward::Core::Types::ArrayOf.new(type: items.fetch(0))
126
171
  end
127
- elsif name == "::Hash" then hash_of(type, path)
172
+ elsif name == '::Hash' then hash_of(type, path)
128
173
  else named(type, path)
129
174
  end
130
175
  end
@@ -135,7 +180,7 @@ class Skyward::Core::Types::Reader
135
180
  def hash_of(type, path)
136
181
  key, *values = type.args.map { |arg| read_within(arg, path) }
137
182
  unless key == Skyward::Core::Types::Scalar.new(name: :string) && values.size == 1
138
- return unreadable(type, "Hash[String, T] (only String keys are supported)")
183
+ return unreadable(type, 'Hash[String, T] (only String keys are supported)')
139
184
  end
140
185
 
141
186
  all_readable(values) { |read| Skyward::Core::Types::HashOf.new(type: read.fetch(0)) }
@@ -146,10 +191,13 @@ class Skyward::Core::Types::Reader
146
191
  # @rbs return: Skyward::Core::Types::result
147
192
  def named(type, path)
148
193
  readers = public_readers(type.name)
149
- return unreadable(type) if readers.empty?
194
+ if readers.empty?
195
+ suggestion = closest(type.name) unless @builder.env.module_name?(type.name)
196
+ return unreadable(type, suggestion:)
197
+ end
150
198
 
151
199
  name = written(type.name)
152
- klass = ruby_class(name)
200
+ klass = ruby_class(type.name)
153
201
  within = enter(name, path)
154
202
  keys = readers.map { |reader| [reader.name, true] } #: Array[[Symbol, bool]]
155
203
  results = readers.map { |reader| locate(read_within(reader.type, within), "#{name}##{reader.name}") }
@@ -186,13 +234,16 @@ class Skyward::Core::Types::Reader
186
234
  end
187
235
  end
188
236
 
189
- # @rbs name: String
237
+ # @rbs type_name: RBS::TypeName
190
238
  # @rbs return: Class
191
- def ruby_class(name)
239
+ def ruby_class(type_name)
240
+ name = written(type_name)
192
241
  found = Object.const_get(name) if Object.const_defined?(name)
193
242
  return found if found.is_a?(Class)
194
243
 
195
- raise Skyward::Core::BootError, "#{name} has an RBS signature, but no Ruby class is loaded with that name"
244
+ message = "#{name} has an RBS signature, but no Ruby class is loaded with that name"
245
+ location = @builder.env.class_entry(type_name, normalized: true)&.primary_decl&.location
246
+ raise Skyward::Core::BootError, explain(message, location&.[](:name))
196
247
  end
197
248
 
198
249
  # @rbs name: String
@@ -202,7 +253,7 @@ class Skyward::Core::Types::Reader
202
253
  return [*path, name] unless path.include?(name)
203
254
 
204
255
  cycle = [*path.drop_while { |entered| entered != name }, name]
205
- raise Skyward::Core::BootError, "Can't read #{name}: it refers to itself through #{cycle.join(" -> ")}"
256
+ raise Skyward::Core::BootError, "Can't read #{name}: it refers to itself through #{cycle.join(' -> ')}"
206
257
  end
207
258
 
208
259
  # @rbs keys: Array[[Symbol, bool]]
@@ -234,11 +285,12 @@ class Skyward::Core::Types::Reader
234
285
 
235
286
  # @rbs type: RBS::Types::t
236
287
  # @rbs instead: String
288
+ # @rbs suggestion: String?
237
289
  # @rbs return: Skyward::Core::Types::Unreadable
238
- def unreadable(type, instead = "a record, or a class with public typed attr_readers")
239
- Skyward::Core::Types::Unreadable.new(type: written(type), at: nil, instead:)
290
+ def unreadable(type, instead = 'a record, or a class with public typed attr_readers', suggestion: nil)
291
+ Skyward::Core::Types::Unreadable.new(type: written(type), at: nil, instead:, suggestion:)
240
292
  end
241
293
 
242
294
  # @rbs (_ToS name) -> String
243
- def written(name) = name.to_s.gsub(/(?<!\w)::/, "")
295
+ def written(name) = name.to_s.gsub(/(?<!\w)::/, '')
244
296
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  module Skyward::Core::Types
4
4
  # @rbs!
5
- # type scalar = :integer | :float | :string | :bool | :date | :time | :untyped
5
+ # type scalar = :integer | :float | :string | :bool | :date | :time | :decimal | :untyped
6
6
  # type literal = String | Integer | bool | nil
7
- # type t = Scalar | Literal | Enum | Nullable | ArrayOf | HashOf | Record | Named | Union
7
+ # type t = Scalar | Literal | Enum | Nullable | ArrayOf | HashOf | Record | Named | Union | Refined
8
8
  # type result = t | Unreadable
9
9
 
10
10
  Scalar = Data.define(
@@ -51,9 +51,15 @@ module Skyward::Core::Types
51
51
  :types #: Array[t]
52
52
  )
53
53
 
54
+ Refined = Data.define(
55
+ :type, #: t
56
+ :as #: Symbol
57
+ )
58
+
54
59
  Unreadable = Data.define(
55
60
  :type, #: String
56
61
  :at, #: String?
57
- :instead #: String
62
+ :instead, #: String
63
+ :suggestion #: String?
58
64
  )
59
65
  end
@@ -4,7 +4,7 @@ module Skyward
4
4
  module Core
5
5
  # x-release-please-start-version
6
6
 
7
- VERSION = "0.3.0" #: String
7
+ VERSION = '0.4.1' #: String
8
8
 
9
9
  # x-release-please-end
10
10
  end
data/lib/skyward/core.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "zeitwerk"
4
- require_relative "core/version"
3
+ require 'zeitwerk'
4
+ require_relative 'core/version'
5
5
 
6
6
  # Skyward Core owns the Skyward namespace: its version.rb opens it, and every
7
7
  # Skyward gem's loader, this one included, extends it.
8
8
  loader = Zeitwerk::Loader.for_gem_extension(Skyward)
9
- loader.inflector.inflect("cli" => "CLI")
9
+ loader.inflector.inflect('cli' => 'CLI')
10
10
  # The skyward executable loads it, and it defines no constant.
11
11
  loader.ignore("#{__dir__}/core/commands.rb")
12
12
  loader.setup
13
13
 
14
14
  dir = __dir__ #: String
15
- Skyward::Core::Signatures.register(File.expand_path("../../sig", dir))
15
+ Skyward::Core::Signatures.register(File.expand_path('../../sig', dir))
@@ -0,0 +1,7 @@
1
+ class WeakRef
2
+ def initialize: (untyped orig) -> void
3
+
4
+ def __getobj__: () -> untyped
5
+
6
+ def weakref_alive?: () -> boolish
7
+ end
@@ -0,0 +1,27 @@
1
+ # Generated from lib/skyward/core/diagnostics/excerpt.rb with RBS::Inline
2
+
3
+ module Skyward::Core::Diagnostics::Excerpt
4
+ INDENT: String
5
+
6
+ # @rbs spots: Array[Skyward::Core::Diagnostics::Spot]
7
+ # @rbs directory: String
8
+ # @rbs return: String?
9
+ def render: (Array[Skyward::Core::Diagnostics::Spot] spots, ?directory: String) -> String?
10
+
11
+ private
12
+
13
+ # @rbs path: String
14
+ # @rbs spots: Array[Skyward::Core::Diagnostics::Spot]
15
+ # @rbs return: String
16
+ def block: (String path, Array[Skyward::Core::Diagnostics::Spot] spots) -> String
17
+
18
+ # @rbs spots: Array[Skyward::Core::Diagnostics::Spot]
19
+ # @rbs indent: Integer
20
+ # @rbs return: Array[String]
21
+ def carets: (Array[Skyward::Core::Diagnostics::Spot] spots, Integer indent) -> Array[String]
22
+
23
+ # @rbs path: String
24
+ # @rbs directory: String
25
+ # @rbs return: String
26
+ def relative: (String path, String directory) -> String
27
+ end
@@ -0,0 +1,101 @@
1
+ # Generated from lib/skyward/core/diagnostics/spots.rb with RBS::Inline
2
+
3
+ module Skyward::Core::Diagnostics::Spots
4
+ # @rbs location: RBS::Location[untyped, untyped]
5
+ # @rbs return: Skyward::Core::Diagnostics::Spot
6
+ def rbs: (RBS::Location[untyped, untyped] location) -> Skyward::Core::Diagnostics::Spot
7
+
8
+ # @rbs method: Method | UnboundMethod
9
+ # @rbs name: Symbol
10
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
11
+ def keyword_parameter: (Method | UnboundMethod method, Symbol name) -> Skyward::Core::Diagnostics::Spot?
12
+
13
+ # @rbs method: Method | UnboundMethod
14
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
15
+ def parameters: (Method | UnboundMethod method) -> Skyward::Core::Diagnostics::Spot?
16
+
17
+ # @rbs method: Method | UnboundMethod
18
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
19
+ def method_name: (Method | UnboundMethod method) -> Skyward::Core::Diagnostics::Spot?
20
+
21
+ # @rbs path: String
22
+ # @rbs line: Integer
23
+ # @rbs call: Symbol
24
+ # @rbs key: Symbol
25
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
26
+ def keyword_key: (String path, Integer line, call: Symbol, key: Symbol) -> Skyward::Core::Diagnostics::Spot?
27
+
28
+ # @rbs path: String
29
+ # @rbs line: Integer
30
+ # @rbs call: Symbol
31
+ # @rbs key: Symbol
32
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
33
+ def keyword_value: (String path, Integer line, call: Symbol, key: Symbol) -> Skyward::Core::Diagnostics::Spot?
34
+
35
+ # @rbs path: String
36
+ # @rbs line: Integer
37
+ # @rbs call: Symbol
38
+ # @rbs value: Symbol
39
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
40
+ def symbol: (String path, Integer line, call: Symbol, value: Symbol) -> Skyward::Core::Diagnostics::Spot?
41
+
42
+ # @rbs path: String
43
+ # @rbs line: Integer
44
+ # @rbs call: Symbol
45
+ # @rbs part: String
46
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
47
+ def string: (String path, Integer line, call: Symbol, part: String) -> Skyward::Core::Diagnostics::Spot?
48
+
49
+ # @rbs path: String
50
+ # @rbs line: Integer
51
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
52
+ def reader_type: (String path, Integer line) -> Skyward::Core::Diagnostics::Spot?
53
+
54
+ private
55
+
56
+ # @rbs path: String
57
+ # @rbs line: Integer
58
+ # @rbs text: String
59
+ # @rbs columns: Range[Integer]
60
+ # @rbs return: Skyward::Core::Diagnostics::Spot
61
+ def spot: (String path, Integer line, String text, Range[Integer] columns) -> Skyward::Core::Diagnostics::Spot
62
+
63
+ # @rbs path: String
64
+ # @rbs &: (Prism::ParseResult) -> Prism::Location?
65
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
66
+ def parse: (String path) { (Prism::ParseResult) -> Prism::Location? } -> Skyward::Core::Diagnostics::Spot?
67
+
68
+ # @rbs method: Method | UnboundMethod
69
+ # @rbs &: (Prism::DefNode) -> Prism::Location?
70
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
71
+ def definition: (Method | UnboundMethod method) { (Prism::DefNode) -> Prism::Location? } -> Skyward::Core::Diagnostics::Spot?
72
+
73
+ # @rbs path: String
74
+ # @rbs line: Integer
75
+ # @rbs name: Symbol
76
+ # @rbs &: (Array[Prism::node]) -> Prism::Location?
77
+ # @rbs return: Skyward::Core::Diagnostics::Spot?
78
+ def arguments: (String path, Integer line, Symbol name) { (Array[Prism::node]) -> Prism::Location? } -> Skyward::Core::Diagnostics::Spot?
79
+
80
+ # @rbs result: Prism::ParseResult
81
+ # @rbs line: Integer
82
+ # @rbs name: Symbol
83
+ # @rbs return: Prism::CallNode?
84
+ def call: (Prism::ParseResult result, Integer line, Symbol name) -> Prism::CallNode?
85
+
86
+ # @rbs arguments: Array[Prism::node]
87
+ # @rbs key: Symbol
88
+ # @rbs return: Prism::AssocNode?
89
+ def pair: (Array[Prism::node] arguments, Symbol key) -> Prism::AssocNode?
90
+
91
+ # @rbs node: Prism::node
92
+ # @rbs value: Symbol
93
+ # @rbs return: bool
94
+ def symbol?: (Prism::node node, Symbol value) -> bool
95
+
96
+ # Prism's locations count bytes, so a part is found by its byte offset.
97
+ # @rbs location: Prism::Location
98
+ # @rbs part: String
99
+ # @rbs return: Prism::Location?
100
+ def within: (Prism::Location location, String part) -> Prism::Location?
101
+ end
@@ -0,0 +1,8 @@
1
+ # Generated from lib/skyward/core/diagnostics/suggestion.rb with RBS::Inline
2
+
3
+ module Skyward::Core::Diagnostics::Suggestion
4
+ # @rbs name: String
5
+ # @rbs names: Array[String]
6
+ # @rbs return: String?
7
+ def closest: (String name, Array[String] names) -> String?
8
+ end
@@ -0,0 +1,21 @@
1
+ # Generated from lib/skyward/core/diagnostics.rb with RBS::Inline
2
+
3
+ module Skyward::Core::Diagnostics
4
+ # The line's text travels with it, so an excerpt never reads the file again.
5
+ class Spot < Data
6
+ attr_reader path(): String
7
+
8
+ attr_reader line(): Integer
9
+
10
+ attr_reader text(): String
11
+
12
+ attr_reader columns(): Range[Integer]?
13
+
14
+ def self.new: (String path, Integer line, String text, Range[Integer]? columns) -> instance
15
+ | (path: String, line: Integer, text: String, columns: Range[Integer]?) -> instance
16
+
17
+ def self.members: () -> [ :path, :line, :text, :columns ]
18
+
19
+ def members: () -> [ :path, :line, :text, :columns ]
20
+ end
21
+ end
@@ -3,6 +3,10 @@
3
3
  class Skyward::Core::Signatures::Loader
4
4
  @gems: Array[String]
5
5
 
6
+ @key: Hash[String, Hash[String, String]]?
7
+
8
+ @builder: WeakRef?
9
+
6
10
  # @rbs return: void
7
11
  def initialize: () -> void
8
12
 
@@ -13,4 +17,19 @@ class Skyward::Core::Signatures::Loader
13
17
  # @rbs directory: String
14
18
  # @rbs return: RBS::DefinitionBuilder
15
19
  def load: (String directory) -> RBS::DefinitionBuilder
20
+
21
+ private
22
+
23
+ # @rbs directory: String
24
+ # @rbs return: Hash[String, Hash[String, String]]
25
+ def contents: (String directory) -> Hash[String, Hash[String, String]]
26
+
27
+ # @rbs decl: RBS::DuplicatedDeclarationError::declaration
28
+ # @rbs return: RBS::Location[untyped, untyped]?
29
+ def name_location: (RBS::DuplicatedDeclarationError::declaration decl) -> RBS::Location[untyped, untyped]?
30
+
31
+ # @rbs message: String
32
+ # @rbs locations: Array[RBS::Location[untyped, untyped]]
33
+ # @rbs return: String
34
+ def failure: (String message, Array[RBS::Location[untyped, untyped]] locations) -> String
16
35
  end
@@ -1,6 +1,8 @@
1
1
  # Generated from lib/skyward/core/types/reader.rb with RBS::Inline
2
2
 
3
3
  class Skyward::Core::Types::Reader
4
+ @refinements: Hash[String, Symbol]
5
+
4
6
  @builder: RBS::DefinitionBuilder
5
7
 
6
8
  SCALARS: Hash[String, Skyward::Core::Types::scalar]
@@ -8,8 +10,9 @@ class Skyward::Core::Types::Reader
8
10
  NIL_LITERAL: Skyward::Core::Types::Literal
9
11
 
10
12
  # @rbs builder: RBS::DefinitionBuilder
13
+ # @rbs refinements: Hash[String, Symbol]
11
14
  # @rbs return: void
12
- def initialize: (RBS::DefinitionBuilder builder) -> void
15
+ def initialize: (RBS::DefinitionBuilder builder, ?refinements: Hash[String, Symbol]) -> void
13
16
 
14
17
  # @rbs type: RBS::Types::t
15
18
  # @rbs return: Skyward::Core::Types::result
@@ -26,6 +29,21 @@ class Skyward::Core::Types::Reader
26
29
 
27
30
  private
28
31
 
32
+ # @rbs error: RBS::DefinitionError
33
+ # @rbs return: String
34
+ def definition_failure: (RBS::DefinitionError error) -> String
35
+
36
+ # @rbs message: String
37
+ # @rbs location: RBS::Location[untyped, untyped]?
38
+ # @rbs suggestion: String?
39
+ # @rbs return: String
40
+ def explain: (String message, RBS::Location[untyped, untyped]? location, ?String? suggestion) -> String
41
+
42
+ # Only a declared name of the same kind, as `String` and not `string` for `Strng`.
43
+ # @rbs type_name: RBS::TypeName
44
+ # @rbs return: String?
45
+ def closest: (RBS::TypeName type_name) -> String?
46
+
29
47
  # @rbs type: RBS::Types::t
30
48
  # @rbs path: Array[String]
31
49
  # @rbs return: Skyward::Core::Types::result
@@ -76,9 +94,9 @@ class Skyward::Core::Types::Reader
76
94
  # @rbs return: Array[RBS::AST::Members::AttrReader]
77
95
  def declared_public_readers: (RBS::AST::Declarations::Class decl) -> Array[RBS::AST::Members::AttrReader]
78
96
 
79
- # @rbs name: String
97
+ # @rbs type_name: RBS::TypeName
80
98
  # @rbs return: Class
81
- def ruby_class: (String name) -> Class
99
+ def ruby_class: (RBS::TypeName type_name) -> Class
82
100
 
83
101
  # @rbs name: String
84
102
  # @rbs path: Array[String]
@@ -102,8 +120,9 @@ class Skyward::Core::Types::Reader
102
120
 
103
121
  # @rbs type: RBS::Types::t
104
122
  # @rbs instead: String
123
+ # @rbs suggestion: String?
105
124
  # @rbs return: Skyward::Core::Types::Unreadable
106
- def unreadable: (RBS::Types::t type, ?String instead) -> Skyward::Core::Types::Unreadable
125
+ def unreadable: (RBS::Types::t type, ?String instead, ?suggestion: String?) -> Skyward::Core::Types::Unreadable
107
126
 
108
127
  # @rbs (_ToS name) -> String
109
128
  def written: (_ToS name) -> String
@@ -1,11 +1,11 @@
1
1
  # Generated from lib/skyward/core/types.rb with RBS::Inline
2
2
 
3
3
  module Skyward::Core::Types
4
- type scalar = :integer | :float | :string | :bool | :date | :time | :untyped
4
+ type scalar = :integer | :float | :string | :bool | :date | :time | :decimal | :untyped
5
5
 
6
6
  type literal = String | Integer | bool | nil
7
7
 
8
- type t = Scalar | Literal | Enum | Nullable | ArrayOf | HashOf | Record | Named | Union
8
+ type t = Scalar | Literal | Enum | Nullable | ArrayOf | HashOf | Record | Named | Union | Refined
9
9
 
10
10
  type result = t | Unreadable
11
11
 
@@ -127,6 +127,19 @@ module Skyward::Core::Types
127
127
  def members: () -> [ :types ]
128
128
  end
129
129
 
130
+ class Refined < Data
131
+ attr_reader type(): t
132
+
133
+ attr_reader as(): Symbol
134
+
135
+ def self.new: (t type, Symbol as) -> instance
136
+ | (type: t, as: Symbol) -> instance
137
+
138
+ def self.members: () -> [ :type, :as ]
139
+
140
+ def members: () -> [ :type, :as ]
141
+ end
142
+
130
143
  class Unreadable < Data
131
144
  attr_reader type(): String
132
145
 
@@ -134,11 +147,13 @@ module Skyward::Core::Types
134
147
 
135
148
  attr_reader instead(): String
136
149
 
137
- def self.new: (String type, String? at, String instead) -> instance
138
- | (type: String, at: String?, instead: String) -> instance
150
+ attr_reader suggestion(): String?
151
+
152
+ def self.new: (String type, String? at, String instead, String? suggestion) -> instance
153
+ | (type: String, at: String?, instead: String, suggestion: String?) -> instance
139
154
 
140
- def self.members: () -> [ :type, :at, :instead ]
155
+ def self.members: () -> [ :type, :at, :instead, :suggestion ]
141
156
 
142
- def members: () -> [ :type, :at, :instead ]
157
+ def members: () -> [ :type, :at, :instead, :suggestion ]
143
158
  end
144
159
  end
@@ -0,0 +1,3 @@
1
+ module Skyward::Core::Diagnostics::Excerpt
2
+ extend ::Skyward::Core::Diagnostics::Excerpt
3
+ end
@@ -0,0 +1,3 @@
1
+ module Skyward::Core::Diagnostics::Spots
2
+ extend ::Skyward::Core::Diagnostics::Spots
3
+ end
@@ -0,0 +1,3 @@
1
+ module Skyward::Core::Diagnostics::Suggestion
2
+ extend ::Skyward::Core::Diagnostics::Suggestion
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skyward-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Bottrall
@@ -9,6 +9,20 @@ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: prism
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.6'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '1.6'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: rbs
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -44,6 +58,7 @@ executables:
44
58
  extensions: []
45
59
  extra_rdoc_files: []
46
60
  files:
61
+ - LICENSE
47
62
  - README.md
48
63
  - exe/skyward
49
64
  - lib/skyward/core.rb
@@ -51,17 +66,26 @@ files:
51
66
  - lib/skyward/core/cli.rb
52
67
  - lib/skyward/core/cli/executable.rb
53
68
  - lib/skyward/core/cli/registry.rb
69
+ - lib/skyward/core/diagnostics.rb
70
+ - lib/skyward/core/diagnostics/excerpt.rb
71
+ - lib/skyward/core/diagnostics/spots.rb
72
+ - lib/skyward/core/diagnostics/suggestion.rb
54
73
  - lib/skyward/core/signatures.rb
55
74
  - lib/skyward/core/signatures/inline.rb
56
75
  - lib/skyward/core/signatures/loader.rb
57
76
  - lib/skyward/core/types.rb
58
77
  - lib/skyward/core/types/reader.rb
59
78
  - lib/skyward/core/version.rb
79
+ - sig/_private/weakref.rbs
60
80
  - sig/generated/skyward/core.rbs
61
81
  - sig/generated/skyward/core/boot_error.rbs
62
82
  - sig/generated/skyward/core/cli.rbs
63
83
  - sig/generated/skyward/core/cli/executable.rbs
64
84
  - sig/generated/skyward/core/cli/registry.rbs
85
+ - sig/generated/skyward/core/diagnostics.rbs
86
+ - sig/generated/skyward/core/diagnostics/excerpt.rbs
87
+ - sig/generated/skyward/core/diagnostics/spots.rbs
88
+ - sig/generated/skyward/core/diagnostics/suggestion.rbs
65
89
  - sig/generated/skyward/core/signatures.rbs
66
90
  - sig/generated/skyward/core/signatures/inline.rbs
67
91
  - sig/generated/skyward/core/signatures/loader.rbs
@@ -70,6 +94,9 @@ files:
70
94
  - sig/generated/skyward/core/version.rbs
71
95
  - sig/manual/skyward/core/cli.rbs
72
96
  - sig/manual/skyward/core/cli/executable.rbs
97
+ - sig/manual/skyward/core/diagnostics/excerpt.rbs
98
+ - sig/manual/skyward/core/diagnostics/spots.rbs
99
+ - sig/manual/skyward/core/diagnostics/suggestion.rbs
73
100
  - sig/manual/skyward/core/signatures.rbs
74
101
  - sig/manual/skyward/core/signatures/inline.rbs
75
102
  homepage: https://github.com/bottrall/skyward