Skip to content

[fix] Support exclusively special characters in component filenames #7664

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
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/compiler/compile/utils/__test__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ describe('get_name_from_filename', () => {
it('handles Windows filenames', () => {
assert.equal(get_name_from_filename('path\\to\\Widget.svelte'), 'Widget');
});

it('handles special characters in filenames', () => {
assert.equal(get_name_from_filename('@.svelte'), '_');
assert.equal(get_name_from_filename('&.svelte'), '_');
assert.equal(get_name_from_filename('~.svelte'), '_');
});
});
9 changes: 4 additions & 5 deletions src/compiler/compile/utils/get_name_from_filename.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { regex_starts_with_underscore, regex_ends_with_underscore } from '../../utils/patterns';

const regex_percentage_characters = /%/g;
const regex_file_ending = /\.[^.]+$/;
const regex_repeated_invalid_variable_identifier_characters = /[^a-zA-Z_$0-9]+/g;
const regex_starts_with_digit = /^(\d)/;
const regex_may_starts_or_ends_with_underscore = /^_?(.+?)_?$/;

export default function get_name_from_filename(filename: string) {
if (!filename) return null;
Expand All @@ -18,12 +17,12 @@ export default function get_name_from_filename(filename: string) {
}
}

const base = parts.pop()
const base = parts
.pop()
.replace(regex_percentage_characters, 'u')
.replace(regex_file_ending, '')
.replace(regex_repeated_invalid_variable_identifier_characters, '_')
.replace(regex_starts_with_underscore, '')
.replace(regex_ends_with_underscore, '')
.replace(regex_may_starts_or_ends_with_underscore, '$1')
.replace(regex_starts_with_digit, '_$1');

if (!base) {
Expand Down