@@ -144,22 +144,25 @@ JsFile.prototype = {
144144 setWhitespaceBefore : function ( token , whitespace ) {
145145 var whitespaceToken = this . getPrevToken ( token , { includeWhitespace : true } ) ;
146146 if ( whitespaceToken && whitespaceToken . type === 'Whitespace' ) {
147+
148+ // Modifying already existing token.
147149 if ( whitespace === '' ) {
148150 this . removeToken ( whitespaceToken ) ;
149151 } else {
150152 whitespaceToken . value = whitespace ;
151153 }
152- } else {
153- if ( whitespace === '' ) {
154- return ;
155- }
156- var tokenIndex = this . _tokens . indexOf ( token ) ;
154+ } else if ( whitespace !== '' ) {
155+ var tokenIndex = token . _tokenIndex ;
156+
157+ // Adding a token before specified one.
157158 this . _tokens . splice ( tokenIndex , 0 , {
158159 type : 'Whitespace' ,
159160 value : whitespace ,
160161 isWhitespace : true
161162 } ) ;
162- for ( var i = tokenIndex , l = this . _tokens . length ; i < l ; i ++ ) {
163+
164+ // Quickly updating modified token order
165+ for ( var i = tokenIndex ; i < this . _tokens . length ; i ++ ) {
163166 this . _tokens [ i ] . _tokenIndex = i ;
164167 }
165168 }
@@ -362,7 +365,7 @@ JsFile.prototype = {
362365 * @returns {Object }
363366 */
364367 getFirstToken : function ( options ) {
365- return this . _getNextTokenFromIndex ( 0 , options ) ;
368+ return this . _getTokenFromIndex ( 0 , 1 , options ) ;
366369 } ,
367370
368371 /**
@@ -374,70 +377,36 @@ JsFile.prototype = {
374377 * @returns {Object }
375378 */
376379 getLastToken : function ( options ) {
377- return this . _getPrevTokenFromIndex ( this . _tokens . length - 1 , options ) ;
380+ return this . _getTokenFromIndex ( this . _tokens . length - 1 , - 1 , options ) ;
378381 } ,
379382
380383 /**
381- * Returns the first token before the given index .
384+ * Returns the first token after the given using direction and specified conditions .
382385 *
383386 * @param {Number } index
387+ * @param {Number } direction `1` - forward or `-1` - backwards
384388 * @param {Object } [options]
385389 * @param {Boolean } [options.includeComments=false]
386390 * @param {Boolean } [options.includeWhitespace=false]
387391 * @returns {Object|null }
388392 */
389- _getPrevTokenFromIndex : function ( index , options ) {
390- if ( index < 0 ) {
391- return null ;
392- }
393+ _getTokenFromIndex : function ( index , direction , options ) {
394+ while ( true ) {
395+ var followingToken = this . _tokens [ index ] ;
393396
394- do {
395- var prevToken = this . _tokens [ index ] ;
396-
397- if ( ! prevToken ) {
397+ if ( ! followingToken ) {
398398 return null ;
399399 }
400400
401401 if (
402- ( ! prevToken . isComment || ( options && options . includeComments ) ) &&
403- ( ! prevToken . isWhitespace || ( options && options . includeWhitespace ) )
402+ ( ! followingToken . isComment || ( options && options . includeComments ) ) &&
403+ ( ! followingToken . isWhitespace || ( options && options . includeWhitespace ) )
404404 ) {
405- return prevToken ;
405+ return followingToken ;
406406 }
407- } while ( -- index >= 0 ) ;
408407
409- return null ;
410- } ,
411-
412- /**
413- * Returns the first token after the given.
414- *
415- * @param {Number } index
416- * @param {Object } [options]
417- * @param {Boolean } [options.includeComments=false]
418- * @param {Boolean } [options.includeWhitespace=false]
419- * @returns {Object|null }
420- */
421- _getNextTokenFromIndex : function ( index , options ) {
422- if ( index >= this . _tokens . length ) {
423- return null ;
408+ index += direction ;
424409 }
425-
426- do {
427- var nextToken = this . _tokens [ index ] ;
428- if ( ! nextToken ) {
429- return null ;
430- }
431-
432- if (
433- ( ! nextToken . isComment || ( options && options . includeComments ) ) &&
434- ( ! nextToken . isWhitespace || ( options && options . includeWhitespace ) )
435- ) {
436- return nextToken ;
437- }
438- } while ( ++ index < this . _tokens . length ) ;
439-
440- return null ;
441410 } ,
442411
443412 /**
@@ -450,7 +419,7 @@ JsFile.prototype = {
450419 * @returns {Object|null }
451420 */
452421 getPrevToken : function ( token , options ) {
453- return this . _getPrevTokenFromIndex ( token . _tokenIndex - 1 , options ) ;
422+ return this . _getTokenFromIndex ( token . _tokenIndex - 1 , - 1 , options ) ;
454423 } ,
455424
456425 /**
@@ -463,7 +432,7 @@ JsFile.prototype = {
463432 * @returns {Object|null }
464433 */
465434 getNextToken : function ( token , options ) {
466- return this . _getNextTokenFromIndex ( token . _tokenIndex + 1 , options ) ;
435+ return this . _getTokenFromIndex ( token . _tokenIndex + 1 , 1 , options ) ;
467436 } ,
468437
469438 /**
0 commit comments