monocart-coverage-reports 2.7.4 → 2.7.5

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.
@@ -8,6 +8,12 @@ const BranchTypes = {
8
8
  AssignmentPattern: 'AssignmentPattern'
9
9
  };
10
10
 
11
+ const setGeneratedOnly = (block, generatedOnly) => {
12
+ if (block && generatedOnly) {
13
+ block.generatedOnly = true;
14
+ }
15
+ };
16
+
11
17
  const getParentFunctionState = (reverseParents) => {
12
18
  const parentFunction = reverseParents.find((it) => it._state && it._state.isFunction);
13
19
  if (!parentFunction) {
@@ -266,6 +272,7 @@ const updateBranchCount = (group) => {
266
272
  const groupBlock = getFunctionBlock(group.start, group.end, functionState);
267
273
  if (groupBlock) {
268
274
  parentCount = groupBlock.count;
275
+ setGeneratedOnly(groupBlock, group.generatedOnly);
269
276
  }
270
277
 
271
278
  // calculate branches count
@@ -277,6 +284,7 @@ const updateBranchCount = (group) => {
277
284
  return;
278
285
  }
279
286
  item.block = getFunctionBlock(start, end, functionState);
287
+ setGeneratedOnly(item.block, group.generatedOnly);
280
288
  });
281
289
 
282
290
 
@@ -314,12 +322,16 @@ const generateBranches = (branchMap) => {
314
322
  }
315
323
  });
316
324
 
317
- branches.push({
325
+ const branch = {
318
326
  type: group.type,
319
327
  start: group.start,
320
328
  end: group.end,
321
329
  locations: group.locations
322
- });
330
+ };
331
+
332
+ setGeneratedOnly(branch, group.generatedOnly);
333
+
334
+ branches.push(branch);
323
335
  });
324
336
 
325
337
  // sort branches
@@ -395,6 +407,34 @@ const generateStatements = (statementNodes) => {
395
407
  return statements;
396
408
  };
397
409
 
410
+ // =======================================================================================
411
+ // handle special generated codes for original coverage
412
+
413
+ const webpackWrapHandler = (node) => {
414
+ // ignore webpack wrap for original function
415
+ // id: { type: 'Identifier', name: '__webpack_modules__' },
416
+ const name = node.id && node.id.name;
417
+ if (name === '__webpack_modules__' && node.init && node.init.properties) {
418
+ // mark all as wrap function
419
+ node.init.properties.forEach((it) => {
420
+ it.value._webpackWrapKey = it.key && it.key.value;
421
+ });
422
+ // console.log('==========================', node.type);
423
+ // console.log(name, parents.length);
424
+ }
425
+ };
426
+
427
+ const viteImportDefaultHandler = (node, group) => {
428
+ // ignore vite conditional expression for __esModule default
429
+ const testObj = node.test.object;
430
+ const testName = testObj && testObj.name;
431
+ if (testName && testName.startsWith('__vite__cjsImport')) {
432
+ setGeneratedOnly(group, true);
433
+ }
434
+
435
+ };
436
+
437
+
398
438
  // =======================================================================================
399
439
 
400
440
  const collectNodes = (ast) => {
@@ -409,18 +449,7 @@ const collectNodes = (ast) => {
409
449
 
410
450
  VariableDeclarator(node, parents) {
411
451
 
412
- // =============================================================
413
- // id: { type: 'Identifier', name: '__webpack_modules__' },
414
- const name = node.id && node.id.name;
415
- if (name === '__webpack_modules__' && node.init && node.init.properties) {
416
- // mark all as wrap function
417
- node.init.properties.forEach((it) => {
418
- it.value._wrapKey = it.key && it.key.value;
419
- });
420
- // console.log('==========================', node.type);
421
- // console.log(name, parents.length);
422
- }
423
- // =============================================================
452
+ webpackWrapHandler(node);
424
453
 
425
454
  },
426
455
 
@@ -473,10 +502,14 @@ const collectNodes = (ast) => {
473
502
  // Ternary
474
503
  // var b = a ? 'consequent' : 'alternate';
475
504
  ConditionalExpression: (node, parents) => {
505
+
476
506
  const { consequent, alternate } = node;
477
507
  const group = createBranchGroup(BranchTypes.ConditionalExpression, node, parents, branchMap);
478
508
  addBranch(group, consequent, locationMap);
479
509
  addBranch(group, alternate, locationMap);
510
+
511
+ viteImportDefaultHandler(node, group);
512
+
480
513
  },
481
514
 
482
515
  // if an if statement; can also be else if.
@@ -619,7 +652,7 @@ const collectAstInfo = (ast, coverageInfo) => {
619
652
  const bodyStart = node.body.start;
620
653
  const bodyEnd = node.body.end;
621
654
  const functionName = id && id.name;
622
- const isWrap = Boolean(node._wrapKey);
655
+ const _webpackWrapKey = node._webpackWrapKey;
623
656
 
624
657
  const functionItem = {
625
658
  start,
@@ -629,9 +662,9 @@ const collectAstInfo = (ast, coverageInfo) => {
629
662
  functionName,
630
663
  count: rootState.count
631
664
  };
632
- if (isWrap) {
633
- functionItem.wrap = true;
634
- }
665
+
666
+ setGeneratedOnly(functionItem, _webpackWrapKey);
667
+
635
668
  functions.push(functionItem);
636
669
 
637
670
  const functionState = {
@@ -641,9 +674,9 @@ const collectAstInfo = (ast, coverageInfo) => {
641
674
 
642
675
  const functionRange = findFunctionRange(item, coverageInfo);
643
676
  if (functionRange) {
644
- if (isWrap) {
645
- functionRange.wrap = true;
646
- }
677
+
678
+ setGeneratedOnly(functionRange, _webpackWrapKey);
679
+
647
680
  functionState.range = functionRange;
648
681
  functionState.count = functionRange.count;
649
682
  functionItem.count = functionRange.count;
@@ -28,7 +28,14 @@ const loadSourceMap = async (url = '') => {
28
28
  return;
29
29
  }
30
30
 
31
- return res.data;
31
+ const content = res.data;
32
+
33
+ // could be string not json, if Content-Type is application/octet-stream
34
+ if (typeof content === 'string') {
35
+ return JSON.parse(content);
36
+ }
37
+
38
+ return content;
32
39
  };
33
40
 
34
41
  const getSourceMapUrl = (content, url) => {
@@ -510,6 +510,11 @@ const handleOriginalBytesCoverage = (state) => {
510
510
 
511
511
  const handleGeneratedBytesCoverage = (state) => {
512
512
 
513
+ // it could be a dist file, do not handle twice
514
+ if (state.addedGeneratedBytes) {
515
+ return;
516
+ }
517
+
513
518
  const { js, coverageList } = state;
514
519
 
515
520
  if (js) {
@@ -524,6 +529,8 @@ const handleGeneratedBytesCoverage = (state) => {
524
529
  });
525
530
  }
526
531
 
532
+ state.addedGeneratedBytes = true;
533
+
527
534
  };
528
535
 
529
536
  // ========================================================================================================
@@ -551,15 +558,16 @@ const handleOriginalFunctionsCoverage = (state, originalStateMap) => {
551
558
 
552
559
  // function count
553
560
  state.astInfo.functions.forEach((it) => {
554
- const {
555
- start, end, bodyStart, bodyEnd, wrap
556
- } = it;
557
561
 
558
562
  // remove webpack wrap functions for functions count, not for ranges here
559
- if (wrap) {
563
+ if (it.generatedOnly) {
560
564
  return;
561
565
  }
562
566
 
567
+ const {
568
+ start, end, bodyStart, bodyEnd
569
+ } = it;
570
+
563
571
  const result = findOriginalRange(start, end, state, originalStateMap);
564
572
  if (result.error) {
565
573
  logMappingErrors('function', result);
@@ -598,6 +606,10 @@ const handleOriginalBranchesCoverage = (state, originalStateMap) => {
598
606
  // function count
599
607
  state.astInfo.branches.forEach((group) => {
600
608
 
609
+ if (group.generatedOnly) {
610
+ return;
611
+ }
612
+
601
613
  const { type, locations } = group;
602
614
 
603
615
  // start
@@ -668,6 +680,11 @@ const handleOriginalStatementsCoverage = (state, originalStateMap) => {
668
680
 
669
681
  // statement count
670
682
  state.astInfo.statements.forEach((it) => {
683
+
684
+ if (it.generatedOnly) {
685
+ return;
686
+ }
687
+
671
688
  const { start, end } = it;
672
689
 
673
690
  const result = findOriginalRange(start, end, state, originalStateMap);
@@ -799,11 +816,10 @@ const handleAllOriginalBytesCoverage = (state, originalStateMap) => {
799
816
 
800
817
  // v8 coverage
801
818
  coverageList.forEach((block) => {
802
- block.ranges.forEach((range, index) => {
819
+ block.ranges.forEach((range) => {
803
820
 
804
821
  // remove wrap functions for original files
805
- if (range.wrap) {
806
- // console.log(range);
822
+ if (range.generatedOnly) {
807
823
  return;
808
824
  }
809
825
 
@@ -119,8 +119,32 @@ const alignText = (gt, ot) => {
119
119
  return gl;
120
120
  };
121
121
 
122
+ const getWordEndPosition = (list, gp, direction) => {
123
+ // for end only
124
+ if (direction !== 'end') {
125
+ return;
126
+ }
127
+
128
+ const prev = list[gp - 1];
129
+ if (prev && prev.original && prev.wordEnd) {
130
+ const op = prev.original.index + 1;
131
+ // console.log(gp, JSON.stringify(gt));
132
+ // console.log(op, JSON.stringify(ot));
133
+ return {
134
+ pos: op
135
+ };
136
+ }
137
+ };
138
+
122
139
  const getAlignPosition = (gt, gp, ot, direction) => {
123
140
 
141
+ // there is no need to align for long text
142
+ const maxLength = 500;
143
+ if (gt.length > maxLength || ot.length > maxLength) {
144
+ // console.log(gt.length, ot.length, gp);
145
+ return;
146
+ }
147
+
124
148
  // generatedText: '"false";\n',
125
149
  // originalText: "'false';\r\n ",
126
150
  // generatedPos: 7
@@ -147,20 +171,7 @@ const getAlignPosition = (gt, gp, ot, direction) => {
147
171
  }
148
172
 
149
173
  // inclusive, for end only
150
- if (gp && direction === 'end') {
151
- const prev = list[gp - 1];
152
- if (prev && prev.original && prev.wordEnd) {
153
- // console.log(direction, '=====================================================================');
154
- const op = prev.original.index + 1;
155
- // console.log(gp, JSON.stringify(gt));
156
- // console.log(op, JSON.stringify(ot));
157
-
158
- return {
159
- pos: op
160
- };
161
-
162
- }
163
- }
174
+ return getWordEndPosition(list, gp, direction);
164
175
 
165
176
  };
166
177
 
@@ -252,13 +263,19 @@ const getComparedPosition = (info, direction) => {
252
263
  const gt = info.generatedText;
253
264
  const gp = info.generatedPos;
254
265
  const ot = info.originalText;
266
+ const olt = info.originalLineText;
267
+
268
+ // seems direction is start only
269
+ if (gp === 0) {
270
+ return;
271
+ }
255
272
 
256
273
  // whole generated text
257
274
  if (gp >= gt.length) {
258
275
  // console.log(JSON.stringify(gt), gt.length, gp);
259
276
  // console.log(JSON.stringify(ot));
260
277
  return {
261
- pos: info.originalLineText.length
278
+ pos: olt.length
262
279
  };
263
280
  }
264
281
 
package/package.json CHANGED
@@ -1,97 +1,98 @@
1
- {
2
- "name": "monocart-coverage-reports",
3
- "version": "2.7.4",
4
- "description": "A code coverage tool to generate native V8 reports or Istanbul reports.",
5
- "main": "./lib/index.js",
6
- "bin": {
7
- "mcr": "./lib/cli.js"
8
- },
9
- "exports": {
10
- ".": {
11
- "types": "./lib/index.d.ts",
12
- "import": "./lib/index.mjs",
13
- "require": "./lib/index.js",
14
- "default": "./lib/index.js"
15
- },
16
- "./register": {
17
- "import": "./lib/register/register.mjs",
18
- "require": "./lib/register/register.js",
19
- "default": "./lib/register/register.js"
20
- },
21
- "./converter": "./lib/converter/converter.js",
22
- "./util": "./lib/utils/util.js",
23
- "./package.json": "./package.json"
24
- },
25
- "types": "./lib/index.d.ts",
26
- "scripts": {
27
- "build-docs": "node ./scripts/build-docs.js",
28
- "build-test": "node ./scripts/build-test.js",
29
- "build": "sf lint && sf b -p && npm run build-test",
30
- "test-node-env": "cross-env NODE_V8_COVERAGE=.temp/v8-coverage-env node ./test/test-node-env.js && node ./test/generate-report.js",
31
- "test-node-api": "cross-env NODE_V8_COVERAGE=.temp/v8-coverage-api node ./test/test-node-api.js",
32
- "test-node-ins": "node ./test/test-node-ins.js",
33
- "test-node-cdp": "node --inspect=9229 ./test/test-node-cdp.js",
34
- "test-node-vm": "node ./test/test-node-vm.js",
35
- "test-node-koa": "node ./test/test-node-koa.js",
36
- "test-node-fgc": "node ./test/test-node-fgc.js",
37
- "test-node": "npm run test-node-env && npm run test-node-api && npm run test-node-ins && npm run test-node-cdp && npm run test-node-vm && npm run test-node-koa",
38
- "test-istanbul": "node ./test/test-istanbul.js",
39
- "test-v8": "node ./test/test-v8.js",
40
- "test-puppeteer": "node ./test/test-puppeteer.js",
41
- "test-anonymous": "node ./test/test-anonymous.js",
42
- "test-css": "node ./test/test-css.js",
43
- "test-minify": "node ./test/test-minify.js",
44
- "test-esbuild": "node ./test/test-esbuild.js",
45
- "test-rollup": "node ./test/test-rollup.js",
46
- "test-v8-and-istanbul": "node ./test/test-v8-and-istanbul.js",
47
- "test-browser": "npm run test-istanbul && npm run test-v8 && npm run test-puppeteer && npm run test-anonymous && npm run test-css && npm run test-minify && npm run test-esbuild && npm run test-rollup && npm run test-v8-and-istanbul",
48
- "test-cli": "npx mcr node ./test/specs/node.test.js -c test/mcr.config.cli.js",
49
- "test-tsx": "npx mcr tsx ./test/specs/tsx.test.ts -c test/mcr.config.tsx.js",
50
- "test-merge": "node ./test/test-merge.js",
51
- "test-merge-istanbul": "node ./test/test-merge-istanbul.js",
52
- "test-client": "node ./test/test-client.js",
53
- "test-all": "npm run test-node && npm run test-browser && npm run test-cli && npm run test-tsx && npm run test-merge && npm run build-docs",
54
- "test": "npx mcr npm run test-all -c test/mcr.config.mcr.js",
55
- "test:snap": "cross-env TEST_SNAPSHOT=true npm run test",
56
- "dev": "sf d v8",
57
- "open": "node ./scripts/open.js",
58
- "patch": "npm run build && npm run test && sf publish patch -r"
59
- },
60
- "workspaces": [
61
- "packages/*",
62
- "test"
63
- ],
64
- "files": [
65
- "lib"
66
- ],
67
- "license": "MIT",
68
- "repository": {
69
- "type": "git",
70
- "url": "git+https://github.com/cenfun/monocart-coverage-reports.git"
71
- },
72
- "dependencies": {
73
- "console-grid": "^2.2.2",
74
- "eight-colors": "^1.3.0",
75
- "istanbul-lib-coverage": "^3.2.2",
76
- "istanbul-lib-report": "^3.0.1",
77
- "istanbul-reports": "^3.1.7",
78
- "lz-utils": "^2.0.2",
79
- "monocart-code-viewer": "^1.1.2",
80
- "monocart-formatter": "^2.3.2",
81
- "turbogrid": "^3.0.13"
82
- },
83
- "devDependencies": {
84
- "commander": "^12.0.0",
85
- "esbuild": "^0.20.2",
86
- "eslint": "^8.57.0",
87
- "eslint-config-plus": "^1.0.6",
88
- "eslint-plugin-html": "^8.0.0",
89
- "eslint-plugin-vue": "^9.23.0",
90
- "minimatch": "^9.0.3",
91
- "stylelint": "^16.2.1",
92
- "stylelint-config-plus": "^1.1.0",
93
- "supports-color": "^9.4.0",
94
- "tsx": "^4.7.1",
95
- "ws": "^8.16.0"
96
- }
97
- }
1
+ {
2
+ "name": "monocart-coverage-reports",
3
+ "version": "2.7.5",
4
+ "description": "A code coverage tool to generate native V8 reports or Istanbul reports.",
5
+ "main": "./lib/index.js",
6
+ "bin": {
7
+ "mcr": "./lib/cli.js"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "types": "./lib/index.d.ts",
12
+ "import": "./lib/index.mjs",
13
+ "require": "./lib/index.js",
14
+ "default": "./lib/index.js"
15
+ },
16
+ "./register": {
17
+ "import": "./lib/register/register.mjs",
18
+ "require": "./lib/register/register.js",
19
+ "default": "./lib/register/register.js"
20
+ },
21
+ "./converter": "./lib/converter/converter.js",
22
+ "./util": "./lib/utils/util.js",
23
+ "./package.json": "./package.json"
24
+ },
25
+ "types": "./lib/index.d.ts",
26
+ "scripts": {
27
+ "build-docs": "node ./scripts/build-docs.js",
28
+ "build-test": "node ./scripts/build-test.js",
29
+ "build": "sf lint && sf b -p && npm run build-test",
30
+ "test-node-env": "cross-env NODE_V8_COVERAGE=.temp/v8-coverage-env node ./test/test-node-env.js && node ./test/generate-report.js",
31
+ "test-node-api": "cross-env NODE_V8_COVERAGE=.temp/v8-coverage-api node ./test/test-node-api.js",
32
+ "test-node-ins": "node ./test/test-node-ins.js",
33
+ "test-node-cdp": "node --inspect=9229 ./test/test-node-cdp.js",
34
+ "test-node-vm": "node ./test/test-node-vm.js",
35
+ "test-node-koa": "node ./test/test-node-koa.js",
36
+ "test-node-fgc": "node ./test/test-node-fgc.js",
37
+ "test-node": "npm run test-node-env && npm run test-node-api && npm run test-node-ins && npm run test-node-cdp && npm run test-node-vm && npm run test-node-koa",
38
+ "test-istanbul": "node ./test/test-istanbul.js",
39
+ "test-v8": "node ./test/test-v8.js",
40
+ "test-puppeteer": "node ./test/test-puppeteer.js",
41
+ "test-anonymous": "node ./test/test-anonymous.js",
42
+ "test-css": "node ./test/test-css.js",
43
+ "test-minify": "node ./test/test-minify.js",
44
+ "test-esbuild": "node ./test/test-esbuild.js",
45
+ "test-rollup": "node ./test/test-rollup.js",
46
+ "test-v8-and-istanbul": "node ./test/test-v8-and-istanbul.js",
47
+ "test-browser": "npm run test-istanbul && npm run test-v8 && npm run test-puppeteer && npm run test-anonymous && npm run test-css && npm run test-minify && npm run test-esbuild && npm run test-rollup && npm run test-v8-and-istanbul",
48
+ "test-cli": "npx mcr node ./test/specs/node.test.js -c test/mcr.config.cli.js",
49
+ "test-tsx": "npx mcr tsx ./test/specs/tsx.test.ts -c test/mcr.config.tsx.js",
50
+ "test-demo": "node ./test/test-demo.js",
51
+ "test-merge": "node ./test/test-merge.js",
52
+ "test-merge-istanbul": "node ./test/test-merge-istanbul.js",
53
+ "test-client": "node ./test/test-client.js",
54
+ "test-all": "npm run test-node && npm run test-browser && npm run test-cli && npm run test-tsx && npm run test-merge && npm run build-docs",
55
+ "test": "npx mcr npm run test-all -c test/mcr.config.mcr.js",
56
+ "test:snap": "cross-env TEST_SNAPSHOT=true npm run test",
57
+ "dev": "sf d v8",
58
+ "open": "node ./scripts/open.js",
59
+ "patch": "npm run build && npm run test && sf publish patch -r"
60
+ },
61
+ "workspaces": [
62
+ "packages/*",
63
+ "test"
64
+ ],
65
+ "files": [
66
+ "lib"
67
+ ],
68
+ "license": "MIT",
69
+ "repository": {
70
+ "type": "git",
71
+ "url": "git+https://github.com/cenfun/monocart-coverage-reports.git"
72
+ },
73
+ "dependencies": {
74
+ "console-grid": "^2.2.2",
75
+ "eight-colors": "^1.3.0",
76
+ "istanbul-lib-coverage": "^3.2.2",
77
+ "istanbul-lib-report": "^3.0.1",
78
+ "istanbul-reports": "^3.1.7",
79
+ "lz-utils": "^2.0.2",
80
+ "monocart-code-viewer": "^1.1.2",
81
+ "monocart-formatter": "^2.3.2",
82
+ "turbogrid": "^3.0.13"
83
+ },
84
+ "devDependencies": {
85
+ "commander": "^12.0.0",
86
+ "esbuild": "^0.20.2",
87
+ "eslint": "^8.57.0",
88
+ "eslint-config-plus": "^1.0.6",
89
+ "eslint-plugin-html": "^8.0.0",
90
+ "eslint-plugin-vue": "^9.23.0",
91
+ "minimatch": "^9.0.3",
92
+ "stylelint": "^16.2.1",
93
+ "stylelint-config-plus": "^1.1.0",
94
+ "supports-color": "^9.4.0",
95
+ "tsx": "^4.7.1",
96
+ "ws": "^8.16.0"
97
+ }
98
+ }