diff --git a/src/utils/collapseSourcemaps.ts b/src/utils/collapseSourcemaps.ts index 66c6284f0f1..e4c87734cd1 100644 --- a/src/utils/collapseSourcemaps.ts +++ b/src/utils/collapseSourcemaps.ts @@ -58,7 +58,7 @@ class Link { const tracedLine: SourceMapSegment[] = []; for (const segment of line) { - if (segment.length == 1) continue; + if (segment.length === 1) continue; const source = this.sources[segment[1]]; if (!source) continue; @@ -130,9 +130,7 @@ class Link { if (segment[0] === column || searchStart === searchEnd) { if (segment.length == 1) return null; const source = this.sources[segment[1]]; - if (!source) { - return null; - } + if (!source) return null; return source.traceSegment( segment[2], diff --git a/src/utils/getOriginalLocation.ts b/src/utils/getOriginalLocation.ts index e0c53e6d26d..4303a1c3003 100644 --- a/src/utils/getOriginalLocation.ts +++ b/src/utils/getOriginalLocation.ts @@ -2,7 +2,7 @@ import type { DecodedSourceMapOrMissing, ExistingDecodedSourceMap } from '../rol export function getOriginalLocation( sourcemapChain: readonly DecodedSourceMapOrMissing[], - location: { column: number; line: number; name?: string; source?: string } + location: { column: number; line: number } ): { column: number; line: number } { const filteredSourcemapChain = sourcemapChain.filter( (sourcemap): sourcemap is ExistingDecodedSourceMap => !!sourcemap.mappings @@ -10,16 +10,16 @@ export function getOriginalLocation( traceSourcemap: while (filteredSourcemapChain.length > 0) { const sourcemap = filteredSourcemapChain.pop()!; const line = sourcemap.mappings[location.line - 1]; - - if (line?.length) { - for (const segment of line) { - if (segment[0] >= location.column || line.length === 1) { - if (segment.length === 1) break; + if (line) { + const filteredLine = line.filter( + (segment): segment is [number, number, number, number] => segment.length > 1 + ); + const lastSegment = filteredLine[filteredLine.length - 1]; + for (const segment of filteredLine) { + if (segment[0] >= location.column || segment === lastSegment) { location = { column: segment[3], - line: segment[2] + 1, - name: segment.length === 5 ? sourcemap.names[segment[4]] : undefined, - source: sourcemap.sources[segment[1]] + line: segment[2] + 1 }; continue traceSourcemap; } diff --git a/test/function/samples/cannot-resolve-sourcemap-warning/_config.js b/test/function/samples/cannot-resolve-sourcemap-warning/_config.js index a803b5b72e8..3ec5a853772 100644 --- a/test/function/samples/cannot-resolve-sourcemap-warning/_config.js +++ b/test/function/samples/cannot-resolve-sourcemap-warning/_config.js @@ -7,7 +7,7 @@ module.exports = { plugins: { name: 'test-plugin', transform() { - return { code: 'export default this', map: { mappings: 'X' } }; + return { code: 'export default this', map: { mappings: '' } }; } } }, diff --git a/test/function/samples/warning-low-resolution-location/_config.js b/test/function/samples/warning-low-resolution-location/_config.js index 01d2d4001c7..a8e18a9d87c 100644 --- a/test/function/samples/warning-low-resolution-location/_config.js +++ b/test/function/samples/warning-low-resolution-location/_config.js @@ -11,7 +11,7 @@ module.exports = { // each entry of each line consist of // [generatedColumn, sourceIndex, sourceLine, sourceColumn]; // this mapping only maps the first line to itself - const decodedMap = [[[0, 0, 0, 0]]]; + const decodedMap = [[[0], [0, 0, 0, 0], [1]]]; return { code: 'export default this', map: { mappings: encode(decodedMap), sources: [] } }; } }