4
4
*/
5
5
'use strict' ;
6
6
7
+ var variableUtil = require ( '../util/variable' ) ;
8
+
7
9
// ------------------------------------------------------------------------------
8
10
// Rule Definition
9
11
// ------------------------------------------------------------------------------
@@ -17,31 +19,53 @@ module.exports = {
17
19
schema : [ ] // no options
18
20
} ,
19
21
create : function ( context ) {
22
+ /**
23
+ * Takes a ObjectExpression and returns the value of the prop if it has it
24
+ * @param {object } node - ObjectExpression node
25
+ * @param {string } propName - name of the prop to look for
26
+ */
27
+ function findObjectProp ( node , propName ) {
28
+ return node . properties . find ( function ( prop ) {
29
+ return prop . key . name === propName ;
30
+ } ) ;
31
+ }
32
+
33
+ /**
34
+ * Takes a JSXElement and returns the value of the prop if it has it
35
+ * @param {object } node - JSXElement node
36
+ * @param {string } propName - name of the prop to look for
37
+ */
38
+ function findJsxProp ( node , propName ) {
39
+ var attributes = node . openingElement . attributes ;
40
+ return attributes . find ( function ( attribute ) {
41
+ if ( attribute . type === 'JSXSpreadAttribute' ) {
42
+ var variable = variableUtil . variablesInScope ( context ) . find ( function ( item ) {
43
+ return item . name === attribute . argument . name ;
44
+ } ) ;
45
+ if ( variable && variable . defs [ 0 ] . node . init ) {
46
+ return findObjectProp ( variable . defs [ 0 ] . node . init , propName ) ;
47
+ }
48
+ }
49
+ return attribute . name && attribute . name . name === propName ;
50
+ } ) ;
51
+ }
52
+
20
53
return {
21
54
JSXElement : function ( node ) {
22
55
var hasChildren = false ;
23
- var attributes = node . openingElement . attributes ;
24
56
25
57
if ( node . children . length ) {
26
58
hasChildren = true ;
27
- } else {
28
- var childrenProp = attributes . find ( function ( attribute ) {
29
- return attribute . name . name === 'children' ;
30
- } ) ;
31
- if ( childrenProp ) {
32
- hasChildren = true ;
33
- }
59
+ } else if ( findJsxProp ( node , 'children' ) ) {
60
+ hasChildren = true ;
34
61
}
35
62
36
- if ( attributes && hasChildren ) {
37
- var jsxElement = attributes . find ( function ( attribute ) {
38
- return attribute . name . name === 'dangerouslySetInnerHTML' ;
39
- } ) ;
40
-
41
-
42
- if ( jsxElement ) {
43
- context . report ( node , 'Only set one of `children` or `props.dangerouslySetInnerHTML`' ) ;
44
- }
63
+ if (
64
+ node . openingElement . attributes
65
+ && hasChildren
66
+ && findJsxProp ( node , 'dangerouslySetInnerHTML' )
67
+ ) {
68
+ context . report ( node , 'Only set one of `children` or `props.dangerouslySetInnerHTML`' ) ;
45
69
}
46
70
} ,
47
71
CallExpression : function ( node ) {
@@ -53,17 +77,21 @@ module.exports = {
53
77
) {
54
78
var hasChildren = false ;
55
79
56
- var props = node . arguments [ 1 ] . properties ;
57
- var dangerously = props . find ( function ( prop ) {
58
- return prop . key . name === 'dangerouslySetInnerHTML' ;
59
- } ) ;
80
+ var props = node . arguments [ 1 ] ;
60
81
82
+ if ( props . type === 'Identifier' ) {
83
+ var variable = variableUtil . variablesInScope ( context ) . find ( function ( item ) {
84
+ return item . name === props . name ;
85
+ } ) ;
86
+ if ( variable && variable . defs [ 0 ] . node . init ) {
87
+ props = variable . defs [ 0 ] . node . init ;
88
+ }
89
+ }
90
+
91
+ var dangerously = findObjectProp ( props , 'dangerouslySetInnerHTML' ) ;
61
92
62
93
if ( node . arguments . length === 2 ) {
63
- var childrenProp = props . find ( function ( prop ) {
64
- return prop . key . name === 'children' ;
65
- } ) ;
66
- if ( childrenProp ) {
94
+ if ( findObjectProp ( props , 'children' ) ) {
67
95
hasChildren = true ;
68
96
}
69
97
} else {
0 commit comments