Skip to content

feat: add allowProperties option to no-restricted-properties #19772

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

Merged
merged 1 commit into from
Jun 2, 2025

Conversation

sethamus
Copy link
Contributor

@sethamus sethamus commented May 27, 2025

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

This PR adds a new allowProperties option to the no-restricted-properties rule, allowing users to restrict all properties on an object except for specific ones.

Closes #19762

Is there anything you'd like reviewers to focus on?

@sethamus sethamus requested a review from a team as a code owner May 27, 2025 18:36
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage May 27, 2025
Copy link

netlify bot commented May 27, 2025

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit a8cc786
🔍 Latest deploy log https://app.netlify.com/projects/docs-eslint/deploys/6836062a772f1e0008f54704
😎 Deploy Preview https://deploy-preview-19772--docs-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label May 27, 2025
@github-actions github-actions bot added the rule Relates to ESLint's core rules label May 27, 2025
@lumirlumir lumirlumir added the accepted There is consensus among the team that this change meets the criteria for inclusion label May 28, 2025
@snitin315
Copy link
Contributor

How should this new option work in case of nested properties? For example, the current behavior is as follows:

/* eslint no-restricted-properties: [2, {
    "object": "config",
    "allowProperties": ["settings", "version"]
}] */

config.apiKey.settings = "12345"; // Error: 'config.apiKey' is restricted from being used
config.timeout = 5000; // Error: 'config.timeout' is restricted from being used 

config.version.node = "1.0.0"; // No error
config.node.version = "1.0.0"; // Error: 'config.node' is restricted from being used

Now, let's suppose I want to allow only config.apiKey.settings & not config.settings, how should my config look?

@snitin315 snitin315 moved this from Needs Triage to Implementing in Triage May 28, 2025
@erosman
Copy link

erosman commented May 30, 2025

IMHO, the rule should apply to the immediate property and therefore any of its sub-properties.

Allow config.settings and any sub-properties

"object": "config",
"allowProperties": ["settings", "version"]

For controlling nested properties, the rule can consider the following format:

"object": "config.node",
"allowProperties": ["version"]

It should be allowed to enter multiple rules e.g.

no-restricted-properties: [2, 
  {
    "object": "config",
    "allowProperties": ["settings", "version"]
  },
  {
    "object": "foo",
    "allowProperties": ["bar", "barbell"]
  },
  {
    "object": "config.node",
    "allowProperties": ["number", "value"]
  },
]

@nzakas
Copy link
Member

nzakas commented May 30, 2025

I agree. The rule should look only at immediate properties and not look at any other level. This mirrors the existing behavior of the rule.

@snitin315
Copy link
Contributor

Makes sense. Let's make the behavior as per #19772 (comment)

/* eslint no-restricted-properties: [2, {
    "object": "config.node",
    "allowProperties": ["version"]
}] */

config.node.version = "1.0.0"; // No error
config.node.other = "1" // Error

@mdjermanovic
Copy link
Member

I think supporting nested properties/complex objects is out of scope for this change, as that feature is not currently supported by this rule, and the accepted proposal is just to add "allowProperties" as the inverse of "property".

@sethamus
Copy link
Contributor Author

sethamus commented Jun 1, 2025

I agree with @mdjermanovic that supporting nested properties is out of scope for this change. I've opened a separate issue to discuss this.

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! Leaving open for a second review.

Copy link
Contributor

@snitin315 snitin315 left a comment

Choose a reason for hiding this comment

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

LGTM

@mdjermanovic mdjermanovic moved this from Implementing to Merge Candidates in Triage Jun 2, 2025
@fasttime fasttime merged commit 786bcd1 into eslint:main Jun 2, 2025
31 checks passed
@github-project-automation github-project-automation bot moved this from Merge Candidates to Complete in Triage Jun 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion contributor pool feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Rule Change: add "allowProperties" option to no-restricted-properties
7 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy