|
| 1 | +--- |
| 2 | +title: no-forbidden-props |
| 3 | +--- |
| 4 | + |
| 5 | +**Full Name in `@eslint-react/eslint-plugin`** |
| 6 | + |
| 7 | +```plain copy |
| 8 | +@eslint-react/no-forbidden-props |
| 9 | +``` |
| 10 | + |
| 11 | +**Full Name in `eslint-plugin-react-x`** |
| 12 | + |
| 13 | +```plain copy |
| 14 | +react-x/no-forbidden-props |
| 15 | +``` |
| 16 | + |
| 17 | +**Features** |
| 18 | + |
| 19 | +`🔧` `🧪` |
| 20 | + |
| 21 | +<Callout type="warning" title="DEPRECATED"> |
| 22 | + Use [`no-restricted-syntax`](https://eslint.org/docs/latest/rules/no-restricted-syntax) rule or type checking instead. This rule is deprecated and will be removed in future releases. |
| 23 | +</Callout> |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +Disallow certain props on components. This rule helps enforce consistent prop naming conventions and prevents the use of specific props that may be problematic or against your team's coding standards. |
| 28 | + |
| 29 | +By default, this rule forbids snake\_case props (props containing underscores) to encourage camelCase naming conventions. |
| 30 | + |
| 31 | +## Rule Options |
| 32 | + |
| 33 | +The rule accepts an object with the following properties: |
| 34 | + |
| 35 | +- `forbid` (array): An array of forbidden prop configurations. Each item can be: |
| 36 | + - A string: The exact prop name to forbid |
| 37 | + - An object with `prop` and optional `excludedNodes` or `includedNodes`: |
| 38 | + - `prop` (string): The prop name or regex pattern to forbid |
| 39 | + - `excludedNodes` (array): Component names where this prop is allowed |
| 40 | + - `includedNodes` (array): Component names where this prop is forbidden (others are allowed) |
| 41 | + |
| 42 | +### Default Configuration |
| 43 | + |
| 44 | +```json |
| 45 | +{ |
| 46 | + "forbid": [{ "prop": "/_/" }] |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +This default configuration forbids any prop containing an underscore (snake\_case). |
| 51 | + |
| 52 | +## Examples |
| 53 | + |
| 54 | +### Default Behavior (Forbids snake\_case props) |
| 55 | + |
| 56 | +#### Failing |
| 57 | + |
| 58 | +```tsx |
| 59 | +<div snake_case="value" /> |
| 60 | +<Component user_name="test" /> |
| 61 | +<ns:Element data_value="test" /> |
| 62 | +``` |
| 63 | + |
| 64 | +#### Passing |
| 65 | + |
| 66 | +```tsx |
| 67 | +<div camelCase="value" /> |
| 68 | +<Component userName="test" /> |
| 69 | +<ns:Element dataValue="test" /> |
| 70 | +``` |
| 71 | + |
| 72 | +### Custom Forbidden Props |
| 73 | + |
| 74 | +Configuration: |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "forbid": ["className", "style"] |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +#### Failing |
| 83 | + |
| 84 | +```tsx |
| 85 | +<div className="test" /> |
| 86 | +<Component style={{}} /> |
| 87 | +``` |
| 88 | + |
| 89 | +#### Passing |
| 90 | + |
| 91 | +```tsx |
| 92 | +<div id="test" /> |
| 93 | +<Component id="test" /> |
| 94 | +``` |
| 95 | + |
| 96 | +### Regex Patterns |
| 97 | + |
| 98 | +Configuration: |
| 99 | + |
| 100 | +```json |
| 101 | +{ |
| 102 | + "forbid": [ |
| 103 | + { "prop": "/^data-/" }, |
| 104 | + { "prop": "/^aria-/" } |
| 105 | + ] |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +#### Failing |
| 110 | + |
| 111 | +```tsx |
| 112 | +<div data-testid="test" /> |
| 113 | +<div aria-label="test" /> |
| 114 | +<div data-cy="test" /> |
| 115 | +``` |
| 116 | + |
| 117 | +#### Passing |
| 118 | + |
| 119 | +```tsx |
| 120 | +<div id="test" /> |
| 121 | +<div className="test" /> |
| 122 | +``` |
| 123 | + |
| 124 | +### Node-Specific Exclusions |
| 125 | + |
| 126 | +Configuration: |
| 127 | + |
| 128 | +```json |
| 129 | +{ |
| 130 | + "forbid": [ |
| 131 | + { |
| 132 | + "prop": "/_/", |
| 133 | + "excludedNodes": ["Button"] |
| 134 | + } |
| 135 | + ] |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +#### Failing |
| 140 | + |
| 141 | +```tsx |
| 142 | +<div snake_case="value" /> |
| 143 | +<Span snake_case="value" /> |
| 144 | +``` |
| 145 | + |
| 146 | +#### Passing |
| 147 | + |
| 148 | +```tsx |
| 149 | +<Button snake_case="value" />; |
| 150 | +``` |
| 151 | + |
| 152 | +### Node-Specific Inclusions |
| 153 | + |
| 154 | +Configuration: |
| 155 | + |
| 156 | +```json |
| 157 | +{ |
| 158 | + "forbid": [ |
| 159 | + { |
| 160 | + "prop": "/_/", |
| 161 | + "includedNodes": ["Button", "Input"] |
| 162 | + } |
| 163 | + ] |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +#### Failing |
| 168 | + |
| 169 | +```tsx |
| 170 | +<Button snake_case="value" /> |
| 171 | +<Input snake_case="value" /> |
| 172 | +``` |
| 173 | + |
| 174 | +#### Passing |
| 175 | + |
| 176 | +```tsx |
| 177 | +<div snake_case="value" /> |
| 178 | +<span snake_case="value" /> |
| 179 | +``` |
| 180 | + |
| 181 | +### Mixed Configuration |
| 182 | + |
| 183 | +```json |
| 184 | +{ |
| 185 | + "forbid": [ |
| 186 | + "className", |
| 187 | + { "prop": "/^data-/", "excludedNodes": ["Button"] }, |
| 188 | + { "prop": "/^aria-/", "includedNodes": ["Input"] } |
| 189 | + ] |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +This configuration: |
| 194 | + |
| 195 | +- Forbids `className` on all components |
| 196 | +- Forbids `data-*` props on all components except `Button` |
| 197 | +- Forbids `aria-*` props only on `Input` components |
| 198 | + |
| 199 | +## Implementation |
| 200 | + |
| 201 | +- [Rule Source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x/src/rules-removed/no-forbidden-props.ts) |
| 202 | +- [Test Source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x/src/rules-removed/no-forbidden-props.spec.ts) |
| 203 | + |
| 204 | +## Notes |
| 205 | + |
| 206 | +- Spread attributes (`{...props}`) are ignored by this rule |
| 207 | +- JSXMemberExpression components (like `React.Component`) are supported for prop checking, but node-specific filtering may not work as expected |
| 208 | +- Regex patterns should be wrapped in forward slashes (e.g., `/pattern/`) |
| 209 | +- The rule processes each forbidden prop configuration independently |
0 commit comments