Code review comment: Also move the de-dup logic to `visit(BlangTypedefinition)`

Code before review:
public void visit(BLangPackage pkgNode) {
if (pkgNode.completedPhases.contains(CompilerPhase.DESUGAR)) {
result = pkgNode;
return;
}
observabilityDesugar.addObserveInternalModuleImport(pkgNode);
observabilityDesugar.addObserveModuleImport(pkgNode);
code2CloudDesugar.addCode2CloudModuleImport(pkgNode);
createPackageInitFunctions(pkgNode, env);
addAttachedFunctionsToPackageLevel(pkgNode, env);
if (!pkgNode.testablePkgs.isEmpty() && pkgNode.getTestablePkg().getMockFunctionNamesMap() != null) {
mockDesugar.generateMockFunctions(pkgNode);
}
pkgNode.constants.stream()
.filter(constant -> constant.expr.getKind() == NodeKind.LITERAL ||
constant.expr.getKind() == NodeKind.NUMERIC_LITERAL)
.forEach(constant -> pkgNode.typeDefinitions.add(constant.associatedTypeDefinition));
BLangBlockStmt serviceAttachments = serviceDesugar.rewriteServiceVariables(pkgNode.services, env);
BLangBlockFunctionBody initFnBody = (BLangBlockFunctionBody) pkgNode.initFunction.body;
for (BLangConstant constant : pkgNode.constants) {
if (constant.symbol.type.tag == TypeTags.MAP) {
BLangSimpleVarRef constVarRef = ASTBuilderUtil.createVariableRef(constant.pos, constant.symbol);
constant.expr = rewrite(constant.expr, SymbolEnv.createTypeEnv(constant.typeNode,
pkgNode.initFunction.symbol.scope, env));
BLangInvocation frozenConstValExpr =
createLangLibInvocationNode(
"cloneReadOnly", constant.expr, new ArrayList<>(), constant.expr.type, constant.pos);
BLangAssignment constInit =
ASTBuilderUtil.createAssignmentStmt(constant.pos, constVarRef, frozenConstValExpr);
initFnBody.stmts.add(constInit);
}
}
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < pkgNode.constants.size(); i++) {
String nextKey = pkgNode.constants.get(i).toString();
String keyOwner = pkgNode.constants.get(i).symbol.owner.toString();
if (map.containsKey(nextKey)) {
if (map.get(nextKey) == keyOwner) {
pkgNode.constants.remove(i);
i -= 1;
} else {
map.put(nextKey, keyOwner);
}
} else {
map.put(nextKey, keyOwner);
}
}
pkgNode.globalVars = desugarGlobalVariables(pkgNode, initFnBody);
pkgNode.services.forEach(service -> serviceDesugar.engageCustomServiceDesugar(service, env));
annotationDesugar.rewritePackageAnnotations(pkgNode, env);
addUserDefinedModuleInitInvocationAndReturn(pkgNode);
pkgNode.typeDefinitions.sort(Comparator.comparing(t -> t.precedence));
pkgNode.typeDefinitions = rewrite(pkgNode.typeDefinitions, env);
pkgNode.xmlnsList = rewrite(pkgNode.xmlnsList, env);
pkgNode.constants = rewrite(pkgNode.constants, env);
pkgNode.globalVars = rewrite(pkgNode.globalVars, env);
desugarClassDefinitions(pkgNode.topLevelNodes);
serviceDesugar.rewriteListeners(pkgNode.globalVars, env, pkgNode.startFunction, pkgNode.stopFunction);
ASTBuilderUtil.appendStatements(serviceAttachments, (BLangBlockFunctionBody) pkgNode.initFunction.body);
addNilReturnStatement((BLangBlockFunctionBody) pkgNode.startFunction.body);
addNilReturnStatement((BLangBlockFunctionBody) pkgNode.stopFunction.body);
pkgNode.initFunction = splitInitFunction(pkgNode, env);
pkgNode.initFunction = rewrite(pkgNode.initFunction, env);
pkgNode.startFunction = rewrite(pkgNode.startFunction, env);
pkgNode.stopFunction = rewrite(pkgNode.stopFunction, env);
pkgNode.functions = rewrite(pkgNode.functions, env);
closureDesugar.visit(pkgNode);
for (BLangTestablePackage testablePkg : pkgNode.getTestablePkgs()) {
rewrite(testablePkg, this.symTable.pkgEnvMap.get(testablePkg.symbol));
}
pkgNode.completedPhases.add(CompilerPhase.DESUGAR);
initFuncIndex = 0;
result = pkgNode;
}

Line under review: Map<String, String> map = new HashMap<String, String>();

Code after addressing the review:
public void visit(BLangPackage pkgNode) {
if (pkgNode.completedPhases.contains(CompilerPhase.DESUGAR)) {
result = pkgNode;
return;
}
observabilityDesugar.addObserveInternalModuleImport(pkgNode);
observabilityDesugar.addObserveModuleImport(pkgNode);
code2CloudDesugar.addCode2CloudModuleImport(pkgNode);
createPackageInitFunctions(pkgNode, env);
addAttachedFunctionsToPackageLevel(pkgNode, env);
if (!pkgNode.testablePkgs.isEmpty() && pkgNode.getTestablePkg().getMockFunctionNamesMap() != null) {
mockDesugar.generateMockFunctions(pkgNode);
}
pkgNode.constants.stream()
.filter(constant -> constant.expr.getKind() == NodeKind.LITERAL ||
constant.expr.getKind() == NodeKind.NUMERIC_LITERAL)
.forEach(constant -> pkgNode.typeDefinitions.add(constant.associatedTypeDefinition));
BLangBlockStmt serviceAttachments = serviceDesugar.rewriteServiceVariables(pkgNode.services, env);
BLangBlockFunctionBody initFnBody = (BLangBlockFunctionBody) pkgNode.initFunction.body;
for (BLangConstant constant : pkgNode.constants) {
if (constant.symbol.type.tag == TypeTags.MAP) {
BLangSimpleVarRef constVarRef = ASTBuilderUtil.createVariableRef(constant.pos, constant.symbol);
constant.expr = rewrite(constant.expr, SymbolEnv.createTypeEnv(constant.typeNode,
pkgNode.initFunction.symbol.scope, env));
BLangInvocation frozenConstValExpr =
createLangLibInvocationNode(
"cloneReadOnly", constant.expr, new ArrayList<>(), constant.expr.getBType(),
constant.pos);
BLangAssignment constInit =
ASTBuilderUtil.createAssignmentStmt(constant.pos, constVarRef, frozenConstValExpr);
initFnBody.stmts.add(constInit);
}
}
pkgNode.constants = removeDuplicateConstants(pkgNode);
pkgNode.globalVars = desugarGlobalVariables(pkgNode, initFnBody);
pkgNode.services.forEach(service -> serviceDesugar.engageCustomServiceDesugar(service, env));
annotationDesugar.rewritePackageAnnotations(pkgNode, env);
addUserDefinedModuleInitInvocationAndReturn(pkgNode);
pkgNode.typeDefinitions.sort(Comparator.comparing(t -> t.precedence));
pkgNode.typeDefinitions = rewrite(pkgNode.typeDefinitions, env);
pkgNode.xmlnsList = rewrite(pkgNode.xmlnsList, env);
pkgNode.constants = rewrite(pkgNode.constants, env);
pkgNode.globalVars = rewrite(pkgNode.globalVars, env);
desugarClassDefinitions(pkgNode.topLevelNodes);
serviceDesugar.rewriteListeners(pkgNode.globalVars, env, pkgNode.startFunction, pkgNode.stopFunction);
ASTBuilderUtil.appendStatements(serviceAttachments, (BLangBlockFunctionBody) pkgNode.initFunction.body);
addNilReturnStatement((BLangBlockFunctionBody) pkgNode.startFunction.body);
addNilReturnStatement((BLangBlockFunctionBody) pkgNode.stopFunction.body);
pkgNode.initFunction = splitInitFunction(pkgNode, env);
pkgNode.initFunction = rewrite(pkgNode.initFunction, env);
pkgNode.startFunction = rewrite(pkgNode.startFunction, env);
pkgNode.stopFunction = rewrite(pkgNode.stopFunction, env);
pkgNode.functions = rewrite(pkgNode.functions, env);
closureDesugar.visit(pkgNode);
for (BLangTestablePackage testablePkg : pkgNode.getTestablePkgs()) {
rewrite(testablePkg, this.symTable.pkgEnvMap.get(testablePkg.symbol));
}
pkgNode.completedPhases.add(CompilerPhase.DESUGAR);
initFuncIndex = 0;
result = pkgNode;
}