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

Conversation

srawlins
Copy link
Contributor

@srawlins srawlins commented May 27, 2020

Which issue, if any, is this issue related to?

Fixes #4683
Closes #4795

Is there anything in the PR that needs further explanation?

This code had a pretty big bug in it; it used .includes on a selector string, not a list, so it was just a substring match; the new code certainly takes more time, with more parsing, but is correct. Open to suggestions about how to do improve performance (perhaps cache parsed lists?).

@mattxwang
Copy link
Member

mattxwang commented May 28, 2020

Just a note, looks like this collides with #4795, which has a more narrow approach to resolve the issue. Looks like these changes supersede #4795 though! Not super familiar with the problem but can review if needed.

@jeddy3
Copy link
Member

jeddy3 commented May 31, 2020

@srawlins Thanks for looking into this (and discovering the big bug behind this).

Open to suggestions about how to do improve performance (perhaps cache parsed lists?).

Let's try caching the parsed lists.

You can use npm run benchmark-rule -- no-duplicate-selectors "[true, {\"disallowInList\": true}]" to benchmark the rule.

At the moment master is:

jeddy3@jeddy3 stylelint % npm run benchmark-rule -- no-duplicate-selectors "[true, {\"disallowInList\": true}]"

> stylelint@13.5.0 benchmark-rule /Users/jeddy3/Projects/stylelint
> node scripts/benchmark-rule.js "no-duplicate-selectors" "[true, {\"disallowInList\": true}]"

Warnings: 78
Mean: 91.46247921052631 ms
Deviation: 28.897587464632007 ms

And this branch is:

jeddy3@jeddy3 stylelint % npm run benchmark-rule -- no-duplicate-selectors "[true, {\"disallowInList\": true}]" 

> stylelint@13.5.0 benchmark-rule /Users/jeddy3/Projects/stylelint
> node scripts/benchmark-rule.js "no-duplicate-selectors" "[true, {\"disallowInList\": true}]"

Warnings: 37
Mean: 10107.0616478 ms
Deviation: 508.2475623037317 ms

We don't need to make this branch comparable to master as correctness is better than speed, but it's worth seeing if caching will take a chunk out of this branch's numbers.

@jeddy3 jeddy3 changed the title no-duplicate-selectors: Fix disallowInList and substrings Fix false positives for universal selector and disallowInList in no-duplicate-selectors May 31, 2020
@srawlins
Copy link
Contributor Author

srawlins commented Jun 1, 2020

Let's try caching the parsed lists.

Thanks so much for the benchmarking tips! I read the code further and discovered what nodeContextLookup does and how the rule loop uses one giant tree anyhow, so I re-used that for a more proper fix. I also added another test that was failing before this refactor.

Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. The benchmarks are now comparable.

While reviewing this pull request, @m-allanson and I had to refamiliarise ourselves with nodeContextLookup as it'd be a long while since either of us had used it. I'm glad you were able to make use of it to fix the bug without impacting the performance of the rule.

# By Mike Allanson (6) and others
# Via GitHub
* master:
  Bump @types/lodash from 4.14.152 to 4.14.154 (#4817)
  Update CHANGELOG.md
  Fix with workaround a TypeError thrown for "html" (#4797)
  Update CHANGELOG.md
  Fix false positives for variables in font-family-no-missing-generic-family-keyword (#4806)
  Update CHANGELOG.md
  Add ignoreSelectors option to block-opening-brace-space-before (#4640)
  Update CHANGELOG.md
  Fix error message percentage/number precision for alpha-value-notation (#4802)
  Create new 'createPartialStylelintResult' module (#4815)
  Move function normalizeAllRuleSettings() out to a separate module (#4810)
Copy link
Member

@m-allanson m-allanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM 👍

(I fixed a merge conflict from a recent dep update)

@jeddy3 jeddy3 merged commit 502184d into master Jun 3, 2020
@jeddy3 jeddy3 deleted the fix-4683 branch June 3, 2020 12:24
@jeddy3
Copy link
Member

jeddy3 commented Jun 3, 2020

  • Fixed: no-duplicate-selectors false positives for universal selector and disallowInList (#4809).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Fix false positives for universal selector and disallowInList in no-duplicate-selectors
4 participants