Skip to content

Commit da7b984

Browse files
committed
In python.ts, added function isExistingPythonModule.
In ClassNameComponent.tsx, check whether the class name entered by the user would cause a collision with an existing python module.
1 parent cf93d39 commit da7b984

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

src/blocks/utils/python.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,15 @@ export function getLegalName(proposedName: string, existingNames: string[]){
287287
}
288288
return newName;
289289
}
290+
291+
export function isExistingPythonModule(moduleName: string): boolean {
292+
for (const pythonData of allPythonData) {
293+
// Process modules.
294+
for (const moduleData of pythonData.modules) {
295+
if (moduleData.moduleName === moduleName) {
296+
return true;
297+
}
298+
}
299+
}
300+
return false;
301+
}

src/i18n/locales/en/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@
9595
"ERROR_LOADING_DEPENDENCIES": "Error loading dependencies.",
9696
"COPYRIGHT": "© 2025 FIRST. All rights reserved."
9797
},
98-
"INVALID_CLASS_NAME": "{{name}} is not a valid name. Please enter a different name.",
98+
"INVALID_CLASS_NAME": "{{className}} is not a valid name. Please enter a different name.",
9999
"CLASS_NAME_ALREADY_EXISTS": "Another Mechanism or OpMode is already named {{name}}. Please enter a different name.",
100+
"PYTHON_MODULE_NAME_ALREADY_EXISTS": "{{name}} is not a valid name because there is a python module named {{moduleName}}.",
100101
"THEME_MODAL": {
101102
"LIGHT": "Light Theme",
102103
"LIGHT_DESCRIPTION": "Clean and bright interface for daytime use",

src/i18n/locales/es/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
},
9999
"INVALID_CLASS_NAME": "{{name}} no es un nombre válido. Por favor ingrese un nombre diferente.",
100100
"CLASS_NAME_ALREADY_EXISTS": "Otro Mecanismo u OpMode ya se llama {{name}}. Por favor ingrese un nombre diferente.",
101+
"PYTHON_MODULE_NAME_ALREADY_EXISTS": "{{name}} no es un nombre válido porque existe un módulo de Python llamado {{moduleName}}.",
101102
"THEME_MODAL": {
102103
"LIGHT": "Tema Claro",
103104
"LIGHT_DESCRIPTION": "Interfaz limpia y brillante para uso diurno",

src/i18n/locales/he/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
},
9898
"INVALID_CLASS_NAME": "{{name}} אינו שם תקין. אנא הזן שם אחר.",
9999
"CLASS_NAME_ALREADY_EXISTS": "מנגנון או אופמוד אחר כבר נקרא {{name}}. אנא הזן שם אחר.",
100+
"PYTHON_MODULE_NAME_ALREADY_EXISTS": "{{name}} אינו שם חוקי מכיוון שקיים מודול פייתון בשם {{moduleName}}.",
100101
"THEME_MODAL": {
101102
"LIGHT": "ערכת נושא בהירה",
102103
"LIGHT_DESCRIPTION": "ממשק נקי ובהיר לשימוש ביום",

src/reactComponents/ClassNameComponent.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as React from 'react';
2626
import * as commonStorage from '../storage/common_storage';
2727
import * as storageProject from '../storage/project';
2828
import * as storageNames from '../storage/names';
29+
import { isExistingPythonModule } from '../blocks/utils/python';
2930

3031
/** Props for the ClassNameComponent. */
3132
interface ClassNameComponentProps {
@@ -75,6 +76,11 @@ export default function ClassNameComponent(props: ClassNameComponentProps): Reac
7576
error = t('CLASS_NAME_ALREADY_EXISTS', { name: newClassName });
7677
}
7778

79+
const moduleName = storageNames.pascalCaseToSnakeCase(newClassName);
80+
if (isExistingPythonModule(moduleName)) {
81+
error = t('PYTHON_MODULE_NAME_ALREADY_EXISTS', { name: newClassName, moduleName: moduleName });
82+
}
83+
7884
if (!error) {
7985
clearError();
8086
props.onAddNewItem();

0 commit comments

Comments
 (0)