feat: support exclusively special characters in component filenames (#7664)

Fixes #7143

---------

Co-authored-by: Tan Li Hau <tanhauhau@users.noreply.github.com>
Co-authored-by: Yuichiro Yamashita <xydybaseball@gmail.com>
pull/8342/head
Dane David 2 years ago committed by GitHub
parent dd371f58fe
commit c0bc86e214
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,4 +13,10 @@ describe('get_name_from_filename', () => {
it('handles Windows filenames', () => { it('handles Windows filenames', () => {
assert.equal(get_name_from_filename('path\\to\\Widget.svelte'), 'Widget'); 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'), '_');
});
}); });

@ -1,9 +1,8 @@
import { regex_starts_with_underscore, regex_ends_with_underscore } from '../../utils/patterns';
const regex_percentage_characters = /%/g; const regex_percentage_characters = /%/g;
const regex_file_ending = /\.[^.]+$/; const regex_file_ending = /\.[^.]+$/;
const regex_repeated_invalid_variable_identifier_characters = /[^a-zA-Z_$0-9]+/g; const regex_repeated_invalid_variable_identifier_characters = /[^a-zA-Z_$0-9]+/g;
const regex_starts_with_digit = /^(\d)/; const regex_starts_with_digit = /^(\d)/;
const regex_may_starts_or_ends_with_underscore = /^_?(.+?)_?$/;
export default function get_name_from_filename(filename: string) { export default function get_name_from_filename(filename: string) {
if (!filename) return null; if (!filename) return null;
@ -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_percentage_characters, 'u')
.replace(regex_file_ending, '') .replace(regex_file_ending, '')
.replace(regex_repeated_invalid_variable_identifier_characters, '_') .replace(regex_repeated_invalid_variable_identifier_characters, '_')
.replace(regex_starts_with_underscore, '') .replace(regex_may_starts_or_ends_with_underscore, '$1')
.replace(regex_ends_with_underscore, '')
.replace(regex_starts_with_digit, '_$1'); .replace(regex_starts_with_digit, '_$1');
if (!base) { if (!base) {

Loading…
Cancel
Save