Skip to content

Commit 868c89d

Browse files
committed
feat(tool): added code coverage tool
tool to analyse unused code
1 parent 914ac68 commit 868c89d

5 files changed

Lines changed: 474 additions & 1 deletion

File tree

docs/tool-reference.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
- **[Emulation](#emulation)** (2 tools)
2222
- [`emulate`](#emulate)
2323
- [`resize_page`](#resize_page)
24-
- **[Performance](#performance)** (3 tools)
24+
- **[Performance](#performance)** (5 tools)
25+
- [`coverage_start`](#coverage_start)
26+
- [`coverage_stop`](#coverage_stop)
2527
- [`performance_analyze_insight`](#performance_analyze_insight)
2628
- [`performance_start_trace`](#performance_start_trace)
2729
- [`performance_stop_trace`](#performance_stop_trace)
@@ -215,6 +217,29 @@
215217

216218
## Performance
217219

220+
### `coverage_start`
221+
222+
**Description:** Starts code coverage tracking on the selected page. This tracks which JavaScript and CSS code is actually used, helping identify unused code that could be removed to improve page performance.
223+
224+
**Parameters:**
225+
226+
- **includeCSS** (boolean) _(optional)_: Whether to include CSS coverage. Defaults to true.
227+
- **includeJS** (boolean) _(optional)_: Whether to include JavaScript coverage. Defaults to true.
228+
- **resetOnNavigation** (boolean) _(optional)_: Whether to reset coverage data on page navigation. Defaults to true.
229+
230+
---
231+
232+
### `coverage_stop`
233+
234+
**Description:** Stops code coverage tracking and returns a comprehensive report showing URLs, total bytes, used bytes, unused bytes, and usage percentage for each JavaScript and CSS resource. Results are sorted by unused bytes (most wasted first) and paginated.
235+
236+
**Parameters:**
237+
238+
- **pageIdx** (integer) _(optional)_: Page index (0-based). Use this to navigate through results. For example, pageIdx: 1 shows the next page.
239+
- **pageSize** (integer) _(optional)_: Number of results to show per page. Maximum and default is 5 to keep output manageable.
240+
241+
---
242+
218243
### `performance_analyze_insight`
219244

220245
**Description:** Provides more detailed information on a specific Performance Insight of an insight set that was highlighted in the results of a trace recording.

src/McpContext.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ export class McpContext implements Context {
106106
#consoleCollector: ConsoleCollector;
107107

108108
#isRunningTrace = false;
109+
#isRunningCoverage = false;
110+
#coverageOptions: {includeJS: boolean; includeCSS: boolean} = {
111+
includeJS: true,
112+
includeCSS: true,
113+
};
109114
#networkConditionsMap = new WeakMap<Page, string>();
110115
#cpuThrottlingRateMap = new WeakMap<Page, number>();
111116
#geolocationMap = new WeakMap<Page, GeolocationOptions>();
@@ -306,6 +311,22 @@ export class McpContext implements Context {
306311
return this.#isRunningTrace;
307312
}
308313

314+
setIsRunningCoverage(x: boolean): void {
315+
this.#isRunningCoverage = x;
316+
}
317+
318+
isRunningCoverage(): boolean {
319+
return this.#isRunningCoverage;
320+
}
321+
322+
setCoverageOptions(options: {includeJS: boolean; includeCSS: boolean}): void {
323+
this.#coverageOptions = options;
324+
}
325+
326+
getCoverageOptions(): {includeJS: boolean; includeCSS: boolean} {
327+
return this.#coverageOptions;
328+
}
329+
309330
getDialog(): Dialog | undefined {
310331
return this.#dialog;
311332
}

src/tools/ToolDefinition.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,20 @@ export interface Response {
8181
/**
8282
* Only add methods required by tools/*.
8383
*/
84+
export interface CoverageOptions {
85+
includeJS: boolean;
86+
includeCSS: boolean;
87+
}
88+
8489
export type Context = Readonly<{
8590
isRunningPerformanceTrace(): boolean;
8691
setIsRunningPerformanceTrace(x: boolean): void;
8792
recordedTraces(): TraceResult[];
8893
storeTraceRecording(result: TraceResult): void;
94+
isRunningCoverage(): boolean;
95+
setIsRunningCoverage(x: boolean): void;
96+
getCoverageOptions(): CoverageOptions;
97+
setCoverageOptions(options: CoverageOptions): void;
8998
getSelectedPage(): Page;
9099
getDialog(): Dialog | undefined;
91100
clearDialog(): void;

0 commit comments

Comments
 (0)