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

Enhancement: [prefer-nullish-coalescing] Ignore env vars #9162

Closed
4 tasks done
koooge opened this issue May 28, 2024 · 1 comment
Closed
4 tasks done

Enhancement: [prefer-nullish-coalescing] Ignore env vars #9162

koooge opened this issue May 28, 2024 · 1 comment
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@koooge
Copy link
Contributor

koooge commented May 28, 2024

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/prefer-nullish-coalescing/

Description

Hi there,
A proposal of prefer-nullish-coalescing, it would be useful to have an option to ignore env vars i.e. process.env also import.meta.env. Env vars could be given like FOO= node foo.js, and the FOO is a string '' in node.js. However, in many case it expects the same as unset FOO, so ?? does not work as expected since '' is valid string.

Fail

// `$ FOO= node foo.js`
const foo = process.env.FOO || 'foobar'; // should be ignored

Pass

// `$ FOO= node foo.js`
const foo = process.env.FOO || 'foobar'; // prefer-nullish-coalescing suggests `??`, but `foobar` is the expected

Additional Info

No response

@koooge koooge added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels May 28, 2024
@bradzacher
Copy link
Member

The rule has no concept of platform or framework specific APIs. We purposely do not encode any specifics into any of our rules.

In this instance we would not be encoding special semantics around node's env var API.

It's also not uncommon that you would want the nullish coalescing behaviour instead of the logical or. For example if you want a default value but you want someone to be able to set any other value.

Theres no hard and fast rule that works here.

Which is why there's an option to ignore specific types of primitives based on your preferences.
https://typescript-eslint.io/rules/prefer-nullish-coalescing/#ignoreprimitives

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2024
@bradzacher bradzacher added wontfix This will not be worked on and removed triage Waiting for maintainers to take a look labels May 28, 2024
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Jun 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants