1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 04:48:52 -05:00

fix(cli/console): only inspect getters with option (#7830)

This commit is contained in:
Casper Beyer 2020-10-12 03:52:20 +08:00 committed by GitHub
parent 08f3ae92d3
commit 86dc55134e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 22 deletions

View file

@ -1956,6 +1956,8 @@ declare namespace Deno {
sorted?: boolean;
/** Add a trailing comma for multiline collections. Defaults to false. */
trailingComma?: boolean;
/*** Evaluate the result of calling getters. Defaults to false. */
getters?: boolean;
}
/** Converts the input into a string that has the same format as printed by

View file

@ -157,6 +157,7 @@
iterableLimit: 100,
showProxy: false,
colors: false,
getters: false,
};
const DEFAULT_INDENT = " "; // Default indent string
@ -760,6 +761,7 @@
const red = maybeColor(colors.red, inspectOptions);
for (const key of stringKeys) {
if (inspectOptions.getters) {
let propertyValue;
let error = null;
try {
@ -768,11 +770,32 @@
error = error_;
}
const inspectedValue = error == null
? inspectValueWithQuotes(propertyValue, ctx, level + 1, inspectOptions)
? inspectValueWithQuotes(
propertyValue,
ctx,
level + 1,
inspectOptions,
)
: red(`[Thrown ${error.name}: ${error.message}]`);
entries.push(`${maybeQuoteString(key)}: ${inspectedValue}`);
} else {
let descriptor = Object.getOwnPropertyDescriptor(value, key);
if (descriptor.get !== undefined && descriptor.set !== undefined) {
entries.push(`${maybeQuoteString(key)}: [Getter/Setter]`);
} else if (descriptor.get !== undefined) {
entries.push(`${maybeQuoteString(key)}: [Getter]`);
} else {
entries.push(
`${maybeQuoteString(key)}: ${
inspectValueWithQuotes(value[key], ctx, level + 1, inspectOptions)
}`,
);
}
}
}
for (const key of symbolKeys) {
if (inspectOptions.getters) {
let propertyValue;
let error;
try {
@ -781,10 +804,30 @@
error = error_;
}
const inspectedValue = error == null
? inspectValueWithQuotes(propertyValue, ctx, level + 1, inspectOptions)
? inspectValueWithQuotes(
propertyValue,
ctx,
level + 1,
inspectOptions,
)
: red(`Thrown ${error.name}: ${error.message}`);
entries.push(`[${maybeQuoteSymbol(key)}]: ${inspectedValue}`);
} else {
let descriptor = Object.getOwnPropertyDescriptor(value, key);
if (descriptor.get !== undefined && descriptor.set !== undefined) {
entries.push(`[${maybeQuoteSymbol(key)}]: [Getter/Setter]`);
} else if (descriptor.get !== undefined) {
entries.push(`[${maybeQuoteSymbol(key)}]: [Getter]`);
} else {
entries.push(
`[${maybeQuoteSymbol(key)}]: ${
inspectValueWithQuotes(value[key], ctx, level + 1, inspectOptions)
}`,
);
}
}
}
// Making sure color codes are ignored when calculating the total length
const totalLength = entries.length + level +
colors.stripColor(entries.join("")).length;

View file

@ -1494,14 +1494,32 @@ unitTest(function inspectString(): void {
);
});
unitTest(function inspectGetterError(): void {
unitTest(function inspectGetters(): void {
assertEquals(
stripColor(Deno.inspect({
get foo() {
return 0;
},
})),
"{ foo: [Getter] }",
);
assertEquals(
stripColor(Deno.inspect({
get foo() {
return 0;
},
}, { getters: true })),
"{ foo: 0 }",
);
assertEquals(
Deno.inspect({
// deno-lint-ignore getter-return
get foo() {
throw new Error("bar");
},
}),
}, { getters: true }),
"{ foo: [Thrown Error: bar] }",
);
});