Skip to content

Recursive rulesets and extending rulesets #2211

Closed

Description

@matthew-dean

I've just started playing with rulesets, and I had this construction.

//core.less
#library {
  .nav() {
    @list: {
      display: block;
      other: property;
    };
  }
}

I wanted to try overriding the detached ruleset. So far, so good.

//overrides.less
#library {
  .nav() {
    @list: {
      display: inline;
    }
  }
}

Works awesome! BUT... then I thought, "Hmm, what if someone wanted to keep the existing ruleset and just extend it." I knew I couldn't use extend because there was no selector, so I tried this (even though I figured it wouldn't work).

//overrides.less
#library {
  .nav() {
    @list: {
     @list();
      display: inline;
    }
  }
}

At that point, the compiler locked up and was unrecoverable.

Now, granted, I wrote what looks like an obvious recursion loop. Clearly my fault. That said, I'm wondering if there's a way to detect / escape / throw an error when this occurs?

(Follow up ?'s: is there a way to 1. extend a ruleset, 2. reference a ruleset via a namespace like: #library > .nav() > @list(); rather than needing to write #library > .nav() { @list(); }. I didn't see this in the docs.)

Activity

  1. seven-phases-max commented on Dec 12, 2014

    @seven-phases-max
    Member

    Speaking of "override" vs. "cascade" stuff - I'd say to use DR for the first (it's a variable and as any plain variable it overrides) and mixin for the second (mixins cascade).
    As for the recursion stuff - I actually would argue if it's a bug. While unconditional selfrecursion probably may be detected and stopped earlier (like it's done for mixins vs. rulesets of the same name - though personally I consider that trick quite artificial too) I don't see any reason why @list inside second definition should be resolved to the first definition (IMHO this would directly violate scope and lazy-loading rules).

    In other words:

    • self = self; is not a declarative statement.
    • and function self() {self();} is always an infinite loop in either (declarative or imperative) approach.
  2. matthew-dean commented on Apr 19, 2015

    @matthew-dean
    MemberAuthor

    As a follow up to this, this is how I solved extending vs. overriding.

    //core.less
    #library {
      .nav() {
        @list: {
          display: block;
          other: property;
        };
      }
    }
    // override
    #library {
      .nav() {
        @list: {
          override: ruleset;
        };
      }
    }
    // extend
    #library {
      .nav(list) {
        extend: value;
      }
    }
    // mixins.less
    #library {
        .nav(@item) {
            #library.nav();
            @rules: @@item;
            @rules();
        }
    }
    // rendering
    ul {
      #library.nav(list);
    }
    
  3. matthew-dean commented on Apr 19, 2015

    @matthew-dean
    MemberAuthor

    Even though I found a solution, I still think this should be left open, because the compiler should detect unrecoverable loops and throw an error, not lock up.

  4. seven-phases-max commented on Apr 19, 2015

    @seven-phases-max
    Member

    That's quite neat pattern.
    (Before DRs I used something like this, but the method there is obviously limited to a single override).

  5. matthew-dean commented on Apr 20, 2015

    @matthew-dean
    MemberAuthor

    Thanks, it took me about 30 iterations to come up with that pattern. I'm building it into an OOLESS library, so it will have a production use at some point.

  6. seven-phases-max commented on Jun 8, 2017

    @seven-phases-max
    Member

    Closing as expected behaviour.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions