From b1fb5aa4e58f1a04636c93a964c022e5d8152e0a Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Mon, 25 Nov 2024 09:42:31 +0100 Subject: [PATCH] fix: Handle duplicate imports / empty info --- .../generator/ControllerByIdDtsGenerator.ts | 19 +++++---- src/linter/xmlTemplate/linter.ts | 13 +++--- .../linter/xmlTemplate/ControllerByIdInfo.ts | 38 ++++++++++++++++++ .../generator/ControllerByIdDtsGenerator.ts | 10 ++++- .../ControllerByIdDtsGenerator.ts.md | 2 - .../ControllerByIdDtsGenerator.ts.snap | Bin 394 -> 388 bytes 6 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 test/lib/linter/xmlTemplate/ControllerByIdInfo.ts diff --git a/src/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts b/src/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts index dc3c76493..bc2dd5f03 100644 --- a/src/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts +++ b/src/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts @@ -1,16 +1,17 @@ import ControllerByIdInfo, {IdModulesMap} from "../ControllerByIdInfo.js"; -interface Import { - localName: string; - moduleName: string; -} export class ControllerByIdDtsGenerator { - private imports = new Set(); + // Maps module names to local names + private imports = new Map(); constructor(private controllerByIdInfo: ControllerByIdInfo) { } generate() { + const mappings = this.controllerByIdInfo.getMappings(); + if (mappings.size === 0) { + return null; + } let out = ""; this.controllerByIdInfo.getMappings().forEach((idToModules, controllerName) => { out += this.generateModuleDeclaration(controllerName, idToModules); @@ -20,8 +21,8 @@ export class ControllerByIdDtsGenerator { generateCollectedImports() { let out = ""; - this.imports.forEach((moduleImport) => { - out += `import ${moduleImport.localName} from "${moduleImport.moduleName}";\n`; + this.imports.forEach((localName, moduleName) => { + out += `import ${localName} from "${moduleName}";\n`; }); out += "\n"; return out; @@ -34,7 +35,9 @@ export class ControllerByIdDtsGenerator { modules.forEach((moduleName) => { const localName = this.getLocalModuleName(moduleName); localNames.push(localName); - this.imports.add({localName, moduleName}); + if (!this.imports.has(moduleName)) { + this.imports.set(moduleName, localName); + } }); out += `\t\t"${id}": ${localNames.join(" | ")};\n`; }); diff --git a/src/linter/xmlTemplate/linter.ts b/src/linter/xmlTemplate/linter.ts index 4616ad309..350b2254e 100644 --- a/src/linter/xmlTemplate/linter.ts +++ b/src/linter/xmlTemplate/linter.ts @@ -39,10 +39,11 @@ export default async function lintXml({filePathsWorkspace, workspace, context}: // Generate dts file with specific byId signatures for controllers based on view IDs const controllerByIdDtsGenerator = new ControllerByIdDtsGenerator(controllerByIdInfo); const controllerByIdDts = controllerByIdDtsGenerator.generate(); - - const dtsResource = createResource({ - path: "/types/@ui5/linter/virtual/ControllerById.d.ts", - string: controllerByIdDts, - }); - await workspace.write(dtsResource); + if (controllerByIdDts) { + const dtsResource = createResource({ + path: "/types/@ui5/linter/virtual/ControllerById.d.ts", + string: controllerByIdDts, + }); + await workspace.write(dtsResource); + } } diff --git a/test/lib/linter/xmlTemplate/ControllerByIdInfo.ts b/test/lib/linter/xmlTemplate/ControllerByIdInfo.ts new file mode 100644 index 000000000..b5adec7ba --- /dev/null +++ b/test/lib/linter/xmlTemplate/ControllerByIdInfo.ts @@ -0,0 +1,38 @@ +import test from "ava"; +import ControllerByIdInfo from "../../../../src/linter/xmlTemplate/ControllerByIdInfo.js"; + +test("ControllerByIdInfo: Empty mappings", (t) => { + const controllerByIdInfo = new ControllerByIdInfo(); + const mappings = controllerByIdInfo.getMappings(); + t.deepEqual(mappings, new Map()); +}); + +test("ControllerByIdInfo: Mappings", (t) => { + const controllerByIdInfo = new ControllerByIdInfo(); + controllerByIdInfo.addMappings("com.myapp.controller.App", new Map([ + ["button", "sap/m/Button"], + ["input", "sap/m/Input"], + ])); + controllerByIdInfo.addMappings("com.myapp.controller.Main", new Map([ + ["button", "sap/m/Button"], + ["input", "sap/m/Input"], + ])); + controllerByIdInfo.addMappings("com.myapp.controller.App", new Map([ + ["button", "sap/ui/commons/Button"], + ])); + controllerByIdInfo.addMappings("com.myapp.controller.App", new Map([ + ["button", "com/myapp/control/Button"], + ])); + + const mappings = controllerByIdInfo.getMappings(); + t.deepEqual(mappings, new Map([ + ["com.myapp.controller.App", new Map([ + ["button", new Set(["sap/m/Button", "sap/ui/commons/Button", "com/myapp/control/Button"])], + ["input", new Set(["sap/m/Input"])], + ])], + ["com.myapp.controller.Main", new Map([ + ["button", new Set(["sap/m/Button"])], + ["input", new Set(["sap/m/Input"])], + ])], + ])); +}); diff --git a/test/lib/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts b/test/lib/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts index 17095185f..38a299e43 100644 --- a/test/lib/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts +++ b/test/lib/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts @@ -21,5 +21,11 @@ test("ControllerByIdDtsGenerator: generate", (t) => { const generator = new ControllerByIdDtsGenerator(controllerByIdInfo); const result = generator.generate(); t.snapshot(result); -} -); +}); + +test("ControllerByIdDtsGenerator: generate with empty info should return null", (t) => { + const controllerByIdInfo = new ControllerByIdInfo(); + const generator = new ControllerByIdDtsGenerator(controllerByIdInfo); + const result = generator.generate(); + t.is(result, null); +}); diff --git a/test/lib/linter/xmlTemplate/generator/snapshots/ControllerByIdDtsGenerator.ts.md b/test/lib/linter/xmlTemplate/generator/snapshots/ControllerByIdDtsGenerator.ts.md index e88502722..18461b321 100644 --- a/test/lib/linter/xmlTemplate/generator/snapshots/ControllerByIdDtsGenerator.ts.md +++ b/test/lib/linter/xmlTemplate/generator/snapshots/ControllerByIdDtsGenerator.ts.md @@ -11,8 +11,6 @@ Generated by [AVA](https://avajs.dev). `import sap_m_Button from "sap/m/Button";␊ import sap_ui_commons_Button from "sap/ui/commons/Button";␊ import sap_m_Input from "sap/m/Input";␊ - import sap_m_Button from "sap/m/Button";␊ - import sap_m_Input from "sap/m/Input";␊ ␊ declare module "com/myapp/controller/App.controller" {␊ interface ByIdMapping {␊ diff --git a/test/lib/linter/xmlTemplate/generator/snapshots/ControllerByIdDtsGenerator.ts.snap b/test/lib/linter/xmlTemplate/generator/snapshots/ControllerByIdDtsGenerator.ts.snap index ae9fb991e29979f49123afdd64b7bcb03adccefe..354cf96ad124efe2a5758f5ce921f13449446cab 100644 GIT binary patch literal 388 zcmV-~0ek*IRzVd^UP z<_k1v7d=Wnhd1z2lBC!ayATCuHS^v)-p{(2N|Vq1>J3n!gvIH#(ddkn;6_!-Z_wX? z2FK92n9kZO2(Ov-&j$T!%{fE%(Em8>9rx;UaMU~LzxRc*#v%30W-1$%h(^=G8AU_S zY^0(V3KP=#Sc)t+N*V3HD@qYaCLI{UkG6z%{UP_>0Zd9ZyW^I&z zm5qkho^PKaeIbF+2(Dl`(0Yn7vsUO?O$6bz%@FSveBECosTHD06L-@lBv_IF7EKEd z1uG@et?AI(#%x-p{BlBJfuOmkbEr(wO>}kUQy$aFAnu}%lc!`OZh4PR=-D8q_vyv0 i1O>VYkdWn1CmJ)M|GLpHx2UcEZH!Na;A6xU0{{RgO|>@w literal 394 zcmV;50d@XCRzV_eU@zw6B@v-jksNxf2N4O|?4&N; zf9y`6L3`*&X+NnXDK^z2MZrDo&di(l_P5N(LT6LASOIbrFgw513au6b?4V589l8fl zU@2-Fkx6p}?i4ldRij%h8KX#_x@X|X;Ca47>H7f*=Ak#9G zB{e3@KH-IZV=gz(fV}x0R|qzz8ITI?kQ&3)q-uD