Skip to content

Commit daee74d

Browse files
authored
Merge pull request #165 from ruby/schnems/remove-legacy-preview-paths
Drop support for old 3.2.0 preview versions
2 parents d8f1bca + d6fc460 commit daee74d

File tree

5 files changed

+84
-52
lines changed

5 files changed

+84
-52
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- 2.7
3030
- '3.0'
3131
- 3.1
32-
- "3.2.0-preview1"
32+
- "3.2.0-rc1"
3333
- head
3434
steps:
3535
- name: Checkout code

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## HEAD (unreleased)
22

3-
- Native support of `SyntaxError#path`, support 3.2.0-preview3 will be dropped with the release of 3.2.0-preview4 ()
3+
- Drop support or Ruby 3.2.0 preview, now that 3.2.0-rc1 is available (https://github.com/ruby/syntax_suggest/pull/165)
4+
- Native support of `SyntaxError#path`, support 3.2.0-preview3 will be dropped with the release of 3.2.0-preview4 (https://github.com/ruby/syntax_suggest/pull/164)
45
- Added dependabot for GitHub Actions (https://github.com/ruby/syntax_suggest/pull/160)
56

67
## 1.0.1

lib/syntax_suggest/core_ext.rb

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# Ruby 3.2+ has a cleaner way to hook into Ruby that doesn't use `require`
44
if SyntaxError.method_defined?(:detailed_message)
55
module SyntaxSuggest
6+
# Mini String IO [Private]
7+
#
8+
# Acts like a StringIO with reduced API, but without having to require that
9+
# class.
610
class MiniStringIO
711
def initialize(isatty: $stderr.isatty)
812
@string = +""
@@ -16,52 +20,49 @@ def puts(value = $/, **)
1620

1721
attr_reader :string
1822
end
19-
end
20-
21-
SyntaxError.prepend Module.new {
22-
def detailed_message(highlight: true, syntax_suggest: true, **kwargs)
23-
return super unless syntax_suggest
24-
25-
require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE)
26-
27-
message = super
28-
29-
file = if respond_to?(:path)
30-
path
31-
elsif highlight
32-
# This branch will be removed when the next Ruby 3.2 preview is released with
33-
# support for SyntaxError#path
34-
SyntaxSuggest::PathnameFromMessage.new(super(highlight: false, **kwargs)).call.name
35-
else
36-
SyntaxSuggest::PathnameFromMessage.new(message).call.name
37-
end
3823

39-
if file
40-
file = Pathname.new(file)
41-
io = SyntaxSuggest::MiniStringIO.new
42-
43-
SyntaxSuggest.call(
44-
io: io,
45-
source: file.read,
46-
filename: file,
47-
terminal: highlight
48-
)
49-
annotation = io.string
50-
51-
annotation + message
52-
else
53-
message
54-
end
55-
rescue => e
56-
if ENV["SYNTAX_SUGGEST_DEBUG"]
57-
$stderr.warn(e.message)
58-
$stderr.warn(e.backtrace)
59-
end
60-
61-
# Ignore internal errors
62-
message
24+
# SyntaxSuggest.record_dir [Private]
25+
#
26+
# Used to monkeypatch SyntaxError via Module.prepend
27+
def self.module_for_detailed_message
28+
Module.new {
29+
def detailed_message(highlight: true, syntax_suggest: true, **kwargs)
30+
return super unless syntax_suggest
31+
32+
require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE)
33+
34+
message = super
35+
36+
if path
37+
file = Pathname.new(path)
38+
io = SyntaxSuggest::MiniStringIO.new
39+
40+
SyntaxSuggest.call(
41+
io: io,
42+
source: file.read,
43+
filename: file,
44+
terminal: highlight
45+
)
46+
annotation = io.string
47+
48+
annotation + message
49+
else
50+
message
51+
end
52+
rescue => e
53+
if ENV["SYNTAX_SUGGEST_DEBUG"]
54+
$stderr.warn(e.message)
55+
$stderr.warn(e.backtrace)
56+
end
57+
58+
# Ignore internal errors
59+
message
60+
end
61+
}
6362
end
64-
}
63+
end
64+
65+
SyntaxError.prepend(SyntaxSuggest.module_for_detailed_message)
6566
else
6667
autoload :Pathname, "pathname"
6768

spec/integration/ruby_command_line_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,17 @@ module SyntaxSuggest
7676
end
7777
end
7878

79-
it "annotates a syntax error in Ruby 3.2+ when require is not used" do
80-
pending("Support for SyntaxError#detailed_message monkeypatch needed https://gist.github.com/schneems/09f45cc23b9a8c46e9af6acbb6e6840d?permalink_comment_id=4172585#gistcomment-4172585")
79+
it "gem can be tested when executing on Ruby with default gem included" do
80+
skip if ruby_core?
81+
skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2")
82+
83+
out = `ruby -I#{lib_dir} -rsyntax_suggest -e "puts SyntaxError.instance_method(:detailed_message).source_location" 2>&1`
8184

85+
expect($?.success?).to be_truthy
86+
expect(out).to include(lib_dir.join("syntax_suggest").join("core_ext.rb").to_s).once
87+
end
88+
89+
it "annotates a syntax error in Ruby 3.2+ when require is not used" do
8290
skip if ruby_core?
8391
skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2")
8492

spec/unit/api_spec.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,20 @@ def fake_error.message
6565
it "respects highlight API" do
6666
skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2")
6767

68-
error = SyntaxError.new("#{fixtures_dir.join("this_project_extra_def.rb.txt")}:1 ")
68+
core_ext_file = lib_dir.join("syntax_suggest").join("core_ext.rb")
69+
require_relative core_ext_file
6970

70-
require "syntax_suggest/core_ext"
71+
error_klass = Class.new do
72+
def path
73+
fixtures_dir.join("this_project_extra_def.rb.txt")
74+
end
75+
76+
def detailed_message(**kwargs)
77+
"error"
78+
end
79+
end
80+
error_klass.prepend(SyntaxSuggest.module_for_detailed_message)
81+
error = error_klass.new
7182

7283
expect(error.detailed_message(highlight: true)).to include(SyntaxSuggest::DisplayCodeWithLineNumbers::TERMINAL_HIGHLIGHT)
7384
expect(error.detailed_message(highlight: false)).to_not include(SyntaxSuggest::DisplayCodeWithLineNumbers::TERMINAL_HIGHLIGHT)
@@ -76,9 +87,20 @@ def fake_error.message
7687
it "can be disabled via falsey kwarg" do
7788
skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2")
7889

79-
error = SyntaxError.new("#{fixtures_dir.join("this_project_extra_def.rb.txt")}:1 ")
90+
core_ext_file = lib_dir.join("syntax_suggest").join("core_ext.rb")
91+
require_relative core_ext_file
8092

81-
require "syntax_suggest/core_ext"
93+
error_klass = Class.new do
94+
def path
95+
fixtures_dir.join("this_project_extra_def.rb.txt")
96+
end
97+
98+
def detailed_message(**kwargs)
99+
"error"
100+
end
101+
end
102+
error_klass.prepend(SyntaxSuggest.module_for_detailed_message)
103+
error = error_klass.new
82104

83105
expect(error.detailed_message(syntax_suggest: true)).to_not eq(error.detailed_message(syntax_suggest: false))
84106
end

0 commit comments

Comments
 (0)