npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stylelint-selector-max-depth

v0.1.5

Published

A stylelint custom rule to limit selector depth

Downloads

6

Readme

stylelint-selector-max-depth

NPM version Build Status

DEPRECATED. Use stylelint's selector-max-compound-selectors standard rule instead.

A stylelint custom rule for limiting CSS selectors depth.

Installation and usage

npm install stylelint-selector-max-depth

In your stylelint config add "stylelint-selector-max-depth" to the plugins array, and "selector-max-depth": N to the rules, specifying a max selector depth as the primary option, like so:

{
  "plugins": [
    "stylelint-selector-max-depth"
  ],
  "rules": {
    // ...
    // Max selector depth of 2
    "selector-max-depth": 1,
    // ...
  },
};

Details

This rule allows you to limit depth for CSS selectors. To put it simply, selector depth in terms of this rule is how many levels of HTML structure (not necessarily direct descendants) the selector is reflecting.

.foo .bar[data-val] > .baz + .boom .lorem {
/* ^        ^         \__________/    ^
   |        |              ^          |
  Lv1      Lv2            Lv3        Lv4  -- these are depth levels */

Only the child (h1 > a) and descendant (h1 a) combinators are considered to create a new depth level; adjacent combinators (p + p, .foo ~ bar) don't.

Rules with many levels of depths in selectors can be very brittle and hard to maintain because:

  • To override such rules we have to either repeat the whole selector (with all the N levels) or add !important to the overriding declarations.
  • If such rules are to be overridden by the rules, that should be defined before them, those rules have to have greater specificity, which requires either !important or adding more selectors.
  • A change in HTML structure could require rewriting all those rule mentioned above.

You can read more about selectors depth is SMACSS book. Although note, that this rule is not about the actual number of HTML levels, since it is usually hard to say how many elements wrap the a in body a without looking in the markup.

Why another rule?

At the moment stylelint - a great CSS linter - has no way to control selector depth without hurting something else. It has rules to control Sass nesting; to disallow certain simple selectors (type selectors, IDs) and even to limit a selector's specificity - but not the depth.

Consider the case when the requirements are: no more than 3 levels of simple selectors; and no limit to selector type.

.class1 .class2 .class3 { ... }      // 1. We want this to work.
.class1 article .class3 { ... }      // 2. And this.
#element h3 { ... }                  // 3. And even this.

.menu .item div a { ... }            // 4. But not this.
.menu li div a span span { ... }     // 5. And not this.
ul li ul li div a span span { ... }  // 6. No way we would want this!

We can't force it with selector-max-specificity: 0,3,0, which is necessary for selector 2 to comply, will cut selector 3. Selector 6, on the other hand, has specificity of just 8 for any classname selector not to comply and as much as 8 to allow eight (!) type selectors in a row.

Neither we can force this with max-nesting-depth, since it only controls Sass nesting and the selectors can be written without using nesting, e.g:

.foo {
  .bar { /* nesting level 1 */
    .buzz .lightyear #to .the .rescue { /* nesting level 2 */
      ...
    }
  }
}