Skip to content

Commit

Permalink
Get rid of eval()
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jul 15, 2024
1 parent b758a36 commit 6cd8800
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To be released.
`node:fs` module.
- LogTape now works well with JavaScript runtimes that do not support
`WeakRef` class.
- Got rid of `eval()` from LogTape.

[#5]: https://github.com/dahlia/logtape/issues/5

Expand Down
11 changes: 8 additions & 3 deletions logtape/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ const levelAbbreviations: Record<LogLevel, string> = {
* @param value The value to inspect.
* @returns The string representation of the value.
*/
const inspect: (value: unknown) => string = eval(`(
const inspect: (value: unknown) => string =
// @ts-ignore: Deno global
"Deno" in globalThis && "inspect" in globalThis.Deno &&
// @ts-ignore: Deno global
typeof globalThis.Deno.inspect === "function"
// @ts-ignore: Deno global
? globalThis.Deno.inspect
// @ts-ignore: Node.js global
: "util" in globalThis && "inspect" in globalThis.util &&
// @ts-ignore: Node.js global
globalThis.util.inspect === "function"
// @ts-ignore: Node.js global
? globalThis.util.inspect
: JSON.stringify
)`);
: JSON.stringify;

/**
* The default text formatter. This formatter formats log records as follows:
Expand Down

0 comments on commit 6cd8800

Please sign in to comment.