@@ -38,13 +38,6 @@ function unescape(str) {
3838
3939tokenize . unescape = unescape ;
4040
41- /**
42- * Gets the current line number.
43- * @typedef TokenizerHandleLine
44- * @type {function }
45- * @returns {number } Line number
46- */
47-
4841/**
4942 * Gets the next token and advances.
5043 * @typedef TokenizerHandleNext
@@ -88,12 +81,12 @@ tokenize.unescape = unescape;
8881/**
8982 * Handle object returned from {@link tokenize}.
9083 * @interface ITokenizerHandle
91- * @property {TokenizerHandleLine } line Gets the current line number
9284 * @property {TokenizerHandleNext } next Gets the next token and advances (`null` on eof)
9385 * @property {TokenizerHandlePeek } peek Peeks for the next token (`null` on eof)
9486 * @property {TokenizerHandlePush } push Pushes a token back to the stack
9587 * @property {TokenizerHandleSkip } skip Skips a token, returns its presence and advances or, if non-optional and not present, throws
9688 * @property {TokenizerHandleCmnt } cmnt Gets the comment on the previous line or the line comment on the specified line, if any
89+ * @property {number } line Current line number
9790 */
9891
9992/**
@@ -110,7 +103,8 @@ function tokenize(source) {
110103 line = 1 ,
111104 commentType = null ,
112105 commentText = null ,
113- commentLine = 0 ;
106+ commentLine = 0 ,
107+ commentLineEmpty = false ;
114108
115109 var stack = [ ] ;
116110
@@ -164,6 +158,15 @@ function tokenize(source) {
164158 function setComment ( start , end ) {
165159 commentType = source . charAt ( start ++ ) ;
166160 commentLine = line ;
161+ commentLineEmpty = false ;
162+ var offset = start - 3 , // "///" or "/**"
163+ c ;
164+ do {
165+ if ( -- offset < 0 || ( c = source . charAt ( offset ) ) === "\n" ) {
166+ commentLineEmpty = true ;
167+ break ;
168+ }
169+ } while ( c === " " || c === "\t" ) ;
167170 var lines = source
168171 . substring ( start , end )
169172 . split ( setCommentSplitRe ) ;
@@ -188,7 +191,7 @@ function tokenize(source) {
188191 prev ,
189192 curr ,
190193 start ,
191- isComment ;
194+ isDoc ;
192195 do {
193196 if ( offset === length )
194197 return null ;
@@ -203,17 +206,17 @@ function tokenize(source) {
203206 if ( ++ offset === length )
204207 throw illegal ( "comment" ) ;
205208 if ( charAt ( offset ) === "/" ) { // Line
206- isComment = charAt ( start = offset + 1 ) === "/" ;
209+ isDoc = charAt ( start = offset + 1 ) === "/" ;
207210 while ( charAt ( ++ offset ) !== "\n" )
208211 if ( offset === length )
209212 return null ;
210213 ++ offset ;
211- if ( isComment )
214+ if ( isDoc ) /// Comment
212215 setComment ( start , offset - 1 ) ;
213216 ++ line ;
214217 repeat = true ;
215218 } else if ( ( curr = charAt ( offset ) ) === "*" ) { /* Block */
216- isComment = charAt ( start = offset + 1 ) === "*" ;
219+ isDoc = charAt ( start = offset + 1 ) === "*" ;
217220 do {
218221 if ( curr === "\n" )
219222 ++ line ;
@@ -223,7 +226,7 @@ function tokenize(source) {
223226 curr = charAt ( offset ) ;
224227 } while ( prev !== "*" || curr !== "/" ) ;
225228 ++ offset ;
226- if ( isComment )
229+ if ( isDoc ) /** Comment */
227230 setComment ( start , offset - 2 ) ;
228231 repeat = true ;
229232 } else
@@ -292,33 +295,32 @@ function tokenize(source) {
292295
293296 /**
294297 * Gets a comment.
295- * @param {number } [trailingLine] Trailing line number if applicable
298+ * @param {number } [trailingLine] Line number if looking for a trailing comment
296299 * @returns {?string } Comment text
297300 * @inner
298301 */
299302 function cmnt ( trailingLine ) {
300- var ret ;
301- if ( trailingLine === undefined )
302- ret = commentLine === line - 1 && commentText || null ;
303- else {
304- if ( ! commentText )
303+ var ret = null ;
304+ if ( trailingLine === undefined ) {
305+ if ( commentLine === line - 1 && ( commentType === "*" || commentLineEmpty ) )
306+ ret = commentText ;
307+ } else {
308+ if ( commentLine !== trailingLine || commentLineEmpty || commentType !== "/" )
305309 peek ( ) ;
306- ret = commentLine === trailingLine && commentType === "/" && commentText || null ;
310+ if ( commentLine === trailingLine && ! commentLineEmpty && commentType === "/" )
311+ ret = commentText ;
307312 }
308- commentType = commentText = null ;
309- commentLine = 0 ;
310313 return ret ;
311314 }
312315
313- return {
316+ return Object . defineProperty ( {
314317 next : next ,
315318 peek : peek ,
316319 push : push ,
317320 skip : skip ,
318- line : function ( ) {
319- return line ;
320- } ,
321321 cmnt : cmnt
322- } ;
322+ } , "line" , {
323+ get : function ( ) { return line ; }
324+ } ) ;
323325 /* eslint-enable callback-return */
324326}
0 commit comments