Skip to content

Commit c90cf94

Browse files
d3xter666matz3
authored andcommitted
refactor: Camelize names before checking for uniqueness
1 parent a40a46b commit c90cf94

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

src/linter/ui5Types/utils/utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,18 @@ export function resolveUniqueName(inputName: string, existingIdentifiers?: Set<s
193193
}
194194
}
195195

196-
return getUniqueName(Array.from(existingIdentifiers ?? []), name ?? inputName, "/");
196+
name = name ?? camelize(identifier);
197+
if (existingIdentifiers?.has(name)) {
198+
name = getUniqueName(Array.from(existingIdentifiers ?? []), inputName, "/");
199+
}
200+
return name;
201+
}
202+
203+
// Camelize a string by replacing invalid identifier characters
204+
function camelize(str: string): string {
205+
return str.replace(/[^\p{ID_Start}\p{ID_Continue}]+([\p{ID_Start}\p{ID_Continue}])/gu, (_match, nextChar) => {
206+
return typeof nextChar === "string" ? nextChar.toUpperCase() : "";
207+
});
197208
}
198209

199210
export function isGlobalAssignment(node: ts.AccessExpression): boolean {

test/lib/autofix/snapshots/autofix.ts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ Generated by [AVA](https://avajs.dev).
505505
"../other/library",␊
506506
"./library",␊
507507
"sap/m/Button",␊
508-
], function(sinonQunit, App_controller, library, otherLibrary, _Library, Button) {␊
508+
], function(sinonQunit, AppController, library, otherLibrary, _Library, Button) {␊
509509
const button = new Button({␊
510510
text: "Hello"␊
511511
});␊
-4 Bytes
Binary file not shown.

test/lib/linter/ui5Types/resolveUniqueNameUtil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ test("resolveUniqueName: Relative library module (one folder up)", (t) => {
2626
});
2727

2828
test("resolveUniqueName: Controller module", (t) => {
29-
t.is(resolveUniqueName("./controller/App.controller"), "App_controller");
29+
t.is(resolveUniqueName("./controller/App.controller"), "AppController");
3030
});
3131

3232
test("resolveUniqueName: sap/ui/thirdparty/sinon-qunit", (t) => {
3333
t.is(resolveUniqueName("sap/ui/thirdparty/sinon-qunit"), "sinonQunit");
3434
});
3535

3636
test("resolveUniqueName: Module name with multiple illegal characters", (t) => {
37-
t.is(resolveUniqueName("./my-super--module"), "mySuper_module");
37+
t.is(resolveUniqueName("./my-super--module"), "mySuperModule");
3838
});

0 commit comments

Comments
 (0)