mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 21:50:00 -05:00
fix(cli/console): only inspect getters with option (#7830)
This commit is contained in:
parent
08f3ae92d3
commit
86dc55134e
3 changed files with 85 additions and 22 deletions
2
cli/dts/lib.deno.ns.d.ts
vendored
2
cli/dts/lib.deno.ns.d.ts
vendored
|
@ -1956,6 +1956,8 @@ declare namespace Deno {
|
||||||
sorted?: boolean;
|
sorted?: boolean;
|
||||||
/** Add a trailing comma for multiline collections. Defaults to false. */
|
/** Add a trailing comma for multiline collections. Defaults to false. */
|
||||||
trailingComma?: boolean;
|
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
|
/** Converts the input into a string that has the same format as printed by
|
||||||
|
|
|
@ -157,6 +157,7 @@
|
||||||
iterableLimit: 100,
|
iterableLimit: 100,
|
||||||
showProxy: false,
|
showProxy: false,
|
||||||
colors: false,
|
colors: false,
|
||||||
|
getters: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const DEFAULT_INDENT = " "; // Default indent string
|
const DEFAULT_INDENT = " "; // Default indent string
|
||||||
|
@ -760,31 +761,73 @@
|
||||||
const red = maybeColor(colors.red, inspectOptions);
|
const red = maybeColor(colors.red, inspectOptions);
|
||||||
|
|
||||||
for (const key of stringKeys) {
|
for (const key of stringKeys) {
|
||||||
let propertyValue;
|
if (inspectOptions.getters) {
|
||||||
let error = null;
|
let propertyValue;
|
||||||
try {
|
let error = null;
|
||||||
propertyValue = value[key];
|
try {
|
||||||
} catch (error_) {
|
propertyValue = value[key];
|
||||||
error = error_;
|
} catch (error_) {
|
||||||
|
error = error_;
|
||||||
|
}
|
||||||
|
const inspectedValue = error == null
|
||||||
|
? 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)
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const inspectedValue = error == null
|
|
||||||
? inspectValueWithQuotes(propertyValue, ctx, level + 1, inspectOptions)
|
|
||||||
: red(`[Thrown ${error.name}: ${error.message}]`);
|
|
||||||
entries.push(`${maybeQuoteString(key)}: ${inspectedValue}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key of symbolKeys) {
|
for (const key of symbolKeys) {
|
||||||
let propertyValue;
|
if (inspectOptions.getters) {
|
||||||
let error;
|
let propertyValue;
|
||||||
try {
|
let error;
|
||||||
propertyValue = value[key];
|
try {
|
||||||
} catch (error_) {
|
propertyValue = value[key];
|
||||||
error = error_;
|
} catch (error_) {
|
||||||
|
error = error_;
|
||||||
|
}
|
||||||
|
const inspectedValue = error == null
|
||||||
|
? 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)
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const inspectedValue = error == null
|
|
||||||
? inspectValueWithQuotes(propertyValue, ctx, level + 1, inspectOptions)
|
|
||||||
: red(`Thrown ${error.name}: ${error.message}`);
|
|
||||||
entries.push(`[${maybeQuoteSymbol(key)}]: ${inspectedValue}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Making sure color codes are ignored when calculating the total length
|
// Making sure color codes are ignored when calculating the total length
|
||||||
const totalLength = entries.length + level +
|
const totalLength = entries.length + level +
|
||||||
colors.stripColor(entries.join("")).length;
|
colors.stripColor(entries.join("")).length;
|
||||||
|
|
|
@ -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(
|
assertEquals(
|
||||||
Deno.inspect({
|
Deno.inspect({
|
||||||
// deno-lint-ignore getter-return
|
// deno-lint-ignore getter-return
|
||||||
get foo() {
|
get foo() {
|
||||||
throw new Error("bar");
|
throw new Error("bar");
|
||||||
},
|
},
|
||||||
}),
|
}, { getters: true }),
|
||||||
"{ foo: [Thrown Error: bar] }",
|
"{ foo: [Thrown Error: bar] }",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue