Skip to content

Commit f6441a9

Browse files
committed
fix: rewrite vitest browser imports to exported paths
1 parent f2f9dab commit f6441a9

4 files changed

Lines changed: 134 additions & 35 deletions

File tree

packages/cli/snap-tests/lint-vite-plus-imports/snap.txt

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
[1]> vp lint src/index.ts src/types.ts # should fail before fix
22

3+
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/test' instead of 'vitest' in Vite+ projects.
4+
╭─[src/types.ts:1:30]
5+
1 │ type TestFn = (typeof import('vitest'))['test'];
6+
· ────────
7+
2 │ type BrowserContext = typeof import('@vitest/browser/context');
8+
╰────
9+
10+
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/test/browser/context' instead of '@vitest/browser/context' in Vite+ projects.
11+
╭─[src/types.ts:2:37]
12+
1 │ type TestFn = (typeof import('vitest'))['test'];
13+
2 │ type BrowserContext = typeof import('@vitest/browser/context');
14+
· ─────────────────────────
15+
3 │ type BrowserClient = typeof import('@vitest/browser/client');
16+
╰────
17+
18+
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/test/client' instead of '@vitest/browser/client' in Vite+ projects.
19+
╭─[src/types.ts:3:36]
20+
2 │ type BrowserContext = typeof import('@vitest/browser/context');
21+
3 │ type BrowserClient = typeof import('@vitest/browser/client');
22+
· ────────────────────────
23+
4 │ type PlaywrightProvider = typeof import('@vitest/browser-playwright/provider');
24+
╰────
25+
26+
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/test/browser/providers/playwright' instead of '@vitest/browser-playwright/provider' in Vite+ projects.
27+
╭─[src/types.ts:4:41]
28+
3 │ type BrowserClient = typeof import('@vitest/browser/client');
29+
4 │ type PlaywrightProvider = typeof import('@vitest/browser-playwright/provider');
30+
· ─────────────────────────────────────
31+
5 │
32+
╰────
33+
34+
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/test/browser-playwright' instead of '@vitest/browser-playwright' in Vite+ projects.
35+
╭─[src/types.ts:6:16]
36+
5 │
37+
6 │ declare module '@vitest/browser-playwright' {}
38+
· ────────────────────────────
39+
7 │ declare module '@vitest/browser-playwright/context' {}
40+
╰────
41+
42+
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/test/browser/context' instead of '@vitest/browser-playwright/context' in Vite+ projects.
43+
╭─[src/types.ts:7:16]
44+
6 │ declare module '@vitest/browser-playwright' {}
45+
7 │ declare module '@vitest/browser-playwright/context' {}
46+
· ────────────────────────────────────
47+
8 │
48+
╰────
49+
50+
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/client' instead of 'vite/client' in Vite+ projects.
51+
╭─[src/types.ts:9:25]
52+
8 │
53+
9 │ import client = require('vite/client');
54+
· ─────────────
55+
10 │
56+
╰────
57+
358
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus' instead of 'vite' in Vite+ projects.
459
╭─[src/index.ts:1:30]
560
1 │ import { defineConfig } from 'vite';
@@ -23,30 +78,7 @@
2378
6 │
2479
╰────
2580

26-
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/test' instead of 'vitest' in Vite+ projects.
27-
╭─[src/types.ts:1:30]
28-
1 │ type TestFn = (typeof import('vitest'))['test'];
29-
· ────────
30-
2 │
31-
╰────
32-
33-
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/test/browser-playwright' instead of '@vitest/browser-playwright' in Vite+ projects.
34-
╭─[src/types.ts:3:16]
35-
2 │
36-
3 │ declare module '@vitest/browser-playwright' {}
37-
· ────────────────────────────
38-
4 │
39-
╰────
40-
41-
× vite-plus(prefer-vite-plus-imports): Use 'vite-plus/client' instead of 'vite/client' in Vite+ projects.
42-
╭─[src/types.ts:5:25]
43-
4 │
44-
5 │ import client = require('vite/client');
45-
· ─────────────
46-
6 │
47-
╰────
48-
49-
Found 0 warnings and 6 errors.
81+
Found 0 warnings and 10 errors.
5082
Finished in <variable>ms on 2 files with <variable> rules using <variable> threads.
5183

5284
> vp lint --fix src/index.ts src/types.ts # rewrite vite and vitest imports via the vite-plus oxlint plugin
@@ -65,12 +97,16 @@ void configPromise;
6597

6698
> cat src/types.ts
6799
type TestFn = (typeof import('vite-plus/test'))['test'];
100+
type BrowserContext = typeof import('vite-plus/test/browser/context');
101+
type BrowserClient = typeof import('vite-plus/test/client');
102+
type PlaywrightProvider = typeof import('vite-plus/test/browser/providers/playwright');
68103

69104
declare module 'vite-plus/test/browser-playwright' {}
105+
declare module 'vite-plus/test/browser/context' {}
70106

71107
import client = require('vite-plus/client');
72108

73-
export type { TestFn };
109+
export type { BrowserClient, BrowserContext, PlaywrightProvider, TestFn };
74110

75111
void client;
76112

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
type TestFn = (typeof import('vitest'))['test'];
2+
type BrowserContext = typeof import('@vitest/browser/context');
3+
type BrowserClient = typeof import('@vitest/browser/client');
4+
type PlaywrightProvider = typeof import('@vitest/browser-playwright/provider');
25

36
declare module '@vitest/browser-playwright' {}
7+
declare module '@vitest/browser-playwright/context' {}
48

59
import client = require('vite/client');
610

7-
export type { TestFn };
11+
export type { BrowserClient, BrowserContext, PlaywrightProvider, TestFn };
812

913
void client;

packages/cli/src/__tests__/oxlint-plugin.spec.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,26 @@ describe('rewriteVitePlusImportSpecifier', () => {
6161
expect(rewriteVitePlusImportSpecifier('vitest')).toBe('vite-plus/test');
6262
expect(rewriteVitePlusImportSpecifier('vitest/config')).toBe('vite-plus');
6363
expect(rewriteVitePlusImportSpecifier('@vitest/browser')).toBe('vite-plus/test/browser');
64+
expect(rewriteVitePlusImportSpecifier('@vitest/browser/context')).toBe(
65+
'vite-plus/test/browser/context',
66+
);
67+
expect(rewriteVitePlusImportSpecifier('@vitest/browser/client')).toBe('vite-plus/test/client');
68+
expect(rewriteVitePlusImportSpecifier('@vitest/browser/locators')).toBe(
69+
'vite-plus/test/locators',
70+
);
71+
expect(rewriteVitePlusImportSpecifier('@vitest/browser-playwright/context')).toBe(
72+
'vite-plus/test/browser/context',
73+
);
6474
expect(rewriteVitePlusImportSpecifier('@vitest/browser-playwright/provider')).toBe(
65-
'vite-plus/test/browser-playwright/provider',
75+
'vite-plus/test/browser/providers/playwright',
6676
);
77+
expect(rewriteVitePlusImportSpecifier('@vitest/browser-preview/provider')).toBe(
78+
'vite-plus/test/browser/providers/preview',
79+
);
80+
expect(rewriteVitePlusImportSpecifier('@vitest/browser-webdriverio/provider')).toBe(
81+
'vite-plus/test/browser/providers/webdriverio',
82+
);
83+
expect(rewriteVitePlusImportSpecifier('@vitest/browser-playwright/locators')).toBeNull();
6784
expect(rewriteVitePlusImportSpecifier('tsx')).toBeNull();
6885
});
6986
});
@@ -80,6 +97,14 @@ new RuleTester({
8097
code: `declare module 'vite-plus/test/browser' {}`,
8198
filename: 'types.ts',
8299
},
100+
{
101+
code: `type BrowserClient = typeof import('vite-plus/test/client')`,
102+
filename: 'types.ts',
103+
},
104+
{
105+
code: `type PlaywrightProvider = typeof import('vite-plus/test/browser/providers/playwright')`,
106+
filename: 'types.ts',
107+
},
83108
{
84109
code: `type TestFn = typeof import('vite-plus/test')['test']`,
85110
filename: 'types.ts',
@@ -113,6 +138,24 @@ new RuleTester({
113138
filename: 'types.ts',
114139
output: `declare module 'vite-plus/test/browser-playwright' {}`,
115140
},
141+
{
142+
code: `declare module '@vitest/browser-playwright/context' {}`,
143+
errors: 1,
144+
filename: 'types.ts',
145+
output: `declare module 'vite-plus/test/browser/context' {}`,
146+
},
147+
{
148+
code: `type BrowserClient = typeof import('@vitest/browser/client')`,
149+
errors: 1,
150+
filename: 'types.ts',
151+
output: `type BrowserClient = typeof import('vite-plus/test/client')`,
152+
},
153+
{
154+
code: `type PlaywrightProvider = typeof import('@vitest/browser-playwright/provider')`,
155+
errors: 1,
156+
filename: 'types.ts',
157+
output: `type PlaywrightProvider = typeof import('vite-plus/test/browser/providers/playwright')`,
158+
},
116159
{
117160
code: `import foo = require('vite/client')`,
118161
errors: 1,

packages/cli/src/oxlint-plugin.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,34 @@ function rewriteVitePlusImportSpecifier(specifier: string): string | null {
5454
return `vite-plus/test/${specifier.slice('vitest/'.length)}`;
5555
}
5656

57-
for (const prefix of [
58-
'@vitest/browser-playwright',
59-
'@vitest/browser-preview',
60-
'@vitest/browser-webdriverio',
61-
'@vitest/browser',
62-
]) {
57+
if (specifier === '@vitest/browser') {
58+
return 'vite-plus/test/browser';
59+
}
60+
61+
const browserSubpathRewrites: Record<string, string> = {
62+
'@vitest/browser/context': 'vite-plus/test/browser/context',
63+
'@vitest/browser/client': 'vite-plus/test/client',
64+
'@vitest/browser/locators': 'vite-plus/test/locators',
65+
};
66+
if (specifier in browserSubpathRewrites) {
67+
return browserSubpathRewrites[specifier];
68+
}
69+
70+
for (const [prefix, provider] of [
71+
['@vitest/browser-playwright', 'playwright'],
72+
['@vitest/browser-preview', 'preview'],
73+
['@vitest/browser-webdriverio', 'webdriverio'],
74+
] as const) {
6375
if (specifier === prefix) {
6476
return `vite-plus/test/${prefix.slice('@vitest/'.length)}`;
6577
}
6678

67-
if (specifier.startsWith(`${prefix}/`)) {
68-
return `vite-plus/test/${specifier.slice('@vitest/'.length)}`;
79+
if (specifier === `${prefix}/context`) {
80+
return 'vite-plus/test/browser/context';
81+
}
82+
83+
if (specifier === `${prefix}/provider`) {
84+
return `vite-plus/test/browser/providers/${provider}`;
6985
}
7086
}
7187

0 commit comments

Comments
 (0)