diff --git a/package-lock.json b/package-lock.json index 2f0a765..16a108a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.9.0", "license": "EUPL-1.2", "dependencies": { - "@projectwallace/format-css": "^2.2.0" + "@projectwallace/css-parser": "^0.13.8", + "@projectwallace/format-css": "^2.2.6" }, "bin": { "css-coverage": "dist/cli.mjs" diff --git a/package.json b/package.json index 41843d5..62c27ab 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ "knip": "knip" }, "dependencies": { - "@projectwallace/format-css": "^2.2.0" + "@projectwallace/css-parser": "^0.13.8", + "@projectwallace/format-css": "^2.2.6" }, "devDependencies": { "@codecov/vite-plugin": "^1.9.1", @@ -56,6 +57,9 @@ "tsdown": "^0.21.2", "typescript": "^5.9.3" }, + "overrides": { + "@projectwallace/css-parser": "$@projectwallace/css-parser" + }, "engines": { "node": ">=20" }, diff --git a/src/lib/chunkify.ts b/src/lib/chunkify.ts index b0e9860..c9db891 100644 --- a/src/lib/chunkify.ts +++ b/src/lib/chunkify.ts @@ -1,3 +1,4 @@ +import { tokenize } from '@projectwallace/css-parser/tokenizer' import type { Coverage } from './parse-coverage' type Chunk = { @@ -51,6 +52,56 @@ function merge(stylesheet: ChunkedCoverage): ChunkedCoverage { } } +export function mark_comments_as_covered(stylesheet: ChunkedCoverage): ChunkedCoverage { + let new_chunks: Chunk[] = [] + + for (let chunk of stylesheet.chunks) { + if (chunk.is_covered) { + new_chunks.push(chunk) + continue + } + + let text = stylesheet.text.slice(chunk.start_offset, chunk.end_offset) + let comments: Array<{ start: number; end: number }> = [] + + for (const _ of tokenize(text, ({ start, end }) => comments.push({ start, end }))) { + // consume the generator to drive the on_comment callback + } + + if (comments.length === 0) { + new_chunks.push(chunk) + continue + } + + let last_end = 0 + for (let comment of comments) { + if (comment.start > last_end) { + new_chunks.push({ + start_offset: chunk.start_offset + last_end, + end_offset: chunk.start_offset + comment.start, + is_covered: false, + }) + } + new_chunks.push({ + start_offset: chunk.start_offset + comment.start, + end_offset: chunk.start_offset + comment.end, + is_covered: true, + }) + last_end = comment.end + } + + if (last_end < text.length) { + new_chunks.push({ + start_offset: chunk.start_offset + last_end, + end_offset: chunk.end_offset, + is_covered: false, + }) + } + } + + return merge({ ...stylesheet, chunks: new_chunks }) +} + export function chunkify(stylesheet: Coverage): ChunkedCoverage { let chunks = [] let offset = 0 diff --git a/src/lib/index.test.ts b/src/lib/index.test.ts index a4b5e19..2197d53 100644 --- a/src/lib/index.test.ts +++ b/src/lib/index.test.ts @@ -30,12 +30,12 @@ test.describe('from