Skip to content

Support for Exclusively Special Characters in Component Filenames #7143

Open
@Acmion

Description

@Acmion

Describe the bug

Certain component filenames will throw the error: Could not derive component name from file {FILENAME}.svelte.

Examples of filenames that will throw an error:
@.svelte
&.svelte
...

Examples of filenames that will not throw an error:
%.svelte
$.svelte

The reason for why this is happening is that function get_name_from_filename replaces certain characters with underscores and if these underscores happen to be leading characters, then they get deleted.

The behavior is somewhat inconsistent, because certain equivalent filenames, but with a different special character do work just fine.

Additionally, this is not how HTML and the web generally work. You may very well have a file named just "@" if you want.

This is a bigger issue in SvelteKit, because now urls like example.com/something/@ are essentially second class citizens (yes the problem can be fixed by creating a directory and a index.svelte file, but this is in some cases extra boilerplate).

Potential fix

It is relatively easy to fix this problem. In the function get_name_from_filename the following code is the culprit:

const base = parts.pop()
  .replace(/%/g, 'u')
  .replace(/\.[^.]+$/, '')
  .replace(/[^a-zA-Z_$0-9]+/g, '_')        // This line replaces "@" => "_"
  .replace(/^_/, '')                       // This line replaces leading "_" => "" and thus causes the bug
  .replace(/_$/, '')
  .replace(/^(\d)/, '_$1');

To fix this problem, either change .replace(/%/g, 'u') to take in other special characters as well, like so: .replace(/[%@#]/g, 'u'). However, this leads to the question of what special characters are to be supported. Maybe there is some way to regex them all? Additionally, should all of these be replaced with "u"?

Alternatively, leading "_" should not be discarded if they are the only characters.

Reproduction

https://svelte.dev/repl/392f9371025f48f88a6274a5f6e6d8ee?version=3.46.2

Logs

Could not derive component name from file @.svelte

System Info

REPL

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerChanges relating to the compiler

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions