Skip to content

Commit 0bbcc53

Browse files
committed
Merge pull request #88 from DanPurdy/feature/mixin-bug-fix
Update mixin-before-declaration rule fix #80
2 parents 072319f + b411333 commit 0bbcc53

File tree

3 files changed

+71
-25
lines changed

3 files changed

+71
-25
lines changed

lib/rules/mixins-before-declarations.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,34 @@ module.exports = {
1414
var result = [],
1515
error;
1616

17-
ast.traverseByType('block', function (block) {
18-
var lastDeclaration = null;
19-
block.traverse(function (item, j) {
20-
if (item.type === 'include') {
21-
if (j > lastDeclaration && lastDeclaration !== null) {
22-
item.forEach('simpleSelector', function (name) {
23-
if (parser.options.exclude.indexOf(name.content[0].content) === -1) {
24-
error = {
25-
'ruleId': parser.rule.name,
26-
'line': item.start.line,
27-
'column': item.start.column,
28-
'message': 'Mixins should come before declarations',
29-
'severity': parser.severity
30-
};
31-
result = helpers.addUnique(result, error);
32-
}
33-
});
34-
}
17+
ast.traverseByType('include', function (node, i, parent) {
18+
var depth = 0,
19+
declarationCount = [depth];
20+
21+
parent.traverse( function (item) {
22+
if (item.type === 'ruleset') {
23+
depth++;
24+
declarationCount[depth] = 0;
25+
}
26+
else if (item.type === 'declaration') {
27+
declarationCount[depth]++;
3528
}
36-
if (item.type === 'declaration') {
37-
lastDeclaration = j;
29+
else if (item.type === 'include') {
30+
item.forEach('simpleSelector', function (name) {
31+
if (parser.options.exclude.indexOf(name.content[0].content) === -1 && declarationCount[depth] > 0) {
32+
error = {
33+
'ruleId': parser.rule.name,
34+
'line': item.start.line,
35+
'column': item.start.column,
36+
'message': 'Mixins should come before declarations',
37+
'severity': parser.severity
38+
};
39+
result = helpers.addUnique(result, error);
40+
}
41+
});
3842
}
3943
});
40-
lastDeclaration = null;
4144
});
42-
4345
return result;
4446
}
4547
};

tests/main.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,35 @@ describe('rule', function () {
194194
});
195195

196196
//////////////////////////////
197-
// Mixins Before DEclarations
197+
// Mixins Before Declarations
198198
//////////////////////////////
199199
it('mixins before declarations', function (done) {
200200
lintFile('mixins-before-declarations.scss', function (data) {
201-
assert.equal(4, data.warningCount);
201+
assert.equal(5, data.warningCount);
202+
done();
203+
});
204+
});
205+
206+
//////////////////////////////
207+
// Mixins Before Declarations - overwrite
208+
//////////////////////////////
209+
it('mixins before declarations - excludes', function (done) {
210+
lintFile('mixins-before-declarations.scss', {
211+
'rules': {
212+
'mixins-before-declarations': [
213+
1,
214+
{
215+
'exclude': [
216+
'test-again',
217+
'waldo',
218+
'mq',
219+
'breakpoint'
220+
]
221+
}
222+
]
223+
}
224+
}, function (data) {
225+
assert.equal(0, data.warningCount);
202226
done();
203227
});
204228
});

tests/sass/mixins-before-declarations.scss

+21-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,25 @@
2525
content: 'where';
2626

2727
@include waldo;
28+
29+
}
30+
31+
&__element {
32+
@include element;
33+
width: 100%;
34+
35+
@include mq(500px) {
36+
content: 'mq';
37+
}
38+
39+
@include waldo;
40+
41+
&--modifier {
42+
@include foo('yo');
43+
@include hello;
44+
@include test;
45+
height: 100px;
46+
@include test-again;
47+
}
2848
}
29-
}
49+
}

0 commit comments

Comments
 (0)