From 88888fd0f8c30f7cb2e59c36bb3de0b83b93b7b9 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Sun, 16 Aug 2020 14:30:57 +0200 Subject: [PATCH] switch to a `do {} while ()` We alreayd know that we have an `@apply` otherwise we would not have called that function in the first place. Moving to a `do {} while ()` allows us to skip 1 call to `hasAtRule(css, 'apply')`. Which is nice because that skips a possible full traversal. --- src/flagged/applyComplexClasses.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/flagged/applyComplexClasses.js b/src/flagged/applyComplexClasses.js index 62487a3f3..aec9065a1 100644 --- a/src/flagged/applyComplexClasses.js +++ b/src/flagged/applyComplexClasses.js @@ -173,7 +173,7 @@ function makeExtractUtilityRules(css, config) { function processApplyAtRules(css, lookupTree, config) { const extractUtilityRules = makeExtractUtilityRules(lookupTree, config) - while (hasAtRule(css, 'apply')) { + do { css.walkRules(rule => { const applyRules = [] @@ -232,7 +232,18 @@ function processApplyAtRules(css, lookupTree, config) { rule.remove() } }) - } + + // We already know that we have at least 1 @apply rule. Otherwise this + // function would not have been called. Therefore we can execute this code + // at least once. This also means that in the best case scenario we only + // call this 2 times, instead of 3 times. + // 1st time -> before we call this function + // 2nd time -> when we check if we have to do this loop again (because do {} while (check)) + // .. instead of + // 1st time -> before we call this function + // 2nd time -> when we check the first time (because while (check) do {}) + // 3rd time -> when we re-check to see if we should do this loop again + } while (hasAtRule(css, 'apply')) return css }