This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00aaf18
commit ae69d9d
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var assert = require('assert'); | ||
var tokenHelper = require('../token-helper'); | ||
|
||
module.exports = function() {}; | ||
|
||
module.exports.prototype = { | ||
configure: function(disallowSemicolons) { | ||
assert( | ||
disallowSemicolons === true, | ||
'disallowSemicolons option requires true value or should be removed' | ||
); | ||
}, | ||
|
||
getOptionName: function() { | ||
return 'disallowSemicolons'; | ||
}, | ||
|
||
check: function(file, errors) { | ||
file.getTokens() | ||
.filter(function(token) { | ||
return token.type === 'Punctuator' && token.value === ';'; | ||
}) | ||
.forEach(function(token) { | ||
var nextToken = file.getNextToken(token); | ||
if (!nextToken || nextToken.loc.end.line > token.loc.end.line) { | ||
errors.add('semicolons are disallowed at the end of a line.', token.loc.end); | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var Checker = require('../../lib/checker'); | ||
var assert = require('assert'); | ||
|
||
describe('rules/disallow-semicolons', function() { | ||
var checker; | ||
|
||
beforeEach(function() { | ||
checker = new Checker(); | ||
checker.registerDefaultRules(); | ||
checker.configure({ disallowSemicolons: true }); | ||
}); | ||
|
||
it('should not allow semicolons at end of line', function() { | ||
assert(checker.checkString([ | ||
'var a = 1;', | ||
'var b = 2;', | ||
'function c() {}', | ||
'd();' | ||
].join('\n')).getErrorCount() === 3); | ||
}); | ||
|
||
it('should allow semicolons inline', function() { | ||
assert(checker.checkString([ | ||
'for (var i = 0; i < l; i++) {', | ||
'go()', | ||
'}' | ||
].join('\n')).isEmpty()); | ||
}); | ||
|
||
it('should allow semicolons at start of line', function() { | ||
assert(checker.checkString([ | ||
'var foo = function () {}', | ||
';[1, 2].forEach(foo)' | ||
].join('\n')).isEmpty()); | ||
}); | ||
}); |