monocart-coverage-reports 2.7.2 → 2.7.4

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.
@@ -1,3 +1,4 @@
1
+ const { diff } = require('../packages/monocart-coverage-vendor');
1
2
  const Util = require('../utils/util.js');
2
3
  const EC = Util.EC;
3
4
 
@@ -38,6 +39,133 @@ const findMapping = (list, offset) => {
38
39
 
39
40
  // ========================================================================================================
40
41
 
42
+ const alignText = (gt, ot) => {
43
+
44
+ const toList = (s) => {
45
+ return s.split('').map((v, i) => {
46
+ return {
47
+ index: i,
48
+ value: v
49
+ };
50
+ });
51
+ };
52
+
53
+ const mergeList = (gList, oList) => {
54
+ const oLen = oList.length;
55
+ if (!oLen) {
56
+ return;
57
+ }
58
+ const gLen = gList.length;
59
+ if (gLen === oLen) {
60
+ gList.forEach((item, i) => {
61
+ item.original = oList[i];
62
+ });
63
+ }
64
+ };
65
+
66
+ const gl = toList(gt);
67
+ const ol = toList(ot);
68
+
69
+ // console.log(gl);
70
+ // console.log(ol);
71
+
72
+ const commonItems = [];
73
+ diff(gl.length, ol.length, (gi, oi) => {
74
+ return gl[gi].value === ol[oi].value;
75
+ }, (len, gi, oi) => {
76
+ commonItems.push([gi, oi, len]);
77
+ });
78
+
79
+ // console.log(commonItems);
80
+ // console.log('================================================================');
81
+ // merge list
82
+ // previous index
83
+ let gpi = 0;
84
+ let opi = 0;
85
+ commonItems.forEach(([gi, oi, len]) => {
86
+
87
+ if (gpi !== gi) {
88
+ const gList = gl.slice(gpi, gi);
89
+ const oList = ol.slice(opi, oi);
90
+ mergeList(gList, oList);
91
+ }
92
+
93
+ for (let i = 0; i < len; i++) {
94
+
95
+ const gii = gi + i;
96
+ const oii = oi + i;
97
+ const gItem = gl[gii];
98
+ gItem.original = ol[oii];
99
+
100
+ // matching a word end
101
+ if (i === len - 1 && len > 1) {
102
+ gItem.wordEnd = true;
103
+ }
104
+
105
+ }
106
+ gpi = gi + len;
107
+ opi = oi + len;
108
+ });
109
+
110
+ // console.log(JSON.stringify(data.gt));
111
+ // console.log(JSON.stringify(data.ot));
112
+ if (gpi < gl.length && opi < ol.length) {
113
+ const gList = gl.slice(gpi);
114
+ const oList = ol.slice(opi);
115
+ mergeList(gList, oList);
116
+ }
117
+
118
+ // console.log(gl);
119
+ return gl;
120
+ };
121
+
122
+ const getAlignPosition = (gt, gp, ot, direction) => {
123
+
124
+ // generatedText: '"false";\n',
125
+ // originalText: "'false';\r\n ",
126
+ // generatedPos: 7
127
+
128
+ // left matched
129
+
130
+ // originalText: '1;',
131
+ // generatedText: '1;else',
132
+ // generatedLeft: '1;'
133
+
134
+ // generatedText: ' var id = options.base ? item[0] + options.base : item[0];\r\n',
135
+ // originalText: ' var id = options.base ? item[0] + options.base : item[0];\n',
136
+
137
+ // right matched
138
+ // only for original first line text
139
+
140
+ // exclusive
141
+ const list = alignText(gt, ot);
142
+ const item = list[gp];
143
+ if (item && item.original) {
144
+ return {
145
+ pos: item.original.index
146
+ };
147
+ }
148
+
149
+ // 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
+ }
164
+
165
+ };
166
+
167
+ // ========================================================================================================
168
+
41
169
  const getBlockStartPosition = (originalLineText) => {
42
170
  // originalText: 'argument) {',
43
171
  // generatedLeft: 'o',
@@ -117,82 +245,56 @@ const getBlockEndPosition = (originalLineText) => {
117
245
 
118
246
  };
119
247
 
248
+ // ========================================================================================================
249
+
120
250
  const getComparedPosition = (info, direction) => {
121
251
 
122
- let gt = info.generatedText;
252
+ const gt = info.generatedText;
123
253
  const gp = info.generatedPos;
124
- let ot = info.originalText;
125
-
126
- // before trim
127
- const oLen = ot.length;
128
-
129
- gt = gt.trimEnd();
130
- ot = ot.trimEnd();
131
-
132
- // no content after trim
133
- if (!ot.length) {
134
- return {
135
- pos: direction === 'start' ? oLen : 0
136
- };
137
- }
254
+ const ot = info.originalText;
138
255
 
139
- // same content
140
- if (gt === ot) {
256
+ // whole generated text
257
+ if (gp >= gt.length) {
258
+ // console.log(JSON.stringify(gt), gt.length, gp);
259
+ // console.log(JSON.stringify(ot));
141
260
  return {
142
- pos: gp
261
+ pos: info.originalLineText.length
143
262
  };
144
263
  }
145
264
 
146
265
  // =============================
266
+ // trim right
267
+ const gtt = gt.trimEnd();
268
+ const ott = ot.trimEnd();
147
269
 
148
- // generatedText: '"false";\n',
149
- // originalText: "'false';\r\n ",
150
- // generatedPos: 7
151
-
152
- // length not change, replace " to '
153
- gt = gt.replace(/"/g, "'");
154
- ot = ot.replace(/"/g, "'");
155
-
156
-
157
- // same content too
158
- if (gt === ot) {
270
+ // no original content after trim
271
+ if (!ott) {
159
272
  return {
160
- pos: gp
273
+ pos: direction === 'start' ? ot.length : 0
161
274
  };
162
275
  }
163
276
 
164
- // ============================
165
- // left matched
166
-
167
- // originalText: '1;',
168
- // generatedText: '1;else',
169
- // generatedLeft: '1;'
170
-
171
- // generatedText: ' var id = options.base ? item[0] + options.base : item[0];\r\n',
172
- // originalText: ' var id = options.base ? item[0] + options.base : item[0];\n',
277
+ // no generated content after trim
278
+ if (!gtt) {
279
+ return;
280
+ }
173
281
 
174
- const left = gt.slice(0, gp);
175
- if (ot.startsWith(left)) {
282
+ // same content
283
+ if (gtt === ott) {
176
284
  return {
177
285
  pos: gp
178
286
  };
179
287
  }
180
288
 
181
- // ============================
182
- // right matched
183
- // only for original first line text
289
+ // =============================
184
290
 
185
- const olt = info.originalLineText;
186
- const right = gt.slice(gp);
187
- if (olt.endsWith(right)) {
188
- return {
189
- pos: olt.length - right.length
190
- };
191
- }
291
+ return getAlignPosition(gt, gp, ot, direction);
192
292
 
193
293
  };
194
294
 
195
- const initOriginalLineText = (info) => {
295
+
296
+ const getSimilarPosition = (info, direction) => {
297
+
196
298
  const originalText = info.originalText;
197
299
  // never cross line, using first line of original text
198
300
  // trim end remove \r\n and \n
@@ -200,44 +302,27 @@ const initOriginalLineText = (info) => {
200
302
  if (originalLines.length === 1) {
201
303
  // already single line
202
304
  info.originalLineText = originalText.trimEnd();
203
- info.originalSingle = true;
204
- return;
305
+ } else {
306
+ // multiple liens
307
+ info.originalLineText = originalLines[0].trimEnd();
205
308
  }
206
- // multiple liens
207
- info.originalLineText = originalLines[0].trimEnd();
208
- };
209
309
 
210
- const getSimilarPosition = (info, direction) => {
211
-
212
- initOriginalLineText(info);
310
+ // no need comparison for fake source
311
+ if (info.fake) {
312
+ return;
313
+ }
213
314
 
214
315
  const textPos = getComparedPosition(info, direction);
215
316
  if (textPos) {
216
317
  return textPos;
217
318
  }
218
319
 
219
- if (info.originalSingle) {
220
- return;
221
- }
222
-
223
- // multiple lines
224
- const { generatedPos, originalLineText } = info;
225
- // generatedPos not in first line
226
- if (generatedPos > originalLineText.length) {
227
- return;
228
- }
229
-
230
- const linePos = getComparedPosition(info, direction);
231
- if (linePos) {
232
- return linePos;
233
- }
234
-
235
320
  // console.log('====================================================================');
236
- // console.log(`${EC.magenta(direction)} similar position can NOT be fixed`);
321
+ // console.log(`${EC.magenta(direction)} similar position can NOT be fixed`, info.originalState.sourcePath);
237
322
  // console.log({
238
- // sourcePath: info.originalState.sourcePath,
239
- // generatedText,
240
- // generatedPos,
323
+ // direction,
324
+ // generatedText: info.generatedText,
325
+ // generatedPos: info.generatedPos,
241
326
  // originalText
242
327
  // });
243
328
 
@@ -273,16 +358,16 @@ const getFixedPosition = (info, direction) => {
273
358
 
274
359
  // ========================================================================================================
275
360
 
276
- const getOriginalEndOffset = (m, rangeInfo) => {
361
+ const getOriginalEndOffset = (m, cache) => {
277
362
  if (!m.originalEndOffset) {
278
- const line = rangeInfo.originalState.locator.getLine(m.originalLine + 1);
363
+ const line = cache.originalState.locator.getLine(m.originalLine + 1);
279
364
  m.originalEndOffset = line ? line.end : m.originalOffset;
280
365
  }
281
366
  return m.originalEndOffset;
282
367
  };
283
368
 
284
- const getOriginalText = (m1, m2, rangeInfo) => {
285
- const originalLocator = rangeInfo.originalState.locator;
369
+ const getOriginalText = (m1, m2, cache) => {
370
+ const originalLocator = cache.originalState.locator;
286
371
 
287
372
  const o1 = m1.originalOffset;
288
373
  const o2 = m2.originalOffset;
@@ -299,7 +384,7 @@ const getOriginalText = (m1, m2, rangeInfo) => {
299
384
  // esbuild fixing two mapping have same original
300
385
  // m1 to end line
301
386
  if (o1 === o2) {
302
- const originalEndOffset = getOriginalEndOffset(m1, rangeInfo);
387
+ const originalEndOffset = getOriginalEndOffset(m1, cache);
303
388
  return {
304
389
  originalOffset: o1,
305
390
  originalText: originalLocator.getSlice(o1, originalEndOffset)
@@ -319,12 +404,12 @@ const getOriginalText = (m1, m2, rangeInfo) => {
319
404
 
320
405
  // if (direction === 'start') {
321
406
  // should be m2 ?
322
- // console.log(direction, rangeInfo.originalState.sourcePath);
407
+ // console.log(direction, cache.originalState.sourcePath);
323
408
  // console.log(m1, m2);
324
409
  // }
325
410
 
326
411
  // should be wrong, ignore m2
327
- const originalEndOffset = getOriginalEndOffset(m1, rangeInfo);
412
+ const originalEndOffset = getOriginalEndOffset(m1, cache);
328
413
  return {
329
414
  originalOffset: o1,
330
415
  originalText: originalLocator.getSlice(o1, originalEndOffset)
@@ -385,10 +470,10 @@ const getGeneratedText = (m1, m2, state, offset) => {
385
470
 
386
471
  // ========================================================================================================
387
472
 
388
- const getFixedOriginalStart = (start, mappings, state, rangeInfo) => {
473
+ const getFixedOriginalStart = (start, mappings, state, cache) => {
389
474
 
390
475
  const [m1, m2] = mappings;
391
- const { originalState, crossStart } = rangeInfo;
476
+ const { originalState, crossStart } = cache;
392
477
 
393
478
  // cross file from start of line
394
479
  if (crossStart) {
@@ -397,11 +482,13 @@ const getFixedOriginalStart = (start, mappings, state, rangeInfo) => {
397
482
  };
398
483
  }
399
484
 
400
- const direction = 'start';
401
- const { originalText, originalOffset } = getOriginalText(m1, m2, rangeInfo);
485
+
486
+ const { originalText, originalOffset } = getOriginalText(m1, m2, cache);
402
487
  const { generatedText, generatedPos } = getGeneratedText(m1, m2, state, start);
488
+ const direction = 'start';
403
489
 
404
490
  const info = {
491
+ fake: state.fake,
405
492
  generatedText,
406
493
  generatedPos,
407
494
  originalText,
@@ -426,14 +513,14 @@ const getFixedOriginalStart = (start, mappings, state, rangeInfo) => {
426
513
  };
427
514
  };
428
515
 
429
- const getFixedOriginalEnd = (end, mappings, state, rangeInfo) => {
516
+ const getFixedOriginalEnd = (end, mappings, state, cache) => {
430
517
 
431
518
  const [m1, m2] = mappings;
432
- const { originalState, crossEnd } = rangeInfo;
519
+ const { originalState, crossEnd } = cache;
433
520
 
434
521
  // cross file until e1 line end (not file end)
435
522
  if (crossEnd) {
436
- const originalEndOffset = getOriginalEndOffset(m1, rangeInfo);
523
+ const originalEndOffset = getOriginalEndOffset(m1, cache);
437
524
  return {
438
525
  originalEnd: originalEndOffset
439
526
  };
@@ -448,36 +535,13 @@ const getFixedOriginalEnd = (end, mappings, state, rangeInfo) => {
448
535
  };
449
536
  }
450
537
 
451
- // quick check next one if exact match
452
- // most of case is same string
453
- if (m2.generatedOffset === end + 1) {
454
- const gt = state.locator.source[end];
455
- const oi = m2.originalOffset - 1;
456
- const ot = originalState.locator.source[oi];
457
- // it could be ; but it quite far away
458
- if (gt === ot && [';', ','].includes(gt)) {
459
- return {
460
- originalEnd: oi
461
- };
462
- }
463
538
 
464
- // Not applicable
465
- // console.log('==================================');
466
- // console.log({
467
- // gp: state.sourcePath,
468
- // g: end,
469
- // gt,
470
- // op: originalState.sourcePath,
471
- // o: oi,
472
- // ot
473
- // });
474
- }
475
-
476
- const direction = 'end';
477
- const { originalText, originalOffset } = getOriginalText(m1, m2, rangeInfo);
539
+ const { originalText, originalOffset } = getOriginalText(m1, m2, cache);
478
540
  const { generatedText, generatedPos } = getGeneratedText(m1, m2, state, end);
541
+ const direction = 'end';
479
542
 
480
543
  const info = {
544
+ fake: state.fake,
481
545
  offset: end,
482
546
  generatedText,
483
547
  generatedPos,
@@ -506,23 +570,57 @@ const getFixedOriginalEnd = (end, mappings, state, rangeInfo) => {
506
570
 
507
571
  // ========================================================================================================
508
572
 
509
- const getOriginalStart = (start, startMapping, state, rangeInfo) => {
510
- if (Array.isArray(startMapping)) {
511
- return getFixedOriginalStart(start, startMapping, state, rangeInfo);
573
+ const getOriginalStartPosition = (cache, state) => {
574
+
575
+ const { start, startMappings } = cache;
576
+
577
+ if (Array.isArray(startMappings)) {
578
+ return getFixedOriginalStart(start, startMappings, state, cache);
512
579
  }
580
+
513
581
  // Exact match
514
582
  return {
515
- originalStart: startMapping.originalOffset
583
+ originalStart: startMappings.originalOffset
516
584
  };
517
585
  };
518
586
 
519
- const getOriginalEnd = (end, endMapping, state, rangeInfo) => {
520
- if (Array.isArray(endMapping)) {
521
- return getFixedOriginalEnd(end, endMapping, state, rangeInfo);
587
+ const getOriginalExclusiveEnd = (cache, state) => {
588
+ // end, exclusive mappings
589
+ const { end } = cache;
590
+ const { decodedMappings } = state;
591
+ const endMappings = findMapping(decodedMappings, end);
592
+ if (Array.isArray(endMappings)) {
593
+ return getFixedOriginalEnd(end, endMappings, state, cache);
522
594
  }
523
- // Exact match
595
+
596
+ // Exact match end
524
597
  return {
525
- originalEnd: endMapping.originalOffset
598
+ originalEnd: endMappings.originalOffset
599
+ };
600
+ };
601
+
602
+ const getOriginalEndPosition = (cache, state) => {
603
+
604
+ const { endMappings, originalState } = cache;
605
+
606
+ // (end - 1), inclusive
607
+ if (Array.isArray(endMappings)) {
608
+ return getOriginalExclusiveEnd(cache, state);
609
+ }
610
+
611
+ // Exact match (end - 1)
612
+
613
+ // check end char
614
+ const oi = endMappings.originalOffset;
615
+ const originalEndChar = originalState.locator.getSlice(oi, oi + 1);
616
+ // the char should never end with "{" or "("
617
+ if (['{', '('].includes(originalEndChar)) {
618
+ return getOriginalExclusiveEnd(cache, state);
619
+ }
620
+
621
+ // inclusive to exclusive
622
+ return {
623
+ originalEnd: oi + 1
526
624
  };
527
625
  };
528
626
 
@@ -536,9 +634,6 @@ const checkSourceFileIndexes = (startIndexes, endIndexes) => {
536
634
  sourceIndex: s1
537
635
  };
538
636
  }
539
- return {
540
- error: true
541
- };
542
637
  };
543
638
 
544
639
  const checkIndex21 = (s1, s2, e1) => {
@@ -553,9 +648,6 @@ const checkSourceFileIndexes = (startIndexes, endIndexes) => {
553
648
  sourceIndex: e1
554
649
  };
555
650
  }
556
- return {
557
- error: true
558
- };
559
651
  };
560
652
 
561
653
  const checkIndex12 = (s1, e1, e2) => {
@@ -570,9 +662,6 @@ const checkSourceFileIndexes = (startIndexes, endIndexes) => {
570
662
  sourceIndex: s1
571
663
  };
572
664
  }
573
- return {
574
- error: true
575
- };
576
665
  };
577
666
 
578
667
  const checkIndex22 = (s1, s2, e1, e2) => {
@@ -602,10 +691,6 @@ const checkSourceFileIndexes = (startIndexes, endIndexes) => {
602
691
  // both between two mappings: 38,39 ~ 38,39
603
692
  // if (s1 === e1 && s2 === e2) {
604
693
  // }
605
-
606
- return {
607
- error: true
608
- };
609
694
  };
610
695
 
611
696
  // both exact matched
@@ -626,13 +711,20 @@ const checkSourceFileIndexes = (startIndexes, endIndexes) => {
626
711
 
627
712
  };
628
713
 
629
- const getOriginalState = (startMappings, endMappings, originalMap) => {
714
+ const getOriginalState = (start, end, state, originalMap) => {
715
+
716
+ const mappingInfo = getMappingInfo(start, end, state);
717
+ if (mappingInfo.error) {
718
+ return mappingInfo;
719
+ }
720
+
721
+ const { startMappings, endMappings } = mappingInfo;
630
722
 
631
723
  // check source file indexes
632
724
  const startIndexes = [].concat(startMappings).map((it) => it.sourceIndex);
633
725
  const endIndexes = [].concat(endMappings).map((it) => it.sourceIndex);
634
- const rangeInfo = checkSourceFileIndexes(startIndexes, endIndexes);
635
- if (rangeInfo.error) {
726
+ const results = checkSourceFileIndexes(startIndexes, endIndexes);
727
+ if (!results) {
636
728
  return {
637
729
  error: true,
638
730
  errors: [`invalid source indexes: ${EC.yellow(`${startIndexes} ~ ${endIndexes}`)}`]
@@ -641,7 +733,7 @@ const getOriginalState = (startMappings, endMappings, originalMap) => {
641
733
 
642
734
  const {
643
735
  sourceIndex, crossStart, crossEnd
644
- } = rangeInfo;
736
+ } = results;
645
737
 
646
738
  // if (crossStart || crossEnd) {
647
739
  // console.log(EC.magenta('cross file'), EC.yellow(`${startIndexes} ~ ${endIndexes}`));
@@ -655,11 +747,17 @@ const getOriginalState = (startMappings, endMappings, originalMap) => {
655
747
  };
656
748
  }
657
749
 
750
+ // cache for original info
658
751
  return {
659
752
  // for debug
660
753
  // startIndexes,
661
754
  // endIndexes,
662
755
 
756
+ start,
757
+ end,
758
+ startMappings,
759
+ endMappings,
760
+
663
761
  sourceIndex,
664
762
  crossStart,
665
763
  crossEnd,
@@ -667,7 +765,9 @@ const getOriginalState = (startMappings, endMappings, originalMap) => {
667
765
  };
668
766
  };
669
767
 
670
- const getMappingInfo = (decodedMappings, start, end) => {
768
+ const getMappingInfo = (start, end, state) => {
769
+
770
+ const { decodedMappings } = state;
671
771
  // possible no length
672
772
  if (decodedMappings.length < 2) {
673
773
  return {
@@ -677,9 +777,6 @@ const getMappingInfo = (decodedMappings, start, end) => {
677
777
  }
678
778
 
679
779
  // start: inclusive
680
- // end: exclusive
681
- // could be 4 mappings found
682
-
683
780
  const startMappings = findMapping(decodedMappings, start);
684
781
  if (!startMappings) {
685
782
  return {
@@ -688,7 +785,8 @@ const getMappingInfo = (decodedMappings, start, end) => {
688
785
  };
689
786
  }
690
787
 
691
- const endMappings = findMapping(decodedMappings, end);
788
+ // end: exclusive
789
+ const endMappings = findMapping(decodedMappings, end - 1);
692
790
  if (!endMappings) {
693
791
  return {
694
792
  error: true,
@@ -696,6 +794,7 @@ const getMappingInfo = (decodedMappings, start, end) => {
696
794
  };
697
795
  }
698
796
 
797
+ // could be 4 mappings found
699
798
  return {
700
799
  startMappings,
701
800
  endMappings
@@ -704,7 +803,7 @@ const getMappingInfo = (decodedMappings, start, end) => {
704
803
 
705
804
  const findOriginalRange = (start, end, state, originalMap) => {
706
805
 
707
- const { sourcePath, decodedMappings } = state;
806
+ const { sourcePath } = state;
708
807
 
709
808
  const createMappingError = (errors) => {
710
809
  return {
@@ -716,29 +815,17 @@ const findOriginalRange = (start, end, state, originalMap) => {
716
815
  };
717
816
  };
718
817
 
719
- const mappingInfo = getMappingInfo(decodedMappings, start, end);
720
- if (mappingInfo.error) {
721
- return createMappingError(mappingInfo.errors);
722
- }
723
-
724
- const { startMappings, endMappings } = mappingInfo;
725
-
726
- const rangeInfo = getOriginalState(startMappings, endMappings, originalMap);
727
- if (rangeInfo.error) {
728
- return createMappingError(rangeInfo.errors);
729
- }
730
-
731
- const originalStartResult = getOriginalStart(start, startMappings, state, rangeInfo);
732
- if (originalStartResult.error) {
733
- return createMappingError(originalStartResult.errors);
734
- }
735
-
736
- const originalEndResult = getOriginalEnd(end, endMappings, state, rangeInfo);
737
- if (originalEndResult.error) {
738
- return createMappingError(originalEndResult.errors);
818
+ const cache = getOriginalState(start, end, state, originalMap);
819
+ if (cache.error) {
820
+ return createMappingError(cache.errors);
739
821
  }
740
822
 
823
+ const originalStartResult = getOriginalStartPosition(cache, state);
741
824
  const { originalStart } = originalStartResult;
825
+ // could be used for end
826
+ cache.originalStart = originalStart;
827
+
828
+ const originalEndResult = getOriginalEndPosition(cache, state);
742
829
  const { originalEnd } = originalEndResult;
743
830
 
744
831
  // range start > end
@@ -746,7 +833,7 @@ const findOriginalRange = (start, end, state, originalMap) => {
746
833
  return createMappingError([`invalid original start > end: ${EC.yellow(originalStart)} > ${EC.yellow(originalEnd)}`]);
747
834
  }
748
835
 
749
- const { originalState } = rangeInfo;
836
+ const { originalState } = cache;
750
837
 
751
838
  return {
752
839
  start: originalStart,