Skip to content

Commit

Permalink
🔍 Add rule: declarations before nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
onishiweb committed Sep 6, 2016
1 parent 718e090 commit 6aa9544
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/rules/declarations-before-nesting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Declarations Before Nesting

Rule `declarations-before-nesting` will enforce that declarations should be written before nesting in a ruleset.

## Examples

When enabled, the following are allowed:

```scss
.foo {
content: 'baz';

.bar {
content: 'qux';
}
}
```

When enabled, the following are disallowed:

```scss
.foo {
.bar {
content: 'qux';
}

content: 'baz';
}
```
47 changes: 47 additions & 0 deletions lib/rules/declarations-before-nesting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

var helpers = require('../helpers');

module.exports = {
'name': 'declarations-before-nesting',
'defaults': {},
'detect': function (ast, parser) {
var result = [],
error;

ast.traverseByType('block', function (block) {
if (block.contains('ruleset') && block.contains('declaration')) {
var rulesetIndex;

block.forEach(function (item, j) {
var declarationIndex;
var declaration;

if (item.is('ruleset') && rulesetIndex === void 0) {
rulesetIndex = j;
}

if (item.is('declaration')) {
declarationIndex = j;
declaration = item;
}

if (rulesetIndex < declarationIndex && declaration) {
error = {
'ruleId': parser.rule.name,
'line': declaration.start.line,
'column': declaration.start.column,
'message': 'declarations should come before nestings',
'severity': parser.severity
};
result = helpers.addUnique(result, error);
}
});

rulesetIndex = null;
}
});

return result;
}
};
36 changes: 36 additions & 0 deletions tests/rules/declarations-before-nesting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var lint = require('./_lint');

//////////////////////////////
// SCSS syntax tests
//////////////////////////////
describe('declarations before nesting - scss', function () {
var file = lint.file('declarations-before-nesting.scss');

it('enforce', function (done) {
lint.test(file, {
'declarations-before-nesting': 1
}, function (data) {
console.log(data);
lint.assert.equal(4, data.warningCount);
done();
});
});
});

//////////////////////////////
// Sass syntax tests
//////////////////////////////
describe('declarations before nesting - sass', function () {
var file = lint.file('declarations-before-nesting.sass');

it('enforce', function (done) {
lint.test(file, {
'declarations-before-nesting': 1
}, function (data) {
lint.assert.equal(4, data.warningCount);
done();
});
});
});
27 changes: 27 additions & 0 deletions tests/sass/declarations-before-nesting.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.bar
content: 'baz'

.qux
content: 'baz'

.foo
.bar
content: 'where'

content: 'baz'

.baz
content: 'where'

content: 'baz'

.foo
.bar
content: 'where'

.baz
content: 'quz'

content: 'baz'

content: 'baz'
35 changes: 35 additions & 0 deletions tests/sass/declarations-before-nesting.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.bar {
content: 'baz';

.qux {
content: 'baz';
}
}

.foo {
.bar {
content: 'where';
}

content: 'baz';

.baz {
content: 'where';
}

content: 'baz';
}

.foo {
.bar {
content: 'where';

.baz {
content: 'quz';
}

content: 'baz';
}

content: 'baz';
}

0 comments on commit 6aa9544

Please sign in to comment.