@@ -46,19 +46,33 @@ const OPTIONS_OR_INTEGER_SCHEMA = {
4646} ;
4747
4848/**
49- * Given a list of comment nodes, return a map with numeric keys (source code line numbers) and comment token values .
50- * @param {Array } comments An array of comment nodes .
51- * @returns {Map<string, Node > } A map with numeric keys (source code line numbers) and comment token values .
49+ * Gets the numbers of the lines that contain nothing but comments and whitespace .
50+ * @param {SourceCode } sourceCode The source code .
51+ * @returns {Set<number > } The numbers of the comment-only lines .
5252 */
53- function getCommentLineNumbers ( comments ) {
54- const map = new Map ( ) ;
53+ function getCommentOnlyLines ( sourceCode ) {
54+ const commentOnlyLines = new Set ( ) ;
5555
56- comments . forEach ( comment => {
57- for ( let i = comment . loc . start . line ; i <= comment . loc . end . line ; i ++ ) {
58- map . set ( i , comment ) ;
56+ for ( const comment of sourceCode . getAllComments ( ) ) {
57+ const tokenBefore = sourceCode . getTokenBefore ( comment ) ;
58+ const tokenAfter = sourceCode . getTokenAfter ( comment ) ;
59+ let start = comment . loc . start . line ;
60+ let end = comment . loc . end . line ;
61+
62+ if ( tokenBefore && astUtils . isTokenOnSameLine ( tokenBefore , comment ) ) {
63+ start ++ ;
64+ }
65+
66+ if ( tokenAfter && astUtils . isTokenOnSameLine ( comment , tokenAfter ) ) {
67+ end -- ;
5968 }
60- } ) ;
61- return map ;
69+
70+ for ( let line = start ; line <= end ; line ++ ) {
71+ commentOnlyLines . add ( line ) ;
72+ }
73+ }
74+
75+ return commentOnlyLines ;
6276}
6377
6478//------------------------------------------------------------------------------
@@ -105,37 +119,14 @@ module.exports = {
105119 maxLines = option ;
106120 }
107121
108- const commentLineNumbers = getCommentLineNumbers (
109- sourceCode . getAllComments ( ) ,
110- ) ;
122+ const commentOnlyLines = skipComments
123+ ? getCommentOnlyLines ( sourceCode )
124+ : null ;
111125
112126 //--------------------------------------------------------------------------
113127 // Helpers
114128 //--------------------------------------------------------------------------
115129
116- /**
117- * Tells if a comment encompasses the entire line.
118- * @param {string } line The source line with a trailing comment
119- * @param {number } lineNumber The one-indexed line number this is on
120- * @param {ASTNode } comment The comment to remove
121- * @returns {boolean } If the comment covers the entire line
122- */
123- function isFullLineComment ( line , lineNumber , comment ) {
124- const start = comment . loc . start ,
125- end = comment . loc . end ,
126- isFirstTokenOnLine =
127- start . line === lineNumber &&
128- ! line . slice ( 0 , start . column ) . trim ( ) ,
129- isLastTokenOnLine =
130- end . line === lineNumber && ! line . slice ( end . column ) . trim ( ) ;
131-
132- return (
133- comment &&
134- ( start . line < lineNumber || isFirstTokenOnLine ) &&
135- ( end . line > lineNumber || isLastTokenOnLine )
136- ) ;
137- }
138-
139130 /**
140131 * Identifies is a node is a FunctionExpression which is part of an IIFE
141132 * @param {ASTNode } node Node to test
@@ -191,25 +182,12 @@ module.exports = {
191182 let lineCount = 0 ;
192183
193184 for ( let i = node . loc . start . line - 1 ; i < node . loc . end . line ; ++ i ) {
194- const line = lines [ i ] ;
195-
196- if ( skipComments ) {
197- if (
198- commentLineNumbers . has ( i + 1 ) &&
199- isFullLineComment (
200- line ,
201- i + 1 ,
202- commentLineNumbers . get ( i + 1 ) ,
203- )
204- ) {
205- continue ;
206- }
185+ if ( skipComments && commentOnlyLines . has ( i + 1 ) ) {
186+ continue ;
207187 }
208188
209- if ( skipBlankLines ) {
210- if ( line . match ( / ^ \s * $ / u) ) {
211- continue ;
212- }
189+ if ( skipBlankLines && / ^ \s * $ / u. test ( lines [ i ] ) ) {
190+ continue ;
213191 }
214192
215193 lineCount ++ ;
0 commit comments