Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit cdb1005

Browse files
committed
PR review fixes
1 parent ed9881f commit cdb1005

2 files changed

Lines changed: 74 additions & 54 deletions

File tree

lib/js-file.js

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

test/specs/js-file.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,4 +1490,55 @@ describe('js-file', function() {
14901490
expect(spy.getCall(1).args[0].value).to.equal('2');
14911491
});
14921492
});
1493+
1494+
describe('getWhitespaceBefore', function() {
1495+
it('should return whitespace before the first token', function() {
1496+
var file = createJsFile(' \nx;');
1497+
expect(file.getWhitespaceBefore(file.getFirstToken())).to.equal(' \n');
1498+
});
1499+
1500+
it('should return whitespace before the EOF', function() {
1501+
var file = createJsFile('\n ');
1502+
expect(file.getWhitespaceBefore(file.getLastToken())).to.equal('\n ');
1503+
});
1504+
1505+
it('should return empty for tokens without whitespace before', function() {
1506+
var file = createJsFile('x');
1507+
expect(file.getWhitespaceBefore(file.getFirstToken())).to.equal('');
1508+
});
1509+
});
1510+
1511+
describe('setWhitespaceBefore', function() {
1512+
it('should alter existing whitespace token', function() {
1513+
var file = createJsFile(' x');
1514+
expect(file.getTokens().length).to.equal(3);
1515+
file.setWhitespaceBefore(file.getFirstToken(), '\n');
1516+
expect(file.getTokens().length).to.equal(3);
1517+
expect(file.getTokens()[0].value).to.equal('\n');
1518+
});
1519+
1520+
it('should insert new whitespace token', function() {
1521+
var file = createJsFile('x');
1522+
expect(file.getTokens().length).to.equal(2);
1523+
file.setWhitespaceBefore(file.getFirstToken(), '\n');
1524+
expect(file.getTokens().length).to.equal(3);
1525+
expect(file.getTokens()[0].value).to.equal('\n');
1526+
});
1527+
1528+
it('should drop whitespace token', function() {
1529+
var file = createJsFile(' x');
1530+
expect(file.getTokens().length).to.equal(3);
1531+
file.setWhitespaceBefore(file.getFirstToken(), '');
1532+
expect(file.getTokens().length).to.equal(2);
1533+
expect(file.getTokens()[0].type).to.equal('Identifier');
1534+
});
1535+
1536+
it('should ignore already absent whitespace token', function() {
1537+
var file = createJsFile('x');
1538+
expect(file.getTokens().length).to.equal(2);
1539+
file.setWhitespaceBefore(file.getFirstToken(), '');
1540+
expect(file.getTokens().length).to.equal(2);
1541+
expect(file.getTokens()[0].type).to.equal('Identifier');
1542+
});
1543+
});
14931544
});

0 commit comments

Comments
 (0)