diff --git a/TypeScript.ContractGenerator/Internals/TypeScriptImportDefaultFromPathStatement.cs b/TypeScript.ContractGenerator/Internals/TypeScriptImportDefaultFromPathStatement.cs deleted file mode 100644 index 9582f88..0000000 --- a/TypeScript.ContractGenerator/Internals/TypeScriptImportDefaultFromPathStatement.cs +++ /dev/null @@ -1,16 +0,0 @@ -using SkbKontur.TypeScript.ContractGenerator.CodeDom; - -namespace SkbKontur.TypeScript.ContractGenerator.Internals -{ - internal class TypeScriptImportDefaultFromPathStatement : TypeScriptImportStatement - { - public override string GenerateCode(ICodeGenerationContext context) - { - return $"import {TypeName} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, PathToUnit)}';"; - } - - public string TypeName { get; set; } - public TypeScriptUnit CurrentUnit { get; set; } - public string PathToUnit { get; set; } - } -} \ No newline at end of file diff --git a/TypeScript.ContractGenerator/Internals/TypeScriptImportFromPathStatement.cs b/TypeScript.ContractGenerator/Internals/TypeScriptImportFromPathStatement.cs index ab1d572..4c53ab3 100644 --- a/TypeScript.ContractGenerator/Internals/TypeScriptImportFromPathStatement.cs +++ b/TypeScript.ContractGenerator/Internals/TypeScriptImportFromPathStatement.cs @@ -6,11 +6,13 @@ internal class TypeScriptImportFromPathStatement : TypeScriptImportStatement { public override string GenerateCode(ICodeGenerationContext context) { - return $"import {{ {TypeName} }} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, PathToUnit)}';"; + var typeKeyword = UseTypeKeyword ? "type " : ""; + return $"import {typeKeyword}{{ {TypeName} }} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, PathToUnit)}';"; } public string TypeName { get; set; } public TypeScriptUnit CurrentUnit { get; set; } public string PathToUnit { get; set; } + public bool UseTypeKeyword { get; set; } } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/Internals/TypeScriptImportFromUnitStatement.cs b/TypeScript.ContractGenerator/Internals/TypeScriptImportFromUnitStatement.cs index b9343be..012bfde 100644 --- a/TypeScript.ContractGenerator/Internals/TypeScriptImportFromUnitStatement.cs +++ b/TypeScript.ContractGenerator/Internals/TypeScriptImportFromUnitStatement.cs @@ -6,11 +6,13 @@ internal class TypeScriptImportFromUnitStatement : TypeScriptImportStatement { public override string GenerateCode(ICodeGenerationContext context) { - return $"import {{ {TypeName} }} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, TargetUnit.Path)}';"; + var typeKeyword = UseTypeKeyword ? "type " : ""; + return $"import {typeKeyword}{{ {TypeName} }} from '{context.GetReferenceFromUnitToAnother(CurrentUnit.Path, TargetUnit.Path)}';"; } public string TypeName { get; set; } public TypeScriptUnit TargetUnit { get; set; } public TypeScriptUnit CurrentUnit { get; set; } + public bool UseTypeKeyword { get; set; } } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/TypeBuilders/RedirectToTypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/RedirectToTypeBuildingContext.cs index 8558505..f31110d 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/RedirectToTypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/RedirectToTypeBuildingContext.cs @@ -14,7 +14,7 @@ public RedirectToTypeBuildingContext(string typeName, string path, ITypeInfo typ protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator) { - return targetUnit.AddTypeImport(type, new TypeScriptInterfaceDeclaration {Name = typeName}, new TypeScriptUnit {Path = path}); + return targetUnit.AddTypeImport(type, new TypeScriptInterfaceDeclaration {Name = typeName}, new TypeScriptUnit {Path = path}, typeGenerator.Options.UseTypeImports); } private readonly string typeName; diff --git a/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs b/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs index 62209b7..176ab57 100644 --- a/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs +++ b/TypeScript.ContractGenerator/TypeBuilders/TypeBuildingContext.cs @@ -22,7 +22,7 @@ public override void Initialize(ITypeGenerator typeGenerator) protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator) { - return targetUnit.AddTypeImport(type, Declaration, Unit); + return targetUnit.AddTypeImport(type, Declaration, Unit, typeGenerator.Options.UseTypeImports); } } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/TypeScriptGenerationOptions.cs b/TypeScript.ContractGenerator/TypeScriptGenerationOptions.cs index b9666f4..f12ccfc 100644 --- a/TypeScript.ContractGenerator/TypeScriptGenerationOptions.cs +++ b/TypeScript.ContractGenerator/TypeScriptGenerationOptions.cs @@ -7,7 +7,7 @@ public class TypeScriptGenerationOptions public bool EnableOptionalProperties { get; set; } = true; public bool UseGlobalNullable { get; set; } public string? CustomContentMarker { get; set; } - + public bool UseTypeImports { get; set; } = false; public static TypeScriptGenerationOptions Default => new TypeScriptGenerationOptions(); } } \ No newline at end of file diff --git a/TypeScript.ContractGenerator/TypeScriptUnit.cs b/TypeScript.ContractGenerator/TypeScriptUnit.cs index 0f3061c..c886856 100644 --- a/TypeScript.ContractGenerator/TypeScriptUnit.cs +++ b/TypeScript.ContractGenerator/TypeScriptUnit.cs @@ -15,7 +15,7 @@ public class TypeScriptUnit public List Body { get; } = new List(); - public TypeScriptTypeReference AddTypeImport(ITypeInfo sourceType, TypeScriptTypeDeclaration typeDeclaration, TypeScriptUnit sourceUnit) + public TypeScriptTypeReference AddTypeImport(ITypeInfo sourceType, TypeScriptTypeDeclaration typeDeclaration, TypeScriptUnit sourceUnit, bool useTypeKeyword) { if (sourceUnit != this && !imports.ContainsKey(sourceType)) { @@ -24,6 +24,7 @@ public TypeScriptTypeReference AddTypeImport(ITypeInfo sourceType, TypeScriptTyp TypeName = typeDeclaration.Name, CurrentUnit = this, TargetUnit = sourceUnit, + UseTypeKeyword = useTypeKeyword }); } return new TypeScriptTypeReference(typeDeclaration.Name); @@ -44,21 +45,6 @@ public TypeScriptVariableReference AddSymbolImport(string symbolName, string pat return new TypeScriptVariableReference(symbolName); } - public TypeScriptVariableReference AddDefaultSymbolImport(string localName, string path) - { - var importedSymbol = new ImportedSymbol("default", localName, path); - if (!symbolImports.ContainsKey(importedSymbol)) - { - symbolImports.Add(importedSymbol, new TypeScriptImportDefaultFromPathStatement - { - TypeName = localName, - CurrentUnit = this, - PathToUnit = path, - }); - } - return new TypeScriptVariableReference(localName); - } - public string GenerateCode(DefaultCodeGenerationContext context) { var result = new StringBuilder();