forked from eslint-community/eslint-plugin-n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-process-env.js
92 lines (87 loc) · 2.76 KB
/
no-process-env.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @author Vignesh Anand
* See LICENSE file in root directory for full license.
*/
"use strict"
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const querySelector = [
`MemberExpression`,
`[computed!=true]`,
`[object.name="process"]`,
`[property.name="env"]`,
`,`,
`MemberExpression`,
`[computed=true]`,
`[object.name="process"]`,
`[property.value="env"]`,
]
/**
* @param {unknown} node [description]
* @returns {node is import('estree').MemberExpression}
*/
function isMemberExpresion(node) {
return (
node != null &&
typeof node === "object" &&
"type" in node &&
node.type === "MemberExpression"
)
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow the use of `process.env`",
recommended: false,
url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/no-process-env.md",
},
fixable: null,
schema: [
{
type: "object",
properties: {
allowedVariables: {
type: "array",
items: { type: "string" },
},
},
additionalProperties: false,
},
],
messages: {
unexpectedProcessEnv: "Unexpected use of process.env.",
},
},
create(context) {
const options = context.options[0] ?? {}
/** @type {string[]} */
const allowedVariables = options.allowedVariables ?? []
return {
/** @param {import('estree').MemberExpression} node */
[querySelector.join("")](node) {
if (
"parent" in node &&
isMemberExpresion(node.parent) &&
node.parent.property != null
) {
const child = node.parent.property
if (
(child.type === "Identifier" &&
node.parent.computed === false &&
allowedVariables.includes(child.name)) ||
(child.type === "Literal" &&
typeof child.value === "string" &&
node.parent.computed === true &&
allowedVariables.includes(child.value))
) {
return
}
}
context.report({ node, messageId: "unexpectedProcessEnv" })
},
}
},
}