Skip to content

Commit

Permalink
Adds analysis for empty rulesets
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman committed Jul 22, 2018
1 parent c29b3f6 commit ed7b196
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ analyze('foo {}')
// unique: []
// },
// rules: {
// total: 1
// total: 1,
// empty: {
// total: 1
// }
// },
// selectors: {
// accessibility: {
Expand Down
10 changes: 9 additions & 1 deletion src/analyzer/rules/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const isEmpty = rule => {
return rule.declarationsCount === 0
}

module.exports = rules => {
const all = rules
const empty = rules.filter(isEmpty)

return {
total: all.length
total: all.length,
empty: {
total: empty.length
}
}
}
5 changes: 3 additions & 2 deletions src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ function processNodes(tree) {
module.exports = css => {
return new Promise(async (resolve, reject) => {
try {
const result = await postcss().process(css, {from: undefined})
resolve(processNodes(result.root))
const result = await postcss.parse(css)
const rootNode = result.toResult().root
resolve(processNodes(rootNode))
} catch (err) {
const {source, line, column, reason} = err
reject(new SyntaxError(
Expand Down
5 changes: 4 additions & 1 deletion test/analyzer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ test('Returns the correct analysis object structure', async t => {
unique: []
},
rules: {
total: 1
total: 1,
empty: {
total: 1
}
},
selectors: {
accessibility: {
Expand Down
14 changes: 14 additions & 0 deletions test/analyzer/rules/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@
}
}
}

/**
* Empty rule and at-rule
*/
.empty {}
.empty-multiline{


}
@media screen {}

@media (min-width: 1px) {
.empty-inside-mq {}
}
5 changes: 4 additions & 1 deletion test/analyzer/rules/output.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"total": 2
"total": 5,
"empty": {
"total": 3
}
}

0 comments on commit ed7b196

Please sign in to comment.