|
1
|
import { createRuleTester } from "../../../test-utils" |
|
2
|
import { rule } from "../rule" |
|
3
|
|
|
4
|
const ruleTester = createRuleTester() |
|
5
|
|
|
6
|
const ADMIN_WIDGET = "src/admin/widgets/product-widget.tsx" |
|
7
|
const ADMIN_ROUTE = "src/admin/routes/custom/page.tsx" |
|
8
|
const ADMIN_LIB = "src/admin/lib/config.ts" |
|
9
|
const NON_ADMIN = "src/api/admin/route.ts" |
|
10
|
const WORKFLOW = "src/workflows/do-thing.ts" |
|
11
|
|
|
12
|
ruleTester.run("admin-env-vars-import-meta", rule, { |
|
13
|
valid: [ |
|
14
|
// Admin code already using import.meta.env — the desired pattern. |
|
15
|
{ |
|
16
|
code: `const url = import.meta.env.VITE_BACKEND_URL`, |
|
17
|
filename: ADMIN_WIDGET, |
|
18
|
}, |
|
19
|
// `process.env` in an API admin route (server code) — not flagged. The |
|
20
|
// file lives under src/api/admin, not src/admin. |
|
21
|
{ |
|
22
|
code: `export const GET = async () => { const x = process.env.SECRET }`, |
|
23
|
filename: NON_ADMIN, |
|
24
|
}, |
|
25
|
// `process.env` in a workflow (server code) — out of scope. |
|
26
|
{ |
|
27
|
code: `const x = process.env.NODE_ENV`, |
|
28
|
filename: WORKFLOW, |
|
29
|
}, |
|
30
|
// Locally-shadowed `process` in admin code — not the Node global. |
|
31
|
{ |
|
32
|
code: `const process = { env: { FOO: "bar" } } |
|
33
|
const x = process.env.FOO`, |
|
34
|
filename: ADMIN_ROUTE, |
|
35
|
}, |
|
36
|
// `process` as a function parameter shadow. |
|
37
|
{ |
|
38
|
code: `function read(process) { return process.env }`, |
|
39
|
filename: ADMIN_LIB, |
|
40
|
}, |
|
41
|
// Unrelated member access on an object named `process` is impossible to |
|
42
|
// confuse here — `env` is the only property we match. A different property |
|
43
|
// is still `process.<x>`, but only `process.env` is the Node env object. |
|
44
|
{ |
|
45
|
code: `const v = process.platform`, |
|
46
|
filename: ADMIN_LIB, |
|
47
|
}, |
|
48
|
], |
|
49
|
invalid: [ |
|
50
|
// Reading a var off process.env in an admin widget. |
|
51
|
{ |
|
52
|
code: `const url = process.env.VITE_BACKEND_URL`, |
|
53
|
filename: ADMIN_WIDGET, |
|
54
|
output: `const url = import.meta.env.VITE_BACKEND_URL`, |
|
55
|
errors: [{ messageId: "useImportMetaEnv", line: 1, column: 13 }], |
|
56
|
}, |
|
57
|
// Admin UI route page. |
|
58
|
{ |
|
59
|
code: `const mode = process.env.NODE_ENV |
|
60
|
const Page = () => null |
|
61
|
export default Page`, |
|
62
|
filename: ADMIN_ROUTE, |
|
63
|
output: `const mode = import.meta.env.NODE_ENV |
|
64
|
const Page = () => null |
|
65
|
export default Page`, |
|
66
|
errors: [{ messageId: "useImportMetaEnv" }], |
|
67
|
}, |
|
68
|
// Bare `process.env` access (no further property) — still flagged & fixed. |
|
69
|
{ |
|
70
|
code: `const env = process.env`, |
|
71
|
filename: ADMIN_LIB, |
|
72
|
output: `const env = import.meta.env`, |
|
73
|
errors: [{ messageId: "useImportMetaEnv" }], |
|
74
|
}, |
|
75
|
// Multiple usages in one admin file → one report each. |
|
76
|
{ |
|
77
|
code: `const a = process.env.VITE_A |
|
78
|
const b = process.env.VITE_B`, |
|
79
|
filename: ADMIN_LIB, |
|
80
|
output: `const a = import.meta.env.VITE_A |
|
81
|
const b = import.meta.env.VITE_B`, |
|
82
|
errors: [ |
|
83
|
{ messageId: "useImportMetaEnv", line: 1 }, |
|
84
|
{ messageId: "useImportMetaEnv", line: 2 }, |
|
85
|
], |
|
86
|
}, |
|
87
|
// Admin file under a nested monorepo layout (`apps/admin/src/admin/...`). |
|
88
|
{ |
|
89
|
code: `const url = process.env.VITE_BACKEND_URL`, |
|
90
|
filename: "apps/dashboard/src/admin/widgets/x.tsx", |
|
91
|
output: `const url = import.meta.env.VITE_BACKEND_URL`, |
|
92
|
errors: [{ messageId: "useImportMetaEnv" }], |
|
93
|
}, |
|
94
|
], |
|
95
|
}) |