@coffeelint/cli
Version:
Lint your CoffeeScript
69 lines (60 loc) • 2.44 kB
JavaScript
(function() {
var CyclomaticComplexity;
module.exports = CyclomaticComplexity = (function() {
class CyclomaticComplexity {
// returns the "complexity" value of the current node.
getComplexity(node) {
var complexity, name, ref;
name = this.astApi.getNodeName(node);
complexity = name === 'If' || name === 'While' || name === 'For' || name === 'Try' ? 1 : name === 'Op' && ((ref = node.operator) === '&&' || ref === '||') ? 1 : name === 'Switch' ? node.cases.length : 0;
return complexity;
}
lintAST(node, astApi) {
this.astApi = astApi;
this.lintNode(node);
return void 0;
}
// Lint the AST node and return its cyclomatic complexity.
lintNode(node) {
var complexity, error, name, ref, rule;
// Get the complexity of the current node.
name = (ref = this.astApi) != null ? ref.getNodeName(node) : void 0;
complexity = this.getComplexity(node);
// Add the complexity of all child's nodes to this one.
node.eachChild((childNode) => {
var childComplexity, ref1;
childComplexity = this.lintNode(childNode);
if (((ref1 = this.astApi) != null ? ref1.getNodeName(childNode) : void 0) !== 'Code') {
return complexity += childComplexity;
}
});
rule = this.astApi.config[this.rule.name];
// If the current node is a function, and it's over our limit, add an
// error to the list.
if (name === 'Code' && complexity >= rule.value) {
error = this.astApi.createError({
context: complexity + 1,
lineNumber: node.locationData.first_line + 1,
lineNumberEnd: node.locationData.last_line + 1,
columnNumber: node.locationData.first_column + 1,
columnNumberEnd: node.locationData.last_column + 1
});
if (error) {
this.errors.push(error);
}
}
// Return the complexity for the benefit of parent nodes.
return complexity;
}
};
CyclomaticComplexity.prototype.rule = {
type: 'problem',
name: 'cyclomatic_complexity',
level: 'ignore',
message: 'The cyclomatic complexity is too damn high',
value: 10,
description: `Examine the complexity of your function.`
};
return CyclomaticComplexity;
}).call(this);
}).call(this);