This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
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
Showing
6 changed files
with
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
}; |
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 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); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
}; |
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,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()); | ||
}); | ||
}); |
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 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()); | ||
}); | ||
}); |