From 72efeaeacf898297ed799daac240176f3594048c Mon Sep 17 00:00:00 2001 From: cenfun Date: Sat, 29 Jun 2024 19:16:33 +0800 Subject: [PATCH] fixed same line issue --- lib/converter/find-original-range.js | 31 ++++++++++++++++------------ test/snapshot/minify.snapshot.json | 8 +++---- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/converter/find-original-range.js b/lib/converter/find-original-range.js index b2968053..d7e11f42 100644 --- a/lib/converter/find-original-range.js +++ b/lib/converter/find-original-range.js @@ -175,6 +175,11 @@ const getAlignPosition = (info, direction) => { // info.state.textLength.push(gt.length); // info.state.textLength.push(ot.length); + // const it = { + // gt, ot, gp + // }; + // info.state.alignTextList.push(it); + // there is no need to align for long text const maxLength = 100; if (gt.length > maxLength || ot.length > maxLength) { @@ -198,11 +203,6 @@ const getAlignPosition = (info, direction) => { // right matched // only for original first line text - // const it = { - // gt, ot, gp - // }; - // info.state.alignTextList.push(it); - // exclusive const list = alignText(gt, ot, info); const item = list[gp]; @@ -438,6 +438,7 @@ const getOriginalText = (m1, m2, cache) => { const o1 = m1.originalOffset; const o2 = m2.originalOffset; + const sameLine = m2.originalLine === m1.originalLine; // o1 < o2: most of time if (o1 < o2) { @@ -452,28 +453,33 @@ const getOriginalText = (m1, m2, cache) => { // }; // } + if (sameLine) { + return { + originalOffset: o1, + originalText: originalLocator.getSlice(o1, o2) + }; + } + // could be multiple lines for original text return { originalOffset: o1, - originalText: originalLocator.getSlice(o1, o2) + originalText: originalLocator.getSlice(o1, getOriginalEndOffset(m1, cache)) }; } // esbuild fixing two mapping have same original // m1 to end line if (o1 === o2) { - const originalEndOffset = getOriginalEndOffset(m1, cache); return { originalOffset: o1, - originalText: originalLocator.getSlice(o1, originalEndOffset) + originalText: originalLocator.getSlice(o1, getOriginalEndOffset(m1, cache)) }; } // o1 > o2: should be wrong sourcemap - // just reverse offsets if few cross lines - const crossLines = m1.originalLine - m2.originalLine; - if (crossLines < 2) { + // just reverse offsets if same line + if (sameLine) { return { originalOffset: o2, originalText: originalLocator.getSlice(o2, o1) @@ -487,10 +493,9 @@ const getOriginalText = (m1, m2, cache) => { // } // should be wrong, ignore m2 - const originalEndOffset = getOriginalEndOffset(m1, cache); return { originalOffset: o1, - originalText: originalLocator.getSlice(o1, originalEndOffset) + originalText: originalLocator.getSlice(o1, getOriginalEndOffset(m1, cache)) }; }; diff --git a/test/snapshot/minify.snapshot.json b/test/snapshot/minify.snapshot.json index 105a42d5..0af584ff 100644 --- a/test/snapshot/minify.snapshot.json +++ b/test/snapshot/minify.snapshot.json @@ -5,7 +5,7 @@ "statements": "39.42 %", "branches": "25.37 %", "functions": "42.80 %", - "lines": "36.71 %" + "lines": "36.73 %" }, "files": { "localhost-8130/demo.js": { @@ -244,9 +244,9 @@ "functions": "27.78 %", "branches": "7.14 %", "statements": "22.86 %", - "lines": "31.10 %", - "bytes": "26.28 %", - "uncoveredLines": "31-250,254-290,309-347,360,365-367,370-398" + "lines": "31.90 %", + "bytes": "27.04 %", + "uncoveredLines": "31-250,254-290,309-347,360,365-366,372-398" }, "monocart-v8/packages/v8/src/app.vue/5edf": { "functions": "",