Skip to content

Commit

Permalink
fixes #295 - multiple variable scoping bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
robocoder committed Sep 6, 2015
1 parent f88ecf1 commit 73c403d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
17 changes: 16 additions & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1272,13 +1272,16 @@ protected function compileChild($child, $out)

if (isset($content)) {
$content->scope = $callingScope;

$this->setRaw(self::$namespaces['special'] . 'content', $content);
}

if (isset($mixin->args)) {
$this->applyArguments($mixin->args, $argValues);
}

$this->env->marker = 'mixin';

foreach ($mixin->children as $child) {
$this->compileChild($child, $out);
}
Expand Down Expand Up @@ -2433,11 +2436,18 @@ protected function setExisting($name, $value, $env = null)

$storeEnv = $env;

$hasNamespace = $name[0] === '^' || $name[0] === '@' || $name[0] === '%';

for (;;) {
if (array_key_exists($name, $env->store)) {
break;
}

if (! $hasNamespace && isset($env->marker)) {
$env = $storeEnv;
break;
}

if (! isset($env->parent)) {
$env = $storeEnv;
break;
Expand Down Expand Up @@ -2491,6 +2501,11 @@ public function get($name, $shouldThrow = true, $env = null)
return $env->store[$name];
}

if (! $hasNamespace && isset($env->marker)) {
$env = $this->rootEnv;
continue;
}

if (! isset($env->parent)) {
break;
}
Expand All @@ -2499,7 +2514,7 @@ public function get($name, $shouldThrow = true, $env = null)
}

if ($shouldThrow) {
$this->throwError('undefined variable $' . $name);
$this->throwError("Undefined variable \$$name");
}

// found nothing
Expand Down
26 changes: 26 additions & 0 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ public function provideScss()
,
'expecting color'
),
array(<<<'END_OF_SCSS'
BODY {
DIV {
$bg: red;
}
background: $bg;
}
END_OF_SCSS
,
'Undefined variable $bg'
),
array(<<<'END_OF_SCSS'
@mixin example {
background: $bg;
}
P {
$bg: red;
@include example;
}
END_OF_SCSS
,
'Undefined variable $bg'
),
);
}

Expand Down
17 changes: 0 additions & 17 deletions tests/FailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,6 @@ public function provideFailing()
transform: scale(0.5); }
to {
transform: scale(1); } }
END_OF_EXPECTED
),
array(
'#295 - variable scope bug', <<<'END_OF_SCSS'
@mixin example {
$color: red;
}
A {
$color: green;
@include example;
color: $color;
}
END_OF_SCSS
, <<<END_OF_EXPECTED
A {
color: green; }
END_OF_EXPECTED
),
/*************************************************************
Expand Down
10 changes: 10 additions & 0 deletions tests/inputs/variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ body {
#sidebar {
width: $width;
}

@mixin example {
$color: red;
}

A {
$color: green;
@include example;
color: $color;
}
3 changes: 3 additions & 0 deletions tests/outputs/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ body {

#sidebar {
width: 5em; }

A {
color: green; }
3 changes: 3 additions & 0 deletions tests/outputs_numbered/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ body {
/* line 63, inputs/variables.scss */
#sidebar {
width: 5em; }
/* line 71, inputs/variables.scss */
A {
color: green; }

0 comments on commit 73c403d

Please sign in to comment.