Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): use istanbul-lib-instrument direc…
Browse files Browse the repository at this point in the history
…tly for karma code coverage

The `istanbul-lib-instrument` package provides the required functionality needed
to instrument code for test coverage within the context of the Angular CLI.
Since the build pipeline already contains a customized babel preset, this package
can be integrated directly into the pipeline.
This reduces the number of dependencies required for `@angular-devkit/build-angular`
including the deprecated `inflight` package.
  • Loading branch information
clydin committed Jun 14, 2024
1 parent 9b702e1 commit 6c69854
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
"ansi-colors": "4.1.3",
"autoprefixer": "10.4.19",
"babel-loader": "9.1.3",
"babel-plugin-istanbul": "6.1.1",
"browser-sync": "3.0.2",
"browserslist": "^4.21.5",
"buffer": "6.0.3",
Expand All @@ -144,6 +143,7 @@
"https-proxy-agent": "7.0.4",
"husky": "9.0.11",
"ini": "4.1.3",
"istanbul-lib-instrument": "6.0.2",
"jasmine": "^5.0.0",
"jasmine-core": "~5.1.0",
"jasmine-spec-reporter": "~7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ ts_library(
"@npm//ansi-colors",
"@npm//autoprefixer",
"@npm//babel-loader",
"@npm//babel-plugin-istanbul",
"@npm//browserslist",
"@npm//copy-webpack-plugin",
"@npm//critters",
Expand All @@ -171,6 +170,7 @@ ts_library(
"@npm//fast-glob",
"@npm//http-proxy-middleware",
"@npm//https-proxy-agent",
"@npm//istanbul-lib-instrument",
"@npm//jsonc-parser",
"@npm//karma",
"@npm//karma-source-map-support",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"ansi-colors": "4.1.3",
"autoprefixer": "10.4.19",
"babel-loader": "9.1.3",
"babel-plugin-istanbul": "6.1.1",
"browserslist": "^4.21.5",
"copy-webpack-plugin": "12.0.2",
"critters": "0.0.22",
Expand All @@ -35,6 +34,7 @@
"fast-glob": "3.3.2",
"http-proxy-middleware": "3.0.0",
"https-proxy-agent": "7.0.4",
"istanbul-lib-instrument": "6.0.2",
"jsonc-parser": "3.2.1",
"karma-source-map-support": "1.4.0",
"less": "4.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
},
},
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @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
*/

declare module 'istanbul-lib-instrument' {
export interface Visitor {
enter(path: import('@babel/core').NodePath<types.Program>): void;
exit(path: import('@babel/core').NodePath<types.Program>): void;
}

export function programVisitor(
types: typeof import('@babel/core').types,
filePath?: string,
options?: { inputSourceMap?: object | null },
): Visitor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,7 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
}

if (options.instrumentCode) {
plugins.push([
require('babel-plugin-istanbul').default,
{
inputSourceMap: options.instrumentCode.inputSourceMap ?? false,
cwd: options.instrumentCode.includedBasePath,
},
]);
plugins.push(require('../plugins/add-code-coverage').default);
}

if (needRuntimeTransform) {
Expand Down
97 changes: 29 additions & 68 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ __metadata:
ansi-colors: "npm:4.1.3"
autoprefixer: "npm:10.4.19"
babel-loader: "npm:9.1.3"
babel-plugin-istanbul: "npm:6.1.1"
browserslist: "npm:^4.21.5"
copy-webpack-plugin: "npm:12.0.2"
critters: "npm:0.0.22"
Expand All @@ -94,6 +93,7 @@ __metadata:
fast-glob: "npm:3.3.2"
http-proxy-middleware: "npm:3.0.0"
https-proxy-agent: "npm:7.0.4"
istanbul-lib-instrument: "npm:6.0.2"
jsonc-parser: "npm:3.2.1"
karma-source-map-support: "npm:1.4.0"
less: "npm:4.2.0"
Expand Down Expand Up @@ -689,7 +689,6 @@ __metadata:
ansi-colors: "npm:4.1.3"
autoprefixer: "npm:10.4.19"
babel-loader: "npm:9.1.3"
babel-plugin-istanbul: "npm:6.1.1"
browser-sync: "npm:3.0.2"
browserslist: "npm:^4.21.5"
buffer: "npm:6.0.3"
Expand All @@ -711,6 +710,7 @@ __metadata:
https-proxy-agent: "npm:7.0.4"
husky: "npm:9.0.11"
ini: "npm:4.1.3"
istanbul-lib-instrument: "npm:6.0.2"
jasmine: "npm:^5.0.0"
jasmine-core: "npm:~5.1.0"
jasmine-spec-reporter: "npm:~7.0.0"
Expand Down Expand Up @@ -1019,7 +1019,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/core@npm:7.24.7, @babel/core@npm:^7.12.3, @babel/core@npm:^7.16.0":
"@babel/core@npm:7.24.7, @babel/core@npm:^7.12.3, @babel/core@npm:^7.16.0, @babel/core@npm:^7.23.9":
version: 7.24.7
resolution: "@babel/core@npm:7.24.7"
dependencies:
Expand Down Expand Up @@ -1322,7 +1322,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/parser@npm:^7.1.0, @babel/parser@npm:^7.14.7, @babel/parser@npm:^7.20.7, @babel/parser@npm:^7.24.7":
"@babel/parser@npm:^7.1.0, @babel/parser@npm:^7.14.7, @babel/parser@npm:^7.20.7, @babel/parser@npm:^7.23.9, @babel/parser@npm:^7.24.7":
version: 7.24.7
resolution: "@babel/parser@npm:7.24.7"
bin:
Expand Down Expand Up @@ -3297,20 +3297,7 @@ __metadata:
languageName: node
linkType: hard

"@istanbuljs/load-nyc-config@npm:^1.0.0":
version: 1.1.0
resolution: "@istanbuljs/load-nyc-config@npm:1.1.0"
dependencies:
camelcase: "npm:^5.3.1"
find-up: "npm:^4.1.0"
get-package-type: "npm:^0.1.0"
js-yaml: "npm:^3.13.1"
resolve-from: "npm:^5.0.0"
checksum: 10c0/dd2a8b094887da5a1a2339543a4933d06db2e63cbbc2e288eb6431bd832065df0c099d091b6a67436e71b7d6bf85f01ce7c15f9253b4cbebcc3b9a496165ba42
languageName: node
linkType: hard

"@istanbuljs/schema@npm:^0.1.2":
"@istanbuljs/schema@npm:^0.1.2, @istanbuljs/schema@npm:^0.1.3":
version: 0.1.3
resolution: "@istanbuljs/schema@npm:0.1.3"
checksum: 10c0/61c5286771676c9ca3eb2bd8a7310a9c063fb6e0e9712225c8471c582d157392c88f5353581c8c9adbe0dff98892317d2fdfc56c3499aa42e0194405206a963a
Expand Down Expand Up @@ -7235,7 +7222,14 @@ __metadata:
languageName: node
linkType: hard

"argparse@npm:^1.0.7, argparse@npm:~1.0.9":
"argparse@npm:^2.0.1":
version: 2.0.1
resolution: "argparse@npm:2.0.1"
checksum: 10c0/c5640c2d89045371c7cedd6a70212a04e360fd34d6edeae32f6952c63949e3525ea77dbec0289d8213a99bbaeab5abfa860b5c12cf88a2e6cf8106e90dd27a7e
languageName: node
linkType: hard

"argparse@npm:~1.0.9":
version: 1.0.10
resolution: "argparse@npm:1.0.10"
dependencies:
Expand All @@ -7244,13 +7238,6 @@ __metadata:
languageName: node
linkType: hard

"argparse@npm:^2.0.1":
version: 2.0.1
resolution: "argparse@npm:2.0.1"
checksum: 10c0/c5640c2d89045371c7cedd6a70212a04e360fd34d6edeae32f6952c63949e3525ea77dbec0289d8213a99bbaeab5abfa860b5c12cf88a2e6cf8106e90dd27a7e
languageName: node
linkType: hard

"array-back@npm:^3.0.1, array-back@npm:^3.1.0":
version: 3.1.0
resolution: "array-back@npm:3.1.0"
Expand Down Expand Up @@ -7535,19 +7522,6 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-istanbul@npm:6.1.1":
version: 6.1.1
resolution: "babel-plugin-istanbul@npm:6.1.1"
dependencies:
"@babel/helper-plugin-utils": "npm:^7.0.0"
"@istanbuljs/load-nyc-config": "npm:^1.0.0"
"@istanbuljs/schema": "npm:^0.1.2"
istanbul-lib-instrument: "npm:^5.0.4"
test-exclude: "npm:^6.0.0"
checksum: 10c0/1075657feb705e00fd9463b329921856d3775d9867c5054b449317d39153f8fbcebd3e02ebf00432824e647faff3683a9ca0a941325ef1afe9b3c4dd51b24beb
languageName: node
linkType: hard

"babel-plugin-polyfill-corejs2@npm:^0.4.10":
version: 0.4.11
resolution: "babel-plugin-polyfill-corejs2@npm:0.4.11"
Expand Down Expand Up @@ -8052,7 +8026,7 @@ __metadata:
languageName: node
linkType: hard

"camelcase@npm:^5.0.0, camelcase@npm:^5.3.1":
"camelcase@npm:^5.0.0":
version: 5.3.1
resolution: "camelcase@npm:5.3.1"
checksum: 10c0/92ff9b443bfe8abb15f2b1513ca182d16126359ad4f955ebc83dc4ddcc4ef3fdd2c078bc223f2673dc223488e75c99b16cc4d056624374b799e6a1555cf61b23
Expand Down Expand Up @@ -10082,7 +10056,7 @@ __metadata:
languageName: node
linkType: hard

"esprima@npm:^4.0.0, esprima@npm:^4.0.1":
"esprima@npm:^4.0.1":
version: 4.0.1
resolution: "esprima@npm:4.0.1"
bin:
Expand Down Expand Up @@ -10793,13 +10767,6 @@ __metadata:
languageName: node
linkType: hard

"get-package-type@npm:^0.1.0":
version: 0.1.0
resolution: "get-package-type@npm:0.1.0"
checksum: 10c0/e34cdf447fdf1902a1f6d5af737eaadf606d2ee3518287abde8910e04159368c268568174b2e71102b87b26c2020486f126bfca9c4fb1ceb986ff99b52ecd1be
languageName: node
linkType: hard

"get-stream@npm:^5.1.0":
version: 5.2.0
resolution: "get-stream@npm:5.2.0"
Expand Down Expand Up @@ -12147,7 +12114,20 @@ __metadata:
languageName: node
linkType: hard

"istanbul-lib-instrument@npm:^5.0.4, istanbul-lib-instrument@npm:^5.1.0":
"istanbul-lib-instrument@npm:6.0.2":
version: 6.0.2
resolution: "istanbul-lib-instrument@npm:6.0.2"
dependencies:
"@babel/core": "npm:^7.23.9"
"@babel/parser": "npm:^7.23.9"
"@istanbuljs/schema": "npm:^0.1.3"
istanbul-lib-coverage: "npm:^3.2.0"
semver: "npm:^7.5.4"
checksum: 10c0/405c6ac037bf8c7ee7495980b0cd5544b2c53078c10534d0c9ceeb92a9ea7dcf8510f58ccfce31336458a8fa6ccef27b570bbb602abaa8c1650f5496a807477c
languageName: node
linkType: hard

"istanbul-lib-instrument@npm:^5.1.0":
version: 5.2.1
resolution: "istanbul-lib-instrument@npm:5.2.1"
dependencies:
Expand Down Expand Up @@ -12329,18 +12309,6 @@ __metadata:
languageName: node
linkType: hard

"js-yaml@npm:^3.13.1":
version: 3.14.1
resolution: "js-yaml@npm:3.14.1"
dependencies:
argparse: "npm:^1.0.7"
esprima: "npm:^4.0.0"
bin:
js-yaml: bin/js-yaml.js
checksum: 10c0/6746baaaeac312c4db8e75fa22331d9a04cccb7792d126ed8ce6a0bbcfef0cedaddd0c5098fade53db067c09fe00aa1c957674b4765610a8b06a5a189e46433b
languageName: node
linkType: hard

"jsbn@npm:1.1.0":
version: 1.1.0
resolution: "jsbn@npm:1.1.0"
Expand Down Expand Up @@ -15932,13 +15900,6 @@ __metadata:
languageName: node
linkType: hard

"resolve-from@npm:^5.0.0":
version: 5.0.0
resolution: "resolve-from@npm:5.0.0"
checksum: 10c0/b21cb7f1fb746de8107b9febab60095187781137fd803e6a59a76d421444b1531b641bba5857f5dc011974d8a5c635d61cec49e6bd3b7fc20e01f0fafc4efbf2
languageName: node
linkType: hard

"resolve-path@npm:^1.4.0":
version: 1.4.0
resolution: "resolve-path@npm:1.4.0"
Expand Down

0 comments on commit 6c69854

Please sign in to comment.