Skip to content

Commit

Permalink
fix(utils): fix color util maximum call stack error (#5733)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored May 17, 2024
1 parent c469c74 commit a4ec583
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 8 additions & 4 deletions packages/utils/src/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ export function createColors(isTTY = false): Colors {
|| 'CI' in process.env)

const replaceClose = (string: string, close: string, replace: string, index: number): string => {
const start = string.substring(0, index) + replace
const end = string.substring(index + close.length)
const nextIndex = end.indexOf(close)
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
let result = ''
let cursor = 0
do {
result += string.substring(cursor, index) + replace
cursor = index + close.length
index = string.indexOf(close, cursor)
} while (~index)
return result + string.substring(cursor)
}

const formatter = (open: string, close: string, replace = open) => {
Expand Down
12 changes: 11 additions & 1 deletion test/core/test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeAll, describe, expect, test } from 'vitest'
import { assertTypes, deepClone, objDisplay, objectAttr, toArray } from '@vitest/utils'
import { assertTypes, createColors, deepClone, objDisplay, objectAttr, toArray } from '@vitest/utils'
import { deepMerge, resetModules } from '../../../packages/vitest/src/utils'
import { deepMergeSnapshot } from '../../../packages/snapshot/src/port/utils'
import type { EncodedSourceMap } from '../../../packages/vite-node/src/types'
Expand Down Expand Up @@ -285,3 +285,13 @@ describe('objDisplay', () => {
expect(objDisplay(value)).toEqual(expected)
})
})

describe(createColors, () => {
test('no maximum call stack error', () => {
process.env.FORCE_COLOR = '1'
delete process.env.GITHUB_ACTIONS
const c = createColors()
expect(c.isColorSupported).toBe(true)
expect(c.blue(c.blue('x').repeat(10000))).toBeTruthy()
})
})

0 comments on commit a4ec583

Please sign in to comment.