Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v4.0] coverage map #588

Merged
merged 7 commits into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Bug-fixes within the same version aren't needed
* improve the detection of cases in which Jest needs to be restarted with `--watchAll` - [@lordofthelake](https://github.com/lordofthelake)
* upgrade all dependencies to the latest, except istanbul-lib-xxx, which requires more code change and will be handled in a separate coverage PR. - @connectdotz
* code base clean up: migrate from tslint to eslint and adopted the latest recommended coding style, adopt semi-colon, added more ci check... - @connectdotz
* upgrade istanbul dependencies to the latest and move to async coverageMap update. @connectdotz
-->

### 3.2.0
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,14 @@
"watch-test": "yarn test -- --watch"
},
"dependencies": {
"istanbul-lib-coverage": "^1.1.1",
"istanbul-lib-source-maps": "^1.1.0",
"istanbul-lib-coverage": "^3.0.0",
"istanbul-lib-source-maps": "^4.0.0",
"jest-editor-support": "^27.2.0",
"jest-snapshot": "^25.5.0"
},
"devDependencies": {
"@types/istanbul-lib-coverage": "^2.0.2",
"@types/istanbul-lib-source-maps": "^4.0.1",
"@types/jest": "^25.2.1",
"@types/node": "^8.0.31",
"@types/vscode": "^1.23.0",
Expand Down
14 changes: 12 additions & 2 deletions src/Coverage/CoverageCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import { GetJestExtByURI } from '../extensionManager';

export class CoverageCodeLensProvider implements vscode.CodeLensProvider {
private getJestExt: GetJestExtByURI;
private onDidChange: vscode.EventEmitter<void>;
onDidChangeCodeLenses: vscode.Event<void>;

constructor(getJestExt: GetJestExtByURI) {
this.getJestExt = getJestExt;
this.onDidChange = new vscode.EventEmitter();
this.onDidChangeCodeLenses = this.onDidChange.event;
}

public provideCodeLenses(document: vscode.TextDocument, _token: vscode.CancellationToken) {
public provideCodeLenses(document: vscode.TextDocument): vscode.CodeLens[] {
const ext = this.getJestExt(document.uri);
const coverage = ext && ext.coverageMapProvider.getFileCoverage(document.fileName);
const coverage =
ext &&
ext.coverageOverlay.enabled &&
ext.coverageMapProvider.getFileCoverage(document.fileName);
if (!coverage) {
return;
}
Expand All @@ -30,4 +37,7 @@ export class CoverageCodeLensProvider implements vscode.CodeLensProvider {

return [new vscode.CodeLens(range, command)];
}
public coverageChanged(): void {
this.onDidChange.fire();
}
}
32 changes: 25 additions & 7 deletions src/Coverage/CoverageMapProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createSourceMapStore, MapStore } from 'istanbul-lib-source-maps';
import { createCoverageMap, CoverageMap } from 'istanbul-lib-coverage';
import {
createCoverageMap,
CoverageMap,
CoverageMapData,
FileCoverage,
} from 'istanbul-lib-coverage';

export class CoverageMapProvider {
private mapStore: MapStore;
Expand All @@ -10,25 +15,38 @@ export class CoverageMapProvider {
private _map: CoverageMap;

constructor() {
this.init();
}

init(): void {
this._map = createCoverageMap();
this.mapStore = createSourceMapStore();
}

get map(): CoverageMap {
return this._map;
}

update(obj: CoverageMap | object) {
async update(obj?: CoverageMap | CoverageMapData): Promise<void> {
const map = createCoverageMap(obj);
const transformed = this.mapStore.transformCoverage(map);
const transformed = await this.mapStore.transformCoverage(map);
if (this._map) {
this._map.merge(transformed.map);
transformed.files().forEach((fileName) => {
this.setFileCoverage(fileName, transformed);
});
} else {
this._map = transformed.map;
this._map = transformed;
}
}

public getFileCoverage(filePath: string) {
setFileCoverage(filePath: string, map: CoverageMap): void {
this._map.data[filePath] = map.fileCoverageFor(filePath);
}
public getFileCoverage(filePath: string): FileCoverage {
return this._map.data[filePath];
}
public onVisibilityChanged(visible: boolean): void {
if (!visible) {
this.init();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably overlooking something, but it seems funny to reset the coverage and source map when the document is no longer visible. I would have expected that we remove the entry from the map.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the visibility here means the coverage, it is not visible when people toggle the coverage off. And when that happens we remove all maps in the init method by reinitializing the map. Maybe the name is confusing? it might be more accurate to call it reset.

}
}
6 changes: 3 additions & 3 deletions src/Coverage/Formatters/GutterFormatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class GutterFormatter extends AbstractFormatter {
});
}

format(editor: vscode.TextEditor) {
format(editor: vscode.TextEditor): void {
const fileCoverage = this.coverageMapProvider.getFileCoverage(editor.document.fileName);
if (!fileCoverage) {
return;
Expand All @@ -72,7 +72,7 @@ export class GutterFormatter extends AbstractFormatter {

for (let line = 1; line <= editor.document.lineCount; line++) {
const zeroBasedLineNumber = line - 1;
if (uncoveredLines.indexOf(line.toString()) >= 0) {
if (uncoveredLines.indexOf(line) >= 0) {
coverageFormatting.uncovered.push(
new vscode.Range(zeroBasedLineNumber, 0, zeroBasedLineNumber, 0)
);
Expand Down Expand Up @@ -121,7 +121,7 @@ export class GutterFormatter extends AbstractFormatter {
return coverageFormatting;
}

clear(editor: vscode.TextEditor) {
clear(editor: vscode.TextEditor): void {
editor.setDecorations(this.coveredLine, []);
editor.setDecorations(this.partiallyCoveredLine, []);
editor.setDecorations(this.uncoveredLine, []);
Expand Down
6 changes: 3 additions & 3 deletions src/Coverage/Formatters/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Location, Position } from 'istanbul-lib-coverage';
import { Range, Location } from 'istanbul-lib-coverage';
connectdotz marked this conversation as resolved.
Show resolved Hide resolved

export function isValidPosition(p: Position) {
export function isValidPosition(p: Location): boolean {
return (p || false) && p.line !== null && p.line >= 0;
}

export function isValidLocation(l: Location) {
export function isValidLocation(l: Range): boolean {
return isValidPosition(l.start) && isValidPosition(l.end);
}
Loading