Skip to content

Commit b221b40

Browse files
committed
Implement correct !global assignments
1 parent 52b8d86 commit b221b40

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

environment.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,29 @@ namespace Sass {
3737
bool current_frame_has(const string key) const
3838
{ return !!current_frame_.count(key); }
3939

40+
void current_frame_set(const string key, T val)
41+
{ current_frame_[key] = val; }
42+
43+
void global_frame_set(const string key, T val)
44+
{ global_frame()->current_frame_[key] = val; }
45+
4046
Environment* grandparent() const
4147
{
4248
if(parent_ && parent_->parent_) return parent_->parent_;
4349
else return 0;
4450
}
4551

52+
Environment* global_frame()
53+
{
54+
Environment* cur = this;
55+
// looks like global variables
56+
// are in the second last parent
57+
while (cur->grandparent()) {
58+
cur = cur->parent_;
59+
}
60+
return cur;
61+
}
62+
4663
bool global_frame_has(const string key) const
4764
{
4865
if(parent_ && !grandparent()) {

eval.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ namespace Sass {
6464
Expression* Eval::operator()(Assignment* a)
6565
{
6666
string var(a->variable());
67-
if (env->has(var)) {
67+
if (a->is_global()) {
68+
env->global_frame_set(var, a->value()->perform(this));
69+
}
70+
else if (env->has(var)) {
6871
Expression* v = static_cast<Expression*>((*env)[var]);
6972
if (!a->is_guarded() || v->concrete_type() == Expression::NULL_VAL) (*env)[var] = a->value()->perform(this);
7073
}
7174
else {
72-
env->current_frame()[var] = a->value()->perform(this);
75+
env->current_frame_set(var, a->value()->perform(this));
7376
}
7477
return 0;
7578
}

expand.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,15 @@ namespace Sass {
219219
{
220220
string var(a->variable());
221221
Selector* p = selector_stack.size() <= 1 ? 0 : selector_stack.back();
222-
if (env->has(var)) {
222+
if (a->is_global()) {
223+
env->global_frame_set(var, a->value()->perform(eval->with(p, env, backtrace)));
224+
}
225+
else if (env->has(var)) {
223226
Expression* v = static_cast<Expression*>((*env)[var]);
224227
if (!a->is_guarded() || v->concrete_type() == Expression::NULL_VAL) (*env)[var] = a->value()->perform(eval->with(p, env, backtrace));
225228
}
226229
else {
227-
env->current_frame()[var] = a->value()->perform(eval->with(p, env, backtrace));
230+
env->current_frame_set(var, a->value()->perform(eval->with(p, env, backtrace)));
228231
}
229232
return 0;
230233
}

functions.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ namespace Sass {
145145
static mt19937 rand(static_cast<unsigned int>(GetSeed()));
146146

147147
// features
148-
static set<string> features;
148+
static set<string> features {
149+
"global-variable-shadowing"
150+
};
149151

150152
////////////////
151153
// RGB FUNCTIONS

0 commit comments

Comments
 (0)