Skip to content

Overview of approaches on a language server #11

Closed
@dummdidumm

Description

@dummdidumm

This thread is intended for an overview of approaches on how to implement a language server for svelte files. Discussion on a prefered solution can be done here aswell.

Current state of the language server

Currently syntax highlighting and some basic autocompletion works for svelte files.
Rich intellisense however is not provided, does not know about all the typescript files in the workspace, and also has no type information about the svelte components.
The same is true for the html part where special svelte syntax is highlighted but intellisense is not working. The vscode-html-languageservice is used which does not know about the special syntax by default.
The current language server does use the svelte compiler for parsing files and getting diagnostics like "no alt attribute on this img tag".
A more indepth analysis: Comment

Existing approaches/solutions

Disclaimer: I'm not the author of any of the existing solutions, I did look at the code however. If some of the information is wrong or not detailed enough, or you are missing a solution, feel free to comment, I will adjust it.

https://github.com/alexprey/sveltedoc-parser

Uses htmlparser2 to parse the html part of a svelte file. Uses hooks of the html parser to add custom parsing and extract relevant information.
Uses espree to parse the script parts of a svelte file. Then walks the AST to extract relevant information.
Provides the results in a JSON-Format.

https://github.com/ArdenIvanov/svelte-intellisense uses it to parse svelte files. It uses the result to attach some additional behavior.

Since this uses a javascript parser, it will not work with typescript.

https://github.com/halfnelson/svelte2tsx

Uses svelte's compiler to parse the HTMLx part of a svelte component. Then transforms the HTMLx plus the original script part to a tsx file with a self-written transformer.

https://github.com/simlrh/svelte-language-server/blob/feature/extend-ts-support/src/plugins/ts-svelte/service.ts (a fork from svelte-language-server) uses it to create shadow-tsx-files of svelte-files. It then uses typescript's language service, but proxies the requests: It replaces the original file path with the file path to the generated tsx-file. The typescript language server therefore checks the generated tsx files. Results are transformed back to get correct document positions.

https://github.com/halfnelson/svelte-type-checker-vscode is another language server using the svelte2tsx-library.

https://github.com/marcus-sa/svelte-ts

Description taken from implementer's comment:

Compiler:

  1. The compiler reads all the source files
  2. Everything except the content of the script tags gets thrown away
  3. The script source code gets compiled to valid JS and declaration files.
  4. Jumps back to step 2, where it replaces the TS source in script tags with the compiled JS
  5. The Svelte compiler compiles the entire source file
  6. The output compilation of above step gets cached, where the HTML AST is then used in the type checker.

It consumes the exported variables and functions from inside a Svelte script, and then generates it as a declaration files which contains a class that extends the runtime SvelteComponent.

Typechecker:

  1. The type checker finds all components in the cached AST.
  2. Checks all components in the template is a valid class which extends SvelteComponent
  3. Checks for valid declarations, properties etc.

Other approaches

See comments of this thread.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions