Skip to content

fix: handle multiple snippet parameters with one or more being optional #10833

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

Merged
merged 2 commits into from
Mar 22, 2024

Conversation

dummdidumm
Copy link
Member

fixes #10825

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.

Tests and linting

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

Copy link

changeset-bot bot commented Mar 18, 2024

🦋 Changeset detected

Latest commit: f97dbca

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

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

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

// If this is a type annotation for a function parameter, Acorn-TS will treat subsequent
// parameters as part of a sequence expression instead, and will then error on optional
// parameters (`?:`). Therefore replace that sequence with something that will not error.
parser.template.slice(parser.index).replace(/\?\s*:/g, ':');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be safe, this combination is only relevant syntax-wise for optional parameters, there's nothing that should get invalid anywhere else with regards Acorn-TS parsing this - or am I overlooking something?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's pretty odd to break things up in the following way, it doesn't support comments:

const f = (n? // foo
  : number): number => n ?? 0 + 1

const g = (n? /* foo */ : number): number => n ?? 0 + 1

Those are both valid in normal TypeScript, but broken with this solution.

It also still supports invalid parameter orderings, such as the first being optional and the second being required. Not sure if that's a separate issue or one that this aims to fix (if it even is an issue - it might just be the repl not caring).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into both the points you made:

  • Supporting comments here would be rare as this is within the template itself – where no one adds comments.
  • The invalid ordering of params isn't such an issue here I don't think. This is something for language tools maybe rather than the parser.

// If this is a type annotation for a function parameter, Acorn-TS will treat subsequent
// parameters as part of a sequence expression instead, and will then error on optional
// parameters (`?:`). Therefore replace that sequence with something that will not error.
parser.template.slice(parser.index).replace(/\?\s*:/g, ':');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into both the points you made:

  • Supporting comments here would be rare as this is within the template itself – where no one adds comments.
  • The invalid ordering of params isn't such an issue here I don't think. This is something for language tools maybe rather than the parser.

@trueadm trueadm merged commit b468978 into main Mar 22, 2024
@trueadm trueadm deleted the snippet-types branch March 22, 2024 17:52
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.

Svelte 5: [reopen] Optional parameters in snippets don't work
3 participants