-
Notifications
You must be signed in to change notification settings - Fork 131
Add rule to stop single element style arrays #234
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
Intellicode
merged 2 commits into
Intellicode:master
from
wakeless:wakeless/no-single-element-style-arrays
Sep 12, 2019
Merged
Changes from 1 commit
Commits
Show all changes
2 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,12 @@ | ||
# No Single Element Style Arrays are allowed | ||
|
||
These cause unnecessary re-renders as each time the array's identity changes. | ||
|
||
## Rule Details | ||
|
||
The following pattern is not allowed: | ||
|
||
```js | ||
<View style={[{height: 10}]} /> | ||
``` | ||
|
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,47 @@ | ||
/** | ||
* @fileoverview Enforce no single element style arrays | ||
* @author Michael Gall | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'Disallow single element style arrays. These cause unnecessary re-renders as the identity of the array always changes', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
url: '', | ||
}, | ||
fixable: 'code', | ||
}, | ||
|
||
create(context) { | ||
function reportNode(JSXExpressionNode) { | ||
context.report({ | ||
node: JSXExpressionNode, | ||
message: | ||
'Single element style arrays are not necessary and cause unnecessary re-renders', | ||
fix(fixer) { | ||
const realStyleNode = JSXExpressionNode.value.expression.elements[0]; | ||
const styleSource = context.getSourceCode().getText(realStyleNode); | ||
return fixer.replaceText(JSXExpressionNode.value, `{${styleSource}}`); | ||
}, | ||
}); | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
return { | ||
JSXAttribute(node) { | ||
if (node.name.name !== 'style') return; | ||
if (node.value.expression.type !== 'ArrayExpression') return; | ||
if (node.value.expression.elements.length === 1) { | ||
reportNode(node); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,80 @@ | ||
/** | ||
* @fileoverview Enforce no single element style arrays | ||
* @author Michael Gall | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/* eslint-disable quotes */ // For better readability on tests involving quotes | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../../../lib/rules/no-single-element-style-arrays'); | ||
|
||
require('babel-eslint'); | ||
|
||
const unnecessaryArrayMessage = 'Single element style arrays are not necessary and cause unnecessary re-renders'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
const config = { | ||
parser: 'babel-eslint', | ||
parserOptions: { | ||
ecmaFeatures: { | ||
classes: true, | ||
jsx: true, | ||
}, | ||
}, | ||
settings: { | ||
'react-native/style-sheet-object-names': ['StyleSheet', 'OtherStyleSheet'], | ||
}, | ||
}; | ||
|
||
const ruleTester = new RuleTester(config); | ||
ruleTester.run('single-element-style-array', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
const Hello = React.createClass({ | ||
render: function() { | ||
return <App {...props}>foo</App>; | ||
} | ||
}); | ||
`, | ||
}, | ||
{ | ||
code: '<App>foo</App>', | ||
}, | ||
{ | ||
code: '<App style={woop}>foo</App>', | ||
}, | ||
{ | ||
code: '<App style={{woop: "woop"}}>foo</App>', | ||
}, | ||
{ | ||
code: '<App style={[woope, wap]}>foo</App>', | ||
}, | ||
{ | ||
code: '<App className="asdf" style={woop}>foo</App>', | ||
}, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: '<App style={[woop]}>foo</App>', | ||
output: '<App style={woop}>foo</App>', | ||
errors: [{ message: unnecessaryArrayMessage }], | ||
}, | ||
{ | ||
code: '<App style={[{woop: "woop"}]}>foo</App>', | ||
output: '<App style={{woop: "woop"}}>foo</App>', | ||
errors: [{ message: unnecessaryArrayMessage }], | ||
}, | ||
|
||
], | ||
}); | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.