-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathadd-code-coverage.ts
43 lines (38 loc) · 1.24 KB
/
add-code-coverage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { NodePath, PluginObj, types } from '@babel/core';
import { Visitor, programVisitor } from 'istanbul-lib-instrument';
import assert from 'node:assert';
/**
* A babel plugin factory function for adding istanbul instrumentation.
*
* @returns A babel plugin object instance.
*/
export default function (): PluginObj {
const visitors = new WeakMap<NodePath, Visitor>();
return {
visitor: {
Program: {
enter(path, state) {
const visitor = programVisitor(types, state.filename, {
// Babel returns a Converter object from the `convert-source-map` package
inputSourceMap: (state.file.inputMap as undefined | { toObject(): object })?.toObject(),
});
visitors.set(path, visitor);
visitor.enter(path);
},
exit(path) {
const visitor = visitors.get(path);
assert(visitor, 'Instrumentation visitor should always be present for program path.');
visitor.exit(path);
visitors.delete(path);
},
},
},
};
}