Skip to content

feat: error on invalid component name #12821

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 5 commits into from
Aug 20, 2024
Merged

feat: error on invalid component name #12821

merged 5 commits into from
Aug 20, 2024

Conversation

paoloricciuti
Copy link
Member

Svelte 5 rewrite

Cheap follow up to #12798 that i've quickly implemented after a brief discussion on sveltejs/kit#12573 (comment)

Given it's already allowed for uppercase components i think it makes sense to also allow tags like arr[0]. If we actually don't want this feel free to close it didn't take me much.

Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).

If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.

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 Aug 13, 2024

🦋 Changeset detected

Latest commit: 5b105ae

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

Co-authored-by: Conduitry <[email protected]>
@Rich-Harris
Copy link
Member

Per comments elsewhere, I think we need to go in the opposite direction and only allow dot notation. This works unintentionally and is buggy

@paoloricciuti
Copy link
Member Author

Per comments elsewhere, I think we need to go in the opposite direction and only allow dot notation. This works unintentionally and is buggy

Ok so by fixing you mean disallow array access even on uppercase components? Because i think we could make it work properly in the uppercase case too.

@Rich-Harris
Copy link
Member

We definitely could make it work, but where do you draw the line?

<Things[currentThing??'default']??Fallback {...props} />

Rather than creating the illusion that any JS expression is valid, we should just have clear rules — a component name can be an identifier, or a member expression with dot notation

@paoloricciuti
Copy link
Member Author

We definitely could make it work, but where do you draw the line?

<Things[currentThing??'default']??Fallback {...props} />

Rather than creating the illusion that any JS expression is valid, we should just have clear rules — a component name can be an identifier, or a member expression with dot notation

Fair enough...should i switch this PR to throwing an error in case the component is not in the right format?

@FoHoOV
Copy link
Contributor

FoHoOV commented Aug 13, 2024

Just my two cents. Something that is not component shouldn't be allowed to be called like <X ... />.
see this for instance.

<script>
	let Component = ()=>console.log("hi");
</script>

<Component />

@7nik
Copy link
Contributor

7nik commented Aug 13, 2024

Just my two cents. Something that is not component shouldn't be allowed to be called like <X ... />. see this for instance.

<script>
	let Component = ()=>console.log("hi");
</script>

<Component />

Components aren't signed like snippets and don't really differ from any other function, so the runtime distinction is tricky and fragile. And it looks more like aiming their own leg, so...

Also, TS completely prevents this, unless Component is any or something.

@Rich-Harris
Copy link
Member

Fair enough...should i switch this PR to throwing an error in case the component is not in the right format?

yes please

Also, TS completely prevents this

Exactly — this is what TypeScript is for. We don't need to do anything beyond that

@paoloricciuti paoloricciuti changed the title feat: treat tag with [ as a component, even if lowercase feat: error on invalid component name Aug 14, 2024
@paoloricciuti
Copy link
Member Author

yes please

@Rich-Harris did it...not entirely convinced on the error message. Also i've thrown directly in the parsing phase because if we know that it's already wrong why even continue parsing and i did consider it a parsing error rather than a validation one but don't know if we want to move it to the validation phase to keep the validation all in the validation phase.

@Rich-Harris
Copy link
Member

tweaked the error message:

  • we don't need to include the name in the message, it's already apparent from the location of the error
  • 'identifier' and 'member expression' are AST jargon that not everyone understands. 'valid variable name' is something that more people will be familiar with
  • messages shouldn't end with a .

@Rich-Harris
Copy link
Member

One thing we're still missing — validation for things like <foo[1]>. This is an invalid tag, but because it isn't identified as a component name, it's not identified as an invalid component name. Not 100% sure how that should be handled, so won't attempt to fix it in this PR, but will open a new issue

@Rich-Harris Rich-Harris merged commit 02c86b1 into sveltejs:main Aug 20, 2024
7 of 9 checks passed
@paoloricciuti
Copy link
Member Author

One thing we're still missing — validation for things like <foo[1]>. This is an invalid tag, but because it isn't identified as a component name, it's not identified as an invalid component name. Not 100% sure how that should be handled, so won't attempt to fix it in this PR, but will open a new issue

I specifically avoided that validation because technically it works...the browser will create an element with foo[1] which is an inline element with no styles or semantic meaning.

I understand that this might be misleading but also since you can't have a component with array access either maybe is less of expected that this will yield a component in the first place.

@paoloricciuti
Copy link
Member Author

tweaked the error message:

Yeah I wasn't really sure about the error message either for the same reasons...thanks for taking care of it.

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.

6 participants