-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: add a way to compile from an AST #8804
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
Conversation
Allows the developer to compile directly from an AST, returned previously from `svelte.parse`. There is currently no way to compile a modified AST to source code, this adds a function, `compileAST`, that provides that functionality. Notes: - The second argument to `new Component` is currently set to null, this might break something, but for our purposes seems to be working. - `@param {object}` could be typed better. - `compile` could call `compileAST` underneath so we don't have to copy/paste code. Hopefully someone has useful feedback!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As suggested it would be if compileAst
would be called by compile
if that doesn't result in more complex code.
Since there's no previous issue for this: What's the use case for this feature?
Thank you for the quick response! Hopefully those patches address your review notes appropriately.
We're implementing an internal GUI tool to modify Svelte files similar to #5972. This tool gets the AST with Although the AST isn't a public API, because
My only concern with this is that we'd be always passing |
@Rich-Harris is there any chance this could be included in svelte 5? |
The use case for this feature would be to be able to do AST transforms. It is much easier to do transforms on an AST than on strings using regexes. Here is a little example: <script>
import Component2 from './Component2.svelte'
let name = 'world';
</script>
<h1>Hello {name}!</h1>
{#if name}
{@const someVar = "literal"}
<Component2 some-prop="literal" some-other-prop={someVar}>test</Component2>
{/if} That I'd like to transform to: <script>
import Component2 from './Component2.svelte'
import {importedVar} from './some/path.js'
let name = 'world';
</script>
<h1>Hello {name}!</h1>
{#if name}
{@const someVar = "literal"}
<Component2 some-prop={importedVar} some-other-prop={someVar}>test</Component2>
{/if} Now, that means I want to replace ONLY the attribute literal value I could do that by finding the AST If we need to do this with something like @Rich-Harris Magic-String library, it gets complicated very quickly. ![]() There are several use cases for wanting to transform the Svelte AST, e.g. we (@ivanhofer and me) were using AST transforms for inlangs i18n library. @Rich-Harris what is your view on this? |
One way to achieve printing the AST would be to write code generators for all of these types: And then use these generators in https://github.com/davidbonnet/astring?tab=readme-ov-file#extending Edit:Maybe a relatively easy way to achieve svelte AST printing is to take the svelte prettier plugin and adapting the |
Just so everybody who ends up here may know, this library is now a reality: https://github.com/xeho91/svelte-ast-print |
Allows the developer to compile directly from an AST, returned previously from
svelte.parse
.There is currently no way to compile a modified AST to source code, this adds a function,
compileAST
, that provides that functionality.Notes:
new Component
is currently set to null, this might break something, but for our purposes seems to be working.@param {object}
could be typed better.compile
could callcompileAST
underneath so we don't have to copy/paste code.Hopefully someone has useful feedback!