Skip to content
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/light-keys-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `declaration-property-value-no-unknown` performance
27 changes: 25 additions & 2 deletions lib/rules/declaration-property-value-no-unknown/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const SYNTAX_DESCRIPTOR = /^syntax$/i;

const UNSUPPORTED_FUNCTIONS_IN_CSSTREE = new Set(['clamp', 'min', 'max', 'env']);

/** @type {WeakMap<object, Set<string>>} */
const validMatchesCacheByLexer = new WeakMap();
const MAX_CACHE_SIZE = 10000;

/** @import { CssNode } from 'css-tree' */
/** @import { ParseError } from '@csstools/css-calc' */

Expand Down Expand Up @@ -126,6 +130,13 @@ const rule = (primary, secondaryOptions) => {
})
: result.stylelint.lexer;

let validMatchesCache = validMatchesCacheByLexer.get(lexer);

if (!validMatchesCache) {
validMatchesCache = new Set();
validMatchesCacheByLexer.set(lexer, validMatchesCache);
}

root.walkDecls((decl) => {
const { prop } = decl;
const value = getDeclarationValue(decl);
Expand All @@ -151,11 +162,19 @@ const rule = (primary, secondaryOptions) => {
// skipping parsing by returning early until this is resolved upstream.
if (/\bvar\s*\(/i.test(value)) return;

const cacheKey = isCustomProperty(prop) ? null : `${prop}:${value}`;
Comment thread
jeddy3 marked this conversation as resolved.

if (cacheKey && validMatchesCache.has(cacheKey)) return;

// Check if value contains math functions that need validation
const [mathFuncResult, mathFuncResultStartOffset, mathFuncResultEndOffset] =
validateMathFunctions(value, prop, lexer, typedCustomPropertyNames);

if (mathFuncResult === 'valid' || mathFuncResult === 'skip-validation') return;
if (mathFuncResult === 'valid' || mathFuncResult === 'skip-validation') {
if (cacheKey && validMatchesCache.size < MAX_CACHE_SIZE) validMatchesCache.add(cacheKey);

return;
}

if (mathFuncResult === 'invalid') {
const valueIndex = declarationValueIndex(decl);
Expand Down Expand Up @@ -217,7 +236,11 @@ const rule = (primary, secondaryOptions) => {
cssTreeValueNode,
);

if (!error) return;
if (!error) {
if (cacheKey && validMatchesCache.size < MAX_CACHE_SIZE) validMatchesCache.add(cacheKey);

return;
}

if (!('mismatchLength' in error)) return;

Expand Down
Loading