Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-moments-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-language-server': patch
---

fix: always treat a script tag as top-level if it's the first tag in the file
9 changes: 9 additions & 0 deletions packages/language-server/src/lib/documents/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ function extractTags(
* If that is BEFORE `{#X`, we are inside a moustache tag.
*/
function isNotInsideControlFlowTag(tag: Node) {
const tagIndex = rootNodes.indexOf(tag);
// Quick check: if the tag has nothing before it, it can't be inside a control flow tag
// This also works around a case where the tag is treated as under a control flow tag when vscode-html-languageservice parses something wrong
if (tagIndex === 0) {
const startContent = text.substring(0, tag.start);
if (startContent.trim() === '') {
return true;
}
}
const nodes = rootNodes.slice(rootNodes.indexOf(tag));
const rootContentAfterTag = nodes
.map((node, idx) => {
Expand Down
16 changes: 15 additions & 1 deletion packages/language-server/test/lib/documents/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('document/utils', () => {
assert.deepStrictEqual(extractStyleTag(text), null);
});

it('is canse sensitive to style/script', () => {
it('is case sensitive to style/script', () => {
const text = `
<Style></Style>
<Script></Script>
Expand Down Expand Up @@ -344,6 +344,20 @@ describe('document/utils', () => {
container: { start: 151, end: 181 }
});
});

it('extract tag correctly if nothing is before the tag', () => {
const text = `<script>let value = 2</script>
{/if}`;
assert.deepStrictEqual(extractScriptTags(text)?.script, {
content: 'let value = 2',
attributes: {},
start: 8,
end: 21,
startPos: Position.create(0, 8),
endPos: Position.create(0, 21),
container: { start: 0, end: 30 }
});
});
});

describe('#getLineAtPosition', () => {
Expand Down