monocart-coverage-reports 2.3.4 → 2.4.1

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.
@@ -3,177 +3,156 @@ const {
3
3
  } = require('../packages/monocart-coverage-vendor.js');
4
4
  const Util = require('../utils/util.js');
5
5
 
6
- const { createBranches, collectBranches } = require('./branches.js');
6
+ const { collectAstInfo } = require('./ast-visitor.js');
7
7
 
8
- const getFunctionRange = (start, end, state) => {
9
-
10
- const {
11
- functionMap, functionNameMap, functionRanges
12
- } = state;
13
-
14
- if (!functionRanges.length) {
15
- return;
16
- }
17
-
18
- // exact matched in functionMap
19
- const range = functionMap.get(start);
20
- if (range) {
21
- return range;
22
- }
23
- // exact matched in functionNameMap
24
- const nameRange = functionNameMap.get(start);
25
- if (nameRange) {
26
- return nameRange;
27
- }
28
-
29
- // find in functionRanges
30
- return Util.findInRanges(start, end, functionRanges, 'startOffset', 'endOffset');
31
- };
32
-
33
- const collectAstInfo = (ast, state) => {
34
-
35
- const functions = [];
36
- const branchMap = new Map();
37
-
38
- // root function branches
39
- const functionRoot = state.functionRoot;
40
- if (functionRoot) {
41
- const rootFunctionInfo = {
42
- count: functionRoot.count,
43
- range: functionRoot
44
- };
45
- createBranches(rootFunctionInfo, branchMap, ast);
46
- }
47
-
48
- // functions branches
49
- Util.visitAst(ast, {
50
-
51
- VariableDeclarator(node, parents) {
52
- // id: { type: 'Identifier', name: '__webpack_modules__' },
53
- const name = node.id && node.id.name;
54
- if (name === '__webpack_modules__' && node.init) {
55
- // mark all as wrap function
56
- node.init.properties.forEach((it) => {
57
- it.value._wrapKey = it.key && it.key.value;
58
- });
59
- // console.log('==========================', node.type);
60
- // console.log(name, parents.length);
61
- }
62
- },
63
-
64
- // Function include FunctionDeclaration, ArrowFunctionExpression, FunctionExpression
65
- Function(node, parents) {
66
-
67
- const {
68
- start, end, id
69
- } = node;
70
- const functionName = id && id.name;
71
-
72
- const functionInfo = {
73
- start,
74
- end,
75
- functionName,
76
- // js default to 1
77
- count: 1
78
- };
79
-
80
- if (node._wrapKey) {
81
- functionInfo.wrap = true;
82
- }
83
-
84
- const range = getFunctionRange(start, end, state);
85
- if (range) {
86
- functionInfo.count = range.count;
87
- functionInfo.range = range;
88
- // set wrap back to range
89
- if (functionInfo.wrap) {
90
- range.wrap = true;
91
- }
92
- }
93
-
94
- functions.push(functionInfo);
95
-
96
- createBranches(functionInfo, branchMap, node.body, node.params, parents);
97
-
98
- }
99
-
100
- });
101
-
102
- const branches = collectBranches(branchMap);
103
-
104
- return {
105
- functions,
106
- branches
107
- };
108
-
109
- };
110
-
111
- const generateCountState = (coverageList) => {
112
- const functionRanges = [];
8
+ const getCoverageInfo = (coverageList) => {
9
+ const functionMap = new Map();
113
10
  const functionNameMap = new Map();
114
- let functionRoot;
11
+ const functionUncoveredRanges = [];
12
+ let rootRange;
115
13
  coverageList.forEach((block) => {
116
14
  const {
117
15
  functionName, ranges, root
118
16
  } = block;
119
17
 
120
- // first one is function coverage info
121
- const functionRange = ranges[0];
18
+ let functionRange;
19
+ const blockRanges = [];
20
+
21
+ ranges.forEach((range, i) => {
22
+ if (i === 0) {
23
+ // function range
24
+ functionRange = range;
25
+ return;
26
+ }
27
+ blockRanges.push(range);
28
+ });
29
+
122
30
  functionRange.functionName = functionName;
123
31
 
124
32
  // blocks
125
- const len = ranges.length;
126
- if (len > 1) {
127
- const blockRanges = [].concat(ranges);
128
- // remove first one
129
- blockRanges.shift();
130
- blockRanges.sort((a, b) => {
131
- return a.startOffset - b.startOffset;
132
- });
33
+ if (blockRanges.length) {
133
34
  const blockMap = new Map();
134
35
  blockRanges.forEach((item) => {
135
36
  blockMap.set(item.startOffset, item);
136
37
  });
137
38
  functionRange.blockMap = blockMap;
138
- functionRange.blockRanges = blockRanges;
39
+ // uncovered is unique
40
+ const blockUncoveredRanges = blockRanges.filter((it) => it.count === 0);
41
+ blockUncoveredRanges.sort((a, b) => {
42
+ return a.startOffset - b.startOffset;
43
+ });
44
+ functionRange.blockUncoveredRanges = blockUncoveredRanges;
45
+ functionRange.blockCoveredRanges = blockRanges.filter((it) => it.count > 0);
139
46
  }
140
47
 
141
- // root function
48
+ // root function from vm
142
49
  if (root) {
143
- functionRoot = functionRange;
50
+ rootRange = functionRange;
144
51
  return;
145
52
  }
146
53
 
54
+ // root function from first range
55
+ if (!rootRange) {
56
+ rootRange = functionRange;
57
+ }
58
+
59
+ functionMap.set(functionRange.startOffset, functionRange);
147
60
  if (functionName) {
148
61
  functionNameMap.set(functionRange.startOffset + functionName.length, functionRange);
149
62
  }
150
- functionRanges.push(functionRange);
63
+
64
+ // uncovered is unique
65
+ if (functionRange.count === 0) {
66
+ functionUncoveredRanges.push(functionRange);
67
+ }
151
68
 
152
69
  });
153
70
 
154
71
  // sort ranges
155
- functionRanges.sort((a, b) => {
72
+ functionUncoveredRanges.sort((a, b) => {
156
73
  return a.startOffset - b.startOffset;
157
74
  });
158
75
 
159
- // keep map index for exact search
160
- const functionMap = new Map();
161
- functionRanges.forEach((item) => {
162
- functionMap.set(item.startOffset, item);
163
- });
164
-
165
76
  return {
166
77
  functionMap,
167
78
  functionNameMap,
168
- functionRanges,
169
- functionRoot
79
+ functionUncoveredRanges,
80
+ rootRange
170
81
  };
171
82
 
172
83
  };
173
84
 
85
+ const getFakeAstInfo = (coverageList) => {
86
+ const functions = [];
87
+ const branches = [];
88
+ const statements = [];
89
+ coverageList.forEach((block) => {
90
+ const {
91
+ isBlockCoverage, functionName, ranges, root
92
+ } = block;
93
+
94
+ ranges.forEach((range, i) => {
95
+
96
+ const {
97
+ startOffset, endOffset, count
98
+ } = range;
99
+
100
+ if (i === 0 && root) {
101
+ return;
102
+ }
103
+
104
+ statements.push({
105
+ start: startOffset,
106
+ end: endOffset,
107
+ count
108
+ });
109
+
110
+ if (isBlockCoverage) {
111
+ // branches
112
+ branches.push({
113
+ type: 'branch',
114
+ start: startOffset,
115
+ end: endOffset,
116
+ locations: [{
117
+ start: startOffset,
118
+ end: endOffset,
119
+ count
120
+ }]
121
+ });
122
+ }
123
+
124
+ if (i === 0) {
125
+ // function range
126
+ functions.push({
127
+ start: startOffset,
128
+ end: endOffset,
129
+ bodyStart: startOffset,
130
+ bodyEnd: endOffset,
131
+ count,
132
+ functionName
133
+ });
134
+
135
+ }
136
+ });
137
+
138
+ });
139
+
140
+ // console.log('fake functions', functions.map((it) => it));
141
+
142
+ return {
143
+ functions,
144
+ branches,
145
+ statements
146
+ };
147
+ };
148
+
174
149
  const getJsAstInfo = (item, coverageList) => {
175
150
 
176
- const { source } = item;
151
+ const { source, fake } = item;
152
+
153
+ if (fake) {
154
+ return getFakeAstInfo(coverageList);
155
+ }
177
156
 
178
157
  const options = {
179
158
  ecmaVersion: 'latest',
@@ -197,10 +176,10 @@ const getJsAstInfo = (item, coverageList) => {
197
176
  ast = acornLoose.parse(source, options);
198
177
  }
199
178
 
200
- const state = generateCountState(coverageList);
201
- // state.item = item;
179
+ const coverageInfo = getCoverageInfo(coverageList);
180
+ coverageInfo.item = item;
202
181
 
203
- return collectAstInfo(ast, state);
182
+ return collectAstInfo(ast, coverageInfo);
204
183
  };
205
184
 
206
185
  // =========================================================================================================
@@ -343,7 +322,8 @@ const getCssAstInfo = (item, coverageList) => {
343
322
 
344
323
  return {
345
324
  functions: [],
346
- branches: []
325
+ branches: [],
326
+ statements: []
347
327
  };
348
328
  };
349
329
 
@@ -126,7 +126,7 @@ const collectSourceMaps = async (v8list, options) => {
126
126
  };
127
127
 
128
128
  // remove comments if not debug
129
- if (Util.loggingType !== 'debug') {
129
+ if (!Util.isDebug()) {
130
130
  sourceData.source = convertSourceMap.removeComments(source);
131
131
  }
132
132