@ec0lint/plugin-css
Version:
ec0lint plugin that provides rules to verify CSS definition objects
80 lines (79 loc) • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const css_utils_1 = require("../utils/css-utils");
const resource_1 = require("../utils/resource");
exports.default = (0, utils_1.createRule)("no-shorthand-property-overrides", {
meta: {
docs: {
description: "disallow shorthand properties that override related longhand properties",
category: "Possible Errors",
recommended: true,
stylelint: "declaration-block-no-shorthand-property-overrides",
},
schema: [],
messages: {
unexpected: "Unexpected shorthand '{{shorthand}}' after '{{original}}'.",
},
type: "problem",
},
create(context) {
function createVisitor(_cssContext) {
let scopeStack = { upper: null, properties: [] };
function verifyProperties(properties) {
if (properties.length <= 1) {
return;
}
const declarations = new Map();
for (const property of properties) {
const name = property.getName();
if (!name) {
continue;
}
const propName = (0, css_utils_1.normalizePropertyName)(name.name, {
keepVendorPrefix: true,
});
declarations.set(propName, name.name);
const normalized = (0, css_utils_1.stripVendorPrefix)(propName);
const prefix = (0, css_utils_1.getVendorPrefix)(propName);
const longhandProps = resource_1.SHORTHAND_PROPERTIES.get(normalized);
if (!longhandProps) {
continue;
}
for (const longhandProp of longhandProps) {
const original = declarations.get(prefix + longhandProp);
if (original == null) {
continue;
}
context.report({
node: name.expression,
messageId: "unexpected",
data: {
shorthand: name.name,
original,
},
});
}
}
}
return {
onRule() {
scopeStack = { upper: scopeStack, properties: [] };
},
onProperty(property) {
scopeStack.properties.push(property);
},
"onRule:exit"() {
verifyProperties(scopeStack.properties);
scopeStack = (scopeStack === null || scopeStack === void 0 ? void 0 : scopeStack.upper) || scopeStack;
},
"onRoot:exit"() {
verifyProperties(scopeStack.properties);
},
};
}
return (0, utils_1.defineCSSVisitor)(context, {
createVisitor,
});
},
});