textlint-rule-no-js-function-paren
Version:
textlint rule preset for JavaScript function notation.
36 lines (34 loc) • 1.05 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 }\`.`));
}
};
};
//# sourceMappingURL=no-js-function-paren.js.map