monocart-coverage-reports 2.0.1 → 2.0.3

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.
@@ -154,14 +154,6 @@ const generateCountState = (coverageList) => {
154
154
 
155
155
  const getJsAstInfo = (item, coverageList) => {
156
156
 
157
- // not for css
158
- if (!item.js) {
159
- return {
160
- functions: [],
161
- branches: []
162
- };
163
- }
164
-
165
157
  const { source } = item;
166
158
 
167
159
  const options = {
@@ -193,76 +185,118 @@ const getJsAstInfo = (item, coverageList) => {
193
185
 
194
186
  // =========================================================================================================
195
187
 
196
- const addCssRules = (rules, coverageList, coveredRanges, locator) => {
197
- let hasCovered = false;
198
- // line and line is 1-base
199
- rules.forEach((rule) => {
188
+ const addRule = (item, coverageList, coveredRanges, count = 0) => {
200
189
 
201
- const { type, position } = rule;
190
+ const { source } = item;
191
+ const { start, end } = source;
192
+ const startOffset = start.offset;
193
+ const endOffset = end.offset;
202
194
 
203
- if (type === 'comment') {
204
- return;
195
+ if (!count) {
196
+ const coveredRange = findInRanges(startOffset, endOffset, coveredRanges);
197
+ if (coveredRange) {
198
+ count = 1;
205
199
  }
200
+ }
206
201
 
207
- const { start, end } = position;
208
-
209
- // both 1-base
210
- // console.log(start, end);
211
- const startOffset = locator.locationToOffset({
212
- line: start.line,
213
- column: start.column - 1
214
- });
215
-
216
- const endOffset = locator.locationToOffset({
217
- line: end.line,
218
- column: end.column - 1
219
- });
220
-
221
- // start should offset @media
222
- if (type === 'media') {
223
- // rules in media
224
- // console.log('media', rule.media, startOffset, endOffset);
225
- const childCoverageList = [];
226
- const hasChildCovered = addCssRules(rule.rules, childCoverageList, coveredRanges, locator);
227
- if (hasChildCovered) {
228
- childCoverageList.forEach((it) => {
229
- coverageList.push(it);
230
- });
231
- return;
232
- }
202
+ coverageList.push({
203
+ start: startOffset,
204
+ end: endOffset,
205
+ count
206
+ });
207
+
208
+ return count;
209
+
210
+ };
211
+
212
+ const addAtRule = (item, coverageList, coveredRanges) => {
213
+ const { name, nodes } = item;
214
+
215
+ if (['charset', 'import', 'namespace'].includes(name)) {
216
+ addRule(item, coverageList, coveredRanges, 1);
217
+ return;
218
+ }
219
+
220
+ if (['media', 'supports', 'container', 'layer'].includes(name)) {
221
+
222
+ const childCoverageList = [];
223
+ const count = addCssRules(nodes, childCoverageList, coveredRanges);
224
+ if (count) {
233
225
 
234
- // add whole media as uncovered
235
226
  coverageList.push({
236
- start: startOffset,
237
- end: endOffset,
238
- count: 0
227
+ start: item.source.start.offset,
228
+ end: childCoverageList[0].start,
229
+ count: 1
230
+ });
231
+
232
+ let end;
233
+ childCoverageList.forEach((it) => {
234
+ coverageList.push(it);
235
+ end = it.end;
239
236
  });
237
+
238
+ coverageList.push({
239
+ start: end,
240
+ end: item.source.end.offset,
241
+ count: 1
242
+ });
243
+
240
244
  return;
241
245
  }
242
246
 
243
- let count = 0;
244
- if (type === 'charset') {
245
- count = 1;
246
- } else {
247
- const coveredRange = findInRanges(startOffset, endOffset, coveredRanges);
248
- if (coveredRange) {
249
- count = 1;
250
- hasCovered = true;
251
- }
247
+ }
248
+
249
+ addRule(item, coverageList, coveredRanges);
250
+
251
+ };
252
+
253
+ const addCssRules = (list, coverageList, coveredRanges) => {
254
+
255
+ let count = 0;
256
+
257
+ // line and line is 1-base
258
+ list.forEach((item) => {
259
+
260
+ const { type } = item;
261
+
262
+ if (type === 'comment') {
263
+ return;
264
+ }
265
+
266
+ if (type === 'rule') {
267
+ count += addRule(item, coverageList, coveredRanges);
268
+ return;
252
269
  }
253
270
 
254
- coverageList.push({
255
- start: startOffset,
256
- end: endOffset,
257
- count
258
- });
271
+ // console.log('=============================================================================');
272
+ // Object.keys(item).forEach((k) => {
273
+ // if (k === 'parent') {
274
+ // return;
275
+ // }
276
+ // if (k === 'raws') {
277
+ // return;
278
+ // }
279
+ // if (k === 'nodes') {
280
+ // console.log(k, item[k].length);
281
+ // return;
282
+ // }
283
+ // if (k === 'source') {
284
+ // console.log(k, item[k].start, item[k].end);
285
+ // return;
286
+ // }
287
+ // console.log(k, item[k]);
288
+ // });
289
+
290
+ if (type === 'atrule') {
291
+ addAtRule(item, coverageList, coveredRanges);
292
+ }
259
293
 
260
294
  });
261
295
 
262
- return hasCovered;
296
+ return count;
263
297
  };
264
298
 
265
- const getCssAstInfo = (item, locator) => {
299
+ const getCssAstInfo = (item, coverageList) => {
266
300
 
267
301
  const {
268
302
  source, sourcePath, ranges
@@ -270,6 +304,7 @@ const getCssAstInfo = (item, locator) => {
270
304
 
271
305
  // sort covered ranges
272
306
  ranges.sort((a, b) => a.start - b.start);
307
+
273
308
  // to offset covered ranges
274
309
  const coveredRanges = ranges.map((it) => {
275
310
  return {
@@ -282,11 +317,13 @@ const getCssAstInfo = (item, locator) => {
282
317
  const ast = parseCss(source);
283
318
  Util.logTime(`parse css: ${sourcePath}`, time_start_parse);
284
319
 
285
- const coverageList = [];
286
- addCssRules(ast.rules, coverageList, coveredRanges, locator);
320
+ // if (sourcePath.endsWith('style.css')) {
321
+ addCssRules(ast.nodes, coverageList, coveredRanges);
322
+ // }
287
323
 
288
324
  return {
289
- coverageList
325
+ functions: [],
326
+ branches: []
290
327
  };
291
328
  };
292
329
 
@@ -108,33 +108,58 @@ const addBranch = (group, node, locationMap, functionInfo) => {
108
108
  return branchInfo;
109
109
  };
110
110
 
111
+ const addNoneBranch = (group) => {
112
+ group.locations.push({
113
+ none: true,
114
+ count: 0
115
+ });
116
+ };
117
+
111
118
  // =======================================================================================
112
119
 
120
+ const hasFunctionBlock = (functionInfo) => {
121
+ const { range } = functionInfo;
122
+ if (range) {
123
+ const { blockMap } = range;
124
+ if (blockMap) {
125
+ return true;
126
+ }
127
+ }
128
+ return false;
129
+ };
130
+
113
131
  const addNodeCount = (node, parents, functionInfo) => {
114
132
 
115
133
  const { start, end } = node;
116
- const { count, range } = functionInfo;
117
134
 
118
- if (range) {
135
+ if (hasFunctionBlock(functionInfo)) {
136
+ const { range } = functionInfo;
119
137
  const { blockMap, blockRanges } = range;
120
- if (blockMap) {
121
- const block = blockMap.get(start);
122
- if (block) {
123
- node._state = {
124
- count: block.count
125
- };
126
- return;
127
- }
128
-
129
- const blockItem = findInRanges(start, end, blockRanges);
130
- if (blockItem) {
131
- node._state = {
132
- count: blockItem.count
133
- };
134
- return;
135
- }
138
+ const block = blockMap.get(start);
139
+ if (block) {
140
+ node._state = {
141
+ count: block.count
142
+ };
143
+ return;
144
+ }
136
145
 
146
+ const blockItem = findInRanges(start, end, blockRanges);
147
+ if (blockItem) {
148
+ node._state = {
149
+ count: blockItem.count
150
+ };
151
+ return;
137
152
  }
153
+
154
+ // continue find in reverseParents
155
+
156
+ } else {
157
+
158
+ // default to 1 if no function block
159
+ node._state = {
160
+ count: 1
161
+ };
162
+ return;
138
163
  }
139
164
 
140
165
  const reverseParents = [].concat(parents).reverse();
@@ -149,7 +174,7 @@ const addNodeCount = (node, parents, functionInfo) => {
149
174
 
150
175
  // function count
151
176
  node._state = {
152
- count: count
177
+ count: functionInfo.count
153
178
  };
154
179
 
155
180
  };
@@ -198,10 +223,7 @@ const createBranches = (ast, functionInfo, branchMap) => {
198
223
  addBranch(group, alternate, locationMap, functionInfo);
199
224
  } else {
200
225
  // add none branch
201
- group.locations.push({
202
- none: true,
203
- count: 0
204
- });
226
+ addNoneBranch(group);
205
227
  // no need update group end, there is no end
206
228
  }
207
229
  },
@@ -306,7 +328,7 @@ const LogicalExpression = (group, parentCount, noBlockList, blockCount) => {
306
328
 
307
329
  // const start = group.loc.start;
308
330
  // if (start > 7874 && start < 7922) {
309
- // console.log(group.type, parentCount, 'locations', group.locations, group.functionInfo.range);
331
+ // console.log(group.type, parentCount, 'locations', group.locations);
310
332
  // }
311
333
 
312
334
  };
@@ -351,6 +373,14 @@ const updateBranchCount = (group) => {
351
373
  const parentCount = getBranchParentCount(reverseParents, functionCount);
352
374
  // console.log(functionInfo.start, 'function count', functionCount, 'parent count', parentCount);
353
375
 
376
+ if (!hasFunctionBlock(functionInfo)) {
377
+ // default to parent if no function range
378
+ locations.forEach((item) => {
379
+ item.count = parentCount;
380
+ });
381
+ return;
382
+ }
383
+
354
384
  // calculate branches count
355
385
  // > 1
356
386
 
@@ -360,9 +390,9 @@ const updateBranchCount = (group) => {
360
390
  if (item.block) {
361
391
  item.count = item.block.count;
362
392
  blockCount += item.count;
363
- } else {
364
- noBlockList.push(item);
393
+ return;
365
394
  }
395
+ noBlockList.push(item);
366
396
  });
367
397
 
368
398
  const handlers = {
@@ -978,7 +978,7 @@ const convertV8List = async (v8list, options) => {
978
978
 
979
979
  // ============================
980
980
  // move functions and ranges to coverageList
981
- let coverageList;
981
+ let coverageList = [];
982
982
  let astInfo;
983
983
  if (js) {
984
984
  coverageList = item.functions;
@@ -988,11 +988,8 @@ const convertV8List = async (v8list, options) => {
988
988
  }
989
989
  astInfo = getJsAstInfo(item, coverageList);
990
990
  } else {
991
- astInfo = getCssAstInfo(item, locator);
992
991
  // convent css covered ranges to rules ranges and include uncovered ranges
993
- coverageList = astInfo.coverageList;
994
- // console.log(coverageList);
995
-
992
+ astInfo = getCssAstInfo(item, coverageList);
996
993
  // remove original ranges
997
994
  if (Util.loggingType !== 'debug') {
998
995
  delete item.ranges;