@coffeelint/cli
Version:
Lint your CoffeeScript
83 lines (68 loc) • 2.33 kB
JavaScript
(function() {
var NoUnnecessaryFatArrows, any;
any = function(arr, test) {
return arr.reduce((function(res, elt) {
return res || test(elt);
}), false);
};
module.exports = NoUnnecessaryFatArrows = (function() {
class NoUnnecessaryFatArrows {
constructor() {
this.isThis = this.isThis.bind(this);
this.needsFatArrow = this.needsFatArrow.bind(this);
}
lintAST(node, astApi) {
this.astApi = astApi;
this.lintNode(node);
return void 0;
}
lintNode(node) {
var error;
if ((this.isFatArrowCode(node)) && (!this.needsFatArrow(node))) {
error = this.astApi.createError({
lineNumber: node.locationData.first_line + 1,
columnNumber: node.locationData.first_column + 1
});
this.errors.push(error);
}
return node.eachChild((child) => {
return this.lintNode(child);
});
}
isCode(node) {
return this.astApi.getNodeName(node) === 'Code';
}
isFatArrowCode(node) {
return this.isCode(node) && node.bound;
}
isValue(node) {
return this.astApi.getNodeName(node) === 'Value';
}
isThis(node) {
var ref;
return ((ref = node.constructor) != null ? ref.name : void 0) === 'ThisLiteral' || this.isValue(node) && node.base.value === 'this';
}
needsFatArrow(node) {
return this.isCode(node) && (any(node.params, (param) => {
return param.contains(this.isThis) != null;
}) || (node.body.contains(this.isThis) != null) || (node.body.contains((child) => {
var ref;
if (!this.astApi.getNodeName(child)) {
return ((ref = child.constructor) != null ? ref.name : void 0) === 'SuperCall';
} else {
return this.isFatArrowCode(child) && this.needsFatArrow(child);
}
}) != null));
}
};
NoUnnecessaryFatArrows.prototype.rule = {
type: 'style',
name: 'no_unnecessary_fat_arrows',
level: 'warn',
message: 'Unnecessary fat arrow',
description: `Disallows defining functions with fat arrows when \`this\`
is not used within the function.`
};
return NoUnnecessaryFatArrows;
}).call(this);
}).call(this);