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 rule-selector-property-disallowed-list false positives for nesting selectors #7558

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ testRule({
endLine: 1,
endColumn: 27,
},
{
code: 'a { @media screen { color: red; } }',
message: messages.rejected('a', 'color'),
line: 1,
column: 21,
endLine: 1,
endColumn: 26,
},
{
code: 'a { margin-top: 0px; }',
message: messages.rejected('a', 'margin-top'),
Expand Down Expand Up @@ -89,6 +97,14 @@ testRule({
endLine: 1,
endColumn: 27,
},
{
code: 'a { b { width: 100% } color: red; }',
message: messages.rejected('a', 'color'),
line: 1,
column: 23,
endLine: 1,
endColumn: 28,
},
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
],
});

Expand All @@ -104,3 +120,49 @@ testRule({
},
],
});

testRule({
ruleName,
config: {
'/^((?!input).)*$/': 'color',
},

accept: [
{
code: '.foo input { color: red; }',
},
{
code: '.foo { & input { color: red; } }',
},
{
code: 'input .foo { color: red; }',
},
],

reject: [
{
code: '.foo form { color: red; }',
message: messages.rejected('.foo form', 'color'),
line: 1,
column: 13,
endLine: 1,
endColumn: 18,
},
{
code: '.search { & form { color: red; } }',
message: messages.rejected('& form', 'color'),
line: 1,
column: 20,
endLine: 1,
endColumn: 25,
},
{
code: 'input { & .foo { color: red; } }',
message: messages.rejected('& .foo', 'color'),
line: 1,
column: 18,
endLine: 1,
endColumn: 23,
},
],
});
33 changes: 30 additions & 3 deletions lib/rules/rule-selector-property-disallowed-list/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ const rule = (primary) => {
return;
}

ruleNode.walkDecls((decl) => {
const { prop } = decl;
ruleNode.walk((node) => {
if (node.type !== 'decl') return;

if (!declarationIsAChildOfRule(node, ruleNode)) return;

const { prop } = node;

if (matchesStringOrRegExp(prop, disallowedProperties)) {
report({
message: messages.rejected,
messageArgs: [ruleNode.selector, prop],
node: decl,
node,
result,
ruleName,
word: prop,
Expand All @@ -75,4 +79,27 @@ rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;

/**
* Check that a given declaration is a child of this rule and not a nested rule
*
* @param {import('postcss').Declaration} decl
* @param {import('postcss').Rule} ruleNode
*/
function declarationIsAChildOfRule(decl, ruleNode) {
if (decl.parent === ruleNode) return true;

/** @type {import('postcss').Document|import('postcss').Root|import('postcss').Container|undefined} */
let parent = decl.parent;

while (parent) {
if (parent === ruleNode) return true;

if (parent.type === 'rule') return false;

parent = parent.parent;
}

return false;
}

module.exports = rule;
33 changes: 30 additions & 3 deletions lib/rules/rule-selector-property-disallowed-list/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ const rule = (primary) => {
return;
}

ruleNode.walkDecls((decl) => {
const { prop } = decl;
ruleNode.walk((node) => {
if (node.type !== 'decl') return;
romainmenke marked this conversation as resolved.
Show resolved Hide resolved

if (!declarationIsAChildOfRule(node, ruleNode)) return;
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved

const { prop } = node;

if (matchesStringOrRegExp(prop, disallowedProperties)) {
report({
message: messages.rejected,
messageArgs: [ruleNode.selector, prop],
node: decl,
node,
result,
ruleName,
word: prop,
Expand All @@ -71,3 +75,26 @@ rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
export default rule;

/**
* Check that a given declaration is a child of this rule and not a nested rule
*
* @param {import('postcss').Declaration} decl
* @param {import('postcss').Rule} ruleNode
*/
function declarationIsAChildOfRule(decl, ruleNode) {
if (decl.parent === ruleNode) return true;
romainmenke marked this conversation as resolved.
Show resolved Hide resolved

/** @type {import('postcss').Document|import('postcss').Root|import('postcss').Container|undefined} */
romainmenke marked this conversation as resolved.
Show resolved Hide resolved
let parent = decl.parent;

while (parent) {
if (parent === ruleNode) return true;

if (parent.type === 'rule') return false;

parent = parent.parent;
}

return false;
}