Skip to content

Commit

Permalink
fixed potential double hoisting edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
d4tocchini authored and paulyoung committed Jan 3, 2015
1 parent 7d32f8d commit 0e75cc2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
16 changes: 11 additions & 5 deletions lib/scoper.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mutate = function(buffer) {
};

_mutate = function(node) {
var child, hoistLevel, level, parent, unscoped, upper_unscoped, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _results;
var child, hoistLevel, hoister, level, parent, unscoped, upper_unscoped, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _results;
_ref = node._childScopes;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i];
Expand All @@ -106,10 +106,16 @@ _mutate = function(node) {
}
parent = parent._parentScope;
}
if (hoistLevel === 1) {
_results.push(unscoped.splice(1, 0, ['^']));
} else if (hoistLevel > 1) {
_results.push(unscoped.splice(1, 0, ['^', hoistLevel]));
if (hoistLevel > 0) {
if (unscoped[1][0] !== '^') {
hoister = ['^'];
if (hoistLevel > 1) {
hoister.push(hoistLevel);
}
_results.push(unscoped.splice(1, 0, hoister));
} else {
_results.push(void 0);
}
} else {
_results.push(void 0);
}
Expand Down
21 changes: 21 additions & 0 deletions spec/compiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2460,3 +2460,24 @@ describe 'CCSS-to-AST', ->
"""

#describe 'PRINT', ->
# it 'PRINT', ->
# console.log(JSON.stringify(parser.parse("""
# .wrap {
# my-size == 100;
# "target" {
# width: == &height == my-size;
# center-y: == ::window[center-y];
# center-x: == ^[center-x];
# }
# .thing {
# width: == &height == my-size;
# center: == "target"[center];
# .other {
# width: == &height == my-size / 2;
# center: == "target"[center];
# }
# }
# }
# """),1,1))
40 changes: 40 additions & 0 deletions spec/scoper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,46 @@ describe "Scoper", ->
}
"""

equivalent "3 level with virtuals",
"""
.wrap {
my-size == 100;
width: == &height == my-size;
"target" {
width: == &height == my-size;
center-y: == ::window[center-y];
center-x: == ^[center-x];
}
.thing {
width: == &height == my-size;
center: == "target"[center];
.other {
width: == &height == my-size / 2;
center: == "target"[center];
}
}
}
""",
"""
.wrap {
my-size == 100;
width: == &height == my-size;
"target" {
width: == &height == ^my-size;
center-y: == ::window[center-y];
center-x: == ^[center-x];
}
.thing {
width: == &height == ^my-size;
center: == ^"target"[center];
.other {
width: == &height == ^^my-size / 2;
center: == ^^"target"[center];
}
}
}
"""

equivalent "3 level moderate",
"""
Expand Down
9 changes: 5 additions & 4 deletions src/scoper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ _mutate = (node) =>
parent = parent._parentScope

# Hoist unscoped get commands by injecting parent scope operators, `^`
if hoistLevel is 1
unscoped.splice 1, 0, ['^']
else if hoistLevel > 1
unscoped.splice 1, 0, ['^', hoistLevel]
if hoistLevel > 0
if unscoped[1][0] isnt '^' # not already hoisted
hoister = ['^']
hoister.push(hoistLevel) if hoistLevel > 1
unscoped.splice 1, 0, hoister

0 comments on commit 0e75cc2

Please sign in to comment.