findProjectWideResponseType function
({bool isExists, String? type, TypeAnnotation? typeNode})
findProjectWideResponseType(
{ - required String innerType,
- required String filePath,
- required CompilationUnit initialUnit,
- required List<String> searchFields,
})
Implementation
({String? type, TypeAnnotation? typeNode, bool isExists})
findProjectWideResponseType({
required String innerType,
required String filePath,
required CompilationUnit initialUnit,
required List<String> searchFields,
}) {
final filesToSearch = <String>{filePath};
// final fileDir = p.dirname(filePath);
final partOfDirective = initialUnit.directives
.whereType<PartOfDirective>()
.firstOrNull;
if (partOfDirective != null && partOfDirective.libraryName != null) {
final libraryName = partOfDirective.libraryName!.name;
final projectRoot = getProjectRootPath(scriptPath: filePath);
// Search for the library file within the project root.
final libraryFilePath = findLibraryFile(projectRoot!, libraryName);
if (libraryFilePath != null) {
final libraryDir = p.dirname(libraryFilePath);
filesToSearch.add(libraryFilePath);
final libraryUnit = parseFileByPath(libraryFilePath);
if (libraryUnit != null) {
for (final directive
in libraryUnit.directives.whereType<PartDirective>()) {
final partUri = directive.uri.stringValue;
if (partUri != null) {
final partPath = p.normalize(p.join(libraryDir, partUri));
filesToSearch.add(partPath);
}
}
}
}
}
// print({filesToSearch.toList().join("\n")});
ClassDeclaration? classNode;
String? classDeclarationPath;
for (final path in filesToSearch) {
classNode = findClassInFile(path, innerType);
if (classNode != null) {
classDeclarationPath = path;
printInfo(
"--- [findProjectWideResponseType] found '${classDeclarationPath}' "
"by findClassInFile at path $classDeclarationPath---",
);
break;
}
}
// print(classNode);
String? responseDataType;
TypeAnnotation? dataTypeNode;
bool isExists = false;
for (final field in searchFields) {
final fieldVisitor = FieldVisitor(fieldName: field);
classNode?.accept(fieldVisitor);
responseDataType = fieldVisitor.dataType;
dataTypeNode = fieldVisitor.dataTypeNode;
isExists = fieldVisitor.isExists;
print("responseDataType $responseDataType");
if (responseDataType != null /*&& dataTypeNode != null*/ ) {
printInfo(
"[findProjectWideResponseType] found '$field' type:$responseDataType",
);
break;
}
printWarning(
'[findProjectWideResponseType] not found "$innerType.$field" in project',
);
}
// print("$innerType");
if (responseDataType == null /*|| dataTypeNode == null*/ ) {
return (type: null, typeNode: null, isExists: false);
}
return (type: responseDataType, typeNode: dataTypeNode, isExists: isExists);
}