Finally fixed @extend within a loop context

Added a test case so it doesn't break again, since I have been banging my head against this one for at least a week now. Fixes #965 which details most of the issue.