@@ -7,8 +7,14 @@ const { getImportIdentifier } = require('../utils/import');
77// General rule - Always return a value from computed properties
88//------------------------------------------------------------------------------
99
10- function isReachable ( segment ) {
11- return segment . reachable ;
10+ function isAnySegmentReachable ( segments ) {
11+ for ( const segment of segments ) {
12+ if ( segment . reachable ) {
13+ return true ;
14+ }
15+ }
16+
17+ return false ;
1218}
1319
1420const ERROR_MESSAGE = 'Always return a value from computed properties' ;
@@ -39,17 +45,17 @@ module.exports = {
3945 codePath : null ,
4046 shouldCheck : false ,
4147 node : null ,
48+ currentSegments : [ ] ,
4249 } ;
4350
4451 function checkLastSegment ( node ) {
45- if ( funcInfo . shouldCheck && funcInfo . codePath . currentSegments . some ( isReachable ) ) {
52+ if ( funcInfo . shouldCheck && isAnySegmentReachable ( funcInfo . currentSegments ) ) {
4653 report ( node ) ;
4754 }
4855 }
4956
5057 let importedEmberName ;
5158 let importedComputedName ;
52-
5359 return {
5460 ImportDeclaration ( node ) {
5561 if ( node . source . value === 'ember' ) {
@@ -61,22 +67,43 @@ module.exports = {
6167 }
6268 } ,
6369
64- onCodePathStart ( codePath ) {
70+ onCodePathStart ( codePath , node ) {
71+ const sourceCode = context . sourceCode ?? context . getSourceCode ( ) ;
72+ const ancestors = sourceCode . getAncestors
73+ ? sourceCode . getAncestors ( node )
74+ : context . getAncestors ( ) ;
75+
6576 funcInfo = {
6677 upper : funcInfo ,
6778 codePath,
6879 shouldCheck :
69- context
70- . getAncestors ( )
71- . findIndex ( ( node ) =>
72- ember . isComputedProp ( node , importedEmberName , importedComputedName )
73- ) > - 1 ,
80+ ancestors . findIndex ( ( node ) =>
81+ ember . isComputedProp ( node , importedEmberName , importedComputedName )
82+ ) > - 1 ,
83+ node,
84+ currentSegments : new Set ( ) ,
7485 } ;
7586 } ,
7687
88+ // Pops this function's information.
7789 onCodePathEnd ( ) {
7890 funcInfo = funcInfo . upper ;
7991 } ,
92+ onUnreachableCodePathSegmentStart ( segment ) {
93+ funcInfo . currentSegments . add ( segment ) ;
94+ } ,
95+
96+ onUnreachableCodePathSegmentEnd ( segment ) {
97+ funcInfo . currentSegments . delete ( segment ) ;
98+ } ,
99+
100+ onCodePathSegmentStart ( segment ) {
101+ funcInfo . currentSegments . add ( segment ) ;
102+ } ,
103+
104+ onCodePathSegmentEnd ( segment ) {
105+ funcInfo . currentSegments . delete ( segment ) ;
106+ } ,
80107
81108 'FunctionExpression:exit' ( node ) {
82109 if ( node . parent . parent . parent === null ) {
0 commit comments