From 78c5af9a527dce16d554bbb3330928de7f822023 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 10 May 2020 22:45:25 +0800 Subject: [PATCH] fix line numbers for context node for sourcemap --- src/compiler/parse/read/context.ts | 12 +- .../each-block-destructured/input.svelte | 4 + .../each-block-destructured/output.json | 206 +++++++++++++----- 3 files changed, 167 insertions(+), 55 deletions(-) diff --git a/src/compiler/parse/read/context.ts b/src/compiler/parse/read/context.ts index a11c6933c0..8d28bd2024 100644 --- a/src/compiler/parse/read/context.ts +++ b/src/compiler/parse/read/context.ts @@ -62,8 +62,18 @@ export default function read_context( const pattern_string = parser.template.slice(start, i); try { + // the length of the `space_with_newline` has to be start - 1 + // because we added a `(` in front of the pattern_string, + // which shifted the entire string to right by 1 + // so we offset it by removing 1 character in the `space_with_newline` + // to achieve that, we remove the 1st space encountered, + // so it will not affect the `column` of the node + let space_with_newline = parser.template.slice(0, start).replace(/[^\n]/g, ' '); + const first_space = space_with_newline.indexOf(' '); + space_with_newline = space_with_newline.slice(0, first_space) + space_with_newline.slice(first_space + 1); + return (parse_expression_at( - `${" ".repeat(start - 1)}(${pattern_string} = 1)`, + `${space_with_newline}(${pattern_string} = 1)`, start - 1 ) as any).left; } catch (error) { diff --git a/test/parser/samples/each-block-destructured/input.svelte b/test/parser/samples/each-block-destructured/input.svelte index 09e9885be0..5313050fa0 100644 --- a/test/parser/samples/each-block-destructured/input.svelte +++ b/test/parser/samples/each-block-destructured/input.svelte @@ -1,3 +1,7 @@ + + {#each animals as [key, value, ...rest]}

{key}: {value}

{/each} diff --git a/test/parser/samples/each-block-destructured/output.json b/test/parser/samples/each-block-destructured/output.json index ce263226f1..69e165c0a4 100644 --- a/test/parser/samples/each-block-destructured/output.json +++ b/test/parser/samples/each-block-destructured/output.json @@ -1,24 +1,31 @@ { "html": { - "start": 0, - "end": 71, + "start": 41, + "end": 112, "type": "Fragment", "children": [ { - "start": 0, - "end": 71, + "start": 39, + "end": 41, + "type": "Text", + "raw": "\n\n", + "data": "\n\n" + }, + { + "start": 41, + "end": 112, "type": "EachBlock", "expression": { "type": "Identifier", - "start": 7, - "end": 14, + "start": 48, + "end": 55, "loc": { "start": { - "line": 1, + "line": 5, "column": 7 }, "end": { - "line": 1, + "line": 5, "column": 14 } }, @@ -26,27 +33,27 @@ }, "children": [ { - "start": 42, - "end": 63, + "start": 83, + "end": 104, "type": "Element", "name": "p", "attributes": [], "children": [ { - "start": 45, - "end": 50, + "start": 86, + "end": 91, "type": "MustacheTag", "expression": { "type": "Identifier", - "start": 46, - "end": 49, + "start": 87, + "end": 90, "loc": { "start": { - "line": 2, + "line": 6, "column": 5 }, "end": { - "line": 2, + "line": 6, "column": 8 } }, @@ -54,27 +61,27 @@ } }, { - "start": 50, - "end": 52, + "start": 91, + "end": 93, "type": "Text", "raw": ": ", "data": ": " }, { - "start": 52, - "end": 59, + "start": 93, + "end": 100, "type": "MustacheTag", "expression": { "type": "Identifier", - "start": 53, - "end": 58, + "start": 94, + "end": 99, "loc": { "start": { - "line": 2, + "line": 6, "column": 12 }, "end": { - "line": 2, + "line": 6, "column": 17 } }, @@ -86,77 +93,77 @@ ], "context": { "type": "ArrayPattern", - "start": 18, - "end": 39, + "start": 59, + "end": 80, "loc": { "start": { - "line": 1, - "column": 18 + "line": 5, + "column": 19 }, "end": { - "line": 1, - "column": 39 + "line": 5, + "column": 40 } }, "elements": [ { "type": "Identifier", - "start": 19, - "end": 22, + "start": 60, + "end": 63, "loc": { "start": { - "line": 1, - "column": 19 + "line": 5, + "column": 20 }, "end": { - "line": 1, - "column": 22 + "line": 5, + "column": 23 } }, "name": "key" }, { "type": "Identifier", - "start": 24, - "end": 29, + "start": 65, + "end": 70, "loc": { "start": { - "line": 1, - "column": 24 + "line": 5, + "column": 25 }, "end": { - "line": 1, - "column": 29 + "line": 5, + "column": 30 } }, "name": "value" }, { "type": "RestElement", - "start": 31, - "end": 38, + "start": 72, + "end": 79, "loc": { "start": { - "line": 1, - "column": 31 + "line": 5, + "column": 32 }, "end": { - "line": 1, - "column": 38 + "line": 5, + "column": 39 } }, "argument": { "type": "Identifier", - "start": 34, - "end": 38, + "start": 75, + "end": 79, "loc": { "start": { - "line": 1, - "column": 34 + "line": 5, + "column": 35 }, "end": { - "line": 1, - "column": 38 + "line": 5, + "column": 39 } }, "name": "rest" @@ -166,5 +173,96 @@ } } ] + }, + "instance": { + "type": "Script", + "start": 0, + "end": 39, + "context": "default", + "content": { + "type": "Program", + "start": 8, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 10, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "declaration": { + "type": "VariableDeclaration", + "start": 17, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 21, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "name": "animals" + }, + "init": null + } + ], + "kind": "let" + }, + "specifiers": [], + "source": null + } + ], + "sourceType": "module" + } } } \ No newline at end of file