Conversation
|
| const char = html[i]; | ||
| if (char === '{') { | ||
| depth++; | ||
| } else if (char === '}' && depth > 0) { |
There was a problem hiding this comment.
I'm not sure if the depth > 0 check in the else if here is correct. It looks wrong.
| .substring(moustacheCheckStart, moustacheCheckEnd) | ||
| .lastIndexOf('{', offset); | ||
|
|
||
| const lastMustacheTagStart = index === -1 ? null : moustacheCheckStart + index; |
There was a problem hiding this comment.
The reason this only check a substring is due to performance. It can be expensive to repeatedly check lastIndex. So this can't be removed.
|
Thank you for the contribution. I am not really sure if this is the proper way to fix this. Checking To be honest, I'll suggest you write your script tag at the top. You might not have a good experience by putting the script tag at the bottom. It'll be affected by a syntax error during editing way more than if you put it at the top. |
|
Yeah I wasn't sure if the code was correct either. The decision to write script tags at the top isn't mine unfortunately. We have a prettier config that reorders script tags to the bottom. The "solution" so far has been to manually override this for files that cause a problem, but it isn't ideal. Is there a better way to deal with this? |
This reverts commit 2a08b55.
|
I reverted the AI's code. I tried implementing a fix by detecting type definition patterns when the parser hits |
|
I wonder how costly would be to do a single pass parsing of the whole component to find script lang ts beforehand. |
|
The reason we use vscode-html-languageservice to parse it here instead of the Svelte compiler is mostly that it is more forgiving of syntax errors, at least in the Svelte 3/4 days when this is written. The result AST is also used in various other places. One solution I can think of is to use the Svelte compiler as a fallback when no script tag is found. |
|
From what I understand the problem is not that it doesn't find the script tag but that it start parsing without knowing it has to use TS and that leads to not finding the script tag. So my idea was to do a single pass parsing specifically looking for the script tag only to figure out if we need TS so that we can properly parse everything. |
It shouldn't be. The problem is that the Ideally, we should use a js/ts parse when the html parser encounters |
|
Closing in favor of #2921 - thank you! |
fixes #2854
In the following example, the svelte language server would fail to detect TS when
<script lang="ts">is at the bottom.{#snippet foo({props}: {props?: Record<string, unknown>})}{/snippet} <script lang="ts"></script>I asked Codex to fix the issue and it did. The LSP no longer fails. I made sure to run the tests and nothing broke. I admit I don't understand the code very well, but it seems to work.
Before:

After:
