Skip to content

Commit b486945

Browse files
committed
feat(coverage): add coverage exclusions without excluding generated docs
Add coverageExclude CLI/config option to exclude files from coverage calculation only Add @coverageIgnore JSDoc tag to exclude class, property, or method from coverage calculation Propagate coverageIgnore metadata through dependency models and coverage pipeline Integrate file-pattern and symbol-level filtering into prepareCoverage with glob support Add CLI tests and fixtures for coverageExclude and @coverageIgnore Update CLI help output and configuration schema closes #1554 closes #528
1 parent 80382a1 commit b486945

20 files changed

Lines changed: 343 additions & 12 deletions

‎src/app/application.ts‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,105 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
21242124
*/
21252125
let files = [];
21262126
let totalProjectStatementDocumented = 0;
2127+
const coverageExcludePatterns =
2128+
Configuration.mainData.coverageExclude || [];
2129+
2130+
const toUnixPath = (filePath: string): string =>
2131+
(filePath || "").replace(/\\/g, "/");
2132+
2133+
const escapeRegex = (value: string): string =>
2134+
value.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
2135+
2136+
const globToRegExp = (globPattern: string): RegExp => {
2137+
const normalized = toUnixPath(globPattern);
2138+
const escaped = escapeRegex(normalized)
2139+
.replace(/\*\*/g, "::DOUBLE_STAR::")
2140+
.replace(/\*/g, "[^/]*")
2141+
.replace(/::DOUBLE_STAR::/g, ".*");
2142+
return new RegExp(`^${escaped}$`);
2143+
};
2144+
2145+
const coverageExcludeRegexes = coverageExcludePatterns
2146+
.map((pattern) => String(pattern || "").trim())
2147+
.filter((pattern) => pattern.length > 0)
2148+
.map((pattern) => globToRegExp(pattern));
2149+
2150+
const hasCoverageIgnoredTag = (tags): boolean => {
2151+
if (!tags || !Array.isArray(tags)) {
2152+
return false;
2153+
}
2154+
2155+
return tags.some((tag) => {
2156+
if (!tag) {
2157+
return false;
2158+
}
2159+
2160+
const explicitTagName =
2161+
tag.tagName?.text ||
2162+
tag.tagName?.escapedText ||
2163+
tag.tagName ||
2164+
tag.name?.text ||
2165+
tag.name ||
2166+
"";
2167+
2168+
if (explicitTagName) {
2169+
return (
2170+
String(explicitTagName).toLowerCase() ===
2171+
"coverageignore"
2172+
);
2173+
}
2174+
2175+
if (typeof tag === "string") {
2176+
return tag.toLowerCase().indexOf("coverageignore") > -1;
2177+
}
2178+
2179+
if (typeof tag.label === "string") {
2180+
return (
2181+
tag.label.toLowerCase().indexOf("coverageignore") > -1
2182+
);
2183+
}
2184+
2185+
return false;
2186+
});
2187+
};
2188+
2189+
const isCoverageIgnoredElement = (element): boolean => {
2190+
if (!element) {
2191+
return false;
2192+
}
2193+
2194+
if (element.coverageIgnore === true) {
2195+
return true;
2196+
}
2197+
2198+
if (hasCoverageIgnoredTag(element.jsdoctags)) {
2199+
return true;
2200+
}
2201+
2202+
return false;
2203+
};
2204+
2205+
const isCoverageIgnoredByFile = (filePath: string): boolean => {
2206+
if (!coverageExcludeRegexes.length || !filePath) {
2207+
return false;
2208+
}
2209+
2210+
const normalizedPath = toUnixPath(filePath);
2211+
return coverageExcludeRegexes.some((regex) =>
2212+
regex.test(normalizedPath),
2213+
);
2214+
};
2215+
2216+
const shouldSkipCoverage = (element): boolean => {
2217+
if (!element) {
2218+
return true;
2219+
}
2220+
2221+
return (
2222+
isCoverageIgnoredByFile(element.file || element.filePath) ||
2223+
isCoverageIgnoredElement(element)
2224+
);
2225+
};
21272226
const getStatus = function (percent) {
21282227
let status;
21292228
if (percent <= 25) {
@@ -2142,6 +2241,9 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
21422241
) => {
21432242
_.forEach(list, (el: any) => {
21442243
const element = (Object as any).assign({}, el);
2244+
if (shouldSkipCoverage(element)) {
2245+
return;
2246+
}
21452247
if (!element.propertiesClass) {
21462248
element.propertiesClass = [];
21472249
}
@@ -2191,6 +2293,10 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
21912293
}
21922294

21932295
_.forEach(element.propertiesClass, (property: any) => {
2296+
if (isCoverageIgnoredElement(property)) {
2297+
totalStatements -= 1;
2298+
return;
2299+
}
21942300
if (
21952301
property.modifierKind === SyntaxKind.PrivateKeyword
21962302
) {
@@ -2206,6 +2312,10 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
22062312
}
22072313
});
22082314
_.forEach(element.methodsClass, (method: any) => {
2315+
if (isCoverageIgnoredElement(method)) {
2316+
totalStatements -= 1;
2317+
return;
2318+
}
22092319
if (method.modifierKind === SyntaxKind.PrivateKeyword) {
22102320
// Doesn't handle private for coverage
22112321
totalStatements -= 1;
@@ -2219,6 +2329,10 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
22192329
}
22202330
});
22212331
_.forEach(element.hostBindings, (property: any) => {
2332+
if (isCoverageIgnoredElement(property)) {
2333+
totalStatements -= 1;
2334+
return;
2335+
}
22222336
if (
22232337
property.modifierKind === SyntaxKind.PrivateKeyword
22242338
) {
@@ -2234,6 +2348,10 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
22342348
}
22352349
});
22362350
_.forEach(element.hostListeners, (method: any) => {
2351+
if (isCoverageIgnoredElement(method)) {
2352+
totalStatements -= 1;
2353+
return;
2354+
}
22372355
if (method.modifierKind === SyntaxKind.PrivateKeyword) {
22382356
// Doesn't handle private for coverage
22392357
totalStatements -= 1;
@@ -2247,6 +2365,10 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
22472365
}
22482366
});
22492367
_.forEach(element.inputsClass, (input: any) => {
2368+
if (isCoverageIgnoredElement(input)) {
2369+
totalStatements -= 1;
2370+
return;
2371+
}
22502372
if (input.modifierKind === SyntaxKind.PrivateKeyword) {
22512373
// Doesn't handle private for coverage
22522374
totalStatements -= 1;
@@ -2260,6 +2382,10 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
22602382
}
22612383
});
22622384
_.forEach(element.outputsClass, (output: any) => {
2385+
if (isCoverageIgnoredElement(output)) {
2386+
totalStatements -= 1;
2387+
return;
2388+
}
22632389
if (output.modifierKind === SyntaxKind.PrivateKeyword) {
22642390
// Doesn't handle private for coverage
22652391
totalStatements -= 1;
@@ -2324,6 +2450,9 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
23242450
};
23252451
let processFunctionsAndVariables = (id, type) => {
23262452
_.forEach(id, (el: any) => {
2453+
if (shouldSkipCoverage(el)) {
2454+
return;
2455+
}
23272456
let cl: any = {
23282457
filePath: el.file,
23292458
type: type,
@@ -2367,6 +2496,9 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
23672496
let processClasses = (list, type, linktype) => {
23682497
_.forEach(list, (cl: any) => {
23692498
let element = (Object as any).assign({}, cl);
2499+
if (shouldSkipCoverage(element)) {
2500+
return;
2501+
}
23702502
if (!element.properties) {
23712503
element.properties = [];
23722504
}
@@ -2398,6 +2530,10 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
23982530
}
23992531

24002532
_.forEach(element.properties, (property: any) => {
2533+
if (isCoverageIgnoredElement(property)) {
2534+
totalStatements -= 1;
2535+
return;
2536+
}
24012537
if (
24022538
property.modifierKind === SyntaxKind.PrivateKeyword
24032539
) {
@@ -2413,6 +2549,10 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
24132549
}
24142550
});
24152551
_.forEach(element.methods, (method: any) => {
2552+
if (isCoverageIgnoredElement(method)) {
2553+
totalStatements -= 1;
2554+
return;
2555+
}
24162556
if (method.modifierKind === SyntaxKind.PrivateKeyword) {
24172557
// Doesn't handle private for coverage
24182558
totalStatements -= 1;
@@ -2472,6 +2612,9 @@ at least one config for the 'info' or 'source' tab in --navTabConfig.`);
24722612
);
24732613

24742614
_.forEach(Configuration.mainData.pipes, (pipe: any) => {
2615+
if (shouldSkipCoverage(pipe)) {
2616+
return;
2617+
}
24752618
let cl: any = {
24762619
filePath: pipe.file,
24772620
type: pipe.type,

‎src/app/compiler/angular-dependencies.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ImportsUtil from "../../utils/imports.util";
1818

1919
import {
2020
getModuleWithProviders,
21+
isCoverageIgnore,
2122
isIgnore,
2223
isModuleWithProviders,
2324
JsdocParserUtil,
@@ -376,6 +377,7 @@ export class AngularDependencies extends FrameworkDependencies {
376377
name,
377378
id: "class-" + name + "-" + hash,
378379
file: file,
380+
coverageIgnore: !!IO.coverageIgnore,
379381
deprecated: IO.deprecated,
380382
deprecationMessage: IO.deprecationMessage,
381383
type: "class",
@@ -713,6 +715,7 @@ export class AngularDependencies extends FrameworkDependencies {
713715
name,
714716
id: "injectable-" + name + "-" + hash,
715717
file: file,
718+
coverageIgnore: !!IO.coverageIgnore,
716719
properties: IO.properties,
717720
methods: IO.methods,
718721
deprecated: IO.deprecated,
@@ -784,6 +787,7 @@ export class AngularDependencies extends FrameworkDependencies {
784787
id: "pipe-" + name + "-" + hash,
785788
file: file,
786789
type: "pipe",
790+
coverageIgnore: !!IO.coverageIgnore,
787791
deprecated: IO.deprecated,
788792
deprecationMessage: IO.deprecationMessage,
789793
description: IO.description,
@@ -971,6 +975,7 @@ export class AngularDependencies extends FrameworkDependencies {
971975
file: file,
972976
ctype: "miscellaneous",
973977
subtype: "function",
978+
coverageIgnore: isCoverageIgnore(node),
974979
deprecated,
975980
deprecationMessage,
976981
rawdescription:
@@ -1061,6 +1066,7 @@ export class AngularDependencies extends FrameworkDependencies {
10611066
name,
10621067
ctype: "miscellaneous",
10631068
subtype: "typealias",
1069+
coverageIgnore: isCoverageIgnore(node),
10641070
rawtype: this.classHelper.visitType(node),
10651071
file: file,
10661072
deprecated,
@@ -1275,6 +1281,9 @@ export class AngularDependencies extends FrameworkDependencies {
12751281
ctype: "miscellaneous",
12761282
subtype: "variable",
12771283
file: file,
1284+
coverageIgnore: isCoverageIgnore(
1285+
variableNode,
1286+
),
12781287
deprecated,
12791288
deprecationMessage,
12801289
};
@@ -1367,6 +1376,8 @@ export class AngularDependencies extends FrameworkDependencies {
13671376
ctype: "miscellaneous",
13681377
subtype: "variable",
13691378
file: file,
1379+
coverageIgnore:
1380+
isCoverageIgnore(node),
13701381
deprecated: false,
13711382
deprecationMessage: "",
13721383
};
@@ -1482,6 +1493,7 @@ export class AngularDependencies extends FrameworkDependencies {
14821493
name,
14831494
ctype: "miscellaneous",
14841495
subtype: "typealias",
1496+
coverageIgnore: isCoverageIgnore(node),
14851497
rawtype: this.classHelper.visitType(node),
14861498
file: file,
14871499
deprecated,
@@ -1523,6 +1535,7 @@ export class AngularDependencies extends FrameworkDependencies {
15231535
ctype: "miscellaneous",
15241536
subtype: "function",
15251537
file: file,
1538+
coverageIgnore: isCoverageIgnore(node),
15261539
deprecated,
15271540
deprecationMessage,
15281541
rawdescription:

‎src/app/compiler/angular/dependencies.interfaces.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface IDep {
33
type?: string;
44
ctype?: string;
55
name: string;
6+
coverageIgnore?: boolean;
67
}
78

89
export interface IInjectableDep extends IDep {

‎src/app/compiler/angular/deps/component-dep.factory.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class ComponentDepFactory {
3939
outputsClass: IO.outputs,
4040
propertiesClass: IO.properties,
4141
methodsClass: IO.methods,
42+
coverageIgnore: IO.coverageIgnore,
4243

4344
deprecated: IO.deprecated,
4445
deprecationMessage: IO.deprecationMessage,
@@ -126,6 +127,7 @@ export interface IComponentDep extends IDep {
126127
outputsClass: Array<any>;
127128
propertiesClass: Array<any>;
128129
methodsClass: Array<any>;
130+
coverageIgnore?: boolean;
129131

130132
deprecated: boolean;
131133
deprecationMessage: string;

‎src/app/compiler/angular/deps/controller-dep.factory.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class ControllerDepFactory {
2121
file: file,
2222
methodsClass: IO.methods,
2323
type: 'controller',
24+
coverageIgnore: IO.coverageIgnore,
2425
description: IO.description,
2526
rawdescription: IO.rawdescription,
2627
sourceCode: srcFile.text,
@@ -46,6 +47,7 @@ export interface IControllerDep extends IDep {
4647
rawdescription: string;
4748
prefix?: string;
4849
methodsClass: Array<any>;
50+
coverageIgnore?: boolean;
4951
deprecated: boolean;
5052
deprecationMessage: string;
5153
extends?: any;

‎src/app/compiler/angular/deps/directive-dep.factory.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class DirectiveDepFactory {
3737

3838
propertiesClass: IO.properties,
3939
methodsClass: IO.methods,
40+
coverageIgnore: IO.coverageIgnore,
4041
exampleUrls: this.helper.getComponentExampleUrls(srcFile.getText())
4142
};
4243

@@ -97,6 +98,7 @@ export interface IDirectiveDep extends IDep {
9798

9899
propertiesClass: any;
99100
methodsClass: any;
101+
coverageIgnore?: boolean;
100102
exampleUrls: Array<string>;
101103

102104
constructorObj?: Object;

‎src/app/compiler/angular/deps/entity-dep.factory.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class EntityDepFactory {
2020
id: 'controller-' + name + '-' + hash,
2121
file: file,
2222
type: 'entity',
23+
coverageIgnore: IO.coverageIgnore,
2324
description: IO.description,
2425
rawdescription: IO.rawdescription,
2526
sourceCode: srcFile.text,
@@ -36,6 +37,7 @@ export interface IEntityDep extends IDep {
3637
sourceCode: string;
3738
description: string;
3839
rawdescription: string;
40+
coverageIgnore?: boolean;
3941
deprecated: boolean;
4042
deprecationMessage: string;
4143
properties: Array<any>;

0 commit comments

Comments
 (0)