Skip to content

Commit

Permalink
feat(no-process-env): Allow users to exclude specific variables (#345)
Browse files Browse the repository at this point in the history
* fix(no-process-env): Include computed Literals

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

* docs(no-process-env): Add allowedVariables documentation

* Remove comments from test cases
  • Loading branch information
scagood authored Oct 9, 2024
1 parent 148e47e commit b16a475
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
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
59 changes: 59 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,24 @@ 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"] }],
},
],

invalid: [
Expand All @@ -25,6 +43,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 +70,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" }],
},
],
})

0 comments on commit b16a475

Please sign in to comment.