-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add a forbid-dom-props rule #1562
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1ae0a99
Add a forbid-dom-props rule
davazp 2c2d831
Add related rules link to both forbid-dom-props and forbid-component-…
davazp 28654e2
Add more invalid tests cases for forbid-dom-props
davazp 268a704
Refine schema for forbid-dom-props options
davazp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Forbid certain props on DOM Nodes (react/forbid-dom-props) | ||
|
||
This rule prevents passing of props to elements. This rule only applies to DOM Nodes (e.g. `<div />`) and not Components (e.g. `<Component />`). | ||
The list of forbidden props can be customized with the `forbid` option. | ||
|
||
## Rule Details | ||
|
||
This rule checks all JSX elements and verifies that no forbidden props are used | ||
on DOM Nodes. This rule is off by default. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
// [1, { "forbid": ["id"] }] | ||
<div id='Joe' /> | ||
``` | ||
|
||
```jsx | ||
// [1, { "forbid": ["style"] }] | ||
<div style={{color: 'red'}} /> | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
// [1, { "forbid": ["id"] }] | ||
<Hello id='foo' /> | ||
``` | ||
|
||
```jsx | ||
// [1, { "forbid": ["id"] }] | ||
<Hello id={{color: 'red'}} /> | ||
``` | ||
|
||
## Rule Options | ||
|
||
```js | ||
... | ||
"react/forbid-dom-props": [<enabled>, { "forbid": [<string>] }] | ||
... | ||
``` | ||
|
||
### `forbid` | ||
|
||
An array of strings, with the names of props that are forbidden. The default value of this option `[]`. | ||
|
||
|
||
### Related rules | ||
|
||
- [forbid-component-props](./forbid-component-props.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @fileoverview Forbid certain props on DOM Nodes | ||
* @author David Vázquez | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Constants | ||
// ------------------------------------------------------------------------------ | ||
|
||
const DEFAULTS = []; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Forbid certain props on DOM Nodes', | ||
category: 'Best Practices', | ||
recommended: false | ||
}, | ||
|
||
schema: [{ | ||
type: 'object', | ||
properties: { | ||
forbid: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
minLength: 1 | ||
}, | ||
uniqueItems: true | ||
} | ||
}, | ||
additionalProperties: false | ||
}] | ||
}, | ||
|
||
create: function(context) { | ||
function isForbidden(prop) { | ||
const configuration = context.options[0] || {}; | ||
|
||
const forbid = configuration.forbid || DEFAULTS; | ||
return forbid.indexOf(prop) >= 0; | ||
} | ||
|
||
return { | ||
JSXAttribute: function(node) { | ||
const tag = node.parent.name.name; | ||
if (!(tag && tag[0] !== tag[0].toUpperCase())) { | ||
// This is a Component, not a DOM node, so exit. | ||
return; | ||
} | ||
|
||
const prop = node.name.name; | ||
|
||
if (!isForbidden(prop)) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: node, | ||
message: `Prop \`${prop}\` is forbidden on DOM Nodes` | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* @fileoverview Tests for forbid-dom-props | ||
*/ | ||
'use strict'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
const rule = require('../../../lib/rules/forbid-dom-props'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 8, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
jsx: true | ||
} | ||
}; | ||
|
||
require('babel-eslint'); | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
const ID_ERROR_MESSAGE = 'Prop `id` is forbidden on DOM Nodes'; | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('forbid-element-props', rule, { | ||
|
||
valid: [{ | ||
code: [ | ||
'var First = createReactClass({', | ||
' render: function() {', | ||
' return <Foo id="foo" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
options: [{forbid: ['id']}] | ||
}, { | ||
code: [ | ||
'var First = createReactClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <Foo id="bar" style={{color: "red"}} />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
options: [{forbid: ['style', 'id']}] | ||
}, { | ||
code: [ | ||
'var First = createReactClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <this.Foo bar="baz" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
options: [{forbid: ['id']}] | ||
}, { | ||
code: [ | ||
'class First extends createReactClass {', | ||
' render() {', | ||
' return <this.foo id="bar" />;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
options: [{forbid: ['id']}] | ||
}, { | ||
code: [ | ||
'const First = (props) => (', | ||
' <this.Foo {...props} />', | ||
');' | ||
].join('\n'), | ||
options: [{forbid: ['id']}] | ||
}, { | ||
code: [ | ||
'const First = (props) => (', | ||
' <div name="foo" />', | ||
');' | ||
].join('\n'), | ||
options: [{forbid: ['id']}] | ||
}], | ||
|
||
invalid: [{ | ||
code: [ | ||
'var First = createReactClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <div id="bar" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
options: [{forbid: ['id']}], | ||
errors: [{ | ||
message: ID_ERROR_MESSAGE, | ||
line: 4, | ||
column: 17, | ||
type: 'JSXAttribute' | ||
}] | ||
}, { | ||
code: [ | ||
'class First extends createReactClass {', | ||
' render() {', | ||
' return <div id="bar" />;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
options: [{forbid: ['id']}], | ||
errors: [{ | ||
message: ID_ERROR_MESSAGE, | ||
line: 3, | ||
column: 17, | ||
type: 'JSXAttribute' | ||
}] | ||
}, { | ||
code: [ | ||
'const First = (props) => (', | ||
' <div id="foo" />', | ||
');' | ||
].join('\n'), | ||
options: [{forbid: ['id']}], | ||
errors: [{ | ||
message: ID_ERROR_MESSAGE, | ||
line: 2, | ||
column: 8, | ||
type: 'JSXAttribute' | ||
}] | ||
}] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this array should be forced to be unique