-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hooks to manipulate the Svelte compiler #11611
Comments
What does it even mean? Unacceptable value of
Can you elaborate on why you want to restrict the content of snippets? |
Yes, like I say I known that some things can be done with Typescript... Ok here it's just a basic example, but we can imagine some more complex validation where it's harder to check in Typescript. Validating the format with code will allow to detect problems during compilation. $$tag.verify('format', (format) => {
if (format === 'full' || format === 'medium' || format === 'small') {
return true;
}
if (/* code that validate the format */) {
return true;
}
return false;
}, "Bad format");
Just to avoid mistakes when using the tag. A lot of components require respecting a specific tree structure. Ex a <FancyList>
item <!-- Error : tag <li> is required ! -->
</FancyList> Or a <Tabs>
<Tab title="Tab one">
Content of the panel
</Tab>
<MyOtherComponent /> <!-- Error : <MyOtherComponent> is not allowed here -->
</Tabs> Or a <Dialog>
<DialogCloseButton />
<DialogHeader> ... </DialogHeader>
<DialogContent> ... </DialogContent>
<DialogFooter> ... </DialogFooter>
</Tabs> Any help that helps prevent an error is always a good thing. |
And it won't work with variables. If the lib authors care, they can do if (!isValid(propX)) {
if (DEV) {
throw new Error(`${propX} is not valid value for propX!`);
}
propX = defaultPropXValue; // or other way to ignore the new value
} Regarding a specific tree structure, this is what slots/snippets are for - the required tree is completely inside the component and user content is inserted into it. <FancyList {items}><!-- uses <ul> and <li> internally -->
{#snippet listItem(item, i)}
{i+1}. {item}
{/snippet}
</FancyList>
<Tabs tabs={[{
title="Tab one",
content=tab1,
},{
title="Tab two",
content=tab2,
}]} />
{#snippet tab1(title)}
Content of the panel {title}
{/snippet}
{#snippet tab2(title, active)}
<b class:fancy={active}>My fancy tab</b>
{/snippet} Also, actions can be used to validate the DOM tree in the DEV runtime only. |
Also also you can actually do most of this with a custom eslint plugin |
Snippet are great to pass portion of code, but verbose and limited on some aspect. I prefer to have something that looks more like HTML, much clearer.
Maybe, but when I design a Svelte component, I find it logical to define rules there, and not in a plugin from another tool which may not be used or configured. |
Describe the problem
As Svelte is a compiler, it would be nice to allow developper to enhance it by providing some hooks.
This will allow to add some specific requirement at compile time, and specific warning/error.
For exemple, I wrote a component
MyComp
and I want to handle some specific warning/error :Or another
FancyList
component that should only accept<li>
or<FancyItem>
:I would be nice if Svelte (6 ?) allow us to add these sort of hooks into the compiler.
I know that some checks can be done via TypeScript, but this could allow more control.
Describe the proposed solution
We could use a
<script context="compiler">
for that (or a specific method incontext="module"
).The code inside this tag will only be used at compile time by the compiler, and will not be included in the generated code (client or server).
A special variable
$$tag
will allow us to manipulate the compiler, in order to display messages/errors :The
$$tag
should also contain utility methods to check the status of the props :It should also include some function that make basic and classic checks :
Snippets
And a
verify_snippet()
to check the format of the snippets.Some examples :
Importance
nice to have
The text was updated successfully, but these errors were encountered: