@@ -67,17 +67,11 @@ module.exports.prototype = {
6767
6868 check : function ( file , errors ) {
6969
70- function addError ( typeString , node ) {
71- var entity = node ;
72-
73- if ( typeString === 'Else' ) {
74- entity = file . findPrevToken (
75- file . getTokenByRangeStart ( node . alternate . range [ 0 ] ) ,
76- 'Keyword' ,
77- 'else'
78- ) ;
79- }
70+ function isNotABlockStatement ( node ) {
71+ return node && node . type !== 'BlockStatement' ;
72+ }
8073
74+ function addError ( typeString , entity ) {
8175 errors . add (
8276 typeString + ' statement without curly braces' ,
8377 entity . loc . start . line ,
@@ -87,7 +81,7 @@ module.exports.prototype = {
8781
8882 function checkBody ( type , typeString ) {
8983 file . iterateNodesByType ( type , function ( node ) {
90- if ( node . body . type !== 'BlockStatement' ) {
84+ if ( isNotABlockStatement ( node . body ) ) {
9185 addError ( typeString , node ) ;
9286 }
9387 } ) ;
@@ -97,14 +91,12 @@ module.exports.prototype = {
9791
9892 if ( typeIndex [ 'if' ] || typeIndex [ 'else' ] ) {
9993 file . iterateNodesByType ( 'IfStatement' , function ( node ) {
100- if ( typeIndex . if && node . consequent . type !== 'BlockStatement' ) {
94+ if ( typeIndex [ 'if' ] && isNotABlockStatement ( node . consequent ) ) {
10195 addError ( 'If' , node ) ;
10296 }
103- if ( typeIndex [ 'else' ] && node . alternate &&
104- node . alternate . type !== 'BlockStatement' &&
105- node . alternate . type !== 'IfStatement'
106- ) {
107- addError ( 'Else' , node ) ;
97+ if ( typeIndex [ 'else' ] && isNotABlockStatement ( node . alternate ) &&
98+ node . alternate . type !== 'IfStatement' ) {
99+ addError ( 'Else' , file . getPrevToken ( file . getFirstNodeToken ( node . alternate ) ) ) ;
108100 }
109101 } ) ;
110102 }
0 commit comments