textlint-rule-no-js-function-paren
Version:
textlint rule preset for JavaScript function notation.
35 lines (33 loc) • 1 kB
JavaScript
// LICENSE : MIT
;
const functionWithParenthesisRegExp = /^(.+)\(\)$/;
const isFunctionWithParenthesis = (code) => {
return functionWithParenthesisRegExp.test(code);
};
const getFunctionName = (code) => {
const [all, name] = code.match(functionWithParenthesisRegExp);
return name;
};
const defaultOptions = {
/**
* A collection of allowed function name
*/
allow: []
};
module.exports = function(context, options) {
const {Syntax, RuleError, report, getSource} = context;
const allow = options.allow || defaultOptions.allow;
return {
[Syntax.Code](node){
const code = node.value || getSource(node);
if (!isFunctionWithParenthesis(code)) {
return;
}
const name = getFunctionName(code);
if (allow.indexOf(name) !== -1) {
return;
}
report(node, new RuleError(`\`${code}\` should be written \`${name}\`.`));
}
}
};