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

feat: loose parser mode #14691

Merged
merged 12 commits into from
Dec 14, 2024
Merged

feat: loose parser mode #14691

merged 12 commits into from
Dec 14, 2024

Conversation

dummdidumm
Copy link
Member

This implements a new option for the parser, loose, which means the parser is forgiving with the syntax and tries to continue parsing even if it encounters broken code. It does not parse no matter what, but handles the most common "in the middle of typing" scenarios gracefully.

closes #4818

Our language tools can then make use of this to provide better intellisense in more situations.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Dec 12, 2024

🦋 Changeset detected

Latest commit: e079744

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-svelte-14691-svelte.vercel.app/

this is an automated message

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@14691

@Rich-Harris
Copy link
Member

Should we just always parse in loose mode, and push errors to an array as we go? Then, if loose !== true, at the end of parsing we throw errors[0], otherwise the errors are accessible just as warnings are today, which feels necessary. Might that simplify things?

@dummdidumm
Copy link
Member Author

For language tools I'm not interested in where the first error happened. Logic-wise, it's a few if blocks here and there now, and trading those for not throwing directly in certain cases would not give us much (i.e. we trade less code in one place for almost the same additional code in another). So I vote for keeping it as it's now.

@Rich-Harris
Copy link
Member

Wouldn't you want to show red squigglies for all the errors? And surely if you don't capture the errors during loose parse you need to do a separate strict parse to show just the first error, so we're not even saving any work?

@dummdidumm
Copy link
Member Author

  • I would only want the very first error. All other ones might be wrong since the parser might be in a different state than it should be
  • I have to do a separate parse anyway. I have to run the the compiler, not the parser, in order to get all warnings etc. - and again, the parser could parse things wrong and so the compiler gives wrong warnings. It might also be that the compiler isn't equipped to handle the somewhat broken AST and throws. So even if there is at some point a way for the compiler to receive the AST to save some work, I would only want to go that step when I'm sure that the parser succeeded.

Bottom line, in the future it might make sense to have an error property which returns the very first error in case of loose parsing, but that's about it.

@Rich-Harris Rich-Harris merged commit f0af633 into main Dec 14, 2024
11 checks passed
@Rich-Harris Rich-Harris deleted the loose-parser branch December 14, 2024 14:05
@github-actions github-actions bot mentioned this pull request Dec 14, 2024
dummdidumm added a commit to sveltejs/language-tools that referenced this pull request Dec 17, 2024
…#2631)

This adds support for the Svelte parser with its new loose mode (sveltejs/svelte#14691) and adjusts code paths to make use of it properly. As a result, intellisense should be a lot more useful in situations where code is in the middle of being typed and the Svelte file is in a broken state.
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.

Make parser more robust / error forgiving
2 participants