Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(no-process-env): Allow users to exclude specific variables #345

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/rules/no-process-env.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ if(config.env === "development") {
}
```

### Options

```json
{
"rules": {
"n/no-process-env": ["error", {
"allowedVariables": ["NODE_ENV"]
}]
}
}
```

#### allowedVariables

Sometimes you need to allow specific environment variables, this option allows you to exclude specific variables from triggering a linting error.

## 🔎 Implementation

- [Rule source](../../lib/rules/no-process-env.js)
Expand Down
52 changes: 51 additions & 1 deletion lib/rules/no-process-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,26 @@ const querySelector = [
`[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: {
Expand All @@ -25,16 +43,48 @@ module.exports = {
url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/no-process-env.md",
},
fixable: null,
schema: [],
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" })
},
}
Expand Down
61 changes: 61 additions & 0 deletions tests/lib/rules/no-process-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ new RuleTester().run("no-process-env", rule, {
"process[env]",
"process.nextTick",
"process.execArgv",

// allowedVariables
{
code: "process.env.NODE_ENV",
options: [{ allowedVariables: ["NODE_ENV"] }],
},
{
code: "process.env['NODE_ENV']",
options: [{ allowedVariables: ["NODE_ENV"] }],
},
{
code: "process['env'].NODE_ENV",
options: [{ allowedVariables: ["NODE_ENV"] }],
},
{
code: "process['env']['NODE_ENV']",
options: [{ allowedVariables: ["NODE_ENV"] }],
},
// "process.env",
// "process.env[NODE_ENV]",
scagood marked this conversation as resolved.
Show resolved Hide resolved
],

invalid: [
Expand All @@ -25,6 +45,15 @@ new RuleTester().run("no-process-env", rule, {
},
],
},
{
code: "process['env']",
errors: [
{
messageId: "unexpectedProcessEnv",
type: "MemberExpression",
},
],
},
{
code: "process.env.ENV",
errors: [
Expand All @@ -43,5 +72,37 @@ new RuleTester().run("no-process-env", rule, {
},
],
},

// allowedVariables
{
code: "process.env['OTHER_VARIABLE']",
options: [{ allowedVariables: ["NODE_ENV"] }],
errors: [{ messageId: "unexpectedProcessEnv" }],
},
{
code: "process.env.OTHER_VARIABLE",
options: [{ allowedVariables: ["NODE_ENV"] }],
errors: [{ messageId: "unexpectedProcessEnv" }],
},
{
code: "process['env']['OTHER_VARIABLE']",
options: [{ allowedVariables: ["NODE_ENV"] }],
errors: [{ messageId: "unexpectedProcessEnv" }],
},
{
code: "process['env'].OTHER_VARIABLE",
options: [{ allowedVariables: ["NODE_ENV"] }],
errors: [{ messageId: "unexpectedProcessEnv" }],
},
{
code: "process.env[NODE_ENV]",
options: [{ allowedVariables: ["NODE_ENV"] }],
errors: [{ messageId: "unexpectedProcessEnv" }],
},
{
code: "process['env'][NODE_ENV]",
options: [{ allowedVariables: ["NODE_ENV"] }],
errors: [{ messageId: "unexpectedProcessEnv" }],
},
],
})