Skip to content

Commit

Permalink
Merge pull request #64 from raileasyuk/feat/type-imports
Browse files Browse the repository at this point in the history
Add option for type-keyword imports
  • Loading branch information
fakefeik authored Nov 9, 2023
2 parents bceb542 + 12c9294 commit d42bcdb
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 37 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
18 changes: 2 additions & 16 deletions TypeScript.ContractGenerator/TypeScriptUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TypeScriptUnit

public List<TypeScriptStatement> Body { get; } = new List<TypeScriptStatement>();

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))
{
Expand All @@ -24,6 +24,7 @@ public TypeScriptTypeReference AddTypeImport(ITypeInfo sourceType, TypeScriptTyp
TypeName = typeDeclaration.Name,
CurrentUnit = this,
TargetUnit = sourceUnit,
UseTypeKeyword = useTypeKeyword
});
}
return new TypeScriptTypeReference(typeDeclaration.Name);
Expand All @@ -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();
Expand Down

0 comments on commit d42bcdb

Please sign in to comment.