UNPKG

stylelint-scss

Version:

A collection of SCSS-specific rules for Stylelint

91 lines (75 loc) 2.31 kB
import valueParser from "postcss-value-parser"; import stylelint from "stylelint"; import declarationValueIndex from "../../utils/declarationValueIndex.js"; import isNativeCssFunction from "../../utils/isNativeCssFunction.js"; import namespace from "../../utils/namespace.js"; import ruleUrl from "../../utils/ruleUrl.js"; const { utils } = stylelint; const ruleName = namespace("function-unquote-no-unquoted-strings-inside"); const messages = utils.ruleMessages(ruleName, { rejected: "Unquote function used with an already-unquoted string" }); const meta = { url: ruleUrl(ruleName), fixable: true }; function rule(primary) { return (root, result) => { const validOptions = utils.validateOptions(result, ruleName, { actual: primary }); if (!validOptions) { return; } // Setup variable naming. const vars = {}; root.walkDecls(decl => { if (decl.prop[0] !== "$") { return; } valueParser(decl.value).walk(node => { vars[decl.prop] = node.type; }); }); root.walkDecls(decl => { valueParser(decl.value).walk(node => { // Verify that we're only looking at functions. if ( node.type !== "function" || isNativeCssFunction(node.value) || node.value === "" ) { return; } // Verify we're only looking at unquote() calls. if (node.value !== "unquote") { return; } // Report error if first character is a quote. // postcss-value-parser represents quoted strings as type 'string' (as opposed to word) if ( (!node.nodes[0].quote && node.nodes[0].value[0] !== "$") || vars[node.nodes[0].value] === "word" ) { const fix = () => { decl.value = decl.value.replace(/unquote\((.*)\)/, "$1"); }; const index = declarationValueIndex(decl) + node.sourceIndex; utils.report({ message: messages.rejected, node: decl, index, endIndex: index + node.value.length, result, ruleName, fix }); } }); }); }; } rule.ruleName = ruleName; rule.messages = messages; rule.meta = meta; export default rule;