@@ -38,13 +38,6 @@ function unescape(str) {
38
38
39
39
tokenize . unescape = unescape ;
40
40
41
- /**
42
- * Gets the current line number.
43
- * @typedef TokenizerHandleLine
44
- * @type {function }
45
- * @returns {number } Line number
46
- */
47
-
48
41
/**
49
42
* Gets the next token and advances.
50
43
* @typedef TokenizerHandleNext
@@ -88,12 +81,12 @@ tokenize.unescape = unescape;
88
81
/**
89
82
* Handle object returned from {@link tokenize}.
90
83
* @interface ITokenizerHandle
91
- * @property {TokenizerHandleLine } line Gets the current line number
92
84
* @property {TokenizerHandleNext } next Gets the next token and advances (`null` on eof)
93
85
* @property {TokenizerHandlePeek } peek Peeks for the next token (`null` on eof)
94
86
* @property {TokenizerHandlePush } push Pushes a token back to the stack
95
87
* @property {TokenizerHandleSkip } skip Skips a token, returns its presence and advances or, if non-optional and not present, throws
96
88
* @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
97
90
*/
98
91
99
92
/**
@@ -110,7 +103,8 @@ function tokenize(source) {
110
103
line = 1 ,
111
104
commentType = null ,
112
105
commentText = null ,
113
- commentLine = 0 ;
106
+ commentLine = 0 ,
107
+ commentLineEmpty = false ;
114
108
115
109
var stack = [ ] ;
116
110
@@ -164,6 +158,15 @@ function tokenize(source) {
164
158
function setComment ( start , end ) {
165
159
commentType = source . charAt ( start ++ ) ;
166
160
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" ) ;
167
170
var lines = source
168
171
. substring ( start , end )
169
172
. split ( setCommentSplitRe ) ;
@@ -188,7 +191,7 @@ function tokenize(source) {
188
191
prev ,
189
192
curr ,
190
193
start ,
191
- isComment ;
194
+ isDoc ;
192
195
do {
193
196
if ( offset === length )
194
197
return null ;
@@ -203,17 +206,17 @@ function tokenize(source) {
203
206
if ( ++ offset === length )
204
207
throw illegal ( "comment" ) ;
205
208
if ( charAt ( offset ) === "/" ) { // Line
206
- isComment = charAt ( start = offset + 1 ) === "/" ;
209
+ isDoc = charAt ( start = offset + 1 ) === "/" ;
207
210
while ( charAt ( ++ offset ) !== "\n" )
208
211
if ( offset === length )
209
212
return null ;
210
213
++ offset ;
211
- if ( isComment )
214
+ if ( isDoc ) /// Comment
212
215
setComment ( start , offset - 1 ) ;
213
216
++ line ;
214
217
repeat = true ;
215
218
} else if ( ( curr = charAt ( offset ) ) === "*" ) { /* Block */
216
- isComment = charAt ( start = offset + 1 ) === "*" ;
219
+ isDoc = charAt ( start = offset + 1 ) === "*" ;
217
220
do {
218
221
if ( curr === "\n" )
219
222
++ line ;
@@ -223,7 +226,7 @@ function tokenize(source) {
223
226
curr = charAt ( offset ) ;
224
227
} while ( prev !== "*" || curr !== "/" ) ;
225
228
++ offset ;
226
- if ( isComment )
229
+ if ( isDoc ) /** Comment */
227
230
setComment ( start , offset - 2 ) ;
228
231
repeat = true ;
229
232
} else
@@ -292,33 +295,32 @@ function tokenize(source) {
292
295
293
296
/**
294
297
* Gets a comment.
295
- * @param {number } [trailingLine] Trailing line number if applicable
298
+ * @param {number } [trailingLine] Line number if looking for a trailing comment
296
299
* @returns {?string } Comment text
297
300
* @inner
298
301
*/
299
302
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 !== "/" )
305
309
peek ( ) ;
306
- ret = commentLine === trailingLine && commentType === "/" && commentText || null ;
310
+ if ( commentLine === trailingLine && ! commentLineEmpty && commentType === "/" )
311
+ ret = commentText ;
307
312
}
308
- commentType = commentText = null ;
309
- commentLine = 0 ;
310
313
return ret ;
311
314
}
312
315
313
- return {
316
+ return Object . defineProperty ( {
314
317
next : next ,
315
318
peek : peek ,
316
319
push : push ,
317
320
skip : skip ,
318
- line : function ( ) {
319
- return line ;
320
- } ,
321
321
cmnt : cmnt
322
- } ;
322
+ } , "line" , {
323
+ get : function ( ) { return line ; }
324
+ } ) ;
323
325
/* eslint-enable callback-return */
324
326
}
0 commit comments