UNPKG

stylelint-sassdoc

Version:

stylelint plugin to check scss files for a valid sassdoc documentation Credits to https://github.com/anneangersbach

65 lines (59 loc) 2.32 kB
const stylelint = require('stylelint'); const checkCommentsForName = require('../../utils/checkCommentsForName'); const isVariableDeclaration = require('../../utils/isVariableDeclaration'); const selectorIncludesPlaceholder = require('../../utils/selectorIncludesPlaceholder'); const ruleName = 'sassdoc/atName'; const messages = stylelint.utils.ruleMessages(ruleName, { expected: 'SassDoc: Expected a maximum of one @name.', notAllowed: 'SassDoc: @name is only allowed on: functions, mixins, placeholders, variables.', }); const nodeNames = ['function', 'mixin']; 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 = []; // 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 = checkCommentsForName(node); } if (isVariableDeclaration(node) || selectorIncludesPlaceholder(node) || nodeNames.includes(node.name)) { // @name is allowed, but found too many times results.forEach((returnComment) => { // found more than 1 @name if (results.length > 1) { stylelint.utils.report({ message: messages.expected, node: returnComment.node, result, ruleName, }); } }); } else { // @name is not allowed, but found results.forEach((returnComment) => { // found @name 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;