public void ComparesWithDeclaration() { var root = CodeNamespace.InitRootNamespace(); var cUsing = new CodeUsing(root) { Name = "using1", }; cUsing.Declaration = new CodeType(cUsing) { Name = "type1" }; var cUsing2 = new CodeUsing(root) { Name = "using2", }; cUsing2.Declaration = new CodeType(cUsing2) { Name = "type2" }; var comparer = new CodeUsingComparer(true); Assert.False(comparer.Equals(cUsing, cUsing2)); Assert.NotEqual(0, comparer.GetHashCode(cUsing)); }
/// <summary> /// Returns the relative import path for the given using and import context namespace. /// </summary> /// <param name="codeUsing">The using to import into the current namespace context</param> /// <param name="currentNamespace">The current namespace</param> /// <returns>The import symbol, it's alias if any and the relative import path</returns> public virtual (string, string, string) GetRelativeImportPathForUsing(CodeUsing codeUsing, CodeNamespace currentNamespace) { if (codeUsing?.IsExternal ?? true) { return(string.Empty, string.Empty, string.Empty); //it's an external import, add nothing } var typeDef = codeUsing.Declaration.TypeDefinition; var importSymbol = codeUsing.Declaration == null ? codeUsing.Name : codeUsing.Declaration.TypeDefinition switch { CodeFunction f => f.Name.ToFirstCharacterLowerCase(), _ => codeUsing.Declaration.TypeDefinition.Name.ToFirstCharacterUpperCase(), }; if (typeDef == null) { return(importSymbol, codeUsing.Alias, "./"); // it's relative to the folder, with no declaration (default failsafe) } else { var importPath = GetImportRelativePathFromNamespaces(currentNamespace, typeDef.GetImmediateParentOfType <CodeNamespace>()); if (string.IsNullOrEmpty(importPath)) { importPath += codeUsing.Name; } else { importPath += codeUsing.Declaration.Name.ToFirstCharacterLowerCase(); } return(importSymbol, codeUsing.Alias, importPath); } }
private static void AddEnumSetImport(CodeElement currentElement) { if (currentElement is CodeClass currentClass && currentClass.IsOfKind(CodeClassKind.Model) && currentClass.GetChildElements(true).OfType <CodeProperty>().Any(x => x.Type is CodeType xType && xType.TypeDefinition is CodeEnum xEnumType && xEnumType.Flags)) { var nUsing = new CodeUsing(currentClass) { Name = "EnumSet", }; nUsing.Declaration = new CodeType(nUsing) { Name = "java.util", IsExternal = true }; currentClass.AddUsing(nUsing); } CrawlTree(currentElement, AddEnumSetImport); }
public void AliasesDuplicateUsingSymbols() { var model = graphNS.AddClass(new CodeClass { Name = "model", Kind = CodeClassKind.Model }).First(); var modelsNS = graphNS.AddNamespace($"{graphNS.Name}.models"); var source1 = modelsNS.AddClass(new CodeClass { Name = "source", Kind = CodeClassKind.Model }).First(); var submodelsNS = modelsNS.AddNamespace($"{modelsNS.Name}.submodels"); var source2 = submodelsNS.AddClass(new CodeClass { Name = "source", Kind = CodeClassKind.Model }).First(); var using1 = new CodeUsing { Name = modelsNS.Name, Declaration = new CodeType { Name = source1.Name, TypeDefinition = source1, IsExternal = false, } }; var using2 = new CodeUsing { Name = submodelsNS.Name, Declaration = new CodeType { Name = source2.Name, TypeDefinition = source2, IsExternal = false, } }; model.AddUsing(using1); model.AddUsing(using2); ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root); Assert.NotEmpty(using1.Alias); Assert.NotEmpty(using2.Alias); Assert.NotEqual(using1.Alias, using2.Alias); }
public void EscapesReservedKeywordsInInternalDeclaration() { var model = root.AddClass(new CodeClass { Name = "break", Kind = CodeClassKind.Model }).First(); var nUsing = new CodeUsing { Name = "some.ns", }; nUsing.Declaration = new CodeType { Name = "break", IsExternal = false, }; model.AddUsing(nUsing); ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.Java }, root); Assert.NotEqual("break", nUsing.Declaration.Name); Assert.Contains("escaped", nUsing.Declaration.Name); }
private static void AddListImport(CodeElement currentElement) { if (currentElement is CodeClass currentClass) { var childElements = currentClass.GetChildElements(true); if (childElements.OfType <CodeProperty>().Any(x => x.Type?.CollectionKind == CodeType.CodeTypeCollectionKind.Complex) || childElements.OfType <CodeMethod>().Any(x => x.ReturnType?.CollectionKind == CodeType.CodeTypeCollectionKind.Complex) || childElements.OfType <CodeMethod>().Any(x => x.Parameters.Any(y => y.Type.CollectionKind == CodeType.CodeTypeCollectionKind.Complex))) { var nUsing = new CodeUsing(currentClass) { Name = "List" }; nUsing.Declaration = new CodeType(nUsing) { Name = "java.util", IsExternal = true }; currentClass.AddUsing(nUsing); } } CrawlTree(currentElement, AddListImport); }