数通智联化工云平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.6 KiB

2 years ago
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = rule;
exports.ruleName = exports.meta = exports.messages = void 0;
var _stylelint = require("stylelint");
var _utils = require("../../utils");
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var ruleName = (0, _utils.namespace)("dollar-variable-no-missing-interpolation");
exports.ruleName = ruleName;
var messages = _stylelint.utils.ruleMessages(ruleName, {
rejected: function rejected(n, v) {
return "Expected variable ".concat(v, " to be interpolated when using it with ").concat(n);
}
});
exports.messages = messages;
var meta = {
url: (0, _utils.ruleUrl)(ruleName)
};
// https://developer.mozilla.org/en/docs/Web/CSS/custom-ident#Lists_of_excluded_values
exports.meta = meta;
var customIdentProps = ["animation", "animation-name", "counter-reset", "counter-increment", "list-style-type", "will-change"];
// https://developer.mozilla.org/en/docs/Web/CSS/At-rule
var customIdentAtRules = ["counter-style", "keyframes", "supports"];
function isAtRule(type) {
return type === "atrule";
}
function isCustomIdentAtRule(node) {
return isAtRule(node.type) && customIdentAtRules.includes(node.name);
}
function isCustomIdentProp(node) {
return customIdentProps.includes(node.prop);
}
function isAtSupports(node) {
return isAtRule(node.type) && node.name === "supports";
}
function isSassVar(value) {
return value[0] === "$";
}
function isStringVal(value) {
return /^(["']).*(["'])$/.test(value);
}
function toRegex(arr) {
return new RegExp("(".concat(arr.join("|"), ")"));
}
function rule(actual) {
return function (root, result) {
var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
actual: actual
});
if (!validOptions) {
return;
}
var stringVars = [];
var vars = [];
function findVars(node) {
node.walkDecls(function (decl) {
var prop = decl.prop,
value = decl.value;
if (!isSassVar(prop) || vars.includes(prop)) {
return;
}
if (isStringVal(value)) {
stringVars.push(prop);
}
vars.push(prop);
});
}
findVars(root);
root.walkRules(findVars);
if (!vars.length) {
return;
}
function shouldReport(node, value) {
if (isAtSupports(node) || isCustomIdentProp(node)) {
return stringVars.includes(value);
}
if (isCustomIdentAtRule(node)) {
return vars.includes(value);
}
return false;
}
function report(node, value) {
var name = node.name,
prop = node.prop,
type = node.type;
var nodeName = isAtRule(type) ? "@".concat(name) : prop;
_stylelint.utils.report({
ruleName: ruleName,
result: result,
node: node,
message: messages.rejected(nodeName, value)
});
}
function exitEarly(node) {
return node.type !== "word" || !node.value;
}
function walkValues(node, value) {
(0, _postcssValueParser["default"])(value).walk(function (valNode) {
var value = valNode.value;
if (exitEarly(valNode) || !shouldReport(node, value)) {
return;
}
report(node, value);
});
}
root.walkDecls(toRegex(customIdentProps), function (decl) {
walkValues(decl, decl.value);
});
root.walkAtRules(toRegex(customIdentAtRules), function (atRule) {
walkValues(atRule, atRule.params);
});
};
}
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;