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

Commit

Permalink
Merge 235b29b into 37cf0e8
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesta committed Oct 1, 2014
2 parents 37cf0e8 + 235b29b commit 4ba7997
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/rules/disallow-space-before-object-values.js 100644 → 100755
@@ -1,4 +1,5 @@
var assert = require('assert');
var tokenHelper = require('../token-helper');

module.exports = function() {};

Expand All @@ -20,13 +21,22 @@ module.exports.prototype = {
},

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]) {
var keyToken = file.getTokenByRangeStart(property.key.range[0]);
var valueToken = file.getTokenByRangeStart(property.value.range[0]);
var colon = file.findNextToken(keyToken, 'Punctuator', ':');
var tokenBeforeValue = file.getPrevToken(valueToken);

var tokenToTest;
if (tokenHelper.isTokenParenthesis(file, tokenBeforeValue.range[0], true)) {
// skip '(' if value is parenthesised
tokenToTest = tokenBeforeValue;
} else {
tokenToTest = valueToken;
}

if (colon.range[1] !== tokenToTest.range[0]) {
errors.add('Illegal space after key colon', colon.loc.end);
}
});
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/require-space-before-object-values.js 100644 → 100755
@@ -1,4 +1,5 @@
var assert = require('assert');
var tokenHelper = require('../token-helper');

module.exports = function() {};

Expand Down Expand Up @@ -26,6 +27,9 @@ module.exports.prototype = {
var value = property.value;
var valuePos = file.getTokenPosByRangeStart(value.range[0]);
var colon = tokens[valuePos - 1];
if (tokenHelper.isTokenParenthesis(file, colon.range[0], true)) {
colon = file.getPrevToken(colon);
}
if (colon.range[1] === value.range[0]) {
errors.add('Missing space after key colon', colon.loc.end);
}
Expand Down
16 changes: 16 additions & 0 deletions test/rules/disallow-space-before-object-values.js 100644 → 100755
Expand Up @@ -13,9 +13,25 @@ describe('rules/disallow-space-before-object-values', function() {
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');
assert.equal(
checker.checkString('var x = { abc:(true), z: (function() { return _z > 0; }) };').getErrorCount(),
1,
'one error is found'
);
assert.equal(
checker.checkString('var x = { abc : (true), b: ("1")};').getErrorCount(),
2,
'two errors are found'
);
});

it('should not report with no space after keys colons and parenthesised expression in property value', function() {
assert(checker.checkString('var x = { a:(1 > 2)};').isEmpty());
assert(checker.checkString('var x = { 0x7f :(y?(z ? 1: 2):(3)) };').isEmpty());
});

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

});
14 changes: 14 additions & 0 deletions test/rules/require-space-before-object-values.js 100644 → 100755
Expand Up @@ -15,6 +15,20 @@ describe('rules/require-space-before-object-values', function() {
assert.equal(checker.checkString('var x = { abc :1, b:2 };').getErrorCount(), 2, 'two errors are found');
});

it('should not report with parenthesised property value', function() {
assert(checker.checkString('var data = { key: (x > 2) };').isEmpty());
assert(checker.checkString('var video = { isFullHD: ((width > 1920) && (height > 1080)) };').isEmpty());
assert(checker.checkString('var data = { key: ( ( ( ( 2 ))))};').isEmpty());
});

it('should not report with array initializer as property value', function() {
assert(checker.checkString('var jsFiles = { src: ["*.js"] }').isEmpty());
});

it('should not report with nested objects', function() {
assert(checker.checkString('var foo = { bar: { baz: 127 } };').isEmpty());
});

it('should not report with end of line after keys colons', function() {
assert(checker.checkString(
'var x = {\n' +
Expand Down

0 comments on commit 4ba7997

Please sign in to comment.