Skip to content

Commit a4ec583

Browse files
authored
fix(utils): fix color util maximum call stack error (#5733)
1 parent c469c74 commit a4ec583

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

packages/utils/src/colors.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ export function createColors(isTTY = false): Colors {
7474
|| 'CI' in process.env)
7575

7676
const replaceClose = (string: string, close: string, replace: string, index: number): string => {
77-
const start = string.substring(0, index) + replace
78-
const end = string.substring(index + close.length)
79-
const nextIndex = end.indexOf(close)
80-
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
77+
let result = ''
78+
let cursor = 0
79+
do {
80+
result += string.substring(cursor, index) + replace
81+
cursor = index + close.length
82+
index = string.indexOf(close, cursor)
83+
} while (~index)
84+
return result + string.substring(cursor)
8185
}
8286

8387
const formatter = (open: string, close: string, replace = open) => {

test/core/test/utils.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { beforeAll, describe, expect, test } from 'vitest'
2-
import { assertTypes, deepClone, objDisplay, objectAttr, toArray } from '@vitest/utils'
2+
import { assertTypes, createColors, deepClone, objDisplay, objectAttr, toArray } from '@vitest/utils'
33
import { deepMerge, resetModules } from '../../../packages/vitest/src/utils'
44
import { deepMergeSnapshot } from '../../../packages/snapshot/src/port/utils'
55
import type { EncodedSourceMap } from '../../../packages/vite-node/src/types'
@@ -285,3 +285,13 @@ describe('objDisplay', () => {
285285
expect(objDisplay(value)).toEqual(expected)
286286
})
287287
})
288+
289+
describe(createColors, () => {
290+
test('no maximum call stack error', () => {
291+
process.env.FORCE_COLOR = '1'
292+
delete process.env.GITHUB_ACTIONS
293+
const c = createColors()
294+
expect(c.isColorSupported).toBe(true)
295+
expect(c.blue(c.blue('x').repeat(10000))).toBeTruthy()
296+
})
297+
})

0 commit comments

Comments
 (0)