Skip to content

Commit

Permalink
fix: Handle duplicate imports / empty info
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Nov 25, 2024
1 parent 280347d commit b1fb5aa
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
19 changes: 11 additions & 8 deletions src/linter/xmlTemplate/generator/ControllerByIdDtsGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import ControllerByIdInfo, {IdModulesMap} from "../ControllerByIdInfo.js";

interface Import {
localName: string;
moduleName: string;
}
export class ControllerByIdDtsGenerator {
private imports = new Set<Import>();
// Maps module names to local names
private imports = new Map<string, string>();

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);
Expand All @@ -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;
Expand All @@ -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`;
});
Expand Down
13 changes: 7 additions & 6 deletions src/linter/xmlTemplate/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
38 changes: 38 additions & 0 deletions test/lib/linter/xmlTemplate/ControllerByIdInfo.ts
Original file line number Diff line number Diff line change
@@ -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"])],
])],
]));
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 {␊
Expand Down
Binary file not shown.

0 comments on commit b1fb5aa

Please sign in to comment.