monocart-coverage-reports 2.3.3 → 2.4.0

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