-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
``` $ tool/sync_default_gems.rb dead_end ``` Incorporates changes from these prs: - [Breaking] Lazy load DeadEnd internals only if there is a Syntax error. Use `require "dead_end"; require "dead_end/api"` to load eagerly all internals. Otherwise `require "dead_end"` will set up an autoload for the first time the DeadEnd module is used in code. This should only happen on a syntax error. (ruby/syntax_suggest#142) - Monkeypatch `SyntaxError#detailed_message` in Ruby 3.2+ instead of `require`, `load`, and `require_relative` (ruby/syntax_suggest#139)
- Loading branch information
Showing
6 changed files
with
110 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "dead_end/api" | ||
require_relative "dead_end/core_ext" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
# Monkey patch kernel to ensure that all `require` calls call the same | ||
# method | ||
module Kernel | ||
module_function | ||
|
||
alias_method :dead_end_original_require, :require | ||
alias_method :dead_end_original_require_relative, :require_relative | ||
alias_method :dead_end_original_load, :load | ||
|
||
def load(file, wrap = false) | ||
dead_end_original_load(file) | ||
rescue SyntaxError => e | ||
DeadEnd.handle_error(e) | ||
end | ||
# Allow lazy loading, only load code if/when there's a syntax error | ||
autoload :DeadEnd, "dead_end/api" | ||
|
||
# Ruby 3.2+ has a cleaner way to hook into Ruby that doesn't use `require` | ||
if SyntaxError.new.respond_to?(:detailed_message) | ||
module DeadEndUnloaded | ||
class MiniStringIO | ||
def initialize(isatty: $stderr.isatty) | ||
@string = +"" | ||
@isatty = isatty | ||
end | ||
|
||
def require(file) | ||
dead_end_original_require(file) | ||
rescue SyntaxError => e | ||
DeadEnd.handle_error(e) | ||
attr_reader :isatty | ||
def puts(value = $/, **) | ||
@string << value | ||
end | ||
|
||
attr_reader :string | ||
end | ||
end | ||
|
||
def require_relative(file) | ||
if Pathname.new(file).absolute? | ||
dead_end_original_require file | ||
else | ||
relative_from = caller_locations(1..1).first | ||
relative_from_path = relative_from.absolute_path || relative_from.path | ||
dead_end_original_require File.expand_path("../#{file}", relative_from_path) | ||
SyntaxError.prepend Module.new { | ||
def detailed_message(highlight: nil, **) | ||
message = super | ||
file = DeadEnd::PathnameFromMessage.new(message).call.name | ||
io = DeadEndUnloaded::MiniStringIO.new | ||
|
||
if file | ||
DeadEnd.call( | ||
io: io, | ||
source: file.read, | ||
filename: file | ||
) | ||
annotation = io.string | ||
|
||
annotation + message | ||
else | ||
message | ||
end | ||
rescue => e | ||
if ENV["DEBUG"] | ||
$stderr.warn(e.message) | ||
$stderr.warn(e.backtrace) | ||
end | ||
|
||
raise e | ||
end | ||
} | ||
else | ||
autoload :Pathname, "pathname" | ||
|
||
# Monkey patch kernel to ensure that all `require` calls call the same | ||
# method | ||
module Kernel | ||
module_function | ||
|
||
alias_method :dead_end_original_require, :require | ||
alias_method :dead_end_original_require_relative, :require_relative | ||
alias_method :dead_end_original_load, :load | ||
|
||
def load(file, wrap = false) | ||
dead_end_original_load(file) | ||
rescue SyntaxError => e | ||
DeadEnd.handle_error(e) | ||
end | ||
|
||
def require(file) | ||
dead_end_original_require(file) | ||
rescue SyntaxError => e | ||
DeadEnd.handle_error(e) | ||
end | ||
|
||
def require_relative(file) | ||
if Pathname.new(file).absolute? | ||
dead_end_original_require file | ||
else | ||
relative_from = caller_locations(1..1).first | ||
relative_from_path = relative_from.absolute_path || relative_from.path | ||
dead_end_original_require File.expand_path("../#{file}", relative_from_path) | ||
end | ||
rescue SyntaxError => e | ||
DeadEnd.handle_error(e) | ||
end | ||
rescue SyntaxError => e | ||
DeadEnd.handle_error(e) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ end | |
|
||
Gem::Specification.new do |spec| | ||
spec.name = "dead_end" | ||
spec.version = DeadEnd::VERSION | ||
spec.version = UnloadedDeadEnd::VERSION | ||
spec.authors = ["schneems"] | ||
spec.email = ["[email protected]"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module DeadEnd | ||
VERSION = "3.1.1" | ||
# Calling `DeadEnd::VERSION` forces an eager load due to | ||
# an `autoload` on the `DeadEnd` constant. | ||
# | ||
# This is used for gemspec access in tests | ||
module UnloadedDeadEnd | ||
VERSION = "3.1.2" | ||
end |