Skip to content
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

Expose parse to the public #2534

Merged
merged 2 commits into from
May 4, 2019
Merged

Expose parse to the public #2534

merged 2 commits into from
May 4, 2019

Conversation

clebert
Copy link
Contributor

@clebert clebert commented Apr 23, 2019

I am currently working on making the prettier-plugin-svelte compatible with Svelte 3. I noticed that compile has to be called to get the AST. This leads to errors in some tests, because they cannot be compiled.

For example, the following test: https://github.com/UnwrittenFun/prettier-plugin-svelte/blob/master/test/printer/samples/binding-shorthand.html
Error: value is not declared

But regardless of my use case, I think it makes sense to get the AST without a compilation step.

@Conduitry
Copy link
Member

If svelte.compile fails, then it's an invalid component (or there's a bug). Do other Prettier plugins support formatting malformed or invalid files?

This isn't to take away from the idea of exposing svelte.parse, but imo that test will need to be updated for v3 because it's no longer a valid component.

@halfnelson
Copy link
Contributor

As a workaround, I have extracted the parser from svelte as a module here
https://github.com/halfnelson/svelte-component-parser

@clebert
Copy link
Contributor Author

clebert commented Apr 24, 2019

@Conduitry I think only the syntactic correctness of a program is important for a formatter. A program does not have to be semantically correct. Of course it is a question of definition, if an undeclared variable is bound, whether it is a semantic or syntactic error? I plead on the former. I can also call a misspelled function in the JavaScript part, and it will still be formatted.

I might create an issue for further discussion. I will temporarily use svelte-component-parser from @halfnelson.

@clebert
Copy link
Contributor Author

clebert commented Apr 29, 2019

I didn't find svelte-component-parser on npm. I have therefore published svelte-parser, and will maintain it until this PR is merged or the parser is made accessible in another way.

@Conduitry
Copy link
Member

I've come around to thinking that it would be nice to expose just the parser, without any of the additional validation that compile performs. The cost seems pretty low, as I don't think this ties us down any more than having ast in the compiler response already did. I'll defer to whatever @Rich-Harris thinks though.

@Rich-Harris Rich-Harris merged commit d3d5fa9 into sveltejs:master May 4, 2019
@Rich-Harris
Copy link
Member

Thanks, look forward to checking out prettier-plugin-svelte 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants