chore: remove locate_1 helper

pull/8381/head
Ben McCann 4 years ago
parent fb5f281e9a
commit a4fe3663af

@ -24,7 +24,7 @@ export function assert_mapped(
`failed to locate "${input_code}" in "${filename}"` `failed to locate "${input_code}" in "${filename}"`
); );
const transformed_loc = preprocessed.locate_1(code); const transformed_loc = preprocessed.locate(code);
assert.notEqual( assert.notEqual(
transformed_loc, transformed_loc,
undefined, undefined,
@ -36,7 +36,7 @@ export function assert_mapped(
{ {
source: filename, source: filename,
name: null, name: null,
line: source_loc.line + 1, line: source_loc.line,
column: source_loc.column column: source_loc.column
}, },
`incorrect mappings for "${input_code}" in "${filename}"` `incorrect mappings for "${input_code}" in "${filename}"`
@ -54,7 +54,7 @@ export function assert_not_mapped(
) { ) {
if (filename === undefined) filename = 'input.svelte'; if (filename === undefined) filename = 'input.svelte';
const transformed_loc = preprocessed.locate_1(code); const transformed_loc = preprocessed.locate(code);
assert.notEqual( assert.notEqual(
transformed_loc, transformed_loc,
undefined, undefined,

@ -31,8 +31,7 @@ describe('sourcemaps', () => {
const inputCode = fs.readFileSync(inputFile, 'utf-8'); const inputCode = fs.readFileSync(inputFile, 'utf-8');
const input = { const input = {
code: inputCode, code: inputCode,
locate: getLocator(inputCode), locate: getLocator(inputCode)
locate_1: getLocator(inputCode, { offsetLine: 1 })
}; };
const preprocessed = await svelte.preprocess( const preprocessed = await svelte.preprocess(
@ -100,20 +99,14 @@ describe('sourcemaps', () => {
); );
} }
// use locate_1 with mapConsumer:
// lines are one-based, columns are zero-based
preprocessed.mapConsumer = preprocessed.map && await new SourceMapConsumer(preprocessed.map); preprocessed.mapConsumer = preprocessed.map && await new SourceMapConsumer(preprocessed.map);
preprocessed.locate = getLocator(preprocessed.code); preprocessed.locate = getLocator(preprocessed.code);
preprocessed.locate_1 = getLocator(preprocessed.code, { offsetLine: 1 });
js.mapConsumer = js.map && await new SourceMapConsumer(js.map); js.mapConsumer = js.map && await new SourceMapConsumer(js.map);
js.locate = getLocator(js.code); js.locate = getLocator(js.code);
js.locate_1 = getLocator(js.code, { offsetLine: 1 });
css.mapConsumer = css.map && await new SourceMapConsumer(css.map); css.mapConsumer = css.map && await new SourceMapConsumer(css.map);
css.locate = getLocator(css.code || ''); css.locate = getLocator(css.code || '');
css.locate_1 = getLocator(css.code || '', { offsetLine: 1 });
await test({ assert, input, preprocessed, js, css }); await test({ assert, input, preprocessed, js, css });
}); });
}); });

@ -9,10 +9,10 @@ export function test({ input, css, js }) {
out_obj = js; out_obj = js;
// we need the second occurrence of 'done_replace_script_2' in output.js // we need the second occurrence of 'done_replace_script_2' in output.js
// the first occurrence is mapped back to markup '{done_replace_script_2}' // the first occurrence is mapped back to markup '{done_replace_script_2}'
loc_output = out_obj.locate_1('done_replace_script_2'); loc_output = out_obj.locate('done_replace_script_2');
loc_output = out_obj.locate_1('done_replace_script_2', loc_output.character + 1); loc_output = out_obj.locate('done_replace_script_2', loc_output.character + 1);
actual = out_obj.mapConsumer.originalPositionFor(loc_output); actual = out_obj.mapConsumer.originalPositionFor(loc_output);
loc_input = input.locate_1('replace_me_script'); loc_input = input.locate('replace_me_script');
expected = { expected = {
source: 'input.svelte', source: 'input.svelte',
name: 'replace_me_script', name: 'replace_me_script',
@ -21,9 +21,9 @@ export function test({ input, css, js }) {
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
out_obj = css; out_obj = css;
loc_output = out_obj.locate_1('.done_replace_style_2'); loc_output = out_obj.locate('.done_replace_style_2');
actual = out_obj.mapConsumer.originalPositionFor(loc_output); actual = out_obj.mapConsumer.originalPositionFor(loc_output);
loc_input = input.locate_1('.replace_me_style'); loc_input = input.locate('.replace_me_style');
expected = { expected = {
source: 'input.svelte', source: 'input.svelte',
name: '.replace_me_style', name: '.replace_me_style',

@ -19,17 +19,16 @@ export async function test({ assert, css, js }) {
const sourcefile = 'input.svelte'; const sourcefile = 'input.svelte';
[ [
// TODO: get line and col num from input.svelte rather than hardcoding here // TODO: get line and col num from input.svelte rather than hardcoding here
[css, '--keep-me', 13, 2], [css, '--keep-me', 12, 2],
// TODO: these should be 7, 2 and 10, 2 // TODO: these should be 6, 2 and 9, 2
// we use locate_1 which means lines are 1-indexed and cols are 0-indexed // source maps are 0-indexed. each tab is 1 col
// each tab is 1 col [css, '--done-replace-once', 5, 4],
[css, '--done-replace-once', 6, 4], [css, '--done-replace-twice', 8, 4]
[css, '--done-replace-twice', 9, 4]
] ]
.forEach(([where, content, line, column]) => { .forEach(([where, content, line, column]) => {
assert.deepEqual( assert.deepEqual(
where.mapConsumer.originalPositionFor( where.mapConsumer.originalPositionFor(
where.locate_1(content) where.locate(content)
), ),
{ {
source: sourcefile, source: sourcefile,

@ -14,12 +14,12 @@ export function test({ assert, preprocessed, js }) {
assert.deepEqual( assert.deepEqual(
where.mapConsumer.originalPositionFor( where.mapConsumer.originalPositionFor(
where.locate_1(content) where.locate(content)
), ),
{ {
source: sourcefile, source: sourcefile,
name: null, name: null,
line: 1, line: 0,
column column
}, },
`failed to locate "${content}" from "${sourcefile}"` `failed to locate "${content}" from "${sourcefile}"`

@ -17,5 +17,5 @@ export function test({ input, preprocessed }) {
}); });
// TS types, removed // TS types, removed
assert_not_located('ITimeoutDestroyer', preprocessed.locate_1); assert_not_located('ITimeoutDestroyer', preprocessed.locate);
} }

Loading…
Cancel
Save