@@ -4,18 +4,15 @@ import { TextCase } from './types';
44import { flatMap } from './utils' ;
55import { RunVitestCommand , DebugVitestCommand } from './vscode' ;
66
7- const caseText = new Set ( [ 'it' , 'describe' , 'test' ] ) ;
7+ const caseText = new Set ( [ 'it' , 'describe' , 'test' ] ) ;
88
99function tryGetVitestTestCase (
1010 typescript : typeof ts ,
1111 callExpression : ts . CallExpression ,
1212 file : ts . SourceFile
1313) : TextCase | undefined {
14- if ( ! typescript . isIdentifier ( callExpression . expression ) ) {
15- return undefined ;
16- }
17-
18- if ( ! caseText . has ( callExpression . expression . text ) ) {
14+ const each = isEach ( typescript , callExpression ) ;
15+ if ( ! each && ! ( typescript . isIdentifier ( callExpression . expression ) && caseText . has ( ( callExpression . expression as ts . Identifier ) . text ) ) ) {
1916 return undefined ;
2017 }
2118
@@ -32,15 +29,55 @@ function tryGetVitestTestCase(
3229 return undefined ;
3330 }
3431
32+ let testNameText = testName . text ;
33+
34+ const start = callExpression . getStart ( file ) ;
35+ if ( each ) {
36+ //
37+ testNameText = testNameText
38+ // From https://github.com/jestjs/jest/blob/0fd5b1c37555f485c56a6ad2d6b010a72204f9f6/packages/jest-each/src/table/array.ts#L15C32-L15C47
39+ // (Did not find inside vitest source code)
40+ . replace ( / % [ s d i f j o O p # ] / g, '.*' )
41+ // When using template string
42+ . replace ( / \$ [ a - z A - Z _ 0 - 9 ] + / g, '.*' ) ;
43+ }
44+
3545 return {
36- start : testName . getStart ( file ) ,
37- end : testName . getEnd ( ) ,
38- text : testName . text
46+ start,
47+ end : callExpression . getEnd ( ) ,
48+ text : testNameText
3949 } ;
4050}
4151
52+ function isEach ( typescript : typeof ts , callExpression : ts . CallExpression ) {
53+ return isEachWithArray ( typescript , callExpression ) || isEachWithTemplate ( typescript , callExpression ) ;
54+ }
55+
56+ function isEachWithArray ( typescript : typeof ts , callExpression : ts . CallExpression ) {
57+ return (
58+ typescript . isCallExpression ( callExpression . expression ) &&
59+ typescript . isPropertyAccessExpression ( callExpression . expression . expression ) &&
60+ typescript . isIdentifier ( callExpression . expression . expression . expression ) &&
61+ typescript . isIdentifier ( callExpression . expression . expression . name ) &&
62+ callExpression . expression . expression . name . text === 'each' &&
63+ caseText . has ( callExpression . expression . expression . expression . text )
64+ ) ;
65+ }
66+
67+ function isEachWithTemplate ( typescript : typeof ts , callExpression : ts . CallExpression ) {
68+ return (
69+ typescript . isTaggedTemplateExpression ( callExpression . expression ) &&
70+ typescript . isPropertyAccessExpression ( callExpression . expression . tag ) &&
71+ typescript . isIdentifier ( callExpression . expression . tag . expression ) &&
72+ typescript . isIdentifier ( callExpression . expression . tag . name ) &&
73+ callExpression . expression . tag . name . text === 'each' &&
74+ caseText . has ( callExpression . expression . tag . expression . text )
75+ ) ;
76+ }
77+
4278export class CodeLensProvider implements vscode . CodeLensProvider {
43- constructor ( private typescript : typeof ts ) { }
79+ constructor ( private typescript : typeof ts ) {
80+ }
4481
4582 provideCodeLenses (
4683 document : vscode . TextDocument ,
0 commit comments