Skip to content

Commit 3dd29af

Browse files
authored
improve LoC calculation, using CSSTree instead of Regex (#309)
1 parent a73cfe6 commit 3dd29af

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function ratio(part, total) {
2828
*/
2929
const analyze = (css) => {
3030
const start = Date.now()
31-
let lines = css.split(/\r?\n/g)
3231

3332
/**
3433
* Recreate the authored CSS from a CSSTree node
@@ -55,16 +54,19 @@ const analyze = (css) => {
5554

5655
const startParse = Date.now()
5756

57+
/** @type import('css-tree').CssNode */
5858
const ast = parse(css, {
5959
parseCustomProperty: true, // To find font-families, colors, etc.
6060
positions: true, // So we can use stringifyNode()
61+
/** @param {string} comment */
6162
onComment: function (comment) {
6263
totalComments++
6364
commentsSize += comment.length
6465
},
6566
})
6667

6768
const startAnalysis = Date.now()
69+
let linesOfCode = ast.loc.end.line - ast.loc.start.line + 1
6870

6971
// Atrules
7072
let totalAtRules = 0
@@ -510,7 +512,7 @@ const analyze = (css) => {
510512
return {
511513
stylesheet: {
512514
sourceLinesOfCode: totalAtRules + totalSelectors + totalDeclarations + keyframeSelectors.size(),
513-
linesOfCode: lines.length,
515+
linesOfCode,
514516
size: css.length,
515517
comments: {
516518
total: totalComments,

0 commit comments

Comments
 (0)