Skip to content

Commit df9a58b

Browse files
authored
Sanitize the folder name into a valid component name (#3152)
Fixes #3150 Signed-off-by: David Thompson <[email protected]>
1 parent 29e98fa commit df9a58b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/webview/create-component/pages/fromLocalCodebase.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,14 @@ export function FromLocalCodebase(props: FromLocalCodebaseProps) {
135135
if (props.rootFolder && props.rootFolder.length != 0) {
136136
setProjectFolder(props.rootFolder);
137137
const isWindowsPath = props.rootFolder.charAt(1) === ':';
138-
const componentNameFromFolder: string = props.rootFolder.substring(props.rootFolder.lastIndexOf(isWindowsPath ? '\\' : '/') + 1);
138+
let componentNameFromFolder: string = props.rootFolder //
139+
.substring(props.rootFolder.lastIndexOf(isWindowsPath ? '\\' : '/') + 1)
140+
.toLocaleLowerCase()
141+
.replace(/[^a-z0-9-]+/g, '-')
142+
.replace(/-+/g, '-')
143+
.replace(/-$/, '')
144+
.replace(/^[0-9]+/, '');
145+
componentNameFromFolder = componentNameFromFolder.length ? componentNameFromFolder : 'component';
139146
setComponentName(componentNameFromFolder);
140147
window.vscodeApi.postMessage({
141148
action: 'validateComponentName',

0 commit comments

Comments
 (0)