summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGopal Patel <nixme@stillhope.com>2023-12-18 14:22:21 -0800
committergit <svn-admin@ruby-lang.org>2024-02-24 03:39:19 +0000
commitb9b0712556980a3af91c976c3fee8ba0e21c39a0 (patch)
treec8ed2454b0e26d4aed8ea5391f2a735e6f9327f1
parentfc656acee9d17f72d9f7b09630d7f03e981beef3 (diff)
[ruby/prism] Use steep to type check RBS and Ruby files
https://github.com/ruby/prism/commit/eabed9f4fd
-rw-r--r--lib/prism/debug.rb6
-rw-r--r--lib/prism/desugar_compiler.rb2
-rw-r--r--lib/prism/lex_compat.rb32
-rw-r--r--lib/prism/node_ext.rb4
-rw-r--r--lib/prism/pack.rb2
-rw-r--r--lib/prism/parse_result/comments.rb6
-rw-r--r--lib/prism/prism.gemspec20
-rw-r--r--prism/templates/lib/prism/dot_visitor.rb.erb2
-rw-r--r--prism/templates/lib/prism/node.rb.erb4
-rwxr-xr-xprism/templates/template.rb12
10 files changed, 59 insertions, 31 deletions
diff --git a/lib/prism/debug.rb b/lib/prism/debug.rb
index 553205a04b..74f824faa7 100644
--- a/lib/prism/debug.rb
+++ b/lib/prism/debug.rb
@@ -55,7 +55,7 @@ module Prism
verbose, $VERBOSE = $VERBOSE, nil
begin
- locals = []
+ locals = [] #: Array[Array[Symbol | Integer]]
stack = [ISeq.new(RubyVM::InstructionSequence.compile(source).to_a)]
while (iseq = stack.pop)
@@ -96,8 +96,8 @@ module Prism
# For the given source, parses with prism and returns a list of all of the
# sets of local variables that were encountered.
def self.prism_locals(source)
- locals = []
- stack = [Prism.parse(source).value]
+ locals = [] #: Array[Array[Symbol | Integer]]
+ stack = [Prism.parse(source).value] #: Array[Prism::node]
while (node = stack.pop)
case node
diff --git a/lib/prism/desugar_compiler.rb b/lib/prism/desugar_compiler.rb
index 00b3097f9f..8d059b0c98 100644
--- a/lib/prism/desugar_compiler.rb
+++ b/lib/prism/desugar_compiler.rb
@@ -82,7 +82,7 @@ module Prism
0,
read_class.new(source, *arguments, node.name_loc),
nil,
- node.operator_loc.slice.chomp("="),
+ node.operator_loc.slice.chomp("=").to_sym,
node.operator_loc.copy(length: node.operator_loc.length - 1),
nil,
ArgumentsNode.new(source, 0, [node.value], node.value.location),
diff --git a/lib/prism/lex_compat.rb b/lib/prism/lex_compat.rb
index c11903423d..3a0431841e 100644
--- a/lib/prism/lex_compat.rb
+++ b/lib/prism/lex_compat.rb
@@ -185,6 +185,8 @@ module Prism
# However, we add a couple of convenience methods onto them to make them a
# little easier to work with. We delegate all other methods to the array.
class Token < SimpleDelegator
+ # @dynamic initialize, each, []
+
# The location of the token in the source.
def location
self[0]
@@ -241,10 +243,10 @@ module Prism
def ==(other) # :nodoc:
return false unless self[0...-1] == other[0...-1]
- if self[4] == Ripper::EXPR_ARG | Ripper::EXPR_LABELED
- other[4] & Ripper::EXPR_ARG | Ripper::EXPR_LABELED > 0
+ if self[3] == Ripper::EXPR_ARG | Ripper::EXPR_LABELED
+ other[3] & Ripper::EXPR_ARG | Ripper::EXPR_LABELED > 0
else
- self[4] == other[4]
+ self[3] == other[3]
end
end
end
@@ -308,7 +310,7 @@ module Prism
def to_a
embexpr_balance = 0
- tokens.each_with_object([]) do |token, results|
+ tokens.each_with_object([]) do |token, results| #$ Array[Token]
case token.event
when :on_embexpr_beg
embexpr_balance += 1
@@ -409,7 +411,7 @@ module Prism
# If every line in the heredoc is blank, we still need to split up the
# string content token into multiple tokens.
if dedent.nil?
- results = []
+ results = [] #: Array[Token]
embexpr_balance = 0
tokens.each do |token|
@@ -444,7 +446,7 @@ module Prism
# If the minimum common whitespace is 0, then we need to concatenate
# string nodes together that are immediately adjacent.
if dedent == 0
- results = []
+ results = [] #: Array[Token]
embexpr_balance = 0
index = 0
@@ -477,7 +479,7 @@ module Prism
# insert on_ignored_sp tokens for the amount of dedent that we need to
# perform. We also need to remove the dedent from the beginning of
# each line of plain string content tokens.
- results = []
+ results = [] #: Array[Token]
dedent_next = true
embexpr_balance = 0
@@ -526,7 +528,7 @@ module Prism
# dedent from the beginning of the line.
if (dedent > 0) && (dedent_next || index > 0)
deleting = 0
- deleted_chars = []
+ deleted_chars = [] #: Array[String]
# Gather up all of the characters that we're going to
# delete, stopping when you hit a character that would put
@@ -603,15 +605,15 @@ module Prism
end
def result
- tokens = []
+ tokens = [] #: Array[LexCompat::Token]
state = :default
- heredoc_stack = [[]]
+ heredoc_stack = [[]] #: Array[Array[Heredoc::PlainHeredoc | Heredoc::DashHeredoc | Heredoc::DedentingHeredoc]]
result = Prism.lex(source, **options)
result_value = result.value
- previous_state = nil
- last_heredoc_end = nil
+ previous_state = nil #: Ripper::Lexer::State?
+ last_heredoc_end = nil #: Integer?
# In previous versions of Ruby, Ripper wouldn't flush the bom before the
# first token, so we had to have a hack in place to account for that. This
@@ -842,7 +844,7 @@ module Prism
# We sort by location to compare against Ripper's output
tokens.sort_by!(&:location)
- ParseResult.new(tokens, result.comments, result.magic_comments, result.data_loc, result.errors, result.warnings, [])
+ ParseResult.new(tokens, result.comments, result.magic_comments, result.data_loc, result.errors, result.warnings, Source.new(source))
end
end
@@ -858,8 +860,8 @@ module Prism
end
def result
- previous = []
- results = []
+ previous = [] #: [[Integer, Integer], Symbol, String, untyped] | []
+ results = [] #: Array[[[Integer, Integer], Symbol, String, untyped]]
lex(source).each do |token|
case token[1]
diff --git a/lib/prism/node_ext.rb b/lib/prism/node_ext.rb
index c0f485f675..688e60a159 100644
--- a/lib/prism/node_ext.rb
+++ b/lib/prism/node_ext.rb
@@ -169,7 +169,7 @@ module Prism
class ParametersNode < Node
# Mirrors the Method#parameters method.
def signature
- names = []
+ names = [] #: Array[[:req | :opt | :rest | :keyreq | :key | :keyrest | :block, Symbol] | [:rest | :keyrest | :nokey]]
requireds.each do |param|
names << (param.is_a?(MultiTargetNode) ? [:req] : [:req, param.name])
@@ -184,7 +184,7 @@ module Prism
# Regardless of the order in which the keywords were defined, the required
# keywords always come first followed by the optional keywords.
- keyopt = []
+ keyopt = [] #: Array[OptionalKeywordParameterNode]
keywords.each do |param|
if param.is_a?(OptionalKeywordParameterNode)
keyopt << param
diff --git a/lib/prism/pack.rb b/lib/prism/pack.rb
index 00caf553c6..0168e5e749 100644
--- a/lib/prism/pack.rb
+++ b/lib/prism/pack.rb
@@ -148,6 +148,8 @@ module Prism
end
when LENGTH_MAX
base + ", as many as possible"
+ else
+ raise
end
when UTF8
"UTF-8 character"
diff --git a/lib/prism/parse_result/comments.rb b/lib/prism/parse_result/comments.rb
index dd8fa8e1c7..0f1522dead 100644
--- a/lib/prism/parse_result/comments.rb
+++ b/lib/prism/parse_result/comments.rb
@@ -120,7 +120,7 @@ module Prism
comment_start = comment.location.start_offset
comment_end = comment.location.end_offset
- targets = []
+ targets = [] #: Array[_Target]
node.comment_targets.map do |value|
case value
when StatementsNode
@@ -133,8 +133,8 @@ module Prism
end
targets.sort_by!(&:start_offset)
- preceding = nil
- following = nil
+ preceding = nil #: _Target?
+ following = nil #: _Target?
left = 0
right = targets.length
diff --git a/lib/prism/prism.gemspec b/lib/prism/prism.gemspec
index ac4bdddc21..8e642192a5 100644
--- a/lib/prism/prism.gemspec
+++ b/lib/prism/prism.gemspec
@@ -122,8 +122,26 @@ Gem::Specification.new do |spec|
"src/options.c",
"src/prism.c",
"prism.gemspec",
+ "sig/manifest.yaml",
"sig/prism.rbs",
- "sig/prism_static.rbs",
+ "sig/prism/compiler.rbs",
+ "sig/prism/debug.rbs",
+ "sig/prism/desugar_compiler.rbs",
+ "sig/prism/dispatcher.rbs",
+ "sig/prism/dot_visitor.rbs",
+ "sig/prism/dsl.rbs",
+ "sig/prism/lex_compat.rbs",
+ "sig/prism/mutation_compiler.rbs",
+ "sig/prism/node_ext.rbs",
+ "sig/prism/node_inspector.rbs",
+ "sig/prism/node.rbs",
+ "sig/prism/pack.rbs",
+ "sig/prism/parse_result/comments.rbs",
+ "sig/prism/parse_result/newlines.rbs",
+ "sig/prism/pattern.rbs",
+ "sig/prism/ripper_compat.rbs",
+ "sig/prism/serialize.rbs",
+ "sig/prism/visitor.rbs",
"rbi/prism.rbi",
"rbi/prism_static.rbi"
]
diff --git a/prism/templates/lib/prism/dot_visitor.rb.erb b/prism/templates/lib/prism/dot_visitor.rb.erb
index 1dc48d265d..56f116b8d3 100644
--- a/prism/templates/lib/prism/dot_visitor.rb.erb
+++ b/prism/templates/lib/prism/dot_visitor.rb.erb
@@ -175,7 +175,7 @@ module Prism
# Inspect a node that has <%= flag.human %> flags to display the flags as a
# comma-separated list.
def <%= flag.human %>_inspect(node)
- flags = []
+ flags = [] #: Array[String]
<%- flag.values.each do |value| -%>
flags << "<%= value.name.downcase %>" if node.<%= value.name.downcase %>?
<%- end -%>
diff --git a/prism/templates/lib/prism/node.rb.erb b/prism/templates/lib/prism/node.rb.erb
index 19fcda936f..99d89712cf 100644
--- a/prism/templates/lib/prism/node.rb.erb
+++ b/prism/templates/lib/prism/node.rb.erb
@@ -150,7 +150,7 @@ module Prism
# def compact_child_nodes: () -> Array[Node]
def compact_child_nodes
<%- if node.fields.any? { |field| field.is_a?(Prism::OptionalNodeField) } -%>
- compact = []
+ compact = [] #: Array[Prism::node]
<%- node.fields.each do |field| -%>
<%- case field -%>
<%- when Prism::NodeField -%>
@@ -179,7 +179,7 @@ module Prism
when Prism::NodeField, Prism::LocationField then field.name
when Prism::OptionalNodeField, Prism::NodeListField, Prism::OptionalLocationField then "*#{field.name}"
end
- }.compact.join(", ") %>]
+ }.compact.join(", ") %>] #: Array[Prism::node | Location]
end
# def copy: (**params) -> <%= node.name %>
diff --git a/prism/templates/template.rb b/prism/templates/template.rb
index 2831b09187..35133169fe 100755
--- a/prism/templates/template.rb
+++ b/prism/templates/template.rb
@@ -103,7 +103,7 @@ module Prism
# references and store them as references.
class NodeField < NodeKindField
def rbs_class
- ruby_type
+ options[:kind] || "Prism::node"
end
def rbi_class
@@ -115,7 +115,7 @@ module Prism
# optionally null. We pass them as references and store them as references.
class OptionalNodeField < NodeKindField
def rbs_class
- "#{ruby_type}?"
+ "#{options[:kind] || "Prism::node"}?"
end
def rbi_class
@@ -127,7 +127,7 @@ module Prism
# references and store them directly on the struct.
class NodeListField < Field
def rbs_class
- "Array[Node]"
+ "Array[Prism::node]"
end
def rbi_class
@@ -549,6 +549,12 @@ module Prism
"src/token_type.c",
"rbi/prism.rbi",
"sig/prism.rbs",
+ "sig/prism/dot_visitor.rbs",
+ "sig/prism/dsl.rbs",
+ "sig/prism/mutation_compiler.rbs",
+ "sig/prism/node.rbs",
+ "sig/prism/ripper_compat.rbs",
+ "sig/prism/visitor.rbs",
]
end