Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when declarations are printed without ruleset #2062

Merged
merged 2 commits into from
May 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SOURCES = \
ast.cpp \
base64vlq.cpp \
bind.cpp \
check_nesting.cpp \
color_maps.cpp \
constants.cpp \
context.cpp \
Expand Down
60 changes: 60 additions & 0 deletions src/check_nesting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "sass.hpp"
#include <vector>

#include "check_nesting.hpp"
#include "context.hpp"
// #include "debugger.hpp"

namespace Sass {

CheckNesting::CheckNesting(Context& ctx)
: ctx(ctx),
parent_stack(std::vector<AST_Node*>())
{ }

AST_Node* CheckNesting::parent()
{
if (parent_stack.size() > 0)
return parent_stack.back();
return 0;
}

Statement* CheckNesting::operator()(Block* b)
{
parent_stack.push_back(b);

for (auto n : *b) {
n->perform(this);
}

parent_stack.pop_back();
return b;
}

Statement* CheckNesting::operator()(Declaration* d)
{
if (!is_valid_prop_parent(parent())) {
throw Exception::InvalidSass(d->pstate(), "Properties are only allowed "
"within rules, directives, mixin includes, or other properties.");
}
return static_cast<Statement*>(d);
}

Statement* CheckNesting::fallback_impl(AST_Node* n)
{
return static_cast<Statement*>(n);
}

bool CheckNesting::is_valid_prop_parent(AST_Node* p)
{
if (Definition* def = dynamic_cast<Definition*>(p)) {
return def->type() == Definition::MIXIN;
}

return dynamic_cast<Ruleset*>(p) ||
dynamic_cast<Keyframe_Rule*>(p) ||
dynamic_cast<Propset*>(p) ||
dynamic_cast<Directive*>(p) ||
dynamic_cast<Mixin_Call*>(p);
}
}
37 changes: 37 additions & 0 deletions src/check_nesting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef SASS_CHECK_NESTING_H
#define SASS_CHECK_NESTING_H

#include "ast.hpp"
#include "context.hpp"
#include "operation.hpp"

namespace Sass {

typedef Environment<AST_Node*> Env;

class CheckNesting : public Operation_CRTP<Statement*, CheckNesting> {

Context& ctx;
std::vector<Block*> block_stack;
std::vector<AST_Node*> parent_stack;

AST_Node* parent();

Statement* fallback_impl(AST_Node* n);

public:
CheckNesting(Context&);
~CheckNesting() { }

Statement* operator()(Block*);
Statement* operator()(Declaration*);

template <typename U>
Statement* fallback(U x) { return fallback_impl(x); }

bool is_valid_prop_parent(AST_Node*);
};

}

#endif
6 changes: 6 additions & 0 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "output.hpp"
#include "expand.hpp"
#include "eval.hpp"
#include "check_nesting.hpp"
#include "cssize.hpp"
#include "listize.hpp"
#include "extend.hpp"
Expand Down Expand Up @@ -648,8 +649,13 @@ namespace Sass {
// create crtp visitor objects
Expand expand(*this, &global, &backtrace);
Cssize cssize(*this, &backtrace);
CheckNesting check_nesting(*this);
// check nesting
root = root->perform(&check_nesting)->block();
// expand and eval the tree
root = root->perform(&expand)->block();
// check nesting
root = root->perform(&check_nesting)->block();
// merge and bubble certain rules
root = root->perform(&cssize)->block();
// should we extend something?
Expand Down
2 changes: 2 additions & 0 deletions win/libsass.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\base64vlq.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\bind.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\b64\cencode.h" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\check_nesting.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\color_maps.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\constants.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\context.hpp" />
Expand Down Expand Up @@ -71,6 +72,7 @@
<ClCompile Include="$(LIBSASS_SRC_DIR)\bind.cpp" />
<ClCompile Condition="$(VisualStudioVersion) &lt; 14.0" Include="$(LIBSASS_SRC_DIR)\c99func.c" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\cencode.c" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\check_nesting.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\color_maps.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\constants.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\context.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions win/libsass.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\bind.hpp">
<Filter>Headers</Filter>
</ClInclude>
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\check_nesting.hpp">
<Filter>Headers</Filter>
</ClInclude>
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\color_maps.hpp">
<Filter>Headers</Filter>
</ClInclude>
Expand Down Expand Up @@ -224,6 +227,9 @@
<ClCompile Include="$(LIBSASS_SRC_DIR)\cencode.c">
<Filter>Sources</Filter>
</ClCompile>
<ClCompile Include="$(LIBSASS_SRC_DIR)\check_nesting.cpp">
<Filter>Sources</Filter>
</ClCompile>
<ClCompile Include="$(LIBSASS_SRC_DIR)\color_maps.cpp">
<Filter>Sources</Filter>
</ClCompile>
Expand Down