Skip to content

Commit

Permalink
feat(import-alias): Code refactored
Browse files Browse the repository at this point in the history
Signed-off-by: Jaskeerat Singh Saluja <[email protected]>
  • Loading branch information
Jaskeerat Singh Saluja authored and Jaskeerat Singh Saluja committed Sep 6, 2024
1 parent a6d615f commit 4334ac1
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/codegen/fromcto/typescript/typescriptvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,23 @@ class TypescriptVisitor {

});

modelFile.getImports().map(importString => ModelUtil.getNamespace(importString)).filter(namespace => namespace !== modelFile.getNamespace()) // Skip own namespace.
.filter((v, i, a) => a.indexOf(v) === i) // Remove any duplicates from direct imports
.forEach(namespace => {
const propertyTypeNames = properties.get(namespace);
if (propertyTypeNames) {
const csvPropertyTypeNames = Array.from(propertyTypeNames).map(type => {
const fqnType = `${namespace}.${type}`;
return aliasedTypesMap.get(fqnType) ? `${type} as ${aliasedTypesMap.get(fqnType)}` : type;
});
parameters.fileWriter.writeLine(0, `import {${csvPropertyTypeNames}} from './${namespace}';`);
}
});
const uniqueNamespaces = new Set(
modelFile.getImports()
.map(importString => ModelUtil.getNamespace(importString))
.filter(namespace => namespace !== modelFile.getNamespace())
);
uniqueNamespaces.forEach(namespace => {
const propertyTypeNames = properties.get(namespace);
if (propertyTypeNames) {
const csvPropertyTypeNames = Array.from(propertyTypeNames).map(type => {
const fqnType = `${namespace}.${type}`;
return aliasedTypesMap.has(fqnType) ? `${type} as ${aliasedTypesMap.get(fqnType)}` : type;
}).join(',');

parameters.fileWriter.writeLine(0, `import {${csvPropertyTypeNames}} from './${namespace}';`);
}
});


parameters.fileWriter.writeLine(0, '\n// interfaces');
parameters.aliasedTypesMap = aliasedTypesMap;
Expand Down

0 comments on commit 4334ac1

Please sign in to comment.