Skip to content

Commit 2cbf2da

Browse files
committed
Changed makeLegalName optionally return a valid python identifier.
1 parent 414c64f commit 2cbf2da

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ const CLASS_METHOD_DEF = {
302302
}
303303
});
304304

305-
const legalName = makeLegalName(name, otherNames, /* mustBeginWithLetter */ true);
305+
const legalName = makeLegalName(name, otherNames, /* mustBeValidPythonIdentifier */ true);
306306
const oldName = nameField.getValue();
307307
if (oldName && oldName !== name && oldName !== legalName) {
308308
// Rename any callers.

src/blocks/mrc_component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ const COMPONENT = {
177177
otherNames.push(block.getFieldValue(FIELD_NAME));
178178
});
179179

180-
const legalName = makeLegalName(name, otherNames, /* mustBeginWithLetter */ true);
180+
const legalName = makeLegalName(name, otherNames, /* mustBeValidPythonIdentifier */ true);
181181
const oldName = nameField.getValue();
182182
if (oldName && oldName !== name && oldName !== legalName) {
183183
// Rename any callers.

src/blocks/mrc_event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const EVENT = {
198198
otherNames.push(block.getFieldValue(FIELD_EVENT_NAME));
199199
});
200200

201-
const legalName = makeLegalName(name, otherNames, /* mustBeginWithLetter */ false);
201+
const legalName = makeLegalName(name, otherNames, /* mustBeValidPythonIdentifier */ false);
202202
const oldName = nameField.getValue();
203203
if (oldName && oldName !== name && oldName !== legalName) {
204204
// Rename any callers.

src/blocks/mrc_mechanism.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const MECHANISM = {
180180
otherNames.push(block.getFieldValue(FIELD_NAME));
181181
});
182182

183-
const legalName = makeLegalName(name, otherNames, /* mustBeginWithLetter */ true);
183+
const legalName = makeLegalName(name, otherNames, /* mustBeValidPythonIdentifier */ true);
184184
const oldName = nameField.getValue();
185185
if (oldName && oldName !== name && oldName !== legalName) {
186186
// Rename any callers.

src/blocks/utils/validator.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
* A legal name is:
2525
* 1. not empty.
2626
* 2. doesn't collide with other names.
27-
* 3. Optional: begins with a letter.
27+
* 3. Optional: is a valid python identifier.
2828
*/
29-
export function makeLegalName(proposedName: string, otherNames: string[], mustBeginWithLetter: boolean): string {
29+
export function makeLegalName(proposedName: string, otherNames: string[], mustBeValidPythonIdentifier: boolean): string {
3030
const otherNamesLowerCase = otherNames.map(n => n.toLowerCase());
3131

3232
// Strip leading and trailing whitespace.
@@ -35,10 +35,27 @@ export function makeLegalName(proposedName: string, otherNames: string[], mustBe
3535
// Make the name non-empty.
3636
name = name || 'unnamed';
3737

38-
if (mustBeginWithLetter) {
39-
// If the name begins with a non-alphabetic character, insert the letter a at the beginning.
40-
if (!name.match(/^[a-zA-Z].*$/)) {
41-
name = 'a' + name;
38+
if (mustBeValidPythonIdentifier) {
39+
if (!name.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
40+
let identifier = '';
41+
const firstChar = name[0];
42+
if (/[a-zA-Z_]/.test(firstChar)) {
43+
identifier += firstChar;
44+
} else if (/[a-zA-Z0-9_]/.test(firstChar)) {
45+
// If the first character would be valid as the second charactor, insert an underscore.
46+
identifier += '_' + firstChar;
47+
} else {
48+
identifier += '_';
49+
}
50+
for (let i = 1; i < name.length; i++) {
51+
const char = name[i];
52+
if (/[a-zA-Z0-9_]/.test(char)) {
53+
identifier += char;
54+
} else {
55+
identifier += '_';
56+
}
57+
}
58+
name = identifier;
4259
}
4360
}
4461

0 commit comments

Comments
 (0)