@teamscale/lib-instrument 0.1.0-beta.8 → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +2 -2
- package/lib/instrumenter.js +3 -3
- package/lib/origins.js +3 -2
- package/lib/visitor.d.ts +1 -1
- package/lib/visitor.d.ts.map +1 -1
- package/lib/visitor.js +38 -14
- package/package.json +12 -16
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.programVisitor = void 0;
|
|
4
|
+
exports.createInstrumenter = createInstrumenter;
|
|
4
5
|
const instrumenter_1 = require("./instrumenter");
|
|
5
6
|
var visitor_1 = require("./visitor");
|
|
6
7
|
Object.defineProperty(exports, "programVisitor", { enumerable: true, get: function () { return visitor_1.programVisitor; } });
|
|
@@ -12,4 +13,3 @@ Object.defineProperty(exports, "programVisitor", { enumerable: true, get: functi
|
|
|
12
13
|
function createInstrumenter(opts) {
|
|
13
14
|
return new instrumenter_1.Instrumenter(opts);
|
|
14
15
|
}
|
|
15
|
-
exports.createInstrumenter = createInstrumenter;
|
package/lib/instrumenter.js
CHANGED
|
@@ -37,9 +37,9 @@ class Instrumenter {
|
|
|
37
37
|
* @returns the instrumented code.
|
|
38
38
|
*/
|
|
39
39
|
async instrument(code, filename, inputSourceMap, shouldInstrumentCallback) {
|
|
40
|
-
filename = filename
|
|
40
|
+
filename = filename !== null && filename !== void 0 ? filename : String(new Date().getTime()) + '.js';
|
|
41
41
|
const { opts } = this;
|
|
42
|
-
const sourceMapToUse = inputSourceMap
|
|
42
|
+
const sourceMapToUse = inputSourceMap !== null && inputSourceMap !== void 0 ? inputSourceMap : opts.inputSourceMap;
|
|
43
43
|
let inputSourceMapConsumer = undefined;
|
|
44
44
|
if (sourceMapToUse) {
|
|
45
45
|
inputSourceMapConsumer = await new source_map_1.SourceMapConsumer(sourceMapToUse);
|
|
@@ -69,7 +69,7 @@ class Instrumenter {
|
|
|
69
69
|
inputSourceMap,
|
|
70
70
|
isInstrumentedToken: opts.isInstrumentedToken,
|
|
71
71
|
codeToPrepend: opts.codeToPrepend,
|
|
72
|
-
shouldInstrumentCallback: shouldInstrumentCallback
|
|
72
|
+
shouldInstrumentCallback: shouldInstrumentCallback !== null && shouldInstrumentCallback !== void 0 ? shouldInstrumentCallback : opts.shouldInstrumentCallback
|
|
73
73
|
});
|
|
74
74
|
return {
|
|
75
75
|
visitor: {
|
package/lib/origins.js
CHANGED
|
@@ -43,13 +43,14 @@ class SourceOrigins {
|
|
|
43
43
|
* the given location to the location in the origin.
|
|
44
44
|
*/
|
|
45
45
|
ensureKnownOrigin(loc) {
|
|
46
|
+
var _a, _b;
|
|
46
47
|
let startPos = undefined;
|
|
47
48
|
let endPos = undefined;
|
|
48
|
-
let filename = loc.filename
|
|
49
|
+
let filename = (_a = loc.filename) !== null && _a !== void 0 ? _a : '';
|
|
49
50
|
if (this.sourceMap) {
|
|
50
51
|
startPos = this.sourceMap.originalPositionFor({ line: loc.start.line, column: loc.start.column });
|
|
51
52
|
endPos = this.sourceMap.originalPositionFor({ line: loc.end.line, column: loc.end.column });
|
|
52
|
-
filename = startPos.source
|
|
53
|
+
filename = (_b = startPos.source) !== null && _b !== void 0 ? _b : loc.filename;
|
|
53
54
|
}
|
|
54
55
|
if (!startPos || !endPos) {
|
|
55
56
|
startPos = { line: loc.start.line, column: loc.start.column, source: null, name: null };
|
package/lib/visitor.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ import { InstrumentationOptions } from "./utils";
|
|
|
19
19
|
* `sourceMappingURL` - any source mapping URL found when processing the file.
|
|
20
20
|
*
|
|
21
21
|
* @param types - an instance of babel-types.
|
|
22
|
-
* @param
|
|
22
|
+
* @param inputSourceMapConsumer - access object for the source map of the input.
|
|
23
23
|
* @param opts - additional options.
|
|
24
24
|
*/
|
|
25
25
|
export declare function programVisitor(types: BabelTypes, inputSourceMapConsumer: SourceMapConsumer | undefined, opts: InstrumentationOptions): {
|
package/lib/visitor.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"visitor.d.ts","sourceRoot":"","sources":["../src/visitor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,QAAQ,EAAQ,MAAM,aAAa,CAAC;AAElD,OAAO,EAK8C,OAAO,EAE3D,MAAM,cAAc,CAAC;AAEtB,KAAK,UAAU,GAAG,cAAc,cAAc,CAAC,CAAA;AAE/C,OAAO,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAG7C,OAAO,EAAC,sBAAsB,EAAC,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"visitor.d.ts","sourceRoot":"","sources":["../src/visitor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,QAAQ,EAAQ,MAAM,aAAa,CAAC;AAElD,OAAO,EAK8C,OAAO,EAE3D,MAAM,cAAc,CAAC;AAEtB,KAAK,UAAU,GAAG,cAAc,cAAc,CAAC,CAAA;AAE/C,OAAO,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAG7C,OAAO,EAAC,sBAAsB,EAAC,MAAM,SAAS,CAAC;AAsmB/C;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,EACjB,sBAAsB,EAAE,iBAAiB,GAAG,SAAS,EACrD,IAAI,EAAE,sBAAsB;gBAYvC,QAAQ,CAAC,OAAO,CAAC,GAAG,IAAI;eASzB,QAAQ,CAAC,OAAO,CAAC;EA+BnC"}
|
package/lib/visitor.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.programVisitor =
|
|
3
|
+
exports.programVisitor = programVisitor;
|
|
4
4
|
const core_1 = require("@babel/core");
|
|
5
5
|
const origins_1 = require("./origins");
|
|
6
6
|
// Pattern for istanbul to ignore a section
|
|
@@ -112,6 +112,7 @@ class VisitState {
|
|
|
112
112
|
return c[name];
|
|
113
113
|
}
|
|
114
114
|
insertCounter(path, increment) {
|
|
115
|
+
var _a, _b;
|
|
115
116
|
const T = this.types;
|
|
116
117
|
if (path.isBlockStatement()) {
|
|
117
118
|
path.node.body.unshift(T.expressionStatement(increment));
|
|
@@ -123,9 +124,9 @@ class VisitState {
|
|
|
123
124
|
T.isVariableDeclarator(path.parent)) {
|
|
124
125
|
// make an attempt to hoist the statement counter, so that
|
|
125
126
|
// function names are maintained.
|
|
126
|
-
const grandParentPath = path.parentPath
|
|
127
|
+
const grandParentPath = (_a = path.parentPath) === null || _a === void 0 ? void 0 : _a.parentPath;
|
|
127
128
|
if (grandParentPath && T.isExportNamedDeclaration(grandParentPath.parent)) {
|
|
128
|
-
grandParentPath.parentPath
|
|
129
|
+
(_b = grandParentPath.parentPath) === null || _b === void 0 ? void 0 : _b.insertBefore(T.expressionStatement(increment));
|
|
129
130
|
}
|
|
130
131
|
else if (grandParentPath &&
|
|
131
132
|
(T.isProgram(grandParentPath.parent) ||
|
|
@@ -144,8 +145,9 @@ class VisitState {
|
|
|
144
145
|
}
|
|
145
146
|
}
|
|
146
147
|
insertFunctionCounter(path) {
|
|
148
|
+
var _a, _b, _c;
|
|
147
149
|
const T = this.types;
|
|
148
|
-
if (!(path.node
|
|
150
|
+
if (!((_a = path.node) === null || _a === void 0 ? void 0 : _a.loc)) {
|
|
149
151
|
return;
|
|
150
152
|
}
|
|
151
153
|
const n = path.node;
|
|
@@ -154,12 +156,12 @@ class VisitState {
|
|
|
154
156
|
case 'FunctionDeclaration':
|
|
155
157
|
case 'FunctionExpression':
|
|
156
158
|
if (n.id) {
|
|
157
|
-
declarationLocation = n.id.loc
|
|
159
|
+
declarationLocation = (_b = n.id.loc) !== null && _b !== void 0 ? _b : undefined;
|
|
158
160
|
}
|
|
159
161
|
break;
|
|
160
162
|
}
|
|
161
163
|
const body = path.get('body');
|
|
162
|
-
const loc = path.node.loc
|
|
164
|
+
const loc = (_c = path.node.loc) !== null && _c !== void 0 ? _c : declarationLocation;
|
|
163
165
|
const [originFileId, originPos] = this.origins.ensureKnownOrigin(loc);
|
|
164
166
|
if (body.isBlockStatement() && this.shouldInstrument(path, originPos)) {
|
|
165
167
|
// For functions, we only cover the first line of its body.
|
|
@@ -169,7 +171,8 @@ class VisitState {
|
|
|
169
171
|
}
|
|
170
172
|
}
|
|
171
173
|
insertStatementCounter(path) {
|
|
172
|
-
|
|
174
|
+
var _a;
|
|
175
|
+
const loc = (_a = path.node) === null || _a === void 0 ? void 0 : _a.loc;
|
|
173
176
|
if (!loc) {
|
|
174
177
|
return;
|
|
175
178
|
}
|
|
@@ -181,7 +184,7 @@ class VisitState {
|
|
|
181
184
|
this.insertCounter(path, increment);
|
|
182
185
|
}
|
|
183
186
|
insertBranchCounter(path, loc) {
|
|
184
|
-
loc = loc
|
|
187
|
+
loc = loc !== null && loc !== void 0 ? loc : path.node.loc;
|
|
185
188
|
if (!loc) {
|
|
186
189
|
return;
|
|
187
190
|
}
|
|
@@ -215,7 +218,7 @@ class VisitState {
|
|
|
215
218
|
* Create a line coverage reporting statement node.
|
|
216
219
|
*/
|
|
217
220
|
function newLineCoverageExpression(originFileId, range) {
|
|
218
|
-
|
|
221
|
+
const argumentList = [
|
|
219
222
|
{ type: 'Identifier', name: originFileId },
|
|
220
223
|
{ type: 'NumericLiteral', value: range.start.line }
|
|
221
224
|
];
|
|
@@ -296,12 +299,33 @@ function coverVariableDeclarator(path) {
|
|
|
296
299
|
function coverClassPropDeclarator(path) {
|
|
297
300
|
this.insertStatementCounter(path.get('value'));
|
|
298
301
|
}
|
|
302
|
+
function coverSequenceExpression(path) {
|
|
303
|
+
const T = this.types;
|
|
304
|
+
if (!path.isSequenceExpression()) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const newExpressions = [];
|
|
308
|
+
for (const expression of path.node.expressions) {
|
|
309
|
+
const [originFileId, originPos] = this.origins.ensureKnownOrigin(expression.loc);
|
|
310
|
+
if (this.shouldInstrument(path, originPos)) {
|
|
311
|
+
const increment = newLineCoverageExpression(originFileId, originPos);
|
|
312
|
+
newExpressions.push(increment);
|
|
313
|
+
}
|
|
314
|
+
// We must add the expression to be evaluated after the coverage increment
|
|
315
|
+
// to not change the return value of the sequence expression.
|
|
316
|
+
newExpressions.push(expression);
|
|
317
|
+
}
|
|
318
|
+
path.replaceWith(T.sequenceExpression(newExpressions));
|
|
319
|
+
}
|
|
299
320
|
function makeBlock(path) {
|
|
300
321
|
const T = this.types;
|
|
301
322
|
if (!path.node) {
|
|
302
323
|
path.replaceWith(T.blockStatement([]));
|
|
303
324
|
}
|
|
304
|
-
if (
|
|
325
|
+
if (path.isSequenceExpression()) {
|
|
326
|
+
coverSequenceExpression.call(this, path);
|
|
327
|
+
}
|
|
328
|
+
else if (!path.isBlockStatement()) {
|
|
305
329
|
path.replaceWith(T.blockStatement([path.node]));
|
|
306
330
|
const block = path.node;
|
|
307
331
|
path.node.loc = block.body[0].loc;
|
|
@@ -448,7 +472,8 @@ const codeVisitor = {
|
|
|
448
472
|
FunctionExpression: entries(coverFunction),
|
|
449
473
|
LabeledStatement: entries(coverStatement),
|
|
450
474
|
ConditionalExpression: entries(coverTernary),
|
|
451
|
-
LogicalExpression: entries(coverLogicalExpression)
|
|
475
|
+
LogicalExpression: entries(coverLogicalExpression),
|
|
476
|
+
SequenceExpression: entries(coverSequenceExpression),
|
|
452
477
|
};
|
|
453
478
|
/**
|
|
454
479
|
* The rewire plugin (and potentially other babel middleware)
|
|
@@ -461,7 +486,7 @@ function alreadyInstrumented(path, visitState) {
|
|
|
461
486
|
return path.scope.hasBinding(visitState.varName);
|
|
462
487
|
}
|
|
463
488
|
function getParentComments(path) {
|
|
464
|
-
if (!path
|
|
489
|
+
if (!(path === null || path === void 0 ? void 0 : path.parent)) {
|
|
465
490
|
return [];
|
|
466
491
|
}
|
|
467
492
|
if (!('comments' in path.parent)) {
|
|
@@ -492,7 +517,7 @@ function shouldIgnoreFile(programNodePath) {
|
|
|
492
517
|
* `sourceMappingURL` - any source mapping URL found when processing the file.
|
|
493
518
|
*
|
|
494
519
|
* @param types - an instance of babel-types.
|
|
495
|
-
* @param
|
|
520
|
+
* @param inputSourceMapConsumer - access object for the source map of the input.
|
|
496
521
|
* @param opts - additional options.
|
|
497
522
|
*/
|
|
498
523
|
function programVisitor(types, inputSourceMapConsumer, opts) {
|
|
@@ -535,4 +560,3 @@ function programVisitor(types, inputSourceMapConsumer, opts) {
|
|
|
535
560
|
}
|
|
536
561
|
};
|
|
537
562
|
}
|
|
538
|
-
exports.programVisitor = programVisitor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teamscale/lib-instrument",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
4
|
"description": "Library for adding coverage statements to JS code; forked from istanbul-lib-coverage",
|
|
5
5
|
"author": "Krishnan Anantheswaran <kananthmail-github@yahoo.com>",
|
|
6
6
|
"maintainers": [
|
|
@@ -14,33 +14,29 @@
|
|
|
14
14
|
"lib"
|
|
15
15
|
],
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@babel/core": "^7.
|
|
18
|
-
"@babel/parser": "^7.
|
|
19
|
-
"@babel/traverse": "^7.
|
|
20
|
-
"@types/node": "^
|
|
17
|
+
"@babel/core": "^7.26.8",
|
|
18
|
+
"@babel/parser": "^7.26.8",
|
|
19
|
+
"@babel/traverse": "^7.26.8",
|
|
20
|
+
"@types/node": "^22.13.4",
|
|
21
21
|
"source-map": "^0.7.4",
|
|
22
|
-
"typescript": "^5.
|
|
22
|
+
"typescript": "^5.7.3"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@babel/cli": "^7.
|
|
26
|
-
"@babel/types": "^7.
|
|
25
|
+
"@babel/cli": "^7.26.4",
|
|
26
|
+
"@babel/types": "^7.26.8",
|
|
27
27
|
"@types/babel__core": "^7.20.5",
|
|
28
28
|
"@types/clone": "^2.1.4",
|
|
29
29
|
"@types/js-yaml": "^4.0.9",
|
|
30
|
-
"@types/jest": "^29.5.
|
|
30
|
+
"@types/jest": "^29.5.14",
|
|
31
31
|
"babel-jest": "^29.7.0",
|
|
32
32
|
"clone": "^2.1.2",
|
|
33
33
|
"js-yaml": "^4.1.0",
|
|
34
|
-
"ts-jest": "^29.
|
|
34
|
+
"ts-jest": "^29.2.5",
|
|
35
35
|
"jest": "^29.7.0",
|
|
36
|
-
"rimraf": "^
|
|
36
|
+
"rimraf": "^6.0.1"
|
|
37
37
|
},
|
|
38
38
|
"license": "BSD-3-Clause",
|
|
39
|
-
"
|
|
40
|
-
"type": "git",
|
|
41
|
-
"url": "https://github.com/cqse/teamscale-javascript-profiler.git",
|
|
42
|
-
"directory": "packages/lib-instrument"
|
|
43
|
-
},
|
|
39
|
+
"homepage": "https://docs.teamscale.com/howto/setting-up-profiler-tga/javascript/",
|
|
44
40
|
"publishConfig": {
|
|
45
41
|
"access": "public"
|
|
46
42
|
},
|