fix line numbers for context node for sourcemap

pull/4596/head
Tan Li Hau 5 years ago
parent 794479664d
commit 78c5af9a52

@ -62,8 +62,18 @@ export default function read_context(
const pattern_string = parser.template.slice(start, i); const pattern_string = parser.template.slice(start, i);
try { 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( return (parse_expression_at(
`${" ".repeat(start - 1)}(${pattern_string} = 1)`, `${space_with_newline}(${pattern_string} = 1)`,
start - 1 start - 1
) as any).left; ) as any).left;
} catch (error) { } catch (error) {

@ -1,3 +1,7 @@
<script>
export let animals;
</script>
{#each animals as [key, value, ...rest]} {#each animals as [key, value, ...rest]}
<p>{key}: {value}</p> <p>{key}: {value}</p>
{/each} {/each}

@ -1,24 +1,31 @@
{ {
"html": { "html": {
"start": 0, "start": 41,
"end": 71, "end": 112,
"type": "Fragment", "type": "Fragment",
"children": [ "children": [
{ {
"start": 0, "start": 39,
"end": 71, "end": 41,
"type": "Text",
"raw": "\n\n",
"data": "\n\n"
},
{
"start": 41,
"end": 112,
"type": "EachBlock", "type": "EachBlock",
"expression": { "expression": {
"type": "Identifier", "type": "Identifier",
"start": 7, "start": 48,
"end": 14, "end": 55,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 5,
"column": 7 "column": 7
}, },
"end": { "end": {
"line": 1, "line": 5,
"column": 14 "column": 14
} }
}, },
@ -26,27 +33,27 @@
}, },
"children": [ "children": [
{ {
"start": 42, "start": 83,
"end": 63, "end": 104,
"type": "Element", "type": "Element",
"name": "p", "name": "p",
"attributes": [], "attributes": [],
"children": [ "children": [
{ {
"start": 45, "start": 86,
"end": 50, "end": 91,
"type": "MustacheTag", "type": "MustacheTag",
"expression": { "expression": {
"type": "Identifier", "type": "Identifier",
"start": 46, "start": 87,
"end": 49, "end": 90,
"loc": { "loc": {
"start": { "start": {
"line": 2, "line": 6,
"column": 5 "column": 5
}, },
"end": { "end": {
"line": 2, "line": 6,
"column": 8 "column": 8
} }
}, },
@ -54,27 +61,27 @@
} }
}, },
{ {
"start": 50, "start": 91,
"end": 52, "end": 93,
"type": "Text", "type": "Text",
"raw": ": ", "raw": ": ",
"data": ": " "data": ": "
}, },
{ {
"start": 52, "start": 93,
"end": 59, "end": 100,
"type": "MustacheTag", "type": "MustacheTag",
"expression": { "expression": {
"type": "Identifier", "type": "Identifier",
"start": 53, "start": 94,
"end": 58, "end": 99,
"loc": { "loc": {
"start": { "start": {
"line": 2, "line": 6,
"column": 12 "column": 12
}, },
"end": { "end": {
"line": 2, "line": 6,
"column": 17 "column": 17
} }
}, },
@ -86,77 +93,77 @@
], ],
"context": { "context": {
"type": "ArrayPattern", "type": "ArrayPattern",
"start": 18, "start": 59,
"end": 39, "end": 80,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 5,
"column": 18 "column": 19
}, },
"end": { "end": {
"line": 1, "line": 5,
"column": 39 "column": 40
} }
}, },
"elements": [ "elements": [
{ {
"type": "Identifier", "type": "Identifier",
"start": 19, "start": 60,
"end": 22, "end": 63,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 5,
"column": 19 "column": 20
}, },
"end": { "end": {
"line": 1, "line": 5,
"column": 22 "column": 23
} }
}, },
"name": "key" "name": "key"
}, },
{ {
"type": "Identifier", "type": "Identifier",
"start": 24, "start": 65,
"end": 29, "end": 70,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 5,
"column": 24 "column": 25
}, },
"end": { "end": {
"line": 1, "line": 5,
"column": 29 "column": 30
} }
}, },
"name": "value" "name": "value"
}, },
{ {
"type": "RestElement", "type": "RestElement",
"start": 31, "start": 72,
"end": 38, "end": 79,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 5,
"column": 31 "column": 32
}, },
"end": { "end": {
"line": 1, "line": 5,
"column": 38 "column": 39
} }
}, },
"argument": { "argument": {
"type": "Identifier", "type": "Identifier",
"start": 34, "start": 75,
"end": 38, "end": 79,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 5,
"column": 34 "column": 35
}, },
"end": { "end": {
"line": 1, "line": 5,
"column": 38 "column": 39
} }
}, },
"name": "rest" "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"
}
} }
} }
Loading…
Cancel
Save