Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions docs/rules/no-process-env.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ The `process.env` object in Node.js is used to store deployment/configuration pa

This rule is aimed at discouraging use of `process.env` to avoid global dependencies. As such, it will warn whenever `process.env` is used.

### Options

You can customize the error message for this rule:

```json
{
"rules": {
"node/no-process-env": [
"error",
{
"customMessage": "Use the env wrapper instead."
Copy link
Author

@devinrhode2 devinrhode2 Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this sample message deliberately bad to drive the point that users need to edit it for their use case.

In my case it'll be something like: "Use safeEnv from 'src/env' instead."

}
]
}
}
```

Examples of **incorrect** code for this rule:

```js
Expand Down
22 changes: 20 additions & 2 deletions lib/rules/no-process-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,25 @@ module.exports = {
"https://github.com/mysticatea/eslint-plugin-node/blob/v11.1.0/docs/rules/no-process-env.md",
},
fixable: null,
schema: [],
schema: [
{
type: "object",
properties: {
customMessage: {
type: "string",
},
},
},
],
messages: {
unexpectedProcessEnv: "Unexpected use of process.env.",
},
},

create(context) {
const options = context.options[0] || {}
const customMessage = options.customMessage

return {
MemberExpression(node) {
const objectName = node.object.name
Expand All @@ -37,7 +49,13 @@ module.exports = {
propertyName &&
propertyName === "env"
) {
context.report({ node, messageId: "unexpectedProcessEnv" })
const report = { node }
if (customMessage) {
report.message = customMessage
} else {
report.messageId = "unexpectedProcessEnv"
}
context.report(report)
}
},
}
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-process-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,20 @@ new RuleTester().run("no-process-env", rule, {
},
],
},
{
code: "f(process.env)",
options: [
{
customMessage:
"custom error message - use the wrapper instead",
},
],
errors: [
{
message: "custom error message - use the wrapper instead",
type: "MemberExpression",
},
],
},
],
})