Skip to content

Commit 6d6fab6

Browse files
committed
Error when declarations are printed without ruleset
This is introduces a vastly reduced `CheckNesting` visitor from Ruby Sass as discussed in #2061. Doing this properly will require some small changes to the parser, and probably eval, which currently try to do some nesting checking of their own. This is just first step to properly introducing the proper nesting checking. Closes #2061 Fixes #1732
1 parent 52628d0 commit 6d6fab6

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
append_block(b);
26+
parent_stack.pop_back();
27+
return b;
28+
}
29+
30+
Statement* CheckNesting::operator()(Declaration* d)
31+
{
32+
if (!is_valid_prop_parent(parent())) {
33+
throw Exception::InvalidSass(d->pstate(), "Properties are only allowed "
34+
"within rules, directives, mixin includes, or other properties.");
35+
}
36+
return static_cast<Statement*>(d);
37+
}
38+
39+
Statement* CheckNesting::fallback_impl(AST_Node* n)
40+
{
41+
return static_cast<Statement*>(n);
42+
}
43+
44+
bool CheckNesting::is_valid_prop_parent(AST_Node* p)
45+
{
46+
if (Definition* def = dynamic_cast<Definition*>(p)) {
47+
return def->type() == Definition::MIXIN;
48+
}
49+
50+
return dynamic_cast<Ruleset*>(p) ||
51+
dynamic_cast<Keyframe_Rule*>(p) ||
52+
dynamic_cast<Propset*>(p) ||
53+
dynamic_cast<Directive*>(p) ||
54+
dynamic_cast<Mixin_Call*>(p);
55+
}
56+
57+
void CheckNesting::append_block(Block* b)
58+
{
59+
for (size_t i = 0, L = b->length(); i < L; ++i) {
60+
Statement* ith = (*b)[i]->perform(this);
61+
if (ith) {
62+
(*b)[i] = ith;
63+
}
64+
}
65+
}
66+
}

src/check_nesting.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
void append_block(Block*);
34+
};
35+
36+
}
37+
38+
#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)