Skip to content

Commit 590ae75

Browse files
kyakdanoetr
authored andcommitted
instrumentor: simplify checks for activating babel transformations
1 parent 7b61799 commit 590ae75

File tree

3 files changed

+93
-42
lines changed

3 files changed

+93
-42
lines changed

package-lock.json

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/instrumentor/instrument.test.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,49 @@ import { sourceCodeCoverage } from "./plugins/sourceCodeCoverage";
2424
describe("shouldInstrument check", () => {
2525
it("should consider includes and excludes", () => {
2626
const instrumentor = new Instrumentor(["include"], ["exclude"]);
27-
expect(instrumentor.shouldInstrument("include")).toBeTruthy();
28-
expect(instrumentor.shouldInstrument("exclude")).toBeFalsy();
27+
expect(instrumentor.shouldInstrumentForFuzzing("include")).toBeTruthy();
28+
expect(instrumentor.shouldInstrumentForFuzzing("exclude")).toBeFalsy();
2929
expect(
30-
instrumentor.shouldInstrument("/some/package/include/files")
30+
instrumentor.shouldInstrumentForFuzzing("/some/package/include/files")
3131
).toBeTruthy();
3232
expect(
33-
instrumentor.shouldInstrument("/some/package/exclude/files")
33+
instrumentor.shouldInstrumentForFuzzing("/some/package/exclude/files")
34+
).toBeFalsy();
35+
expect(
36+
instrumentor.shouldInstrumentForFuzzing("/something/else")
3437
).toBeFalsy();
35-
expect(instrumentor.shouldInstrument("/something/else")).toBeFalsy();
3638
});
3739

3840
it("should include everything with *", () => {
3941
const instrumentor = new Instrumentor(["*"], []);
40-
expect(instrumentor.shouldInstrument("include")).toBeTruthy();
41-
expect(instrumentor.shouldInstrument("/something/else")).toBeTruthy();
42+
expect(instrumentor.shouldInstrumentForFuzzing("include")).toBeTruthy();
43+
expect(
44+
instrumentor.shouldInstrumentForFuzzing("/something/else")
45+
).toBeTruthy();
4246
});
4347

4448
it("should include nothing with emtpy string", () => {
4549
const instrumentorWithEmptyInclude = new Instrumentor(["include", ""], []);
4650
expect(
47-
instrumentorWithEmptyInclude.shouldInstrument("include")
51+
instrumentorWithEmptyInclude.shouldInstrumentForFuzzing("include")
4852
).toBeTruthy();
4953
expect(
50-
instrumentorWithEmptyInclude.shouldInstrument("/something/else")
54+
instrumentorWithEmptyInclude.shouldInstrumentForFuzzing("/something/else")
5155
).toBeFalsy();
5256

5357
const instrumentorWithEmptyExclude = new Instrumentor(["include"], [""]);
5458
expect(
55-
instrumentorWithEmptyExclude.shouldInstrument("include")
59+
instrumentorWithEmptyExclude.shouldInstrumentForFuzzing("include")
5660
).toBeTruthy();
5761
expect(
58-
instrumentorWithEmptyExclude.shouldInstrument("/something/else")
62+
instrumentorWithEmptyExclude.shouldInstrumentForFuzzing("/something/else")
5963
).toBeFalsy();
6064
});
6165

6266
it("should exclude with precedence", () => {
6367
const instrumentor = new Instrumentor(["include"], ["*"]);
6468
expect(
65-
instrumentor.shouldInstrument("/some/package/include/files")
69+
instrumentor.shouldInstrumentForFuzzing("/some/package/include/files")
6670
).toBeFalsy();
6771
});
6872
});

packages/instrumentor/instrument.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export class Instrumentor {
4747
private readonly includes: string[] = ["*"],
4848
private readonly excludes: string[] = ["node_modules"],
4949
private readonly customHooks: string[] = [],
50-
private readonly collectSourceCodeCoverage = false,
51-
private readonly dryRun = false,
50+
private readonly shouldCollectSourceCodeCoverage = false,
51+
private readonly isDryRun = false,
5252
private readonly idStrategy: EdgeIdStrategy = new MemorySyncIdStrategy()
5353
) {}
5454

@@ -62,7 +62,7 @@ export class Instrumentor {
6262
instrument(code: string, filename: string): string {
6363
const transformations: PluginItem[] = [];
6464

65-
const shouldInstrumentFile = this.shouldInstrument(filename);
65+
const shouldInstrumentFile = this.shouldInstrumentForFuzzing(filename);
6666

6767
if (shouldInstrumentFile) {
6868
transformations.push(codeCoverage(this.idStrategy), compareHooks);
@@ -72,14 +72,14 @@ export class Instrumentor {
7272
transformations.push(functionHooks(filename));
7373
}
7474

75-
if (shouldInstrumentFile) {
76-
this.idStrategy.startForSourceFile(filename);
77-
}
78-
7975
if (this.shouldCollectCodeCoverage(filename)) {
8076
transformations.push(sourceCodeCoverage(filename));
8177
}
8278

79+
if (shouldInstrumentFile) {
80+
this.idStrategy.startForSourceFile(filename);
81+
}
82+
8383
const transformedCode =
8484
this.transform(filename, code, transformations)?.code || code;
8585

@@ -156,17 +156,28 @@ export class Instrumentor {
156156
delete require.cache[require.resolve(module)];
157157
});
158158
}
159-
shouldInstrument(
159+
shouldInstrumentForFuzzing(filepath: string): boolean {
160+
return (
161+
!this.isDryRun &&
162+
Instrumentor.doesMatchFilters(filepath, this.includes, this.excludes)
163+
);
164+
}
165+
166+
private shouldCollectCodeCoverage(filepath: string): boolean {
167+
return (
168+
this.shouldCollectSourceCodeCoverage &&
169+
(Instrumentor.doesMatchFilters(filepath, this.includes, this.excludes) ||
170+
Instrumentor.doesMatchFilters(filepath, this.customHooks, ["nothing"]))
171+
);
172+
}
173+
174+
private static doesMatchFilters(
160175
filepath: string,
161-
dryRun = this.dryRun,
162-
includes = this.includes,
163-
excludes = this.excludes
176+
includes: string[],
177+
excludes: string[]
164178
): boolean {
165-
if (dryRun) {
166-
return false;
167-
}
168-
const cleanedIncludes = this.cleanup(includes);
169-
const cleanedExcludes = this.cleanup(excludes);
179+
const cleanedIncludes = Instrumentor.cleanup(includes);
180+
const cleanedExcludes = Instrumentor.cleanup(excludes);
170181
const included =
171182
cleanedIncludes.find((include) => filepath.includes(include)) !==
172183
undefined;
@@ -176,20 +187,7 @@ export class Instrumentor {
176187
return included && !excluded;
177188
}
178189

179-
shouldCollectCodeCoverage(filepath: string): boolean {
180-
const shouldCoverCustomHooks = this.shouldInstrument(
181-
filepath,
182-
false,
183-
this.customHooks,
184-
["nothing"]
185-
);
186-
return (
187-
this.collectSourceCodeCoverage &&
188-
(shouldCoverCustomHooks || this.shouldInstrument(filepath, false))
189-
);
190-
}
191-
192-
cleanup(settings: string[]): string[] {
190+
private static cleanup(settings: string[]): string[] {
193191
return settings
194192
.filter((setting) => setting)
195193
.map((setting) => (setting === "*" ? "" : setting)); // empty string matches every file

0 commit comments

Comments
 (0)