Skip to content
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

get_name crashes on file named [...parts].svelte #2843

Closed
IlyaSemenov opened this issue May 22, 2019 · 2 comments
Closed

get_name crashes on file named [...parts].svelte #2843

IlyaSemenov opened this issue May 22, 2019 · 2 comments
Labels

Comments

@IlyaSemenov
Copy link
Contributor

If there's a file named [...parts].svelte (syntax used by Sapper), it wouldn't compile because get_name(filename) would crash:

function get_name(filename) {
if (!filename) return null;
const parts = filename.split(/[\/\\]/);
if (parts.length > 1 && /^index\.\w+/.test(parts[parts.length - 1])) {
parts.pop();
}
const base = parts.pop()
.replace(/\..+/, "")
.replace(/[^a-zA-Z_$0-9]+/g, '_')
.replace(/^_/, '')
.replace(/_$/, '')
.replace(/^(\d)/, '_$1');
return base[0].toUpperCase() + base.slice(1);
}

The sequence of .replace(...) leads to the empty string, and then base[0].toUpperCase() crashes with Cannot read property 'toUpperCase' of undefined.

Related Sapper issue: sveltejs/sapper#709

Of course this particular problem can be fixed by prepending the chain of replacements with something like .replace(/\.{2,}/g, "") but perhaps it needs a more generic fix as well? Like if the replacements lead to the empty string, generate the component name with something like Component_${hash(lastPart))}?

@IlyaSemenov
Copy link
Contributor Author

IlyaSemenov commented May 23, 2019

Also, I think .replace(/\..+/, "") should rather be .replace(/\.[^\.]+$/, "") (remove last file extension, not first). If it were the case, the original problem wouldn't appear in the first place.

file.foo.svelte would become File_Foo which makes perfect sense.

@IlyaSemenov
Copy link
Contributor Author

I also noticed inconsistency in preparing parts:

  • For "usual" svelte files, parts is ['Users', 'semenov', ..., 'some.svelte'] (extension(s) included)
  • for index files, parts is ['Users', 'semenov', ..., 'last_dir'] (no extension)

Meaning the suggestion above (removing last file extension) will work inconsistently if de-indexing code is not updated to append the extension, something like:

            if (parts.length > 1) {
                const indexMatch = parts[parts.length - 1].match(/^index(\.\w+)/)
                if (indexMatch) {
                    parts.pop();
                    parts[parts.length - 1] += indexMatch[1]
                }
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants