Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
5ffccfad
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
5ffccfad
编写于
5月 08, 2020
作者:
X
Xavier Noria
提交者:
GitHub
5月 08, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #39140 from Shopify/missing-name-corner-case
Make NameError#missing_name work even for real Ruby NameError
上级
1edf103b
8c0b94b9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
50 addition
and
5 deletion
+50
-5
actionpack/test/controller/helper_test.rb
actionpack/test/controller/helper_test.rb
+5
-1
activesupport/lib/active_support/core_ext/name_error.rb
activesupport/lib/active_support/core_ext/name_error.rb
+29
-2
activesupport/lib/active_support/dependencies.rb
activesupport/lib/active_support/dependencies.rb
+12
-2
activesupport/test/core_ext/name_error_test.rb
activesupport/test/core_ext/name_error_test.rb
+4
-0
未找到文件。
actionpack/test/controller/helper_test.rb
浏览文件 @
5ffccfad
...
...
@@ -88,7 +88,11 @@ class HelpersTypoControllerTest < ActiveSupport::TestCase
def
test_helper_typo_error_message
e
=
assert_raise
(
NameError
)
{
HelpersTypoController
.
helper
"admin/users"
}
# This message is better if autoloading.
assert_equal
"uninitialized constant Admin::UsersHelper"
,
e
.
message
if
RUBY_VERSION
>=
"2.6"
assert_equal
"uninitialized constant Admin::UsersHelper
\n
Did you mean? Admin::UsersHelpeR"
,
e
.
message
else
assert_equal
"uninitialized constant Admin::UsersHelper"
,
e
.
message
end
end
end
...
...
activesupport/lib/active_support/core_ext/name_error.rb
浏览文件 @
5ffccfad
...
...
@@ -14,9 +14,22 @@ def missing_name
# It extends NameError#message with spell corrections which are SLOW.
# We should use original_message message instead.
message
=
respond_to?
(
:original_message
)
?
original_message
:
self
.
message
return
unless
message
.
start_with?
(
"uninitialized constant "
)
unless
/undefined local variable or method/
.
match?
(
message
)
$1
if
/((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/
=~
message
receiver
=
begin
self
.
receiver
rescue
ArgumentError
nil
end
if
receiver
==
Object
name
.
to_s
elsif
receiver
"
#{
real_mod_name
(
receiver
)
}
::
#{
self
.
name
}
"
else
if
match
=
message
.
match
(
/((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/
)
match
[
1
]
end
end
end
...
...
@@ -35,4 +48,18 @@ def missing_name?(name)
missing_name
==
name
.
to_s
end
end
private
UNBOUND_METHOD_MODULE_NAME
=
Module
.
instance_method
(
:name
)
private_constant
:UNBOUND_METHOD_MODULE_NAME
if
UnboundMethod
.
method_defined?
(
:bind_call
)
def
real_mod_name
(
mod
)
UNBOUND_METHOD_MODULE_NAME
.
bind_call
(
mod
)
end
else
def
real_mod_name
(
mod
)
UNBOUND_METHOD_MODULE_NAME
.
bind
(
mod
).
call
end
end
end
activesupport/lib/active_support/dependencies.rb
浏览文件 @
5ffccfad
...
...
@@ -592,8 +592,8 @@ def load_missing_constant(from_mod, const_name)
end
end
name_error
=
NameError
.
new
(
"uninitialized constant
#{
qualified_name
}
"
,
const_name
)
name_error
.
set_backtrace
(
caller
.
reject
{
|
l
|
l
.
start_with?
(
__FILE__
)
})
name_error
=
uninitialized_constant
(
qualified_name
,
const_name
,
receiver:
from_mod
)
name_error
.
set_backtrace
(
caller
.
reject
{
|
l
|
l
.
start_with?
__FILE__
})
raise
name_error
end
...
...
@@ -801,6 +801,16 @@ def log(message)
end
private
if
RUBY_VERSION
<
"2.6"
def
uninitialized_constant
(
qualified_name
,
const_name
,
receiver
:)
NameError
.
new
(
"uninitialized constant
#{
qualified_name
}
"
,
const_name
)
end
else
def
uninitialized_constant
(
qualified_name
,
const_name
,
receiver
:)
NameError
.
new
(
"uninitialized constant
#{
qualified_name
}
"
,
const_name
,
receiver:
receiver
)
end
end
# Returns the original name of a class or module even if `name` has been
# overridden.
def
real_mod_name
(
mod
)
...
...
activesupport/test/core_ext/name_error_test.rb
浏览文件 @
5ffccfad
...
...
@@ -11,6 +11,9 @@ def test_name_error_should_set_missing_name
assert_equal
"NameErrorTest::SomeNameThatNobodyWillUse____Really"
,
exc
.
missing_name
assert
exc
.
missing_name?
(
:SomeNameThatNobodyWillUse____Really
)
assert
exc
.
missing_name?
(
"NameErrorTest::SomeNameThatNobodyWillUse____Really"
)
if
RUBY_VERSION
>=
"2.6"
assert_equal
NameErrorTest
,
exc
.
receiver
end
end
def
test_missing_method_should_ignore_missing_name
...
...
@@ -19,5 +22,6 @@ def test_missing_method_should_ignore_missing_name
end
assert_not
exc
.
missing_name?
(
:Foo
)
assert_nil
exc
.
missing_name
assert_equal
self
,
exc
.
receiver
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录