-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: add print(...) function
#16188
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
base: main
Are you sure you want to change the base?
Conversation
|
|
Nice! I'll be happy to sunset |
manuel3108
left a comment
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.
This already works super great.
I updated my pr for sv sveltejs/cli#568 to not only use parse but also print, and i basically didn't need to change anything in comparison to svelte-ast-print. I'm testing this with the paraglide addon, that is now printed nearly correctly, apart from the quoting issue mentioned below.
Apart from that we still need to figure out formatting, as the paraglide addon is currently printed in one line. This happens because I manually mutate the ast during the runtime and then print the ast.
I know all of this is in very early stages, but wanted to already leave some feedback
|
elliott-with-the-longest-name-on-github
left a comment
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.
overall lgtm, but would be good to get a second pair of eyes...
packages/svelte/package.json
Outdated
| "clsx": "^2.1.1", | ||
| "esm-env": "^1.2.1", | ||
| "esrap": "^1.4.8", | ||
| "esrap": "https://pkg.pr.new/sveltejs/esrap@17c22f5", |
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.
are we shipping this with a pkg.pr.new dep?
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.
no, this PR is placeholder — it exists primarily to validate that the new esrap API makes sense
packages/svelte/src/compiler/phases/3-transform/client/transform-client.js
Show resolved
Hide resolved
realized I approved instead of commenting; will re-review after it's out of draft
Over on sveltejs/esrap#68 we're working on making
esrappluggable, so that it can be used to print any AST composed of{ type: string, ... }nodes rather than justestreeand its TypeScript extensions. That includes Svelte ASTs.The main motivation for exposing this is so that we can make it easier to write preprocessors. Historically, Svelte exposed a
preprocessAPI, but it's all strings and duct tape, and it's difficult to integrate preprocessors cleanly with bundlers as we've seen withenhanced-img.When the preprocessor API was introduced, things looked very different. Preprocessing was necessary to support things like TypeScript and Sass. Today, TypeScript is supported natively, and CSS is sufficiently capable that Sass is little more than a historical curiosity.
In the long term, we'd therefore like to move away from the preprocessor API in favour of providing more robust lower-level utilities. For example
enhanced-img, which can only be used in a Vite context, really should just be a Vite plugin:There are other potential uses, such as migrations or
sv addor having a 'format' button in the playground.As a side-effect, quality of compiler output will be slightly better in certain cases, such as when encountering comments inside nodes. (Today, we attach
leadingCommentsandtrailingCommentsto each node, but this is a brittle and not-very-widely-used convention. The newesrapAPI expects an array ofcommentsto be passed instead.)This functionality already exists in
svelte-ast-print(thank you @xeho91!), but having it in core means we can re-use theesrapversion that's already installed alongsidesvelte, and will help ensure it stays current with new features.Before submitting the PR, please make sure you do the following
feat:,fix:,chore:, ordocs:.packages/svelte/src, add a changeset (npx changeset).Tests and linting
pnpm testand lint the project withpnpm lint