Skip to content

Commit 2106ece

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Require arguments to Source.for
ruby/prism@b38010c420
1 parent 726205b commit 2106ece

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

lib/prism/parse_result.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@ class Source
1919
# be used instead of `new` and it will return either a `Source` or a
2020
# specialized and more performant `ASCIISource` if no multibyte characters
2121
# are present in the source code.
22-
#--
23-
#: (String source, ?Integer start_line, ?Array[Integer] offsets) -> Source
24-
def self.for(source, start_line = 1, offsets = [])
22+
#
23+
# Note that if you are calling this method manually, you will need to supply
24+
# the start_line and offsets parameters. start_line is the line number that
25+
# the source starts on, which is typically 1 but can be different if this
26+
# source is a subset of a larger source or if this is an eval. offsets is an
27+
# array of byte offsets for the start of each line in the source code, which
28+
# can be calculated by iterating through the source code and recording the
29+
# byte offset whenever a newline character is encountered.
30+
#--
31+
#: (String source, Integer start_line, Array[Integer] offsets) -> Source
32+
def self.for(source, start_line, offsets)
2533
if source.ascii_only?
2634
ASCIISource.new(source, start_line, offsets)
2735
elsif source.encoding == Encoding::BINARY
@@ -55,8 +63,8 @@ def self.for(source, start_line = 1, offsets = [])
5563

5664
# Create a new source object with the given source code.
5765
#--
58-
#: (String source, ?Integer start_line, ?Array[Integer] offsets) -> void
59-
def initialize(source, start_line = 1, offsets = [])
66+
#: (String source, Integer start_line, Array[Integer] offsets) -> void
67+
def initialize(source, start_line, offsets)
6068
@source = source
6169
@start_line = start_line # set after parsing is done
6270
@offsets = offsets # set after parsing is done

prism/templates/lib/prism/dsl.rb.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Prism
55
# The DSL module provides a set of methods that can be used to create prism
66
# nodes in a more concise manner. For example, instead of writing:
77
#
8-
# source = Prism::Source.for("[1]")
8+
# source = Prism::Source.for("[1]", 1, [])
99
#
1010
# Prism::ArrayNode.new(
1111
# source,
@@ -62,7 +62,7 @@ module Prism
6262
#--
6363
#: (String string) -> Source
6464
def source(string)
65-
Source.for(string)
65+
Source.for(string, 1, [])
6666
end
6767

6868
# Create a new Location object.
@@ -136,7 +136,7 @@ module Prism
136136
#--
137137
#: () -> Source
138138
def default_source
139-
Source.for("")
139+
Source.for("", 1, [])
140140
end
141141

142142
# The default location object that gets attached to nodes if no location is

prism/templates/lib/prism/serialize.rb.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module Prism
2727
#: (String input, String serialized, bool freeze) -> ParseResult
2828
def self.load_parse(input, serialized, freeze)
2929
input = input.dup
30-
source = Source.for(input)
30+
source = Source.for(input, 1, [])
3131
loader = Loader.new(source, serialized)
3232

3333
loader.load_header
@@ -81,7 +81,7 @@ module Prism
8181
#--
8282
#: (String input, String serialized, bool freeze) -> LexResult
8383
def self.load_lex(input, serialized, freeze)
84-
source = Source.for(input)
84+
source = Source.for(input, 1, [])
8585
loader = Loader.new(source, serialized)
8686

8787
tokens = loader.load_tokens
@@ -127,7 +127,7 @@ module Prism
127127
#--
128128
#: (String input, String serialized, bool freeze) -> Array[Comment]
129129
def self.load_parse_comments(input, serialized, freeze)
130-
source = Source.for(input)
130+
source = Source.for(input, 1, [])
131131
loader = Loader.new(source, serialized)
132132

133133
loader.load_header
@@ -151,7 +151,7 @@ module Prism
151151
#--
152152
#: (String input, String serialized, bool freeze) -> ParseLexResult
153153
def self.load_parse_lex(input, serialized, freeze)
154-
source = Source.for(input)
154+
source = Source.for(input, 1, [])
155155
loader = Loader.new(source, serialized)
156156

157157
tokens = loader.load_tokens

0 commit comments

Comments
 (0)