Skip to content

feat: warn on obvious legacy component instantiation #12648

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 3 commits into from
Jul 29, 2024
Merged

Conversation

dummdidumm
Copy link
Member

Adds a compiler warning that warns about legacy component instantiation (i.e. using new Component(..)). This won't catch all cases, but the most obvious ones which probably make up ~80%

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

Adds a compiler warning that warns about legacy component instantiation (i.e. using `new Component(..)`). This won't catch all cases, but the most obvious ones which probably make up ~80%
Copy link

changeset-bot bot commented Jul 29, 2024

⚠️ No Changeset found

Latest commit: 0dfdf1e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@Rich-Harris
Copy link
Member

False negatives are okay but we should be careful to avoid false positives.

Are we concerned that people might rely on extensionless imports, i.e. importing file.svelte.js as ./file.svelte?

@paoloricciuti
Copy link
Member

Are we concerned that people might rely on extensionless imports, i.e. importing file.svelte.js as ./file.svelte?

In my experience it really depends on the editor and tsconfig setup...I let my editor auto complete and sometimes it import extension less. We could change the generated tsconfig in sveltekit so that import the .js but I don't know if there's also some editor settings.

@Rich-Harris
Copy link
Member

Rich-Harris commented Jul 29, 2024

Yeah, I mean it even works in our own playground. I'm not sure we can realistically make this change. This feels like something that should be solved with typechecking, rather than in the compiler itself.

@dummdidumm
Copy link
Member Author

dummdidumm commented Jul 29, 2024

I forgot the "is a default import" check, which silences all the false positives except the import DefaultClass from './module-which-looks-like-a-svelte-component.svelte, which I think will be super rare.
We can make it even more rare by checking that the new expression is called with exactly one argument.
Relying on types is not an option because we have to make it backwards compatible on a type level due to the componentApi option.

@Rich-Harris
Copy link
Member

Fair enough. Should we go one step further and check that if the sole argument is an object literal, it has a target property since that's a required option if it's a component?

@dummdidumm
Copy link
Member Author

yeah sounds good - it's very rare that you would pass a variable into the constructor that takes care of that for you

@dummdidumm dummdidumm merged commit 363535c into main Jul 29, 2024
9 checks passed
@dummdidumm dummdidumm deleted the legacy-warn branch July 29, 2024 13:51
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.

3 participants