Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix at-rule-property-required-list performance #6865

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-cows-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `at-rule-property-required-list` performance
36 changes: 24 additions & 12 deletions lib/rules/at-rule-property-required-list/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const flattenArray = require('../../utils/flattenArray');
const createMapWithSet = require('../../utils/createMapWithSet');
const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
Expand Down Expand Up @@ -30,39 +30,51 @@ const rule = (primary) => {
return;
}

const propLists = createMapWithSet(primary);

/** @type {Set<string>} */
const currentPropList = new Set();

root.walkAtRules((atRule) => {
if (!isStandardSyntaxAtRule(atRule)) {
return;
}

const { name, nodes } = atRule;
const atRuleName = name.toLowerCase();
const propList = flattenArray(primary[atRuleName]);
const propList = propLists.get(atRuleName);

if (!propList) {
return;
}

for (const property of propList) {
const propertyName = property.toLowerCase();
currentPropList.clear();

for (const node of nodes) {
if (!node || node.type !== 'decl') continue;

const propName = node.prop.toLowerCase();

if (!propList.has(propName)) continue;

const hasProperty = nodes.find(
(node) => node.type === 'decl' && node.prop.toLowerCase() === propertyName,
);
currentPropList.add(propName);
}

if (currentPropList.size === propList.size) {
romainmenke marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if (hasProperty) {
continue;
}
for (const requiredProp of propList) {
if (currentPropList.has(requiredProp)) continue;

report({
message: messages.expected,
messageArgs: [atRuleName, propertyName],
messageArgs: [atRuleName, requiredProp],
node: atRule,
word: `@${atRule.name}`,
result,
ruleName,
});
continue;
}
});
};
Expand Down
25 changes: 25 additions & 0 deletions lib/utils/createMapWithSet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const flattenArray = require('./flattenArray');

/**
* Create a map with unique sets of values from a record.
*
* @template T
* @param {Record<string, T | T[]>} record
* @returns {Map<string, Set<T>>}
*/
module.exports = function createMapWithSet(record) {
/** @type {Map<string, Set<T>>} */
const map = new Map();

for (const [key, value] of Object.entries(record)) {
const list = flattenArray(value);

if (!list) continue;

map.set(key, new Set(list));
}

return map;
};