@coffeelint/cli
Version:
Lint your CoffeeScript
88 lines (78 loc) • 2.58 kB
JavaScript
(function() {
var ColonAssignmentSpacing;
module.exports = ColonAssignmentSpacing = (function() {
class ColonAssignmentSpacing {
lintToken(token, tokenApi) {
var checkSpacing, isLeftSpaced, isRightSpaced, nextToken, previousToken, spaceRules;
spaceRules = tokenApi.config[this.rule.name].spacing;
previousToken = tokenApi.peek(-1);
nextToken = tokenApi.peek(1);
checkSpacing = function(direction) {
var minDirection, spacing;
spacing = (function() {
switch (direction) {
case 'left':
return token[2].first_column - previousToken[2].last_column - 1;
case 'right':
return nextToken[2].first_column - token[2].first_column - 1;
}
})();
// when spacing is negative, the neighboring token is a newline
if (spacing < 0) {
return true;
} else {
minDirection = parseInt(spaceRules['min_' + direction], 10);
// if a minimal spacing is specified, only check that
if (minDirection >= 0) {
return spacing >= minDirection;
} else {
// otherwise check exact spacing
return spacing === parseInt(spaceRules[direction], 10);
}
}
};
isLeftSpaced = checkSpacing('left');
isRightSpaced = checkSpacing('right');
if (token.jsxColon || isLeftSpaced && isRightSpaced) {
return null;
} else {
return {
token: token,
context: `Incorrect spacing around column ${token[2].first_column}`
};
}
}
};
ColonAssignmentSpacing.prototype.rule = {
type: 'style',
name: 'colon_assignment_spacing',
level: 'ignore',
message: 'Colon assignment without proper spacing',
spacing: {
left: 0,
right: 0
},
description: `<p>This rule checks to see that there is spacing before and
after the colon in a colon assignment (i.e., classes, objects).
The spacing amount is specified by
spacing.left and spacing.right, respectively.
A zero value means no spacing required.
</p>
<pre><code>
#
# If spacing.left and spacing.right is 1
#
# Doesn't throw an error
object = {spacing : true}
class Dog
canBark : true
# Throws an error
object = {spacing: true}
class Cat
canBark: false
</code></pre>`
};
ColonAssignmentSpacing.prototype.tokens = [':'];
return ColonAssignmentSpacing;
}).call(this);
}).call(this);