Skip to content

Commit effc01c

Browse files
authored
Adds analysis for empty rulesets (#48)
1 parent c29b3f6 commit effc01c

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ analyze('foo {}')
9696
// unique: []
9797
// },
9898
// rules: {
99-
// total: 1
99+
// total: 1,
100+
// empty: {
101+
// total: 1
102+
// }
100103
// },
101104
// selectors: {
102105
// accessibility: {

src/analyzer/rules/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
const isEmpty = rule => {
2+
return rule.declarationsCount === 0
3+
}
4+
15
module.exports = rules => {
26
const all = rules
7+
const empty = rules.filter(isEmpty)
38

49
return {
5-
total: all.length
10+
total: all.length,
11+
empty: {
12+
total: empty.length
13+
}
614
}
715
}

src/parser/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ function processNodes(tree) {
1616
module.exports = css => {
1717
return new Promise(async (resolve, reject) => {
1818
try {
19-
const result = await postcss().process(css, {from: undefined})
20-
resolve(processNodes(result.root))
19+
const result = await postcss.parse(css)
20+
const rootNode = result.toResult().root
21+
resolve(processNodes(rootNode))
2122
} catch (err) {
2223
const {source, line, column, reason} = err
2324
reject(new SyntaxError(

test/analyzer/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ test('Returns the correct analysis object structure', async t => {
7979
unique: []
8080
},
8181
rules: {
82-
total: 1
82+
total: 1,
83+
empty: {
84+
total: 1
85+
}
8386
},
8487
selectors: {
8588
accessibility: {

test/analyzer/rules/input.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,17 @@
1111
}
1212
}
1313
}
14+
15+
/**
16+
* Empty rule and at-rule
17+
*/
18+
.empty {}
19+
.empty-multiline{
20+
21+
22+
}
23+
@media screen {}
24+
25+
@media (min-width: 1px) {
26+
.empty-inside-mq {}
27+
}

test/analyzer/rules/output.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"total": 2
2+
"total": 5,
3+
"empty": {
4+
"total": 3
5+
}
36
}

0 commit comments

Comments
 (0)