Skip to content

Commit d8fb898

Browse files
committed
Merge pull request #2062 from xzyfer/fix/issue-1732
Error when declarations are printed without ruleset
2 parents 52628d0 + c75f24d commit d8fb898

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

Makefile.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ SOURCES = \
44
ast.cpp \
55
base64vlq.cpp \
66
bind.cpp \
7+
check_nesting.cpp \
78
color_maps.cpp \
89
constants.cpp \
910
context.cpp \

src/check_nesting.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "sass.hpp"
2+
#include <vector>
3+
4+
#include "check_nesting.hpp"
5+
#include "context.hpp"
6+
// #include "debugger.hpp"
7+
8+
namespace Sass {
9+
10+
CheckNesting::CheckNesting(Context& ctx)
11+
: ctx(ctx),
12+
parent_stack(std::vector<AST_Node*>())
13+
{ }
14+
15+
AST_Node* CheckNesting::parent()
16+
{
17+
if (parent_stack.size() > 0)
18+
return parent_stack.back();
19+
return 0;
20+
}
21+
22+
Statement* CheckNesting::operator()(Block* b)
23+
{
24+
parent_stack.push_back(b);
25+
26+
for (auto n : *b) {
27+
n->perform(this);
28+
}
29+
30+
parent_stack.pop_back();
31+
return b;
32+
}
33+
34+
Statement* CheckNesting::operator()(Declaration* d)
35+
{
36+
if (!is_valid_prop_parent(parent())) {
37+
throw Exception::InvalidSass(d->pstate(), "Properties are only allowed "
38+
"within rules, directives, mixin includes, or other properties.");
39+
}
40+
return static_cast<Statement*>(d);
41+
}
42+
43+
Statement* CheckNesting::fallback_impl(AST_Node* n)
44+
{
45+
return static_cast<Statement*>(n);
46+
}
47+
48+
bool CheckNesting::is_valid_prop_parent(AST_Node* p)
49+
{
50+
if (Definition* def = dynamic_cast<Definition*>(p)) {
51+
return def->type() == Definition::MIXIN;
52+
}
53+
54+
return dynamic_cast<Ruleset*>(p) ||
55+
dynamic_cast<Keyframe_Rule*>(p) ||
56+
dynamic_cast<Propset*>(p) ||
57+
dynamic_cast<Directive*>(p) ||
58+
dynamic_cast<Mixin_Call*>(p);
59+
}
60+
}

src/check_nesting.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef SASS_CHECK_NESTING_H
2+
#define SASS_CHECK_NESTING_H
3+
4+
#include "ast.hpp"
5+
#include "context.hpp"
6+
#include "operation.hpp"
7+
8+
namespace Sass {
9+
10+
typedef Environment<AST_Node*> Env;
11+
12+
class CheckNesting : public Operation_CRTP<Statement*, CheckNesting> {
13+
14+
Context& ctx;
15+
std::vector<Block*> block_stack;
16+
std::vector<AST_Node*> parent_stack;
17+
18+
AST_Node* parent();
19+
20+
Statement* fallback_impl(AST_Node* n);
21+
22+
public:
23+
CheckNesting(Context&);
24+
~CheckNesting() { }
25+
26+
Statement* operator()(Block*);
27+
Statement* operator()(Declaration*);
28+
29+
template <typename U>
30+
Statement* fallback(U x) { return fallback_impl(x); }
31+
32+
bool is_valid_prop_parent(AST_Node*);
33+
};
34+
35+
}
36+
37+
#endif

src/context.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "output.hpp"
1919
#include "expand.hpp"
2020
#include "eval.hpp"
21+
#include "check_nesting.hpp"
2122
#include "cssize.hpp"
2223
#include "listize.hpp"
2324
#include "extend.hpp"
@@ -648,8 +649,13 @@ namespace Sass {
648649
// create crtp visitor objects
649650
Expand expand(*this, &global, &backtrace);
650651
Cssize cssize(*this, &backtrace);
652+
CheckNesting check_nesting(*this);
653+
// check nesting
654+
root = root->perform(&check_nesting)->block();
651655
// expand and eval the tree
652656
root = root->perform(&expand)->block();
657+
// check nesting
658+
root = root->perform(&check_nesting)->block();
653659
// merge and bubble certain rules
654660
root = root->perform(&cssize)->block();
655661
// should we extend something?

win/libsass.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\base64vlq.hpp" />
1919
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\bind.hpp" />
2020
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\b64\cencode.h" />
21+
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\check_nesting.hpp" />
2122
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\color_maps.hpp" />
2223
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\constants.hpp" />
2324
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\context.hpp" />
@@ -71,6 +72,7 @@
7172
<ClCompile Include="$(LIBSASS_SRC_DIR)\bind.cpp" />
7273
<ClCompile Condition="$(VisualStudioVersion) &lt; 14.0" Include="$(LIBSASS_SRC_DIR)\c99func.c" />
7374
<ClCompile Include="$(LIBSASS_SRC_DIR)\cencode.c" />
75+
<ClCompile Include="$(LIBSASS_SRC_DIR)\check_nesting.cpp" />
7476
<ClCompile Include="$(LIBSASS_SRC_DIR)\color_maps.cpp" />
7577
<ClCompile Include="$(LIBSASS_SRC_DIR)\constants.cpp" />
7678
<ClCompile Include="$(LIBSASS_SRC_DIR)\context.cpp" />

win/libsass.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\bind.hpp">
6767
<Filter>Headers</Filter>
6868
</ClInclude>
69+
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\check_nesting.hpp">
70+
<Filter>Headers</Filter>
71+
</ClInclude>
6972
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\color_maps.hpp">
7073
<Filter>Headers</Filter>
7174
</ClInclude>
@@ -224,6 +227,9 @@
224227
<ClCompile Include="$(LIBSASS_SRC_DIR)\cencode.c">
225228
<Filter>Sources</Filter>
226229
</ClCompile>
230+
<ClCompile Include="$(LIBSASS_SRC_DIR)\check_nesting.cpp">
231+
<Filter>Sources</Filter>
232+
</ClCompile>
227233
<ClCompile Include="$(LIBSASS_SRC_DIR)\color_maps.cpp">
228234
<Filter>Sources</Filter>
229235
</ClCompile>

0 commit comments

Comments
 (0)