Fix and cover case when preprocessor returns no source map

pull/5754/head
dmitrage 5 years ago
parent 20499ee77e
commit 35d7628e13

@ -167,11 +167,13 @@ export class StringWithSourcemap {
} }
static from_processed(string: string, map?: DecodedSourceMap): StringWithSourcemap { static from_processed(string: string, map?: DecodedSourceMap): StringWithSourcemap {
const line_count = string.split('\n').length;
if (map) { if (map) {
// ensure that count of source map mappings lines // ensure that count of source map mappings lines
// is equal to count of generated code lines // is equal to count of generated code lines
// (some tools may produce less) // (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++) { for (let i = 0; i < missing_lines; i++) {
map.mappings.push([]); map.mappings.push([]);
} }
@ -182,7 +184,6 @@ export class StringWithSourcemap {
map = { version: 3, names: [], sources: [], mappings: [] }; map = { version: 3, names: [], sources: [], mappings: [] };
// add empty SourceMapSegment[] for every line // 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([]); for (let i = 0; i < line_count; i++) map.mappings.push([]);
return new StringWithSourcemap(string, map); return new StringWithSourcemap(string, map);
} }

@ -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( export function assert_not_located(
code: string, code: string,
locate: ReturnType<typeof getLocator>, locate: ReturnType<typeof getLocator>,

@ -0,0 +1,13 @@
export default {
css_map_sources: [],
preprocess: [
{
style: ({ content }) => {
return { code: content };
},
script: ({ content }) => {
return { code: content };
}
}
]
};

@ -0,0 +1,13 @@
<script>
export let name;
console.log(name);
</script>
<main>
<div>{name}</div>
</main>
<style>
main { font-weight: bold; }
</style>

@ -0,0 +1,36 @@
import { assert_mapped, assert_not_mapped } from '../../helpers';
export function test({ input, preprocessed }) {
// markup (start)
assert_mapped({
code: '<script>',
input: input.locate,
preprocessed
});
// script
assert_not_mapped({
code: 'console.log(name);',
preprocessed
});
// markup (middle)
assert_mapped({
code: '<div>{name}</div>',
input: input.locate,
preprocessed
});
// style content
assert_not_mapped({
code: 'font-weight: bold;',
preprocessed
});
// markup (end)
assert_mapped({
code: '</style>',
input: input.locate,
preprocessed
});
}
Loading…
Cancel
Save