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

no-process-env: support custom error message "use the env wrapper" #319

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/rules/no-process-env.md
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

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
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
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",
},
],
},
],
})