Skip to content

Commit

Permalink
Fix dispose logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-rowe committed Aug 24, 2024
1 parent ff14947 commit c7c5164
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 34 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish

on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- run: npx jsr publish
38 changes: 20 additions & 18 deletions bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ import { createCustomInspect } from './utils.ts'

const LINE_LENGTH = 16

export const inspectBytes = createCustomInspect(function inspectBytes(this: Uint8Array, options) {
if (options.currentDepth > options.depth) {
return `${colors.cyan(`[${this[Symbol.toStringTag]}]`)}`
}
export const inspectBytes: (this: Uint8Array, ...args: unknown[]) => string = createCustomInspect(
function inspectBytes(this: Uint8Array, options) {
if (options.currentDepth > options.depth) {
return `${colors.cyan(`[${this[Symbol.toStringTag]}]`)}`
}

const lines = debugBinary(this, {
maxLines: Math.ceil(options.iterableLimit / LINE_LENGTH),
}).split('\n')
const lines = debugBinary(this, {
maxLines: Math.ceil(options.iterableLimit / LINE_LENGTH),
}).split('\n')

const out = `${this[Symbol.toStringTag]}(${this.length}) [${
this.length
? `\n${
lines
.map((x) => ' '.repeat(2) + x).join('\n')
}\n`
: ''
}]`
const out = `${this[Symbol.toStringTag]}(${this.length}) [${
this.length
? `\n${
lines
.map((x) => ' '.repeat(2) + x).join('\n')
}\n`
: ''
}]`

const outLines = out.split('\n')
const outLines = out.split('\n')

return [outLines[0], ...outLines.slice(1).map((x) => ' '.repeat(options.indentationLvl) + x)].join('\n')
})
return [outLines[0], ...outLines.slice(1).map((x) => ' '.repeat(options.indentationLvl) + x)].join('\n')
},
)

type DebugBinaryOptions = {
maxLines: number
Expand Down
8 changes: 8 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"lock": false,
"name": "@li/custom-inspects",
"version": "0.0.0",
"exports": {
".": "./mod.ts",
"./bytes.ts": "./bytes.ts",
"./minimal.ts": "./minimal.ts",
"./utils.ts": "./utils.ts"
},
"tasks": {},
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion minimal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as colors from '@std/fmt/colors'
import { createCustomInspect } from './utils.ts'

export const inspectMinimal = createCustomInspect(
export const inspectMinimal: (this: unknown, ...args: unknown[]) => string = createCustomInspect(
function inspectMinimal(this: unknown, _options) {
// @ts-expect-error name
return colors.cyan(`${this[Symbol.toStringTag] ?? this.constructor.name ?? this.name} <${this.toString()}>`)
Expand Down
28 changes: 28 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ Deno.test(inspectMinimal.name, () => {
assertEquals(Deno.inspect(new Intl.Locale('en-US'), { colors: true }), '\x1b[36mIntl.Locale <en-US>\x1b[39m')
})

Deno.test(String(Symbol.dispose.description), () => {
{
const fn = () => ':)'
using _ = patch(Array.prototype, fn)

{
using _ = patch(Array.prototype, inspectMinimal)
assertEquals(Deno.inspect([0, 1]), 'Array <0,1>')
// @ts-expect-error custom inspect
assertEquals(Array.prototype[Symbol.for('Deno.customInspect')], inspectMinimal)
// @ts-expect-error custom inspect
assertEquals(Array.prototype[Symbol.for('nodejs.util.inspect.custom')], inspectMinimal)
}

assertEquals(Deno.inspect([0, 1]), ':)')
// @ts-expect-error custom inspect
assertEquals(Array.prototype[Symbol.for('Deno.customInspect')], fn)
// @ts-expect-error custom inspect
assertEquals(Array.prototype[Symbol.for('nodejs.util.inspect.custom')], fn)
}

assertEquals(Deno.inspect([0, 1]), '[ 0, 1 ]')
// @ts-expect-error custom inspect
assertEquals(Array.prototype[Symbol.for('Deno.customInspect')], undefined)
// @ts-expect-error custom inspect
assertEquals(Array.prototype[Symbol.for('nodejs.util.inspect.custom')], undefined)
})

Deno.test(inspectBytes.name, async (t) => {
using _ = patch(Uint8Array.prototype, inspectBytes)

Expand Down
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './minimal.ts'
export * from './bytes.ts'
export * from './minimal.ts'
export * from './utils.ts'
35 changes: 21 additions & 14 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ function getOptions(args: unknown[]): InspectOptions {
return inspectOptions
}

export function createCustomInspect<T>(fn: (this: T, options: InspectOptions) => string) {
export function createCustomInspect<T>(
fn: (this: T, options: InspectOptions) => string,
): (this: T, ...args: unknown[]) => string {
const inspect = function (this: T, ...args: unknown[]) {
const options = getOptions(args)
using _ = handleColor(options)
Expand All @@ -65,23 +67,28 @@ export type CustomInspect<T> = ReturnType<typeof createCustomInspect<T>>
export function patch<T>(
obj: T,
customInspect: CustomInspect<T>,
) {
for (
const sym of [
Symbol.for('Deno.customInspect'),
Symbol.for('nodejs.util.inspect.custom'),
]
) {
): {
[Symbol.dispose](): void
} {
const syms = [
Symbol.for('Deno.customInspect'),
Symbol.for('nodejs.util.inspect.custom'),
]
const oldVals: unknown[] = []

for (const sym of syms) {
// @ts-expect-error custom inspect
const oldVal = obj[sym]
oldVals.push(obj[sym])
// @ts-expect-error custom inspect
obj[sym] = customInspect
}

return {
[Symbol.dispose]() {
return {
[Symbol.dispose]() {
for (const [idx, sym] of syms.entries()) {
// @ts-expect-error custom inspect
obj[sym] = oldVal
},
}
obj[sym] = oldVals[idx]
}
},
}
}

0 comments on commit c7c5164

Please sign in to comment.