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

Commit

Permalink
Merge ca27224 into 9f2ec59
Browse files Browse the repository at this point in the history
  • Loading branch information
vtintillier committed Sep 8, 2014
2 parents 9f2ec59 + ca27224 commit 2efdf3d
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Expand Up @@ -1394,6 +1394,52 @@ var x = {a : 1};
var x = {a: 1};
```

### disallowSpaceAfterObjectKeysColons

Disallows space after object keys.

Type: `Boolean`

Values: `true`

#### Example

```js
"disallowSpaceAfterObjectKeysColons": true
```

##### Valid
```js
var x = {a:1};
```
##### Invalid
```js
var x = {a: 1};
```

### requireSpaceAfterObjectKeysColons

Requires space after object keys.

Type: `Boolean`

Values: `true`

#### Example

```js
"requireSpaceAfterObjectKeysColons": true
```

##### Valid
```js
var x = {a: 1};
```
##### Invalid
```js
var x = {a:1};
```

### disallowCommaBeforeLineBreak

Disallows commas as last token on a line in lists.
Expand Down
36 changes: 36 additions & 0 deletions lib/rules/disallow-space-after-object-keys-colons.js
@@ -0,0 +1,36 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(disallow) {
assert(
typeof disallow === 'boolean',
this.getOptionName() + ' option requires boolean value'
);
assert(
disallow === true,
this.getOptionName() + ' option requires true value or should be removed'
);
},

getOptionName: function() {
return 'disallowSpaceAfterObjectKeysColons';
},

check: function(file, errors) {
var tokens = file.getTokens();
file.iterateNodesByType('ObjectExpression', function(node) {
node.properties.forEach(function(property) {
var value = property.value;
var valuePos = file.getTokenPosByRangeStart(value.range[0]);
var colon = tokens[valuePos - 1];
if (colon.range[1] !== value.range[0]) {
errors.add('Illegal space after key colon', colon.loc.end);
}
});
});
}

};
36 changes: 36 additions & 0 deletions lib/rules/require-space-after-object-keys-colons.js
@@ -0,0 +1,36 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(disallow) {
assert(
typeof disallow === 'boolean',
this.getOptionName() + ' option requires boolean value'
);
assert(
disallow === true,
this.getOptionName() + ' option requires true value or should be removed'
);
},

getOptionName: function() {
return 'requireSpaceAfterObjectKeysColons';
},

check: function(file, errors) {
var tokens = file.getTokens();
file.iterateNodesByType('ObjectExpression', function(node) {
node.properties.forEach(function(property) {
var value = property.value;
var valuePos = file.getTokenPosByRangeStart(value.range[0]);
var colon = tokens[valuePos - 1];
if (colon.range[1] === value.range[0]) {
errors.add('Missing space after key colon', colon.loc.end);
}
});
});
}

};
2 changes: 2 additions & 0 deletions lib/string-checker.js
Expand Up @@ -71,7 +71,9 @@ StringChecker.prototype = {
this.registerRule(new (require('./rules/disallow-spaces-inside-parentheses'))());
this.registerRule(new (require('./rules/require-blocks-on-newline'))());
this.registerRule(new (require('./rules/require-space-after-object-keys'))());
this.registerRule(new (require('./rules/require-space-after-object-keys-colons'))());
this.registerRule(new (require('./rules/disallow-space-after-object-keys'))());
this.registerRule(new (require('./rules/disallow-space-after-object-keys-colons'))());
this.registerRule(new (require('./rules/disallow-quoted-keys-in-objects'))());
this.registerRule(new (require('./rules/disallow-dangling-underscores'))());
this.registerRule(new (require('./rules/require-aligned-object-values'))());
Expand Down
21 changes: 21 additions & 0 deletions test/rules/disallow-space-after-object-keys-colons.js
@@ -0,0 +1,21 @@
var Checker = require('../../lib/checker');
var assert = require('assert');

describe('rules/disallow-space-after-object-keys-colons', function() {
var checker;

beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
checker.configure({ disallowSpaceAfterObjectKeysColons: true });
});

it('should report with space after keys colons', function() {
assert.equal(checker.checkString('var x = { a:1, b: 2 };').getErrorCount(), 1, 'one error is found');
assert.equal(checker.checkString('var x = { abc : 1, b: 2 };').getErrorCount(), 2, 'two errors are found');
});

it('should not report with no space after keys colons', function() {
assert(checker.checkString('var x = { a:1, bcd :2 };').isEmpty());
});
});
30 changes: 30 additions & 0 deletions test/rules/require-space-after-object-keys-colons.js
@@ -0,0 +1,30 @@
var Checker = require('../../lib/checker');
var assert = require('assert');

describe('rules/require-space-after-object-keys-colons', function() {
var checker;

beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
checker.configure({ requireSpaceAfterObjectKeysColons: true });
});

it('should report with no space after keys colons', function() {
assert.equal(checker.checkString('var x = { a:1, b: 2 };').getErrorCount(), 1, 'one error is found');
assert.equal(checker.checkString('var x = { abc :1, b:2 };').getErrorCount(), 2, 'two errors are found');
});

it('should not report with end of line after keys colons', function() {
assert(checker.checkString(
'var x = {\n' +
' a:\n' +
' 2\n' +
'}'
).isEmpty());
});

it('should not report with space after keys colons', function() {
assert(checker.checkString('var x = { a: 1, bcd: 2 };').isEmpty());
});
});

0 comments on commit 2efdf3d

Please sign in to comment.