Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/printer/src/formatters/escapeQuotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function escapeQuotes(str: string, quotes: string) {
if (!quotes) return str
if (quotes == '"') return JSON.stringify(str)
str = str.split(quotes).join('\\' + quotes)
return `${quotes}${str}${quotes}`
}
9 changes: 4 additions & 5 deletions packages/printer/src/formatters/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isString } from '@logpot/utils'
import { toColorizer } from '../consoleColor'
import { PrintContext } from '../printContext'
import { Printer } from '../printer'
import { escapeQuotes } from './escapeQuotes'
import { getText } from './getText'

// A common interface for all formatters.
Expand Down Expand Up @@ -41,10 +42,8 @@ export class StringFormatter extends PrimitiveFormatter {
return isString(value)
}
format(value: unknown, ctx: PrintContext) {
return getText(
ctx,
toColorizer(ctx.colorConfig.string)(`${ctx.quotes}${value}${ctx.quotes}`)
)
const str = escapeQuotes(value as string, ctx.quotes)
return getText(ctx, toColorizer(ctx.colorConfig.string)(str))
}
}

Expand Down Expand Up @@ -147,7 +146,7 @@ export class DateFormatter extends PrimitiveFormatter {
case 'epoch':
return colorizer(date.getTime().toString())
}
return getText(ctx, colorizer(`${ctx.quotes}${str}${ctx.quotes}`))
return getText(ctx, colorizer(escapeQuotes(str, ctx.quotes)))
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/printer/src/formatters/objectFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isEmptyPlainObject, isPlainObject } from '@logpot/utils'
import { toColorizer } from '../consoleColor'
import { PrintContext } from '../printContext'
import { Printer } from '../printer'
import { escapeQuotes } from './escapeQuotes'
import { IFormatter } from './formatter'
import { getText } from './getText'
import { ObjectFormatterConfig } from './objectFormatterConfig'
Expand Down Expand Up @@ -84,7 +85,7 @@ export class ObjectFormatter implements IFormatter {
printer: Printer
): string {
const coloredKey = toColorizer(ctx.colorConfig.key)(
`${ctx.quotes}${key}${ctx.quotes}`
escapeQuotes(key, ctx.quotes)
)

let indent = ctx.indent
Expand Down
50 changes: 50 additions & 0 deletions packages/printer/src/formatters/stringFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, it } from 'vitest'

import { colorlessConfig } from '../colorConfig'
import { PrintContext } from '../printContext'
import { StringFormatter } from './formatter'

const makeCtx = (
overrides: Partial<Pick<PrintContext, 'quotes'>> = {}
): PrintContext => ({
indent: '',
maxIndent: 10,
indentString: ' ',
colorConfig: colorlessConfig,
seen: new WeakSet(),
quotes: overrides.quotes ?? '"',
keys: [],
prefix: '',
})

describe('StringFormatter', () => {
it('escapes double quotes when using double quotes', () => {
const fmt = new StringFormatter()
const ctx = makeCtx()
const value = 'a "quote" b'
const result = fmt.format(value, ctx)
const expected = `${ctx.quotes}${value
.split(ctx.quotes)
.join(`\\${ctx.quotes}`)}${ctx.quotes}`
expect(result).toBe(expected)
})

it('escapes single quotes when using single quotes', () => {
const fmt = new StringFormatter()
const ctx = makeCtx({ quotes: "'" })
const value = "a 'quote' b"
const result = fmt.format(value, ctx)
const expected = `${ctx.quotes}${value
.split(ctx.quotes)
.join(`\\${ctx.quotes}`)}${ctx.quotes}`
expect(result).toBe(expected)
})

it('does not escape when quotes are disabled', () => {
const fmt = new StringFormatter()
const ctx = makeCtx({ quotes: '' })
const value = 'a "quote" b'
const result = fmt.format(value, ctx)
expect(result).toBe(value)
})
})