Skip to content

Commit f7e9135

Browse files
authored
implement LOC metrics (#138)
* implement LOC metrics * fixed a test that has NEVER worked
1 parent 71d6efe commit f7e9135

File tree

9 files changed

+143
-14
lines changed

9 files changed

+143
-14
lines changed

package-lock.json

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

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"name": "@projectwallace/css-analyzer",
33
"version": "2.4.0",
44
"author": "Bart Veneman",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/projectwallace/css-analyzer"
8+
},
59
"homepage": "https://www.projectwallace.com/oss",
610
"issues": "https://github.com/projectwallace/css-analyzer/issues",
711
"license": "MIT",
@@ -65,6 +69,7 @@
6569
"postcss": "^7.0.14",
6670
"postcss-values-parser": "^3.0.2",
6771
"specificity": "^0.4.1",
72+
"split-lines": "^2.0.0",
6873
"string-natural-compare": "^2.0.3",
6974
"tinycolor2": "^1.4.1",
7075
"vendor-prefixes": "^1.0.0"

src/analyzer/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ module.exports = async rawCss => {
1616
const values = require('./values')(css.declarations)
1717
const stylesheets = require('./stylesheets')({
1818
rawCss,
19+
css,
1920
atrules,
2021
rules: css.rules,
2122
selectors,
2223
properties,
23-
values
24+
values,
25+
declarations
2426
})
2527

2628
return Promise.resolve(

src/analyzer/stylesheets/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
module.exports = ({rawCss, atrules, rules, selectors, properties, values}) => {
1+
module.exports = ({
2+
rawCss,
3+
css,
4+
atrules,
5+
rules,
6+
selectors,
7+
properties,
8+
values
9+
}) => {
210
const filesize = require('./size.js')(rawCss)
311
const simplicity = require('./simplicity.js')(rules, selectors)
412
const cohesion = require('./cohesion.js')(rules)
@@ -8,12 +16,19 @@ module.exports = ({rawCss, atrules, rules, selectors, properties, values}) => {
816
properties,
917
values
1018
)
19+
const linesOfCode = require('./lines-of-code')({
20+
rawCss,
21+
atRules: css.atRules,
22+
selectors: css.selectors,
23+
declarations: css.declarations
24+
})
1125

1226
return {
1327
size: filesize.uncompressed.totalBytes,
1428
filesize,
1529
simplicity,
1630
cohesion,
17-
browserhacks
31+
browserhacks,
32+
linesOfCode
1833
}
1934
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const splitLines = require('split-lines')
2+
3+
module.exports = ({rawCss, atRules, selectors, declarations}) => {
4+
const totalLinesOfCode = splitLines(rawCss).length
5+
const totalSourceLinesOfCode =
6+
atRules.length + selectors.length + declarations.length
7+
8+
return {
9+
total: totalLinesOfCode,
10+
sourceLinesOfCode: {
11+
total: totalSourceLinesOfCode
12+
}
13+
}
14+
}

test/analyzer/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ test('it returns the correct analysis object structure', async t => {
110110
'stylesheets.filesize.compressed.gzip.compressionRatio': -4,
111111
'stylesheets.filesize.compressed.gzip.totalBytes': 25,
112112
'stylesheets.filesize.uncompressed.totalBytes': 5,
113+
'stylesheets.linesOfCode.sourceLinesOfCode.total': 1,
114+
'stylesheets.linesOfCode.total': 1,
113115
'stylesheets.simplicity': 1,
114116
'stylesheets.size': 5,
115117
'stylesheets.browserhacks.total': 0,

test/analyzer/stylesheets/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ const FIXTURE = {
1313
supports: {browserhacks: {total: 0, totalUnique: 0}}
1414
},
1515
properties: {browserhacks: {total: 0, totalUnique: 0}},
16-
values: {browserhacks: {total: 0, totalUnique: 0}}
16+
values: {browserhacks: {total: 0, totalUnique: 0}},
17+
css: {
18+
atRules: [],
19+
selectors: [],
20+
declarations: []
21+
}
1722
}
1823

1924
test('it responds with the correct structure', t => {
@@ -45,6 +50,7 @@ test('it responds with the correct structure', t => {
4550
browserhacks: {
4651
total: 0,
4752
totalUnique: 0
48-
}
53+
},
54+
linesOfCode: {total: 1, sourceLinesOfCode: {total: 0}}
4955
})
5056
})
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const test = require('ava')
2+
const analyze = require('../../../src/analyzer/stylesheets/lines-of-code')
3+
4+
const EMPTY_FIXTURE = {
5+
rawCss: '',
6+
atRules: [],
7+
selectors: [],
8+
declarations: []
9+
}
10+
11+
test('it counts lines of code', t => {
12+
t.is(1, analyze(EMPTY_FIXTURE).total)
13+
t.is(1, analyze({...EMPTY_FIXTURE, rawCss: 'foo{}'}).total)
14+
t.is(1, analyze({...EMPTY_FIXTURE, rawCss: 'a { color: red; }'}).total)
15+
t.is(
16+
3,
17+
analyze({
18+
...EMPTY_FIXTURE,
19+
rawCss: `a {
20+
color: red;
21+
}`
22+
}).total
23+
)
24+
t.is(
25+
7,
26+
analyze({
27+
...EMPTY_FIXTURE,
28+
rawCss: `a {
29+
color: red;
30+
}
31+
32+
b {
33+
color: green;
34+
}`
35+
}).total
36+
)
37+
38+
t.is(
39+
11,
40+
analyze({
41+
...EMPTY_FIXTURE,
42+
rawCss: `/**
43+
* 3 comment lines
44+
*/
45+
46+
a {
47+
color: red;
48+
}
49+
50+
b {
51+
color: green;
52+
}`
53+
}).total
54+
)
55+
})
56+
57+
test('it counts source lines of code', t => {
58+
t.is(0, analyze(EMPTY_FIXTURE).sourceLinesOfCode.total)
59+
t.is(
60+
1,
61+
analyze({...EMPTY_FIXTURE, selectors: ['foo']}).sourceLinesOfCode.total
62+
)
63+
t.is(
64+
4,
65+
analyze({
66+
...EMPTY_FIXTURE,
67+
atRules: [
68+
{
69+
type: 'media',
70+
params: '(min-width: 1em)'
71+
}
72+
],
73+
selectors: ['.selector', '#selector'],
74+
declarations: [
75+
{
76+
property: 'color',
77+
value: 'red'
78+
}
79+
]
80+
}).sourceLinesOfCode.total
81+
)
82+
})

test/smoke/test.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
const {promisify} = require('util')
2-
const {readFile} = require('fs')
1+
const {readFileSync} = require('fs')
32
const {join} = require('path')
43
const test = require('ava')
54
const analyze = require('../../src/analyzer')
65

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

1411
test('it analyzes large CSS files without errors - css-tricks', async t => {
15-
const css = await readFileAsync(join(__dirname, '/css-tricks-20190319.css'))
12+
const css = readFileSync(join(__dirname, '/css-tricks-20190319.css'), 'utf8')
1613
await t.notThrowsAsync(analyze(css))
1714
})
1815

1916
test('it analyzes large CSS files without errors - smashing magazine', async t => {
20-
const css = await readFileAsync(
21-
join(__dirname, '/smashing-magazine-20190319.css')
17+
const css = readFileSync(
18+
join(__dirname, '/smashing-magazine-20190319.css'),
19+
'utf8'
2220
)
2321
await t.notThrowsAsync(analyze(css))
2422
})

0 commit comments

Comments
 (0)