Skip to content

Commit 1fb1f5b

Browse files
author
Anthon Fredriksson
committed
Add allow option in style-prop-object
1 parent c82746c commit 1fb1f5b

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ sftp-config.json
2121
# Only apps should have lockfiles
2222
yarn.lock
2323
package-lock.json
24+
25+
# Visual Code folder
26+
.vscode

docs/rules/style-prop-object.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,32 @@ React.createElement("Hello", { style: { color: 'red' }});
4949
const styles = { height: '100px' };
5050
React.createElement("div", { style: styles });
5151
```
52+
53+
## Rule Options
54+
55+
```js
56+
...
57+
"react/style-prop-object": [<enabled>, {
58+
"allow": [<string>]
59+
}]
60+
...
61+
```
62+
63+
### `allow`
64+
A list of elements that are allowed to have a non-object value in their style attribute. The default value is `[]`.
65+
66+
#### Example
67+
```js
68+
{
69+
"allow": ["MyComponent"]
70+
}
71+
```
72+
The following patterns are considered warnings:
73+
```js
74+
<Hello style="a string">
75+
```
76+
77+
The following patterns are **not** considered warnings:
78+
```js
79+
<MyComponent style="a string">
80+
```

lib/rules/style-prop-object.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@ module.exports = {
1919
recommended: false,
2020
url: docsUrl('style-prop-object')
2121
},
22-
schema: []
22+
schema: [
23+
{
24+
type: 'object',
25+
properties: {
26+
allow: {
27+
type: 'array',
28+
items: {
29+
type: 'string'
30+
}
31+
}
32+
}
33+
}
34+
]
2335
},
2436

2537
create: function(context) {
38+
const allowed = context.options.length > 0 && context.options[0].allow || [];
39+
2640
/**
2741
* @param {object} node An Identifier node
2842
*/
@@ -71,6 +85,22 @@ module.exports = {
7185
return;
7286
}
7387

88+
// parent element is a JSXOpeningElement
89+
if (node.parent && node.parent.type === 'JSXOpeningElement') {
90+
91+
// store parent element
92+
const jsxOpeningElement = node.parent;
93+
94+
// get the name of the JSX element
95+
const name = jsxOpeningElement.name && jsxOpeningElement.name.name;
96+
97+
// allowed list contains the name
98+
if (allowed.indexOf(name) > -1) {
99+
// abort operation
100+
return;
101+
}
102+
}
103+
74104
if (node.value.type !== 'JSXExpressionContainer' || isNonNullaryLiteral(node.value.expression)) {
75105
context.report(node, 'Style prop value must be an object');
76106
} else if (node.value.expression.type === 'Identifier') {

tests/lib/rules/style-prop-object.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ ruleTester.run('style-prop-object', rule, {
191191
' });',
192192
'};'
193193
].join('\n')
194+
},
195+
{
196+
code: '<MyComponent style="myStyle" />',
197+
options: [
198+
{
199+
allow: ['MyComponent']
200+
}
201+
]
194202
}
195203
],
196204
invalid: [
@@ -262,6 +270,20 @@ ruleTester.run('style-prop-object', rule, {
262270
column: 22,
263271
type: 'Identifier'
264272
}]
273+
},
274+
{
275+
code: '<MyComponent style="myStyle" />',
276+
options: [
277+
{
278+
allow: ['MyOtherComponent']
279+
}
280+
],
281+
errors: [{
282+
message: 'Style prop value must be an object',
283+
line: 1,
284+
column: 14,
285+
type: 'JSXAttribute'
286+
}]
265287
}
266288
]
267289
});

0 commit comments

Comments
 (0)