Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
f0a23556
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f0a23556
编写于
8月 20, 2019
作者:
K
Kitson Kelly
提交者:
Ryan Dahl
8月 19, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support custom inspection of objects (#2791)
上级
4faab6a7
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
35 addition
and
7 deletion
+35
-7
js/console.ts
js/console.ts
+8
-1
js/console_test.ts
js/console_test.ts
+19
-2
js/deno.ts
js/deno.ts
+1
-1
js/globals.ts
js/globals.ts
+7
-3
未找到文件。
js/console.ts
浏览文件 @
f0a23556
...
...
@@ -326,7 +326,9 @@ function createObjectString(
value
:
{},
...
args
:
[
ConsoleContext
,
number
,
number
]
):
string
{
if
(
value
instanceof
Error
)
{
if
(
customInspect
in
value
&&
typeof
value
[
customInspect
]
===
"
function
"
)
{
return
String
(
value
[
customInspect
]
!
());
}
else
if
(
value
instanceof
Error
)
{
return
String
(
value
.
stack
);
}
else
if
(
Array
.
isArray
(
value
))
{
return
createArrayString
(
value
,
...
args
);
...
...
@@ -752,6 +754,11 @@ export class Console {
}
}
/** A symbol which can be used as a key for a custom method which will be called
* when `Deno.inspect()` is called, or when the object is logged to the console.
*/
export
const
customInspect
=
Symbol
.
for
(
"
Deno.customInspect
"
);
/**
* `inspect()` converts input into string that has the same format
* as printed by `console.log(...)`;
...
...
js/console_test.ts
浏览文件 @
f0a23556
...
...
@@ -3,8 +3,15 @@ import { assert, assertEquals, test } from "./test_util.ts";
// Some of these APIs aren't exposed in the types and so we have to cast to any
// in order to "trick" TypeScript.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const
{
Console
,
stringifyArgs
,
inspect
,
write
,
stdout
}
=
Deno
as
any
;
const
{
Console
,
customInspect
,
stringifyArgs
,
inspect
,
write
,
stdout
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
=
Deno
as
any
;
function
stringify
(...
args
:
unknown
[]):
string
{
return
stringifyArgs
(
args
).
replace
(
/
\n
$/
,
""
);
...
...
@@ -173,6 +180,16 @@ test(function consoleTestStringifyWithDepth(): void {
);
});
test
(
function
consoleTestWithCustomInspector
():
void
{
class
A
{
[
customInspect
]():
string
{
return
"
b
"
;
}
}
assertEquals
(
stringify
(
new
A
()),
"
b
"
);
});
test
(
function
consoleTestWithIntegerFormatSpecifier
():
void
{
assertEquals
(
stringify
(
"
%i
"
),
"
%i
"
);
assertEquals
(
stringify
(
"
%i
"
,
42.0
),
"
42
"
);
...
...
js/deno.ts
浏览文件 @
f0a23556
...
...
@@ -79,7 +79,7 @@ export {
ProcessStatus
,
Signal
}
from
"
./process
"
;
export
{
inspect
}
from
"
./console
"
;
export
{
inspect
,
customInspect
}
from
"
./console
"
;
export
{
build
,
platform
,
OperatingSystem
,
Arch
}
from
"
./build
"
;
export
{
version
}
from
"
./version
"
;
export
const
args
:
string
[]
=
[];
...
...
js/globals.ts
浏览文件 @
f0a23556
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// This is a "special" module, in that it define the global runtime scope of
// Deno, and therefore it defines a lot of the runtime environ
em
nt that code
// Deno, and therefore it defines a lot of the runtime environ
me
nt that code
// is evaluated in. We use this file to automatically build the runtime type
// library.
...
...
@@ -12,7 +12,7 @@ import * as blob from "./blob";
import
*
as
consoleTypes
from
"
./console
"
;
import
*
as
csprng
from
"
./get_random_values
"
;
import
*
as
customEvent
from
"
./custom_event
"
;
import
*
as
d
eno
from
"
./deno
"
;
import
*
as
D
eno
from
"
./deno
"
;
import
*
as
domTypes
from
"
./dom_types
"
;
import
*
as
domFile
from
"
./dom_file
"
;
import
*
as
event
from
"
./event
"
;
...
...
@@ -62,6 +62,10 @@ declare global {
interface
ErrorConstructor
{
prepareStackTrace
(
error
:
Error
,
structuredStackTrace
:
CallSite
[]):
string
;
}
interface
Object
{
[
consoleTypes
.
customInspect
]?():
string
;
}
}
// A self reference to the global object.
...
...
@@ -70,7 +74,7 @@ window.window = window;
// This is the Deno namespace, it is handled differently from other window
// properties when building the runtime type library, as the whole module
// is flattened into a single namespace.
window
.
Deno
=
d
eno
;
window
.
Deno
=
D
eno
;
Object
.
freeze
(
window
.
Deno
);
// Globally available functions and object instances.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录