Skip to content

Commit 6bfdb5f

Browse files
authored
refactor: improve highlighted function (#4967)
1 parent dcf2e9f commit 6bfdb5f

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

packages/vitest/src/node/hoistMocks.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import type { AwaitExpression, CallExpression, Identifier, ImportDeclaration, Va
55
import { findNodeAround } from 'acorn-walk'
66
import type { PluginContext } from 'rollup'
77
import { esmWalker } from '@vitest/utils/ast'
8-
import { highlight } from '@vitest/utils'
8+
import type { Colors } from '@vitest/utils'
9+
import { highlightCode } from '../utils/colors'
910
import { generateCodeFrame } from './error'
1011

1112
export type Positioned<T> = T & {
@@ -62,7 +63,7 @@ export function getBetterEnd(code: string, node: Node) {
6263
const regexpHoistable = /[ \t]*\b(vi|vitest)\s*\.\s*(mock|unmock|hoisted)\(/
6364
const hashbangRE = /^#!.*\n/
6465

65-
export function hoistMocks(code: string, id: string, parse: PluginContext['parse']) {
66+
export function hoistMocks(code: string, id: string, parse: PluginContext['parse'], colors?: Colors) {
6667
const needHoisting = regexpHoistable.test(code)
6768

6869
if (!needHoisting)
@@ -256,7 +257,7 @@ export function hoistMocks(code: string, id: string, parse: PluginContext['parse
256257
name: 'SyntaxError',
257258
message: _error.message,
258259
stack: _error.stack,
259-
frame: generateCodeFrame(highlight(code), 4, insideCall.start + 1),
260+
frame: generateCodeFrame(highlightCode(id, code, colors), 4, insideCall.start + 1),
260261
}
261262
throw error
262263
}

packages/vitest/src/node/logger.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { createLogUpdate } from 'log-update'
22
import c from 'picocolors'
3-
import { highlight } from '@vitest/utils'
4-
import { extname } from 'pathe'
53
import { version } from '../../../../package.json'
64
import type { ErrorWithDiff } from '../types'
75
import type { TypeCheckError } from '../typecheck/typechecker'
86
import { toArray } from '../utils'
7+
import { highlightCode } from '../utils/colors'
98
import { divider } from './reporters/renderers/utils'
109
import { RandomSequencer } from './sequencers/RandomSequencer'
1110
import type { Vitest } from './core'
@@ -23,14 +22,6 @@ const ERASE_DOWN = `${ESC}J`
2322
const ERASE_SCROLLBACK = `${ESC}3J`
2423
const CURSOR_TO_START = `${ESC}1;1H`
2524
const CLEAR_SCREEN = '\x1Bc'
26-
const HIGHLIGHT_SUPPORTED_EXTS = new Set(['js', 'ts'].flatMap(lang => [
27-
`.${lang}`,
28-
`.m${lang}`,
29-
`.c${lang}`,
30-
`.${lang}x`,
31-
`.m${lang}x`,
32-
`.c${lang}x`,
33-
]))
3425

3526
export class Logger {
3627
outputStream = process.stdout
@@ -112,11 +103,7 @@ export class Logger {
112103
highlight(filename: string, source: string) {
113104
if (this._highlights.has(filename))
114105
return this._highlights.get(filename)!
115-
const ext = extname(filename)
116-
if (!HIGHLIGHT_SUPPORTED_EXTS.has(ext))
117-
return source
118-
const isJsx = ext.endsWith('x')
119-
const code = highlight(source, { jsx: isJsx, colors: c })
106+
const code = highlightCode(filename, source)
120107
this._highlights.set(filename, code)
121108
return code
122109
}

packages/vitest/src/utils/colors.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Colors } from '@vitest/utils'
2+
import { highlight } from '@vitest/utils'
3+
import { extname } from 'pathe'
4+
import c from 'picocolors'
5+
6+
const HIGHLIGHT_SUPPORTED_EXTS = new Set(['js', 'ts'].flatMap(lang => [
7+
`.${lang}`,
8+
`.m${lang}`,
9+
`.c${lang}`,
10+
`.${lang}x`,
11+
`.m${lang}x`,
12+
`.c${lang}x`,
13+
]))
14+
15+
export function highlightCode(
16+
id: string,
17+
source: string,
18+
colors?: Colors,
19+
) {
20+
const ext = extname(id)
21+
if (!HIGHLIGHT_SUPPORTED_EXTS.has(ext))
22+
return source
23+
const isJsx = ext.endsWith('x')
24+
return highlight(source, { jsx: isJsx, colors: colors || c })
25+
}

test/core/test/injector-mock.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parseAst } from 'rollup/parseAst'
22
import { describe, expect, it, test } from 'vitest'
33
import stripAnsi from 'strip-ansi'
4+
import { getDefaultColors } from '@vitest/utils'
45
import { hoistMocks } from '../../../packages/vitest/src/node/hoistMocks'
56

67
function parse(code: string, options: any) {
@@ -1205,7 +1206,7 @@ await vi
12051206
describe('throws an error when nodes are incompatible', () => {
12061207
const getErrorWhileHoisting = (code: string) => {
12071208
try {
1208-
hoistSimpleCode(code)
1209+
hoistMocks(code, '/test.js', parse, getDefaultColors())?.code.trim()
12091210
}
12101211
catch (err: any) {
12111212
return err

0 commit comments

Comments
 (0)