Skip to content

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

Merged
fasttime merged 1 commit into
eslint:mainfrom
sethamus:allowProperties
Jun 2, 2025
Merged

fasttime merged 1 commit into
eslint:mainfrom
sethamus:allowProperties

Conversation

@sethamus

@sethamus sethamus commented May 27, 2025 •

Copy link
Copy Markdown
Contributor

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
@netlify

netlify Bot commented May 27, 2025 •

Copy link
Copy Markdown

✅ 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
Copy Markdown
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

erosman commented May 30, 2025 •

Copy link
Copy Markdown

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

nzakas commented May 30, 2025

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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

sethamus commented Jun 1, 2025

Copy link
Copy Markdown
Contributor Author

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

@mdjermanovic mdjermanovic left a comment

Copy link
Copy Markdown
Member

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.

@snitin315 snitin315 left a comment

Copy link
Copy Markdown
Contributor

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
@github-project-automation github-project-automation Bot moved this from Merge Candidates to Complete in Triage Jun 2, 2025
@sethamus
sethamus deleted the allowProperties branch February 3, 2026 23:22
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