-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add rule to forbid className and style being passed to Components #703
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
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Forbid certain props on Components (forbid-component-props) | ||
|
||
By default this rule prevents passing of [props that add lots of complexity](https://medium.com/brigade-engineering/don-t-pass-css-classes-between-components-e9f7ab192785) (`className`, `style`) to Components. This rule only applies to Components (e.g. `<Foo />`) and not DOM nodes (e.g. `<div />`). 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 Components. This rule is off by default. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
<Hello className='foo' /> | ||
``` | ||
|
||
```jsx | ||
<Hello style={{color: 'red'}} /> | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```jsx | ||
<Hello name='Joe' /> | ||
``` | ||
|
||
```jsx | ||
<div className='foo' /> | ||
``` | ||
|
||
```jsx | ||
<div style={{color: 'red'}} /> | ||
``` | ||
|
||
## Rule Options | ||
|
||
```js | ||
... | ||
"forbid-component-props": [<enabled>, { "forbid": [<string>] }] | ||
... | ||
``` | ||
|
||
### `forbid` | ||
|
||
An array of strings, with the names of props that are forbidden. The default value of this option is `['className', 'style']`. |
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,65 @@ | ||
/** | ||
* @fileoverview Forbid certain props on components | ||
* @author Joe Lencioni | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Constants | ||
// ------------------------------------------------------------------------------ | ||
|
||
var DEFAULTS = ['className', 'style']; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: {}, | ||
|
||
schema: [{ | ||
type: 'object', | ||
properties: { | ||
forbid: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
}, | ||
additionalProperties: true | ||
}] | ||
}, | ||
|
||
create: function(context) { | ||
|
||
function isForbidden(prop) { | ||
var configuration = context.options[0] || {}; | ||
|
||
var forbid = configuration.forbid || DEFAULTS; | ||
return forbid.indexOf(prop) >= 0; | ||
} | ||
|
||
return { | ||
JSXAttribute: function(node) { | ||
var tag = node.parent.name.name; | ||
if (tag[0] !== tag[0].toUpperCase()) { | ||
// This is a DOM node, not a Component, so exit. | ||
return; | ||
} | ||
|
||
var prop = node.name.name; | ||
|
||
if (!isForbidden(prop)) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: node, | ||
message: 'Prop `' + prop + '` is forbidden on Components' | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
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,153 @@ | ||
/** | ||
* @fileoverview Tests for forbid-component-props | ||
*/ | ||
'use strict'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
var rule = require('../../../lib/rules/forbid-component-props'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
jsx: true | ||
} | ||
}; | ||
|
||
require('babel-eslint'); | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
var CLASSNAME_ERROR_MESSAGE = 'Prop `className` is forbidden on Components'; | ||
var STYLE_ERROR_MESSAGE = 'Prop `style` is forbidden on Components'; | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('forbid-component-props', rule, { | ||
|
||
valid: [{ | ||
code: [ | ||
'var First = React.createClass({', | ||
' render: function() {', | ||
' return <div className="foo" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var First = React.createClass({', | ||
' render: function() {', | ||
' return <div style={{color: "red"}} />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
options: [{forbid: ['style']}], | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var First = React.createClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <Foo bar="baz" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var First = React.createClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <Foo className="bar" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
options: [{forbid: ['style']}], | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var First = React.createClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <Foo className="bar" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
options: [{forbid: ['style', 'foo']}], | ||
parserOptions: parserOptions | ||
}], | ||
|
||
invalid: [{ | ||
code: [ | ||
'var First = React.createClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <Foo className="bar" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: CLASSNAME_ERROR_MESSAGE, | ||
line: 4, | ||
column: 17, | ||
type: 'JSXAttribute' | ||
}] | ||
}, { | ||
code: [ | ||
'var First = React.createClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <Foo style={{color: "red"}} />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: STYLE_ERROR_MESSAGE, | ||
line: 4, | ||
column: 17, | ||
type: 'JSXAttribute' | ||
}] | ||
}, { | ||
code: [ | ||
'var First = React.createClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <Foo className="bar" />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
options: [{forbid: ['className', 'style']}], | ||
errors: [{ | ||
message: CLASSNAME_ERROR_MESSAGE, | ||
line: 4, | ||
column: 17, | ||
type: 'JSXAttribute' | ||
}] | ||
}, { | ||
code: [ | ||
'var First = React.createClass({', | ||
' propTypes: externalPropTypes,', | ||
' render: function() {', | ||
' return <Foo style={{color: "red"}} />;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
options: [{forbid: ['className', 'style']}], | ||
errors: [{ | ||
message: STYLE_ERROR_MESSAGE, | ||
line: 4, | ||
column: 17, | ||
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.
would this not need a "react/" prefix?
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.
Yes, I think so. I was copying how it is in other rules documentation though so we should probably fix them all at the same time.
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.
Sounds good.