Skip to content

Commit 5187be2

Browse files
committed
refactor: ConfigurationOptions
1 parent 9658581 commit 5187be2

File tree

6 files changed

+72
-119
lines changed

6 files changed

+72
-119
lines changed

src/coverage-decorations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
MarkdownString,
1010
} from "vscode";
1111
import {
12-
CONFIG_OPTION_SHOW_DECORATIONS,
12+
ConfigurationOptions,
1313
ExtensionConfiguration,
1414
} from "./extension-configuration";
1515
import { Coverage } from "./coverage-info";
@@ -186,7 +186,7 @@ export class CoverageDecorations extends Disposable {
186186
*/
187187
private onConfigOptionUpdated(configOption: string): void {
188188
if (
189-
configOption === CONFIG_OPTION_SHOW_DECORATIONS &&
189+
configOption === ConfigurationOptions.showDecorations &&
190190
window.activeTextEditor
191191
) {
192192
const activeFile = window.activeTextEditor.document.uri.fsPath;

src/extension-configuration.ts

Lines changed: 62 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -6,136 +6,89 @@ import {
66
} from "vscode";
77

88
export const CONFIG_SECTION_NAME = "markiscodecoverage";
9-
export const CONFIG_OPTION_ENABLE_ON_STARTUP = "enableOnStartup";
10-
export const CONFIG_OPTION_SEARCH_CRITERIA = "searchCriteria";
11-
export const CONFIG_OPTION_COVERAGE_THRESHOLD = "coverageThreshold";
12-
export const CONFIG_OPTION_SHOW_DECORATIONS = "enableDecorations";
13-
14-
export const DEFAULT_SEARCH_CRITERIA = "coverage/lcov*.info";
15-
export const DEFAULT_CODE_COVERAGE_THRESHOLD = 80;
9+
const DEFAULT_SHOW_COVERAGE = true;
10+
const DEFAULT_SHOW_DECORATIONS = false;
11+
const DEFAULT_SEARCH_CRITERIA = "coverage/lcov*.info";
12+
const DEFAULT_CODE_COVERAGE_THRESHOLD = 80;
13+
14+
export enum ConfigurationOptions {
15+
enableOnStartup = "enableOnStartup",
16+
searchCriteria = "searchCriteria",
17+
coverageThreshold = "coverageThreshold",
18+
showDecorations = "enableDecorations",
19+
}
1620

1721
export class ExtensionConfiguration extends Disposable {
18-
private readonly _onConfigOptionUpdated = new EventEmitter<string>();
19-
private _isDisposed = false;
20-
private _showCoverage = true;
21-
private _searchCriteria = "";
22-
private _coverageThreshold = 0;
23-
private _showDecorations = false;
24-
25-
constructor(config: WorkspaceConfiguration) {
26-
// use dummy function for callOnDispose since dispose() will be overrided
27-
super(() => true);
28-
29-
this.showCoverage =
30-
!config.has(CONFIG_OPTION_ENABLE_ON_STARTUP) ||
31-
(config.get(CONFIG_OPTION_ENABLE_ON_STARTUP) ?? true);
32-
33-
const configSearchCriteria =
34-
config.has(CONFIG_OPTION_SEARCH_CRITERIA) &&
35-
config.get(CONFIG_OPTION_SEARCH_CRITERIA);
36-
this.searchCriteria =
37-
configSearchCriteria && typeof configSearchCriteria === "string"
38-
? configSearchCriteria
39-
: DEFAULT_SEARCH_CRITERIA;
40-
41-
this.coverageThreshold =
42-
config.get(CONFIG_OPTION_COVERAGE_THRESHOLD) ??
43-
DEFAULT_CODE_COVERAGE_THRESHOLD;
44-
45-
this.showDecorations = config.get(CONFIG_OPTION_SHOW_DECORATIONS, false);
46-
}
47-
48-
public override dispose(): void {
49-
if (!this._isDisposed) {
50-
this._onConfigOptionUpdated.dispose();
51-
52-
this._isDisposed = true;
53-
}
22+
constructor(
23+
config: WorkspaceConfiguration,
24+
public showCoverage = config.get(
25+
ConfigurationOptions.enableOnStartup,
26+
DEFAULT_SHOW_COVERAGE,
27+
),
28+
public showDecorations = config.get(
29+
ConfigurationOptions.showDecorations,
30+
DEFAULT_SHOW_DECORATIONS,
31+
),
32+
public searchCriteria = config.get(
33+
ConfigurationOptions.searchCriteria,
34+
DEFAULT_SEARCH_CRITERIA,
35+
),
36+
public coverageThreshold = config.get(
37+
ConfigurationOptions.coverageThreshold,
38+
DEFAULT_CODE_COVERAGE_THRESHOLD,
39+
),
40+
private configSectionName = CONFIG_SECTION_NAME,
41+
private readonly _onConfigOptionUpdated:
42+
| EventEmitter<string>
43+
| undefined = new EventEmitter<string>(),
44+
) {
45+
super(() => {
46+
_onConfigOptionUpdated.dispose();
47+
});
5448
}
5549

5650
public onConfigOptionUpdated(listener: (e: string) => any): Disposable {
57-
this._checkDisposed();
51+
if (!this._onConfigOptionUpdated) return Disposable.from();
5852
return this._onConfigOptionUpdated.event(listener);
5953
}
6054

61-
get showCoverage() {
62-
this._checkDisposed();
63-
return this._showCoverage;
64-
}
65-
set showCoverage(value: boolean) {
66-
this._checkDisposed();
67-
this._showCoverage = value;
68-
}
69-
70-
get showDecorations() {
71-
this._checkDisposed();
72-
return this._showDecorations;
73-
}
74-
set showDecorations(value: boolean) {
75-
this._checkDisposed();
76-
this._showDecorations = value;
77-
}
78-
79-
get searchCriteria() {
80-
this._checkDisposed();
81-
return this._searchCriteria;
82-
}
83-
set searchCriteria(value: string) {
84-
this._checkDisposed();
85-
this._searchCriteria = value;
86-
}
87-
88-
get coverageThreshold() {
89-
this._checkDisposed();
90-
return this._coverageThreshold;
91-
}
92-
set coverageThreshold(value: number) {
93-
this._checkDisposed();
94-
this._coverageThreshold = value;
95-
}
96-
9755
dispatchConfigUpdate(
9856
evtSrc: ConfigurationChangeEvent,
9957
latestSnapshot: WorkspaceConfiguration,
10058
): void {
101-
this._checkDisposed();
102-
103-
if (this._hasBeenUpdated(evtSrc, CONFIG_OPTION_SEARCH_CRITERIA)) {
104-
const configSearchCriteria =
105-
latestSnapshot.has(CONFIG_OPTION_SEARCH_CRITERIA) &&
106-
latestSnapshot.get(CONFIG_OPTION_SEARCH_CRITERIA);
107-
this.searchCriteria =
108-
configSearchCriteria && typeof configSearchCriteria === "string"
109-
? configSearchCriteria
110-
: this.searchCriteria;
59+
if (!this._onConfigOptionUpdated) return;
11160

112-
this._onConfigOptionUpdated.fire(CONFIG_OPTION_SEARCH_CRITERIA);
113-
} else if (this._hasBeenUpdated(evtSrc, CONFIG_OPTION_COVERAGE_THRESHOLD)) {
114-
this.coverageThreshold =
115-
latestSnapshot.get(CONFIG_OPTION_COVERAGE_THRESHOLD) ??
116-
this.coverageThreshold;
117-
118-
this._onConfigOptionUpdated.fire(CONFIG_OPTION_COVERAGE_THRESHOLD);
119-
} else if (this._hasBeenUpdated(evtSrc, CONFIG_OPTION_SHOW_DECORATIONS)) {
61+
if (this._hasBeenUpdated(evtSrc, ConfigurationOptions.searchCriteria)) {
62+
this.searchCriteria = latestSnapshot.get(
63+
ConfigurationOptions.searchCriteria,
64+
DEFAULT_SEARCH_CRITERIA,
65+
);
66+
this._onConfigOptionUpdated.fire(ConfigurationOptions.searchCriteria);
67+
} else if (
68+
this._hasBeenUpdated(evtSrc, ConfigurationOptions.coverageThreshold)
69+
) {
70+
this.coverageThreshold = latestSnapshot.get(
71+
ConfigurationOptions.coverageThreshold,
72+
DEFAULT_CODE_COVERAGE_THRESHOLD,
73+
);
74+
this._onConfigOptionUpdated.fire(ConfigurationOptions.coverageThreshold);
75+
} else if (
76+
this._hasBeenUpdated(evtSrc, ConfigurationOptions.showDecorations)
77+
) {
12078
this.showDecorations = latestSnapshot.get(
121-
CONFIG_OPTION_SHOW_DECORATIONS,
122-
this.showDecorations,
79+
ConfigurationOptions.showDecorations,
80+
DEFAULT_SHOW_DECORATIONS,
12381
);
124-
125-
this._onConfigOptionUpdated.fire(CONFIG_OPTION_SHOW_DECORATIONS);
82+
this._onConfigOptionUpdated.fire(ConfigurationOptions.showDecorations);
12683
}
12784
}
12885

12986
private _hasBeenUpdated(
13087
evtSrc: ConfigurationChangeEvent,
13188
optionName: string,
13289
): boolean {
133-
return evtSrc.affectsConfiguration(`${CONFIG_SECTION_NAME}.${optionName}`);
134-
}
135-
136-
private _checkDisposed() {
137-
if (this._isDisposed) {
138-
throw new Error("illegal state - object is disposed");
139-
}
90+
return evtSrc.affectsConfiguration(
91+
`${this.configSectionName}.${optionName}`,
92+
);
14093
}
14194
}

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import {
2020
LineCoverageInfo,
2121
} from "./coverage-info";
2222
import {
23-
CONFIG_OPTION_SEARCH_CRITERIA,
2423
CONFIG_SECTION_NAME,
24+
ConfigurationOptions,
2525
ExtensionConfiguration,
2626
} from "./extension-configuration";
2727
import { parse as parseLcov } from "./parse-lcov";
@@ -72,7 +72,7 @@ export async function activate(
7272
// Register watchers and listen if the coverage file directory has changed
7373
registerWatchers();
7474
extensionConfiguration.onConfigOptionUpdated((e) => {
75-
if (e && e === CONFIG_OPTION_SEARCH_CRITERIA) {
75+
if (e && e === ConfigurationOptions.searchCriteria) {
7676
registerWatchers();
7777
}
7878
});

src/file-coverage-info-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
Uri,
1010
} from "vscode";
1111
import {
12-
CONFIG_OPTION_COVERAGE_THRESHOLD,
12+
ConfigurationOptions,
1313
ExtensionConfiguration,
1414
} from "./extension-configuration";
1515
import { Coverage } from "./coverage-info";
@@ -128,7 +128,7 @@ export class FileCoverageInfoProvider
128128
private handleConfigUpdate(e: string): void {
129129
if (
130130
this._isDisposing ||
131-
e !== CONFIG_OPTION_COVERAGE_THRESHOLD ||
131+
e !== ConfigurationOptions.coverageThreshold ||
132132
this._coverageThreshold === this._configuration.coverageThreshold
133133
) {
134134
return;

src/parse-lcov.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export async function parse(file: string): Promise<CoverageCollection> {
115115
!existsSync(file)
116116
? reject(new Error(`File not found: ${file}`))
117117
: readFile(file, "utf8", (_, str) => {
118-
resolve(parseFile(str));
119-
});
118+
resolve(parseFile(str));
119+
});
120120
});
121121
}

test/suite/extension.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "vscode";
1414
import { ExtensionExports } from "../../src/extension";
1515
import {
16-
CONFIG_OPTION_SHOW_DECORATIONS,
16+
ConfigurationOptions,
1717
CONFIG_SECTION_NAME,
1818
} from "../../src/extension-configuration";
1919
import { Coverage } from "../../src/coverage-info";
@@ -123,7 +123,7 @@ suite("code-coverage", function () {
123123
test("check decorations can be generated from coverage", async () => {
124124
const configuration = workspace.getConfiguration(CONFIG_SECTION_NAME);
125125
await configuration.update(
126-
CONFIG_OPTION_SHOW_DECORATIONS,
126+
ConfigurationOptions.showDecorations,
127127
true,
128128
ConfigurationTarget.Global,
129129
);

0 commit comments

Comments
 (0)