stylelint-sassdoc
Version:
stylelint plugin to check scss files for a valid sassdoc documentation Credits to https://github.com/anneangersbach
71 lines (63 loc) • 2.33 kB
JavaScript
const stylelint = require('stylelint');
const {
checkCommentsForType,
isVariableDeclaration,
} = require('../../utils');
const ruleName = 'sassdoc/variable-has-type';
const messages = stylelint.utils.ruleMessages(ruleName, {
expected: (node) => `Sassdoc: Expected variable "${node.prop}" to be documented with {0 - 1} @type.`,
notAllowed: 'SassDoc: @type is only allowed on variables.',
});
module.exports = stylelint.createPlugin(ruleName, (actual) => (root, result) => {
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual,
});
if (!validOptions) {
return;
}
// walk all nodes
root.walk((node) => {
let results = [];
// declaration is not a variable
// or is nested in a code block (function, mixin, etc)
// or it is private
if (!isVariableDeclaration(node) || node.parent !== root || node.prop[1] === '_') {
return;
}
// Avoid checking nodes that are comments,
// they will be checked by all other nodes.
// If we don't, we will get a lot of duplicates.
if (node.type !== 'comment') {
results = checkCommentsForType(node);
}
if (isVariableDeclaration(node)) {
// @type is allowed, but found too many times
results.forEach((returnComment) => {
// found more than 1 @type
if (results.length > 1) {
stylelint.utils.report({
message: messages.expected(node),
node: returnComment.node,
result,
ruleName,
});
}
});
} else {
// @type is not allowed, but found
results.forEach((returnComment) => {
// found @type on element that's not allowed
if (results.length > 0) {
stylelint.utils.report({
message: messages.notAllowed,
node: returnComment.node,
result,
ruleName,
});
}
});
}
});
});
module.exports.ruleName = ruleName;
module.exports.messages = messages;