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

Commit

Permalink
TokenAssert: simplify and strengthen linesBetween rules
Browse files Browse the repository at this point in the history
Fixes #1089
Closes gh-1114
  • Loading branch information
mikesherov committed Feb 24, 2015
1 parent b615013 commit c9265a1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
7 changes: 1 addition & 6 deletions lib/rules/require-line-feed-at-file-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ module.exports = function() {};
module.exports.prototype = {

configure: function(requireLineFeedAtFileEnd) {
assert(
typeof requireLineFeedAtFileEnd === 'boolean',
'requireLineFeedAtFileEnd option requires boolean value'
);
assert(
requireLineFeedAtFileEnd === true,
'requireLineFeedAtFileEnd option requires true value or should be removed'
Expand All @@ -36,10 +32,9 @@ module.exports.prototype = {
check: function(file, errors) {
var lastToken = file.getLastToken();
var prevToken = file.getPrevToken(lastToken, {includeComments: true});
errors.assert.linesBetween({
errors.assert.differentLine({
token: prevToken,
nextToken: lastToken,
atLeast: 1,
message: 'Missing line feed at file end'
});
}
Expand Down
46 changes: 16 additions & 30 deletions lib/token-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,6 @@ TokenAssert.prototype.noWhitespaceBetween = function(options) {
* @param {Boolean} [options.requireNextToken=false]
*/
TokenAssert.prototype.sameLine = function(options) {
var token = options.token;
var nextToken = options.nextToken;

if (options.requireNextToken && (!nextToken || nextToken.type === 'EOF')) {
this.emit('error', {
message: options.message || token.value + ' should not be the last in the file',
line: token.loc.end.line,
column: token.loc.end.column
});
return;
}

if (!token || !nextToken) {
return;
}

options.message = options.message || token.value + ' and ' + nextToken.value + ' should be on the same line';
options.exactly = 0;

this.linesBetween(options);
Expand All @@ -116,14 +99,6 @@ TokenAssert.prototype.sameLine = function(options) {
* @param {Object} [options.message]
*/
TokenAssert.prototype.differentLine = function(options) {
var token = options.token;
var nextToken = options.nextToken;

if (!token || !nextToken) {
return;
}

options.message = options.message || token.value + ' and ' + nextToken.value + ' should be on different lines';
options.atLeast = 1;

this.linesBetween(options);
Expand All @@ -149,6 +124,10 @@ TokenAssert.prototype.linesBetween = function(options) {
return;
}

if (token === nextToken) {
throw new Error('You cannot specify the same token as both token and nextToken');
}

var atLeast = options.atLeast;
var atMost = options.atMost;
var exactly = options.exactly;
Expand All @@ -167,20 +146,27 @@ TokenAssert.prototype.linesBetween = function(options) {
throw new Error('atLeast and atMost are in conflict');
}

// support sameLine
if (exactly === 0 && !options.message) {
options.message = token.value + ' and ' + nextToken.value + ' should be on the same line';
}

// support differentLine
if (atLeast === 1 && atMost === undefined && !options.message) {
options.message = token.value + ' and ' + nextToken.value + ' should be on different lines';
}

var linesBetween = Math.abs(token.loc.end.line - nextToken.loc.start.line);

var errorMessage = token.value + ' and ' + nextToken.value + ' should have ';
var emitError = (function(message, whitespaceBefore) {
var fixed = typeof whitespaceBefore !== 'undefined';
if (fixed) {
nextToken.whitespaceBefore = whitespaceBefore;
}
nextToken.whitespaceBefore = whitespaceBefore;

this.emit('error', {
message: options.message || errorMessage + message + ' line(s) between them',
line: token.loc.end.line,
column: token.loc.end.column,
fixed: fixed
fixed: true
});
}).bind(this);

Expand Down
11 changes: 11 additions & 0 deletions test/token-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,17 @@ describe('modules/token-assert', function() {
}).bind(this),
/atLeast and atMost are in conflict/);
});

it('should throw if token and nextToken are the same', function() {
assert.throws((function() {
this.tokenAssert.linesBetween({
token: this.tokens[0],
nextToken: this.tokens[0],
atLeast: 1
});
}).bind(this),
/You cannot specify the same token as both token and nextToken/);
});
});

it('should not throw if token or nextToken properties are undefined', function() {
Expand Down

0 comments on commit c9265a1

Please sign in to comment.