Skip to content

Commit

Permalink
fix false negatives
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke committed Jun 3, 2023
1 parent b9647c9 commit 16886da
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions lib/rules/unit-disallowed-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,17 @@ const rule = (primary, secondaryOptions) => {
const primaryValues = [primary].flat();

/**
* Ignore wrong units within `url` function
* Ignore units within function that match `ignoreFunctions` option
*
* @param {import('@csstools/css-parser-algorithms').ComponentValue} componentValue
* @returns {boolean}
* @param {import('@csstools/css-parser-algorithms').FunctionNode} functionNode
* @param {{ignored: boolean}} state
* @returns {void}
*/
function componentValueIsIgnored(componentValue) {
if (!isFunctionNode(componentValue)) {
return false;
}

const name = componentValue.getName().toLowerCase();
function setWalkerState(functionNode, state) {
const name = functionNode.getName().toLowerCase();

return name === 'url' || optionsMatches(secondaryOptions, 'ignoreFunctions', name);
// Ignore wrong units within `url` function
// Ignore units within function that match `ignoreFunctions` option
state.ignored =
name.toLowerCase() === 'url' || optionsMatches(secondaryOptions, 'ignoreFunctions', name);
}

/**
Expand Down Expand Up @@ -129,19 +126,21 @@ const rule = (primary, secondaryOptions) => {

if (isMediaFeature(entry.node)) {
entry.state.mediaFeatureName = entry.node.getName().toLowerCase();

return;
}

if (!entry.state.mediaFeatureName) return;

if (!isTokenNode(entry.node)) return;

check(
atRule,
atRuleParamIndex,
entry.node,
entry.state.mediaFeatureName,
secondaryOptions?.ignoreMediaFeatureNames,
);
if (isTokenNode(entry.node)) {
check(
atRule,
atRuleParamIndex,
entry.node,
entry.state.mediaFeatureName,
secondaryOptions?.ignoreMediaFeatureNames,
);
}
}, state);
});
});
Expand All @@ -165,13 +164,21 @@ const rule = (primary, secondaryOptions) => {
return;
}

if (!isFunctionNode(componentValue) && !isSimpleBlockNode(componentValue)) return;
const state = {
ignored: false,
};

componentValue.walk(
(entry) => {
if (!entry.state) return;
if (isFunctionNode(componentValue)) {
setWalkerState(componentValue, state);
} else if (!isSimpleBlockNode(componentValue)) {
return;
}

componentValue.walk((entry) => {
if (!entry.state) return;

if (isTokenNode(entry.node) && !entry.state.ignored) {
if (isTokenNode(entry.node)) {
if (!entry.state.ignored) {
check(
decl,
declarationValueIndex,
Expand All @@ -181,12 +188,13 @@ const rule = (primary, secondaryOptions) => {
);
}

entry.state.ignored = componentValueIsIgnored(entry.node);
},
{
ignored: componentValueIsIgnored(componentValue),
},
);
return;
}

if (isFunctionNode(entry.node)) {
setWalkerState(entry.node, entry.state);
}
}, state);
});
});
};
Expand Down

0 comments on commit 16886da

Please sign in to comment.