Skip to content

Commit

Permalink
fix: formatting of static members
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 21, 2024
1 parent dd2530f commit c438a75
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions examples/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { obj } from './values.js'

const html = dump(obj, {
styles: themes.nightOwl,
inspectStaticMembers: true,
collapse: ['DateTime'],
})

Expand Down
7 changes: 7 additions & 0 deletions examples/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class User {
return this.#attributes.username
}

static keys = {
a: 1,
b: 2,
}

static attributes = new Map([['id', { name: 'id' }]])

static booted = false
static boot() {
this.booted = true
Expand Down
23 changes: 16 additions & 7 deletions formatters/console/printers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const ConsolePrinters: TokenPrinters = {
formatter.indentation.increment()
const styles = formatter.styles.objectLabel
const label =
formatter.context.isStaticMember || token.constructorName === 'Object'
(formatter.context.isStaticMember && formatter.context.staticDepth === 0) ||
token.constructorName === 'Object'
? ''
: styles(`${token.constructorName || 'Object [null]'}`) + ' '

Expand Down Expand Up @@ -79,8 +80,11 @@ export const ConsolePrinters: TokenPrinters = {
*/
let prefix = ''
if (formatter.context.isStaticMember) {
const prefixStyles = formatter.styles.objectKeyPrefix
prefix = `${prefixStyles('static')} `
formatter.context.staticDepth++
if (formatter.context.staticDepth === 1) {
const prefixStyles = formatter.styles.objectKeyPrefix
prefix = `${prefixStyles('static')} `
}
}

return indent + prefix + styles(value) + ': '
Expand All @@ -105,7 +109,10 @@ export const ConsolePrinters: TokenPrinters = {
return ''
},

'object-value-end': () => {
'object-value-end': (_, formatter) => {
if (formatter.context.isStaticMember) {
formatter.context.staticDepth--
}
return `,`
},

Expand Down Expand Up @@ -179,7 +186,7 @@ export const ConsolePrinters: TokenPrinters = {
'map-start': (token, formatter) => {
formatter.indentation.increment()
const styles = formatter.styles.mapLabel
const label = `Map:${token.size} `
const label = `Map(${token.size}) `

return styles(label) + openingBrace(formatter)
},
Expand Down Expand Up @@ -248,7 +255,7 @@ export const ConsolePrinters: TokenPrinters = {
'set-start': (token, formatter) => {
formatter.indentation.increment()
const styles = formatter.styles.setLabel
const label = `Set:${token.size} `
const label = `Set(${token.size}) `

return styles(label) + openingBrackets(formatter)
},
Expand Down Expand Up @@ -432,11 +439,13 @@ export const ConsolePrinters: TokenPrinters = {

'static-members-start': function (_, formatter): string {
formatter.context.isStaticMember = true
return ''
formatter.context.staticDepth = 0
return ' '
},

'static-members-end': function (_, formatter): string {
formatter.context.isStaticMember = false
formatter.context.staticDepth = 0
return ''
},
}
22 changes: 16 additions & 6 deletions formatters/html/printers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ export const HTMLPrinters: TokenPrinters = {
formatter.indentation.increment()
const styles = formatter.styles.objectLabel
const toggleStyles = formatter.styles.toggle
const label = formatter.context.isStaticMember
? ' '
: `${token.constructorName || 'Object [null]'} `
const label =
formatter.context.isStaticMember && formatter.context.staticDepth === 0
? ' '
: `${token.constructorName || 'Object [null]'} `

return (
'<span class="dumper-group dumper-object-group">' +
Expand Down Expand Up @@ -91,8 +92,12 @@ export const HTMLPrinters: TokenPrinters = {
*/
let prefix = ''
if (formatter.context.isStaticMember) {
const prefixStyles = formatter.styles.objectKeyPrefix
prefix = `<span class="dumper-object-prefix" style="${prefixStyles}">` + `static ` + '</span>'
formatter.context.staticDepth++
if (formatter.context.staticDepth === 1) {
const prefixStyles = formatter.styles.objectKeyPrefix
prefix =
`<span class="dumper-object-prefix" style="${prefixStyles}">` + `static ` + '</span>'
}
}

return (
Expand Down Expand Up @@ -123,7 +128,10 @@ export const HTMLPrinters: TokenPrinters = {
return ''
},

'object-value-end': () => {
'object-value-end': (_, formatter) => {
if (formatter.context.isStaticMember) {
formatter.context.staticDepth--
}
return `,`
},

Expand Down Expand Up @@ -504,11 +512,13 @@ export const HTMLPrinters: TokenPrinters = {

'static-members-start': function (_, formatter): string {
formatter.context.isStaticMember = true
formatter.context.staticDepth = 0
return ''
},

'static-members-end': function (_, formatter): string {
formatter.context.isStaticMember = false
formatter.context.staticDepth = 0
return ''
},
}

0 comments on commit c438a75

Please sign in to comment.