From 35d7628e13b6593e2128666320bc421a2b549da5 Mon Sep 17 00:00:00 2001 From: dmitrage Date: Sun, 13 Dec 2020 02:12:15 +0300 Subject: [PATCH] Fix and cover case when preprocessor returns no source map --- src/compiler/utils/string_with_sourcemap.ts | 5 +-- test/sourcemaps/helpers.ts | 30 ++++++++++++++++ .../samples/preprocess-no-map/_config.js | 13 +++++++ .../samples/preprocess-no-map/input.svelte | 13 +++++++ .../samples/preprocess-no-map/test.js | 36 +++++++++++++++++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 test/sourcemaps/samples/preprocess-no-map/_config.js create mode 100644 test/sourcemaps/samples/preprocess-no-map/input.svelte create mode 100644 test/sourcemaps/samples/preprocess-no-map/test.js diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index 1707c86aac..1766a0db06 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -167,11 +167,13 @@ export class StringWithSourcemap { } static from_processed(string: string, map?: DecodedSourceMap): StringWithSourcemap { + const line_count = string.split('\n').length; + if (map) { // ensure that count of source map mappings lines // is equal to count of generated code lines // (some tools may produce less) - const missing_lines = string.split('\n').length - map.mappings.length; + const missing_lines = line_count - map.mappings.length; for (let i = 0; i < missing_lines; i++) { map.mappings.push([]); } @@ -182,7 +184,6 @@ export class StringWithSourcemap { map = { version: 3, names: [], sources: [], mappings: [] }; // add empty SourceMapSegment[] for every line - const line_count = (string.match(/\n/g) || '').length; for (let i = 0; i < line_count; i++) map.mappings.push([]); return new StringWithSourcemap(string, map); } diff --git a/test/sourcemaps/helpers.ts b/test/sourcemaps/helpers.ts index c1a6e6798c..f546566a95 100644 --- a/test/sourcemaps/helpers.ts +++ b/test/sourcemaps/helpers.ts @@ -43,6 +43,36 @@ export function assert_mapped( ); } +type AssertNotMappedParameters = { + code: string; + filename?: string; + preprocessed: any; +}; + +export function assert_not_mapped( + { code, filename, preprocessed }: AssertNotMappedParameters +) { + if (filename === undefined) filename = 'input.svelte'; + + const transformed_loc = preprocessed.locate_1(code); + assert.notEqual( + transformed_loc, + undefined, + `failed to locate "${code}" in transformed "${filename}"` + ); + + assert.deepEqual( + preprocessed.mapConsumer.originalPositionFor(transformed_loc), + { + source: null, + name: null, + line: null, + column: null + }, + `incorrect mappings for "${code}" in "${filename}"` + ); +} + export function assert_not_located( code: string, locate: ReturnType, diff --git a/test/sourcemaps/samples/preprocess-no-map/_config.js b/test/sourcemaps/samples/preprocess-no-map/_config.js new file mode 100644 index 0000000000..d9ac6f3c0b --- /dev/null +++ b/test/sourcemaps/samples/preprocess-no-map/_config.js @@ -0,0 +1,13 @@ +export default { + css_map_sources: [], + preprocess: [ + { + style: ({ content }) => { + return { code: content }; + }, + script: ({ content }) => { + return { code: content }; + } + } + ] +}; diff --git a/test/sourcemaps/samples/preprocess-no-map/input.svelte b/test/sourcemaps/samples/preprocess-no-map/input.svelte new file mode 100644 index 0000000000..febfa5191b --- /dev/null +++ b/test/sourcemaps/samples/preprocess-no-map/input.svelte @@ -0,0 +1,13 @@ + + +
+
{name}
+
+ + diff --git a/test/sourcemaps/samples/preprocess-no-map/test.js b/test/sourcemaps/samples/preprocess-no-map/test.js new file mode 100644 index 0000000000..89afd9961b --- /dev/null +++ b/test/sourcemaps/samples/preprocess-no-map/test.js @@ -0,0 +1,36 @@ +import { assert_mapped, assert_not_mapped } from '../../helpers'; + +export function test({ input, preprocessed }) { + // markup (start) + assert_mapped({ + code: '