sveltejs / language-tools

The Svelte Language Server, and official extensions which use it
MIT License
1.18k stars 190 forks source link

Support for self-closing tags #299

Open TheComputerM opened 3 years ago

TheComputerM commented 3 years ago

As of Svelte v3.24.0, support has been added for self-closing tags: https://github.com/sveltejs/svelte/issues/5080.

However when implementing this, a syntax error is shown, the build is working just fine.

image

I do not know if this is the correct place to file this bug, or prettier-plugin-svelte so please guide me.

Thanks

jasonlyu123 commented 3 years ago

The parser(parse5) we use doesn't recognize the self-closing style tag. probably because it's not valid HTML. Need to find a way to work around it. https://github.com/sveltejs/language-tools/blob/16cece264f4a2a108fc90c3f4ce747d78e109805/packages/svelte2tsx/src/htmlxparser.ts#L22

dummdidumm commented 3 years ago

If it's only about finding the script and styles tag there, we could do the same as in extractTag in svelte-language-server and use the parser of the html-language-server which supports this I think.

jasonlyu123 commented 3 years ago

@dummdidumm what about we also use the preprocess regex in the compiler to remove style in svelte2tsx for now. https://github.com/sveltejs/svelte/blob/a62d7362150bacc6c84fe8c813ba9b8849216763/src/compiler/preprocess/index.ts#L117 The html-language-service approach also has some problem that needs to be taking care of.

dummdidumm commented 3 years ago

Good idea! It's only about blanking them out, finding the top level script is done elsewhere.

dummdidumm commented 3 years ago

I started implementing this for svelte2tsx.parseHtmlx but now found that we actually have more to do than just parse it differently: The support was implemented for preprocessors, not the compiler itself. So that means svelte.compile('<style />') will still throw an error. For us this means we would somehow have to transform /> to ></style while at the same time keeping the source mappings correct. EDIT: Maybe we could just blank the tag out completely then.