@coffeelint/cli
Version:
Lint your CoffeeScript
63 lines (55 loc) • 2.04 kB
JavaScript
(function() {
var PreferLogicalOperator;
module.exports = PreferLogicalOperator = (function() {
class PreferLogicalOperator {
lintToken(token, tokenApi) {
var actual_token, context, first_column, last_column, line;
// Compare the actual token with the lexed token.
({first_column, last_column} = token[2]);
line = tokenApi.lines[tokenApi.lineNumber];
actual_token = line.slice(first_column, +last_column + 1 || 9e9);
if (token[0] === 'COMPOUND_ASSIGN' && (actual_token === 'or=' || actual_token === 'and=')) {
actual_token = token.origin[1];
}
context = (function() {
switch (actual_token) {
case 'is':
return 'Replace "is" with "=="';
case 'isnt':
return 'Replace "isnt" with "!="';
case 'or':
return 'Replace "or" with "||"';
case 'and':
return 'Replace "and" with "&&"';
case 'not':
return 'Replace "not" with "!"';
case 'yes':
return 'Replace "yes" with true';
case 'on':
return 'Replace "on" with true';
case 'off':
return 'Replace "off" with false';
case 'no':
return 'Replace "no" with false';
default:
return void 0;
}
})();
if (context != null) {
return {token, context};
}
}
};
PreferLogicalOperator.prototype.rule = {
type: 'style',
name: 'prefer_logical_operator',
level: 'ignore',
message: 'Don\'t use is, isnt, not, and, or, yes, on, no, off',
doubleNotLevel: 'ignore',
description: `This rule prohibits is, isnt, not, and, or, yes, on, no, off.
Use ==, !=, !, &&, ||, true, false instead.`
};
PreferLogicalOperator.prototype.tokens = ['COMPARE', 'UNARY', 'BOOL', 'COMPOUND_ASSIGN', '&&', '||'];
return PreferLogicalOperator;
}).call(this);
}).call(this);