@@ -35,11 +35,28 @@ module.exports = {
3535 const sourceCode = context . getSourceCode ( ) ;
3636
3737 /**
38+ * @param {Scope } scope
39+ * @return {Array<ASTNode> }
40+ */
41+ function getScopeBody ( scope ) {
42+ return scope . type === 'function' ? scope . block . body . body : scope . block . body ;
43+ }
44+
45+ /**
46+ * @param {Scope } scope
47+ * @return {number }
48+ */
49+ function getScopeStart ( scope ) {
50+ return scope . type === 'function' ? scope . block . body . start : scope . block . start ;
51+ }
52+
53+ /**
54+ * @param {Scope } upperScope
3855 * @param {ASTNode } classNode
3956 * @return {boolean }
4057 */
41- function isCollisionWithAnotherClassBefore ( classNode ) {
42- const upperScopeBody = context . getScope ( ) . upper . block . body ;
58+ function isCollisionBefore ( upperScope , classNode ) {
59+ const upperScopeBody = getScopeBody ( upperScope ) ;
4360 const classIndexInScope = upperScopeBody . indexOf ( classNode ) ;
4461 const classJSDocComment = getJSDocComment ( classNode , sourceCode ) ;
4562
@@ -56,11 +73,12 @@ module.exports = {
5673 }
5774
5875 /**
76+ * @param {Scope } upperScope
5977 * @param {ASTNode } classNode
6078 * @return {boolean }
6179 */
62- function isCollisionWithAnotherClassAfter ( classNode ) {
63- const upperScopeBody = context . getScope ( ) . upper . block . body ;
80+ function isCollisionAfter ( upperScope , classNode ) {
81+ const upperScopeBody = getScopeBody ( upperScope ) ;
6482 const classIndexInScope = upperScopeBody . indexOf ( classNode ) ;
6583
6684 const nodeAfterClass = upperScopeBody [ classIndexInScope + 1 ] || null ;
@@ -81,24 +99,31 @@ module.exports = {
8199 * @param {ASTNode } classNode
82100 */
83101 function check ( classNode ) {
102+ const upperScope = context . getScope ( ) . upper ;
103+ const upperScopeStart = getScopeStart ( upperScope ) ;
84104 const classJSDocComment = getJSDocComment ( classNode , sourceCode ) ;
85105
86106 const tokenBeforeClass = sourceCode . getTokenBefore ( ( classJSDocComment || classNode ) , {
87- includeComments : true
107+ includeComments : true ,
108+ filter ( token ) {
109+ return token . start > upperScopeStart ;
110+ }
88111 } ) ;
89112
90113 const tokenAfterClass = sourceCode . getTokenAfter ( ( classNode ) , {
91- includeComments : true
114+ includeComments : true ,
115+ filter ( token ) {
116+ return token . start > upperScopeStart ;
117+ }
92118 } ) ;
93119
94120 if ( tokenBeforeClass ) {
95- if ( isCollisionWithAnotherClassBefore ( classNode ) && options . collisionPriority !== 'before' ) {
121+ if ( isCollisionBefore ( upperScope , classNode ) && options . collisionPriority !== 'before' ) {
96122 return ;
97123 }
98124
99125 const classStartLine = ( classJSDocComment || classNode ) . loc . start . line ;
100126 const tokenBeforeClassEndLine = tokenBeforeClass . loc . end . line ;
101-
102127 const newlinesAmountBeforeClass = Math . max ( classStartLine - tokenBeforeClassEndLine - 1 , 0 ) ;
103128
104129 if ( newlinesAmountBeforeClass !== options . before ) {
@@ -122,13 +147,12 @@ module.exports = {
122147 }
123148
124149 if ( tokenAfterClass ) {
125- if ( isCollisionWithAnotherClassAfter ( classNode ) && options . collisionPriority !== 'after' ) {
150+ if ( isCollisionAfter ( upperScope , classNode ) && options . collisionPriority !== 'after' ) {
126151 return ;
127152 }
128153
129154 const classEndLine = classNode . loc . end . line ;
130155 const tokenAfterClassStartLine = tokenAfterClass . loc . start . line ;
131-
132156 const newlinesAmountAfterClass = Math . max ( tokenAfterClassStartLine - classEndLine - 1 , 0 ) ;
133157
134158 if ( newlinesAmountAfterClass !== options . after ) {
0 commit comments