@@ -13,122 +13,128 @@ var DEFAULTS = ['any', 'array', 'object'];
1313// Rule Definition
1414// ------------------------------------------------------------------------------
1515
16- module . exports = function ( context ) {
16+ module . exports = {
17+ meta : {
18+ docs : { } ,
19+
20+ schema : [ {
21+ type : 'object' ,
22+ properties : {
23+ forbid : {
24+ type : 'array' ,
25+ items : {
26+ type : 'string'
27+ }
28+ }
29+ } ,
30+ additionalProperties : true
31+ } ]
32+ } ,
1733
18- function isForbidden ( type ) {
19- var configuration = context . options [ 0 ] || { } ;
34+ create : function ( context ) {
2035
21- var forbid = configuration . forbid || DEFAULTS ;
22- return forbid . indexOf ( type ) >= 0 ;
23- }
36+ function isForbidden ( type ) {
37+ var configuration = context . options [ 0 ] || { } ;
2438
25- /**
26- * Checks if node is `propTypes` declaration
27- * @param {ASTNode } node The AST node being checked.
28- * @returns {Boolean } True if node is `propTypes` declaration, false if not.
29- */
30- function isPropTypesDeclaration ( node ) {
31-
32- // Special case for class properties
33- // (babel-eslint does not expose property name so we have to rely on tokens)
34- if ( node . type === 'ClassProperty' ) {
35- var tokens = context . getFirstTokens ( node , 2 ) ;
36- if ( tokens [ 0 ] . value === 'propTypes' || ( tokens [ 1 ] && tokens [ 1 ] . value === 'propTypes' ) ) {
37- return true ;
38- }
39- return false ;
39+ var forbid = configuration . forbid || DEFAULTS ;
40+ return forbid . indexOf ( type ) >= 0 ;
4041 }
4142
42- return Boolean (
43- node &&
44- node . name === 'propTypes'
45- ) ;
46- }
47-
48-
49- /**
50- * Checks if propTypes declarations are forbidden
51- * @param {Array } declarations The array of AST nodes being checked.
52- * @returns {void }
53- */
54- function checkForbidden ( declarations ) {
55- declarations . forEach ( function ( declaration ) {
56- if ( declaration . type !== 'Property' ) {
57- return ;
58- }
59- var target ;
60- var value = declaration . value ;
61- if (
62- value . type === 'MemberExpression' &&
63- value . property &&
64- value . property . name &&
65- value . property . name === 'isRequired'
66- ) {
67- value = value . object ;
68- }
69- if (
70- value . type === 'CallExpression' &&
71- value . callee . type === 'MemberExpression'
72- ) {
73- value = value . callee ;
74- }
75- if ( value . property ) {
76- target = value . property . name ;
77- } else if ( value . type === 'Identifier' ) {
78- target = value . name ;
79- }
80- if ( isForbidden ( target ) ) {
81- context . report ( {
82- node : declaration ,
83- message : 'Prop type `' + target + '` is forbidden'
84- } ) ;
43+ /**
44+ * Checks if node is `propTypes` declaration
45+ * @param {ASTNode } node The AST node being checked.
46+ * @returns {Boolean } True if node is `propTypes` declaration, false if not.
47+ */
48+ function isPropTypesDeclaration ( node ) {
49+
50+ // Special case for class properties
51+ // (babel-eslint does not expose property name so we have to rely on tokens)
52+ if ( node . type === 'ClassProperty' ) {
53+ var tokens = context . getFirstTokens ( node , 2 ) ;
54+ if ( tokens [ 0 ] . value === 'propTypes' || ( tokens [ 1 ] && tokens [ 1 ] . value === 'propTypes' ) ) {
55+ return true ;
56+ }
57+ return false ;
8558 }
86- } ) ;
87- }
8859
89- return {
90- ClassProperty : function ( node ) {
91- if ( isPropTypesDeclaration ( node ) && node . value && node . value . type === 'ObjectExpression' ) {
92- checkForbidden ( node . value . properties ) ;
93- }
94- } ,
60+ return Boolean (
61+ node &&
62+ node . name === 'propTypes'
63+ ) ;
64+ }
9565
96- MemberExpression : function ( node ) {
97- if ( isPropTypesDeclaration ( node . property ) ) {
98- var right = node . parent . right ;
99- if ( right && right . type === 'ObjectExpression' ) {
100- checkForbidden ( right . properties ) ;
101- }
102- }
103- } ,
10466
105- ObjectExpression : function ( node ) {
106- node . properties . forEach ( function ( property ) {
107- if ( ! property . key ) {
67+ /**
68+ * Checks if propTypes declarations are forbidden
69+ * @param {Array } declarations The array of AST nodes being checked.
70+ * @returns {void }
71+ */
72+ function checkForbidden ( declarations ) {
73+ declarations . forEach ( function ( declaration ) {
74+ if ( declaration . type !== 'Property' ) {
10875 return ;
10976 }
110-
111- if ( ! isPropTypesDeclaration ( property . key ) ) {
112- return ;
77+ var target ;
78+ var value = declaration . value ;
79+ if (
80+ value . type === 'MemberExpression' &&
81+ value . property &&
82+ value . property . name &&
83+ value . property . name === 'isRequired'
84+ ) {
85+ value = value . object ;
11386 }
114- if ( property . value . type === 'ObjectExpression' ) {
115- checkForbidden ( property . value . properties ) ;
87+ if (
88+ value . type === 'CallExpression' &&
89+ value . callee . type === 'MemberExpression'
90+ ) {
91+ value = value . callee ;
92+ }
93+ if ( value . property ) {
94+ target = value . property . name ;
95+ } else if ( value . type === 'Identifier' ) {
96+ target = value . name ;
97+ }
98+ if ( isForbidden ( target ) ) {
99+ context . report ( {
100+ node : declaration ,
101+ message : 'Prop type `' + target + '` is forbidden'
102+ } ) ;
116103 }
117104 } ) ;
118105 }
119106
120- } ;
121- } ;
122-
123- module . exports . schema = [ {
124- type : 'object' ,
125- properties : {
126- forbid : {
127- type : 'array' ,
128- items : {
129- type : 'string'
107+ return {
108+ ClassProperty : function ( node ) {
109+ if ( isPropTypesDeclaration ( node ) && node . value && node . value . type === 'ObjectExpression' ) {
110+ checkForbidden ( node . value . properties ) ;
111+ }
112+ } ,
113+
114+ MemberExpression : function ( node ) {
115+ if ( isPropTypesDeclaration ( node . property ) ) {
116+ var right = node . parent . right ;
117+ if ( right && right . type === 'ObjectExpression' ) {
118+ checkForbidden ( right . properties ) ;
119+ }
120+ }
121+ } ,
122+
123+ ObjectExpression : function ( node ) {
124+ node . properties . forEach ( function ( property ) {
125+ if ( ! property . key ) {
126+ return ;
127+ }
128+
129+ if ( ! isPropTypesDeclaration ( property . key ) ) {
130+ return ;
131+ }
132+ if ( property . value . type === 'ObjectExpression' ) {
133+ checkForbidden ( property . value . properties ) ;
134+ }
135+ } ) ;
130136 }
131- }
132- } ,
133- additionalProperties : true
134- } ] ;
137+
138+ } ;
139+ }
140+ } ;
0 commit comments