Skip to content

Commit 5f75790

Browse files
authored
refactor: replace strip-ansi with stripVTControlCharacters from node:util (#6608)
1 parent 9edfa19 commit 5f75790

26 files changed

+72
-96
lines changed

packages/vitest/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@
200200
"micromatch": "^4.0.7",
201201
"pretty-format": "^29.7.0",
202202
"prompts": "^2.4.2",
203-
"strip-ansi": "^7.1.0",
204203
"strip-literal": "^2.1.0",
205204
"tinyglobby": "^0.2.6",
206205
"ws": "^8.18.0"

packages/vitest/src/node/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-disable prefer-template */
22
import { existsSync, readFileSync } from 'node:fs'
33
import { Writable } from 'node:stream'
4+
import { stripVTControlCharacters } from 'node:util'
45
import { normalize, relative } from 'pathe'
56
import c from 'tinyrainbow'
67
import cliTruncate from 'cli-truncate'
78
import type { ErrorWithDiff, ParsedStack } from '@vitest/utils'
89
import { inspect } from '@vitest/utils'
9-
import stripAnsi from 'strip-ansi'
1010
import {
1111
lineSplitRE,
1212
positionToOffset,
@@ -398,7 +398,7 @@ export function generateCodeFrame(
398398
const lineLength = lines[j].length
399399

400400
// too long, maybe it's a minified file, skip for codeframe
401-
if (stripAnsi(lines[j]).length > 200) {
401+
if (stripVTControlCharacters(lines[j]).length > 200) {
402402
return ''
403403
}
404404

packages/vitest/src/node/reporters/benchmark/table/tableRender.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { stripVTControlCharacters } from 'node:util'
12
import c from 'tinyrainbow'
23
import cliTruncate from 'cli-truncate'
3-
import stripAnsi from 'strip-ansi'
44
import type { Task } from '@vitest/runner'
55
import { getTests, notNullish } from '../../../../utils'
66
import { F_RIGHT } from '../../../../utils/figures'
@@ -75,7 +75,7 @@ function renderBenchmarkItems(result: BenchmarkResult) {
7575
function computeColumnWidths(results: BenchmarkResult[]): number[] {
7676
const rows = [tableHead, ...results.map(v => renderBenchmarkItems(v))]
7777
return Array.from(tableHead, (_, i) =>
78-
Math.max(...rows.map(row => stripAnsi(row[i]).length)))
78+
Math.max(...rows.map(row => stripVTControlCharacters(row[i]).length)))
7979
}
8080

8181
function padRow(row: string[], widths: number[]) {
@@ -215,7 +215,7 @@ export function renderTree(
215215
if (task.result?.state !== 'pass' && outputMap.get(task) != null) {
216216
let data: string | undefined = outputMap.get(task)
217217
if (typeof data === 'string') {
218-
data = stripAnsi(data.trim().split('\n').filter(Boolean).pop()!)
218+
data = stripVTControlCharacters(data.trim().split('\n').filter(Boolean).pop()!)
219219
if (data === '') {
220220
data = undefined
221221
}

packages/vitest/src/node/reporters/github-actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { stripVTControlCharacters } from 'node:util'
12
import { getTasks } from '@vitest/runner/utils'
2-
import stripAnsi from 'strip-ansi'
33
import type { File } from '@vitest/runner'
44
import { getFullName } from '../../utils'
55
import { capturePrintError } from '../error'
@@ -64,7 +64,7 @@ export class GithubActionsReporter implements Reporter {
6464
line: String(stack.line),
6565
column: String(stack.column),
6666
},
67-
message: stripAnsi(result.output),
67+
message: stripVTControlCharacters(result.output),
6868
})
6969
this.ctx.logger.log(`\n${formatted}`)
7070
}

packages/vitest/src/node/reporters/junit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { existsSync, promises as fs } from 'node:fs'
22
import { hostname } from 'node:os'
3+
import { stripVTControlCharacters } from 'node:util'
34
import { dirname, relative, resolve } from 'pathe'
45

56
import type { Task } from '@vitest/runner'
67
import { getSuites } from '@vitest/runner/utils'
7-
import stripAnsi from 'strip-ansi'
88
import type { Vitest } from '../core'
99
import type { Reporter } from '../types/reporter'
1010
import { getOutputFile } from '../../utils/config-helpers'
@@ -233,7 +233,7 @@ export class JUnitReporter implements Reporter {
233233
{ project: this.ctx.getProjectByTaskId(task.id), task },
234234
)
235235
await this.baseLog(
236-
escapeXML(stripAnsi(result.output.trim())),
236+
escapeXML(stripVTControlCharacters(result.output.trim())),
237237
)
238238
},
239239
)

packages/vitest/src/node/reporters/renderers/listRenderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { stripVTControlCharacters } from 'node:util'
12
import c from 'tinyrainbow'
23
import cliTruncate from 'cli-truncate'
3-
import stripAnsi from 'strip-ansi'
44
import type { SuiteHooks, Task } from '@vitest/runner'
55
import { getTests, notNullish } from '../../../utils'
66
import { F_RIGHT } from '../../../utils/figures'
@@ -183,7 +183,7 @@ function renderTree(
183183
if (task.result?.state !== 'pass' && outputMap.get(task) != null) {
184184
let data: string | undefined = outputMap.get(task)
185185
if (typeof data === 'string') {
186-
data = stripAnsi(data.trim().split('\n').filter(Boolean).pop()!)
186+
data = stripVTControlCharacters(data.trim().split('\n').filter(Boolean).pop()!)
187187
if (data === '') {
188188
data = undefined
189189
}

packages/vitest/src/node/reporters/renderers/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { stripVTControlCharacters } from 'node:util'
12
import { basename, dirname, isAbsolute, relative } from 'pathe'
23
import c from 'tinyrainbow'
3-
import stripAnsi from 'strip-ansi'
44
import type { SuiteHooks, Task } from '@vitest/runner'
55
import type { SnapshotSummary } from '@vitest/snapshot'
66
import { slash } from '../../../utils/base'
@@ -37,7 +37,7 @@ export function divider(text?: string, left?: number, right?: number) {
3737
const cols = getCols()
3838

3939
if (text) {
40-
const textLength = stripAnsi(text).length
40+
const textLength = stripVTControlCharacters(text).length
4141
if (left == null && right != null) {
4242
left = cols - textLength - right
4343
}

packages/vitest/src/node/watch-filter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import readline from 'node:readline'
22
import type { Writable } from 'node:stream'
3+
import { stripVTControlCharacters } from 'node:util'
34
import c from 'tinyrainbow'
4-
import stripAnsi from 'strip-ansi'
55
import { createDefer } from '@vitest/utils'
66
import { stdout as getStdout } from '../utils'
77

@@ -198,7 +198,7 @@ export class WatchFilter {
198198
const columns = 'columns' in this.stdout ? this.stdout.columns : 80
199199

200200
// We have to take care of screen width in case of long lines
201-
rows += 1 + Math.floor(Math.max(stripAnsi(line).length - 1, 0) / columns)
201+
rows += 1 + Math.floor(Math.max(stripVTControlCharacters(line).length - 1, 0) / columns)
202202
}
203203

204204
this.write(`${ESC}1G`) // move to the beginning of the line

pnpm-lock.yaml

Lines changed: 0 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/browser/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"playwright": "^1.41.0",
2929
"react": "^18.3.1",
3030
"react-dom": "^18.3.1",
31-
"strip-ansi": "^7.1.0",
3231
"url": "^0.11.3",
3332
"vitest": "workspace:*",
3433
"vitest-browser-react": "^0.0.1",

0 commit comments

Comments
 (0)