Skip to content

Commit 24d1d43

Browse files
authored
skip selectors that cannot be analyzed for complexity (#143)
* skip selectors that cannot be analyzed for complexity * fix catch cause failing on node < 10
1 parent 8d837e6 commit 24d1d43

File tree

8 files changed

+1092
-26
lines changed

8 files changed

+1092
-26
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@
5353
]
5454
},
5555
"dependencies": {
56-
"brotli-size": "0.0.3",
56+
"brotli-size": "^0.1.0",
5757
"color-sorter": "^3.0.0",
5858
"css-at-supports-browser-h4cks-analyzer": "^1.0.0",
5959
"css-media-query-browser-h4cks-analyzer": "^1.0.0",
6060
"css-property-browser-h4cks-analyzer": "^1.1.0",
6161
"css-selector-browser-h4cks-analyzer": "^1.1.0",
62-
"css-selector-complexity": "^0.1.1",
62+
"css-selector-complexity": "^0.1.2",
6363
"css-shorthand-expand": "^1.2.0",
6464
"css-unit-sort": "^3.3.0",
6565
"css-value-browser-h4cks-analyzer": "^1.0.1",

src/analyzer/selectors/complexity.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ const uniquer = require('../../utils/uniquer')
33

44
module.exports = selectors => {
55
const all = selectors.map(selector => {
6+
let complexity = 0
7+
8+
try {
9+
complexity = selectorComplexity(selector)
10+
} catch (error) {
11+
// Fail silently, ignoring the error
12+
}
13+
614
return {
715
selector,
8-
complexity: selectorComplexity(selector)
16+
complexity
917
}
1018
})
1119
const allComplexities = all.map(selector => selector.complexity)

test/analyzer/selectors/complexity.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,35 @@ test('it calculates the average complexity per selector', t => {
8585

8686
t.is(actual, 26 / 6)
8787
})
88+
89+
test('it reports a default complexity of 1 in case of an invalid selector', t => {
90+
const provider = ['+.box-group', '.w-(20: 20%']
91+
const expected = {
92+
max: {
93+
value: 0,
94+
count: 2,
95+
selectors: [
96+
{
97+
value: '+.box-group',
98+
count: 1
99+
},
100+
{
101+
value: '.w-(20: 20%',
102+
count: 1
103+
}
104+
]
105+
},
106+
average: 0,
107+
sum: 0,
108+
unique: [
109+
{
110+
value: 0,
111+
count: 2
112+
}
113+
],
114+
totalUnique: 1
115+
}
116+
const actual = analyze(provider)
117+
118+
t.deepEqual(actual, expected)
119+
})

test/smoke/bol-com-20190617.css

Lines changed: 806 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/smoke/lego-20190617.css

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/smoke/test.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
const {readFileSync} = require('fs')
1+
const {readFile} = require('fs')
2+
const {promisify} = require('util')
23
const {join} = require('path')
34
const test = require('ava')
4-
const analyze = require('../../src/analyzer')
5+
const analyze = require('../..')
56

6-
test('it analyzes large CSS files without errors - facebook', async t => {
7-
const css = readFileSync(join(__dirname, '/facebook-20190319.css'), 'utf8')
8-
await t.notThrowsAsync(analyze(css))
9-
})
7+
const readFileAsync = promisify(readFile)
108

11-
test('it analyzes large CSS files without errors - css-tricks', async t => {
12-
const css = readFileSync(join(__dirname, '/css-tricks-20190319.css'), 'utf8')
13-
await t.notThrowsAsync(analyze(css))
14-
})
9+
const fileNames = [
10+
'facebook-20190319',
11+
'css-tricks-20190319',
12+
'smashing-magazine-20190319',
13+
'trello-20190617',
14+
'bol-com-20190617',
15+
'lego-20190617'
16+
]
1517

16-
test('it analyzes large CSS files without errors - smashing magazine', async t => {
17-
const css = readFileSync(
18-
join(__dirname, '/smashing-magazine-20190319.css'),
19-
'utf8'
20-
)
21-
await t.notThrowsAsync(analyze(css))
18+
fileNames.forEach(fileName => {
19+
test(`It doesn't fail on real-life CSS - ${fileName}`, async t => {
20+
const css = await readFileAsync(join(__dirname, `${fileName}.css`), 'utf8')
21+
await t.notThrowsAsync(() => analyze(css))
22+
})
2223
})

test/smoke/trello-20190617.css

Lines changed: 214 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)