coffeelint-use-strict
Version:
A CoffeeLint rule that enforces usage of strict mode
89 lines (80 loc) • 2.81 kB
JavaScript
// Generated by CoffeeScript 1.10.0
;
var UseStrictRule, isUseStrictValue, useStrictAtTopOf;
isUseStrictValue = function(node) {
return (node.constructor.name === 'Value') && node.isString() && (node.base.value === '"use strict"' || node.base.value === "'use strict'");
};
useStrictAtTopOf = function(node) {
var firstChild;
firstChild = null;
if (node.isEmpty()) {
return {
boolean: true
};
}
node.eachChild(function(child) {
firstChild = child;
return false;
});
return {
boolean: isUseStrictValue(firstChild),
firstChild: firstChild
};
};
UseStrictRule = (function() {
function UseStrictRule() {}
UseStrictRule.prototype.rule = {
name: 'use_strict',
level: 'error',
message: 'Missing \'use strict\' statement',
allowGlobal: true,
requireGlobal: false,
description: "<p>This rule enforces the ussage of strict mode. By setting\nrequireGlobal to true, you can also require a file to\nhave a global 'use strict' statement at the top. And, by setting\nallowGlobal to true (or false) you can allow\n(or disallow) 'use strict' statements at the top of a file</p>\n<pre><code>\n#\n# If requireGlobal is false\n#\n\n# Good\ngoodfunc = (please) ->\n 'use strict'\n allright\n\n# Bad\nbadfunc = (idonot) ->\n obey\n</code></pre>"
};
UseStrictRule.prototype.lintAST = function(node, astApi) {
var allowGlobal, createError, firstChild, ref, requireGlobal, useStrict;
createError = astApi.createError;
ref = astApi.config[this.rule.name], requireGlobal = ref.requireGlobal, allowGlobal = ref.allowGlobal;
firstChild = null;
if (node.isEmpty()) {
return void 0;
}
useStrict = useStrictAtTopOf(node);
if (useStrict.boolean === true) {
if (allowGlobal === false) {
this.errors.push(createError({
lineNumber: useStrict.firstChild.locationData.first_line + 1,
message: '\'use strict\' at top of file'
}));
} else {
allowGlobal === true;
return void 0;
}
} else {
if (requireGlobal === true) {
this.errors.push(createError({
lineNumber: useStrict.firstChild.locationData.first_line + 1
}));
return void 0;
}
}
node.traverseChildren(false, (function(_this) {
return function(child) {
var useStr;
useStr = null;
if (child.constructor.name === 'Code') {
useStr = useStrictAtTopOf(child.body);
if (!useStr.boolean) {
_this.errors.push(createError({
lineNumber: useStr.firstChild.locationData.first_line + 1
}));
}
}
return true;
};
})(this));
return void 0;
};
return UseStrictRule;
})();
module.exports = UseStrictRule;