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 false positives for universal selector and disallowInList in no-duplicate-selectors #4809

Merged
merged 6 commits into from Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/rules/no-duplicate-selectors/__tests__/index.js
Expand Up @@ -120,7 +120,7 @@ testRule({
},
{
code: 'a, b {} b, a {}',
description: 'essentially duplicate selector lists',
description: 'duplicate selector lists with different order',
warnings: [
{
message: messages.rejected('b, a', 1),
Expand All @@ -129,6 +129,17 @@ testRule({
},
],
},
{
code: 'a b {}\na b {}',
description: 'duplicate selectors with multiple components',
warnings: [
{
message: messages.rejected('a b', 1),
line: 2,
column: 1,
},
],
},
{
code: '.foo a, b\t> .bar,\n#baz {}\n #baz,\n\n .foo a,b>.bar {}',
description: 'essentially duplicate selector lists with varied spacing',
Expand Down Expand Up @@ -196,6 +207,18 @@ testRule({
code: 'input, button {}; textarea {}',
description: 'no duplicate within a grouping selector',
},
{
code: '*::a {}; a {}',
description: 'no duplicate when one selector is substring of another',
},
{
code: '*::a, b {}; a {}',
description: 'no duplicate when one selector is substring of another within a list',
},
{
code: 'a b, c {}; a {}',
description: 'no duplicate when one selector is component of another within a list',
},
],

reject: [
Expand All @@ -208,6 +231,15 @@ testRule({
},
],
},
{
code: 'textarea {}; input, textarea {}',
description: 'duplicate within a grouping selector, reversed',
warnings: [
{
message: messages.rejected('input, textarea', 1),
},
],
},
{
code: 'input, div {};\n textarea, section {};\n textarea {}',
description: 'duplicate within a grouping selector. multiline',
Expand Down
61 changes: 35 additions & 26 deletions lib/rules/no-duplicate-selectors/index.js
Expand Up @@ -7,6 +7,7 @@ const findAtRuleContext = require('../../utils/findAtRuleContext');
const isKeyframeRule = require('../../utils/isKeyframeRule');
const nodeContextLookup = require('../../utils/nodeContextLookup');
const normalizeSelector = require('normalize-selector');
const parseSelector = require('../../utils/parseSelector');
const report = require('../../utils/report');
const resolvedNestedSelector = require('postcss-resolve-nested-selector');
const ruleMessages = require('../../utils/ruleMessages');
Expand Down Expand Up @@ -38,6 +39,8 @@ function rule(actual, options) {
return;
}

const shouldDisallowDuplicateInList = _.get(options, 'disallowInList');

// The top level of this map will be rule sources.
// Each source maps to another map, which maps rule parents to a set of selectors.
// This ensures that selectors are only checked against selectors
Expand All @@ -55,37 +58,35 @@ function rule(actual, options) {
}, []);

const normalizedSelectorList = resolvedSelectors.map(normalizeSelector);
const selectorLine = rule.source.start.line;

// Complain if the same selector list occurs twice

// Sort the selectors list so that the order of the constituents
// doesn't matter
const sortedSelectorList = normalizedSelectorList.slice().sort().join(',');

const checkPreviousDuplicationPosition = (
selectorList,
{ shouldDisallowDuplicateInList },
) => {
let duplicationPosition = null;

if (shouldDisallowDuplicateInList) {
// iterate throw Map for checking, was used this selector in a group selector
contextSelectorSet.forEach((selectorLine, selector) => {
if (selector.includes(selectorList)) {
duplicationPosition = selectorLine;
}
});
} else {
duplicationPosition = contextSelectorSet.get(selectorList);
}
const selectorLine = rule.source.start.line;

// Complain if the same selector list occurs twice

return duplicationPosition;
};
let previousDuplicatePosition;
// When `disallowInList` is true, we must parse `sortedSelectorList` into
// list items.
let selectorListParsed = [];

const previousDuplicatePosition = checkPreviousDuplicationPosition(sortedSelectorList, {
shouldDisallowDuplicateInList: _.get(options, 'disallowInList'),
});
if (shouldDisallowDuplicateInList) {
parseSelector(sortedSelectorList, result, rule, (selectors) => {
selectors.each((s) => {
const selector = String(s);

selectorListParsed.push(selector);

if (contextSelectorSet.get(selector)) {
previousDuplicatePosition = contextSelectorSet.get(selector);
}
});
});
} else {
previousDuplicatePosition = contextSelectorSet.get(sortedSelectorList);
}

if (previousDuplicatePosition) {
// If the selector isn't nested we can use its raw value; otherwise,
Expand All @@ -104,7 +105,7 @@ function rule(actual, options) {
const presentedSelectors = new Set();
const reportedSelectors = new Set();

// Or complain if one selector list contains the same selector more than one
// Or complain if one selector list contains the same selector more than once
rule.selectors.forEach((selector) => {
const normalized = normalizeSelector(selector);

Expand All @@ -125,7 +126,15 @@ function rule(actual, options) {
}
});

contextSelectorSet.set(sortedSelectorList, selectorLine);
if (shouldDisallowDuplicateInList) {
for (let selector of selectorListParsed) {
// [selectorLine] will not really be accurate for multi-line
// selectors, such as "bar" in "foo,\nbar {}".
contextSelectorSet.set(selector, selectorLine);
}
} else {
contextSelectorSet.set(sortedSelectorList, selectorLine);
}
});
};
}
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -166,7 +166,7 @@
"@types/debug": "^4.1.5",
"@types/global-modules": "^2.0.0",
"@types/globjoin": "^0.1.0",
"@types/lodash": "^4.14.152",
"@types/lodash": "^4.14.151",
"@types/micromatch": "^4.0.1",
"benchmark": "^2.1.4",
"common-tags": "^1.8.0",
Expand Down