Skip to content

Commit

Permalink
Improve and polish ported extender code
Browse files Browse the repository at this point in the history
- Added some more documentation from dart sass
- Use references where possible to avoid copies
- Move extend check to extender for less API surface
  • Loading branch information
mgreter committed Jun 15, 2019
1 parent 0de2276 commit 0ba6876
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 228 deletions.
2 changes: 2 additions & 0 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ namespace Sass {
void clear() { return elements_.clear(); }
T last() const { return elements_.back(); }
T first() const { return elements_.front(); }
T& last() { return elements_.back(); }
T& first() { return elements_.front(); }

bool operator== (const Vectorized<T>& rhs) const {
// Abort early if sizes do not match
Expand Down
18 changes: 7 additions & 11 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,22 +654,18 @@ namespace Sass {
}
// expand and eval the tree
root = expand(root);

Extension unsatisfied;
// check that all extends were used
if (extender.checkForUnsatisfiedExtends(unsatisfied)) {
throw Exception::UnsatisfiedExtend(traces, unsatisfied);
}

// check nesting
check_nesting(root);
// merge and bubble certain rules
root = cssize(root);

ExtSmplSelSet originals = extender.getSimpleSelectors();
for (auto target : extender.extensions) {
SimpleSelector* key = target.first;
ExtSelExtMapEntry& val = target.second;
if (originals.find(key) == originals.end()) {
const Extension& extension = val.front().second;
if (extension.isOptional) continue;
throw Exception::UnsatisfiedExtend(traces, extension);
}
}

// clean up by removing empty placeholders
// ToDo: maybe we can do this somewhere else?
Remove_Placeholders remove_placeholders;
Expand Down
28 changes: 17 additions & 11 deletions src/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,30 @@ namespace Sass {
return selector_stack;
}

SelectorListObj Expand::selector()
SelectorListObj& Expand::selector()
{
if (selector_stack.size() > 0) {
auto asd = selector_stack.back();
if (asd.isNull()) return {};
return asd;
auto& sel = selector_stack.back();
if (sel.isNull()) return sel;
return sel;
}
return {};
// Avoid the need to return copies
// We always want an empty first item
selector_stack.push_back({});
return selector_stack.back();;
}

SelectorListObj Expand::original()
SelectorListObj& Expand::original()
{
if (originalStack.size() > 0) {
auto asd = originalStack.back();
if (asd.isNull()) return {};
return asd;
}
return {};
auto& sel = originalStack.back();
if (sel.isNull()) return sel;
return sel;
}
// Avoid the need to return copies
// We always want an empty first item
originalStack.push_back({});
return originalStack.back();
}

SelectorListObj Expand::popFromSelectorStack()
Expand Down
4 changes: 2 additions & 2 deletions src/expand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ namespace Sass {
public:

Env* environment();
SelectorListObj selector();
SelectorListObj original();
SelectorListObj& selector();
SelectorListObj& original();
SelectorListObj popFromSelectorStack();
SelectorStack getSelectorStack();
void pushToSelectorStack(SelectorListObj selector);
Expand Down
Loading

0 comments on commit 0ba6876

Please sign in to comment.