@@ -55,7 +55,56 @@ module.exports = {
55
55
* @returns {boolean } True if there is at least a line between the tokens
56
56
*/
57
57
function isPaddingBetweenTokens ( first , second ) {
58
- return second . loc . start . line - first . loc . end . line >= 2 ;
58
+ const comments = sourceCode . getCommentsBefore ( second ) ;
59
+ const len = comments . length ;
60
+
61
+ // If there is no comments
62
+ if ( len === 0 ) {
63
+ const linesBetweenFstAndSnd = second . loc . start . line - first . loc . end . line - 1 ;
64
+
65
+ return linesBetweenFstAndSnd >= 1 ;
66
+ }
67
+
68
+
69
+ // If there are comments
70
+ let sumOfCommentLines = 0 ; // the numbers of lines of comments
71
+ let prevCommentLineNum = - 1 ; // line number of the end of the previous comment
72
+
73
+ for ( let i = 0 ; i < len ; i ++ ) {
74
+ const commentLinesOfThisComment = comments [ i ] . loc . end . line - comments [ i ] . loc . start . line + 1 ;
75
+
76
+ sumOfCommentLines += commentLinesOfThisComment ;
77
+
78
+ /*
79
+ * If this comment and the previous comment are in the same line,
80
+ * the count of comment lines is duplicated. So decrement sumOfCommentLines.
81
+ */
82
+ if ( prevCommentLineNum === comments [ i ] . loc . start . line ) {
83
+ sumOfCommentLines -= 1 ;
84
+ }
85
+
86
+ prevCommentLineNum = comments [ i ] . loc . end . line ;
87
+ }
88
+
89
+ /*
90
+ * If the first block and the first comment are in the same line,
91
+ * the count of comment lines is duplicated. So decrement sumOfCommentLines.
92
+ */
93
+ if ( first . loc . end . line === comments [ 0 ] . loc . start . line ) {
94
+ sumOfCommentLines -= 1 ;
95
+ }
96
+
97
+ /*
98
+ * If the last comment and the second block are in the same line,
99
+ * the count of comment lines is duplicated. So decrement sumOfCommentLines.
100
+ */
101
+ if ( comments [ len - 1 ] . loc . end . line === second . loc . start . line ) {
102
+ sumOfCommentLines -= 1 ;
103
+ }
104
+
105
+ const linesBetweenFstAndSnd = second . loc . start . line - first . loc . end . line - 1 ;
106
+
107
+ return linesBetweenFstAndSnd - sumOfCommentLines >= 1 ;
59
108
}
60
109
61
110
return {
@@ -65,8 +114,7 @@ module.exports = {
65
114
for ( let i = 0 ; i < body . length - 1 ; i ++ ) {
66
115
const curFirst = sourceCode . getFirstToken ( body [ i ] ) ;
67
116
const curLast = sourceCode . getLastToken ( body [ i ] ) ;
68
- const comments = sourceCode . getCommentsBefore ( body [ i + 1 ] ) ;
69
- const nextFirst = comments . length ? comments [ 0 ] : sourceCode . getFirstToken ( body [ i + 1 ] ) ;
117
+ const nextFirst = sourceCode . getFirstToken ( body [ i + 1 ] ) ;
70
118
const isPadded = isPaddingBetweenTokens ( curLast , nextFirst ) ;
71
119
const isMulti = ! astUtils . isTokenOnSameLine ( curFirst , curLast ) ;
72
120
const skip = ! isMulti && options [ 1 ] . exceptAfterSingleLine ;
0 commit comments