Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0f6348e

Browse files
authoredOct 30, 2021
Fix wrong location when having multiple <script> (#132)
* Fix wrong location when having multiple `<script>` * Update fixtures
1 parent 88539f6 commit 0f6348e

File tree

23 files changed

+3760
-19
lines changed

23 files changed

+3760
-19
lines changed
 

‎src/script-setup/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ export function parseScriptSetupElements(
289289
}
290290
result.ast.body.sort((a, b) => a.range[0] - b.range[0])
291291

292+
const programStartOffset = result.ast.body.reduce(
293+
(start, node) => Math.min(start, node.range[0]),
294+
result.ast.range[0],
295+
)
296+
result.ast.range[0] = programStartOffset
297+
result.ast.loc.start =
298+
locationCalculator.getLocFromIndex(programStartOffset)
299+
if (result.ast.start != null) {
300+
result.ast.start = [scriptSetupElement, scriptElement].reduce(
301+
(start, node) => {
302+
const textNode = node.children[0]
303+
return Math.min(
304+
start,
305+
textNode != null && textNode.type === "VText"
306+
? textNode.range[0]
307+
: node.startTag.range[1],
308+
)
309+
},
310+
result.ast.start,
311+
)
312+
}
313+
292314
const programEndOffset = result.ast.body.reduce(
293315
(end, node) => Math.max(end, node.range[1]),
294316
0,
@@ -303,7 +325,7 @@ export function parseScriptSetupElements(
303325
end,
304326
textNode != null && textNode.type === "VText"
305327
? textNode.range[1]
306-
: node.endTag?.range[1] ?? node.range[1],
328+
: node.endTag?.range[0] ?? node.range[1],
307329
)
308330
},
309331
0,

‎test/fixtures/ast/multiple-scripts-10/ast.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "Program",
3-
"start": 9,
4-
"end": 57,
3+
"start": 8,
4+
"end": 48,
55
"loc": {
66
"start": {
77
"line": 2,

0 commit comments

Comments
 (0)
Please sign in to comment.