Skip to content

Commit 04d52b7

Browse files
authored
fix: treat a script tag as top-level if it's the first tag in file (#2886)
1 parent d085830 commit 04d52b7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

.changeset/plenty-moments-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-language-server': patch
3+
---
4+
5+
fix: always treat a script tag as top-level if it's the first tag in the file

packages/language-server/src/lib/documents/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ function extractTags(
7272
* If that is BEFORE `{#X`, we are inside a moustache tag.
7373
*/
7474
function isNotInsideControlFlowTag(tag: Node) {
75+
const tagIndex = rootNodes.indexOf(tag);
76+
// Quick check: if the tag has nothing before it, it can't be inside a control flow tag
77+
// This also works around a case where the tag is treated as under a control flow tag when vscode-html-languageservice parses something wrong
78+
if (tagIndex === 0) {
79+
const startContent = text.substring(0, tag.start);
80+
if (startContent.trim() === '') {
81+
return true;
82+
}
83+
}
7584
const nodes = rootNodes.slice(rootNodes.indexOf(tag));
7685
const rootContentAfterTag = nodes
7786
.map((node, idx) => {

packages/language-server/test/lib/documents/utils.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('document/utils', () => {
5050
assert.deepStrictEqual(extractStyleTag(text), null);
5151
});
5252

53-
it('is canse sensitive to style/script', () => {
53+
it('is case sensitive to style/script', () => {
5454
const text = `
5555
<Style></Style>
5656
<Script></Script>
@@ -344,6 +344,20 @@ describe('document/utils', () => {
344344
container: { start: 151, end: 181 }
345345
});
346346
});
347+
348+
it('extract tag correctly if nothing is before the tag', () => {
349+
const text = `<script>let value = 2</script>
350+
{/if}`;
351+
assert.deepStrictEqual(extractScriptTags(text)?.script, {
352+
content: 'let value = 2',
353+
attributes: {},
354+
start: 8,
355+
end: 21,
356+
startPos: Position.create(0, 8),
357+
endPos: Position.create(0, 21),
358+
container: { start: 0, end: 30 }
359+
});
360+
});
347361
});
348362

349363
describe('#getLineAtPosition', () => {

0 commit comments

Comments
 (0)