stylelint-sassdoc
Version:
stylelint plugin to check scss files for a valid sassdoc documentation Credits to https://github.com/anneangersbach
86 lines (77 loc) • 3.57 kB
JavaScript
const stylelint = require('stylelint');
const { checkCommentsForParam, regExCheck } = require('../../utils');
const ruleName = 'sassdoc/atParameter';
const messages = stylelint.utils.ruleMessages(ruleName, {
wrongParamCount: (results, count) => `SassDoc: Documented number of parameters {${results.length}} doesn't match actual parameter count {${count}}.`,
expectedType: (returnComment) => `SassDoc: Expected {${returnComment.node.text}} to have a {type} definition.`,
expectedName: (returnComment) => `SassDoc: Expected {${returnComment.node.text}} to have a $name definition.`,
});
const declWithParams = /function|mixin/;
module.exports = stylelint.createPlugin(ruleName, (actual) => (root, result) => {
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual,
});
if (!validOptions) {
return;
}
root.walk((node) => {
let results = [];
// checking for mixin & function
if (regExCheck(node.name, declWithParams)) {
// node is nested in a code block (function, mixin, etc)
// or it is private (params is the name of the mixin)
if (node.parent !== root || node.params[0] === '_') {
return;
}
// capture the parameters of the node
const nodeParameters = node.params.match(/(\(.+\))/);
// if there are parameters, check them
if (nodeParameters) {
// check how many params we have
const parameterCount = nodeParameters[0].split(',').length;
// counting @param occurences in preceeding comments
// and doing regex checks for {type} and $name
results = checkCommentsForParam(node);
if (results.length < parameterCount) {
stylelint.utils.report({
message: messages.wrongParamCount(results, parameterCount),
node,
result,
ruleName,
});
}
results.forEach((returnComment) => {
// number of documented params doesn't match params in mixin/function
if (results.length !== parameterCount) {
stylelint.utils.report({
message: messages.wrongParamCount(results, parameterCount),
node: returnComment.node,
result,
ruleName,
});
}
// check if param has a {type declaration}
if (!returnComment.hasType) {
stylelint.utils.report({
message: messages.expectedType(returnComment),
node: returnComment.node,
result,
ruleName,
});
}
// check if param has a $name declaration
if (!returnComment.hasName) {
stylelint.utils.report({
message: messages.expectedName(returnComment),
node: returnComment.node,
result,
ruleName,
});
}
});
}
}
});
});
module.exports.ruleName = ruleName;
module.exports.messages = messages;