-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #866 from onishiweb/feature/declarations-before-ne…
…sting Add rule: declarations before nesting
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |