method_source 0.8.pre.3 → 0.8.pre.4
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.
- data/Rakefile +1 -1
- data/lib/method_source/code_helpers.rb +13 -5
- data/lib/method_source/version.rb +1 -1
- data/test/test_code_helpers.rb +41 -0
- metadata +4 -2
data/Rakefile
CHANGED
@@ -7,10 +7,14 @@ module MethodSource
|
|
7
7
|
#
|
8
8
|
# @param [Array<String>, File, String] file The file to parse, either as a File or as
|
9
9
|
# @param [Fixnum] line_number The line number at which to look.
|
10
|
-
# NOTE: The first line in a file is
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
10
|
+
# NOTE: The first line in a file is
|
11
|
+
# line 1!
|
12
|
+
# @param [Hash] options The optional configuration parameters.
|
13
|
+
# @option options [Boolean] :strict If set to true, then only completely
|
14
|
+
# valid expressions are returned. Otherwise heuristics are used to extract
|
15
|
+
# expressions that may have been valid inside an eval.
|
16
|
+
# @option options [Fixnum] :consume A number of lines to automatically
|
17
|
+
# consume (add to the expression buffer) without checking for validity.
|
14
18
|
# @return [String] The first complete expression
|
15
19
|
# @raise [SyntaxError] If the first complete expression can't be identified
|
16
20
|
def expression_at(file, line_number, options={})
|
@@ -80,7 +84,9 @@ module MethodSource
|
|
80
84
|
# Get the first expression from the input.
|
81
85
|
#
|
82
86
|
# @param [Array<String>] lines
|
83
|
-
# @param [
|
87
|
+
# @param [Fixnum] consume A number of lines to automatically
|
88
|
+
# consume (add to the expression buffer) without checking for validity.
|
89
|
+
# @yield a clean-up function to run before checking for complete_expression
|
84
90
|
# @return [String] a valid ruby expression
|
85
91
|
# @raise [SyntaxError]
|
86
92
|
def extract_first_expression(lines, consume=0, &block)
|
@@ -117,8 +123,10 @@ module MethodSource
|
|
117
123
|
# fixed by adding more input to the buffer.
|
118
124
|
module IncompleteExpression
|
119
125
|
def self.===(ex)
|
126
|
+
return false unless SyntaxError === ex
|
120
127
|
case ex.message
|
121
128
|
when /unexpected (\$end|end-of-file|END_OF_FILE)/, # mri, jruby, ironruby
|
129
|
+
/embedded document meets end of file/, # =begin
|
122
130
|
/unterminated (quoted string|string|regexp) meets end of file/, # "quoted string" is ironruby
|
123
131
|
/missing 'end' for/, /: expecting '[})\]]'$/, /can't find string ".*" anywhere before EOF/, /: expecting keyword_end/, /expecting kWHEN/ # rbx
|
124
132
|
true
|
@@ -0,0 +1,41 @@
|
|
1
|
+
describe MethodSource::CodeHelpers do
|
2
|
+
before do
|
3
|
+
@tester = Object.new.extend(MethodSource::CodeHelpers)
|
4
|
+
end
|
5
|
+
|
6
|
+
[
|
7
|
+
["p = '", "'"],
|
8
|
+
["def", "a", "(); end"],
|
9
|
+
["p = <<FOO", "lots", "and", "lots of", "foo", "FOO"],
|
10
|
+
["[", ":lets,", "'list',", "[/nested/", "], things ]"],
|
11
|
+
["abc =~ /hello", "/"],
|
12
|
+
["issue = %W/", "343/"],
|
13
|
+
["pouts(<<HI, 'foo", "bar", "HI", "baz')"],
|
14
|
+
["=begin", "no-one uses this syntax anymore...", "=end"],
|
15
|
+
["puts 1, 2,", "3"],
|
16
|
+
["puts 'hello'\\", "'world'"]
|
17
|
+
].each do |lines|
|
18
|
+
it "should not raise an error on broken lines: #{lines.join("\\n")}" do
|
19
|
+
1.upto(lines.size - 1) do |i|
|
20
|
+
@tester.complete_expression?(lines[0...i].join("\n") + "\n").should == false
|
21
|
+
end
|
22
|
+
@tester.complete_expression?(lines.join("\n")).should == true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
[
|
27
|
+
["end"],
|
28
|
+
["puts )("],
|
29
|
+
["1 1"],
|
30
|
+
["puts :"]
|
31
|
+
] + (RbConfig::CONFIG['ruby_install_name'] == 'rbx' ? [] : [
|
32
|
+
["def", "method(1"], # in this case the syntax error is "expecting ')'".
|
33
|
+
["o = Object.new.tap{ def o.render;","'MEH'", "}"] # in this case the syntax error is "expecting keyword_end".
|
34
|
+
]).compact.each do |foo|
|
35
|
+
it "should raise an error on invalid syntax like #{foo.inspect}" do
|
36
|
+
lambda{
|
37
|
+
@tester.complete_expression?(foo.join("\n"))
|
38
|
+
}.should.raise(SyntaxError)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_source
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.pre.
|
4
|
+
version: 0.8.pre.4
|
5
5
|
prerelease: 4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bacon
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- lib/method_source/version.rb
|
63
63
|
- method_source.gemspec
|
64
64
|
- test/test.rb
|
65
|
+
- test/test_code_helpers.rb
|
65
66
|
- test/test_helper.rb
|
66
67
|
homepage: http://banisterfiend.wordpress.com
|
67
68
|
licenses: []
|
@@ -89,4 +90,5 @@ specification_version: 3
|
|
89
90
|
summary: retrieve the sourcecode for a method
|
90
91
|
test_files:
|
91
92
|
- test/test.rb
|
93
|
+
- test/test_code_helpers.rb
|
92
94
|
- test/test_helper.rb
|