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

Commit

Permalink
New Rule: disallowSemicolons
Browse files Browse the repository at this point in the history
Only allows semicolons at the beginning of a line

Fixes gh-712
Closes gh-732
  • Loading branch information
christophercliff authored and mikesherov committed Dec 8, 2014
1 parent 00aaf18 commit ae69d9d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3667,6 +3667,34 @@ Invalid:
*/
```

### disallowSemicolons

Disallows lines from ending in a semicolon.

Type: `Boolean`

Value: `true`

#### Example

```js
"disallowSemicolons": true
```

##### Valid

```js
var a = 1
;[b].forEach(c)
```

##### Invalid

```js
var a = 1;
[b].forEach(c);
```

### ~~validateJSDoc~~

Please use the [JSCS-JSDoc](https://github.com/jscs-dev/jscs-jsdoc) plugin instead.
Expand Down
2 changes: 2 additions & 0 deletions lib/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ Configuration.prototype.registerDefaultRules = function() {
this.registerRule(require('../rules/disallow-capitalized-comments'));

this.registerRule(require('../rules/require-line-break-after-variable-assignment'));

this.registerRule(require('../rules/disallow-semicolons'));
};

/**
Expand Down
30 changes: 30 additions & 0 deletions lib/rules/disallow-semicolons.js
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);
}
});
}
};
36 changes: 36 additions & 0 deletions test/rules/disallow-semicolons.js
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());
});
});

0 comments on commit ae69d9d

Please sign in to comment.