From cb8a048e58f20bd453abc2397122dc1bc9899787 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Fri, 28 Feb 2020 15:48:36 -0800 Subject: [PATCH] Refactor Extender._extendOrReplace() (#965) This combines all targets into a single extender invocation, which is more efficient and allows it to more aggressively do redundant selector elimination. --- CHANGELOG.md | 5 +++++ lib/src/extend/extender.dart | 33 ++++++++++++++++++--------------- pubspec.yaml | 2 +- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d30986f7..ddac713a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.26.2 + +* More aggressively eliminate redundant selectors in the `selector.extend()` and + `selector.replace()` functions. + ## 1.26.1 ### Command Line Interface diff --git a/lib/src/extend/extender.dart b/lib/src/extend/extender.dart index 8956055c..be90ba97 100644 --- a/lib/src/extend/extender.dart +++ b/lib/src/extend/extender.dart @@ -90,25 +90,28 @@ class Extender { /// A helper function for [extend] and [replace]. static SelectorList _extendOrReplace(SelectorList selector, SelectorList source, SelectorList targets, ExtendMode mode) { - var extenders = Map.fromIterable( - source.components, - value: (complex) => Extension.oneOff(complex as ComplexSelector)); - for (var complex in targets.components) { - if (complex.components.length != 1) { - throw SassScriptException("Can't extend complex selector $complex."); - } + var extenders = { + for (var complex in source.components) complex: Extension.oneOff(complex) + }; - var compound = complex.components.first as CompoundSelector; - var extensions = { + var compoundTargets = [ + for (var complex in targets.components) + if (complex.components.length != 1) + throw SassScriptException("Can't extend complex selector $complex.") + else + complex.components.first as CompoundSelector + ]; + + var extensions = { + for (var compound in compoundTargets) for (var simple in compound.components) simple: extenders - }; + }; - var extender = Extender._mode(mode); - if (!selector.isInvisible) { - extender._originals.addAll(selector.components); - } - selector = extender._extendList(selector, extensions, null); + var extender = Extender._mode(mode); + if (!selector.isInvisible) { + extender._originals.addAll(selector.components); } + selector = extender._extendList(selector, extensions, null); return selector; } diff --git a/pubspec.yaml b/pubspec.yaml index 754240fa..dfc90747 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: sass -version: 1.26.1 +version: 1.26.2 description: A Sass implementation in Dart. author: Sass Team homepage: https://github.com/sass/dart-sass