Skip to content

Commit

Permalink
no-process-env: support custom error message
Browse files Browse the repository at this point in the history
closes mysticatea#318 (no-process-env: accept a custom error message)
  • Loading branch information
devinrhode2 committed Jan 23, 2022
1 parent f45c614 commit 0d18cac
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
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."
}
]
}
}
```

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

0 comments on commit 0d18cac

Please sign in to comment.